From 5fa14f6a741503e4bc039be4e84ca626b0794ae4 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Fri, 5 Dec 2025 16:25:47 +0000 Subject: [PATCH] cmake: Fix multi-thread support building --- .github/workflows/main.yml | 2 +- CMakeLists.txt | 20 +++++++++++++++----- cmake_coap_config.h.in | 3 +++ src/coap_threadsafe.c | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 69aaa8de..33a40325 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -200,7 +200,7 @@ jobs: - name: MS CMake configure run: | cd build_test - cmake .. -DWARNING_TO_ERROR=ON -DWITH_OPENSSL=YES + cmake .. -DWARNING_TO_ERROR=ON -DWITH_OPENSSL=YES -DENABLE_THREAD_SAFE=OFF - name: MS CMake build run: | cd build_test diff --git a/CMakeLists.txt b/CMakeLists.txt index 56ce30d7..a4509238 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -306,7 +306,13 @@ check_function_exists(memset HAVE_MEMSET) check_function_exists(select HAVE_SELECT) check_function_exists(socket HAVE_SOCKET) check_function_exists(strcasecmp HAVE_STRCASECMP) +if(MINGW OR WIN32) + set(CMAKE_REQUIRED_LIBRARIES pthread) +endif() check_function_exists(pthread_mutex_lock HAVE_PTHREAD_MUTEX_LOCK) +if(MINGW OR WIN32) + set(CMAKE_REQUIRED_LIBRARIES "") +endif() check_function_exists(getaddrinfo HAVE_GETADDRINFO) check_function_exists(strnlen HAVE_STRNLEN) check_function_exists(strrchr HAVE_STRRCHR) @@ -428,22 +434,25 @@ else() endif() if(${ENABLE_THREAD_SAFE}) - set(COAP_THREAD_SAFE "${ENABLE_THREAD_SAFE}") + set(COAP_THREAD_SAFE "1") message(STATUS "compiling with thread safe support") +else() + set(ENABLE_THREAD_RECURSIVE_LOCK_CHECK OFF) + set(ENABLE_THREAD_NUM_LOGGING OFF) endif() if(${ENABLE_THREAD_RECURSIVE_LOCK_CHECK}) - set(COAP_THREAD_RECURSIVE_CHECK "${ENABLE_THREAD_RECURSIVE_LOCK_CHECK}") + set(COAP_THREAD_RECURSIVE_CHECK "1") message(STATUS "compiling with thread recursive lock detection support") endif() if(${ENABLE_THREAD_NUM_LOGGING}) - set(COAP_THREAD_NUM_LOGGING "${ENABLE_THREAD_NUM_LOGGING}") + set(COAP_THREAD_NUM_LOGGING "1") message(STATUS "compiling with thread number logging support") endif() if(${ENABLE_SMALL_STACK}) - set(COAP_CONSTRAINED_STACK "${ENABLE_SMALL_STACK}") + set(COAP_CONSTRAINED_STACK "1") message(STATUS "compiling with small stack support") endif() @@ -738,7 +747,8 @@ target_link_libraries( $<$:MbedTLS::mbedtls> $<$:wolfssl::wolfssl> $<$:ws2_32> - $<$:iphlpapi>) + $<$:iphlpapi> + $<$:pthread>) target_compile_options( ${COAP_LIBRARY_NAME} diff --git a/cmake_coap_config.h.in b/cmake_coap_config.h.in index 3a81e334..4c1895a5 100644 --- a/cmake_coap_config.h.in +++ b/cmake_coap_config.h.in @@ -85,6 +85,9 @@ /* Define to 1 if you have the `select' function. */ #cmakedefine HAVE_SELECT @HAVE_SELECT@ +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SIGNAL_H @HAVE_SIGNAL_H@ + /* Define to 1 if you have the `socket' function. */ #cmakedefine HAVE_SOCKET @HAVE_SOCKET@ diff --git a/src/coap_threadsafe.c b/src/coap_threadsafe.c index 499921a7..14e014cb 100644 --- a/src/coap_threadsafe.c +++ b/src/coap_threadsafe.c @@ -119,7 +119,7 @@ coap_io_process_worker_thread(void *arg) { coap_thread_param_t *thread_param = (coap_thread_param_t *)arg; coap_context_t *context = thread_param->context; #if (COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG) - long unsigned int thread_pid = coap_thread_pid; + long unsigned int thread_pid = (long unsigned int)coap_thread_pid; #endif thread_no = thread_param->thread_no;