From: Azat Khuzhin Date: Fri, 28 Aug 2020 22:15:20 +0000 (+0300) Subject: Detect existence of pthread_mutexattr_setprotocol() X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=972289f356102d22a23aac4a93fe42de845dd917;p=libevent Detect existence of pthread_mutexattr_setprotocol() Fixes: #1084 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a296216..014044cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,9 +189,9 @@ if (EVENT__COVERAGE) message(STATUS "Setting coverage compiler flags") - set(CMAKE_REQUIRED_LIBRARIES "--coverage") + list(APPEND CMAKE_REQUIRED_LIBRARIES "--coverage") add_compiler_flags(-g -O0 --coverage) - set(CMAKE_REQUIRED_LIBRARIES "") + list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "--coverage") endif() set(GNUC 0) @@ -340,11 +340,19 @@ endif() # Winsock. if(WIN32) - set(CMAKE_REQUIRED_LIBRARIES ws2_32 shell32 advapi32 bcrypt) + list(APPEND CMAKE_REQUIRED_LIBRARIES + ws2_32 + shell32 + advapi32 + bcrypt + ) set(CMAKE_REQUIRED_DEFINITIONS -FIwinsock2.h -FIws2tcpip.h -D_WIN32_WINNT=0x0600) endif() if (SOLARIS) - set(CMAKE_REQUIRED_LIBRARIES socket nsl) + list(APPEND CMAKE_REQUIRED_LIBRARIES + socket + nsl + ) endif() # Check if _GNU_SOURCE is available. @@ -433,6 +441,13 @@ if (APPLE) ) endif() +if (NOT EVENT__DISABLE_THREAD_SUPPORT AND NOT WIN32) + list(APPEND FILES_TO_CHECK pthread.h) + # (Only `CHECK_TYPE_SIZE()' will use `CMAKE_EXTRA_INCLUDE_FILES') + list(APPEND CMAKE_EXTRA_INCLUDE_FILES pthread.h) +endif() + +# Fills EVENT_INCLUDES foreach(FILE ${FILES_TO_CHECK}) CHECK_INCLUDE_FILE_CONCAT(${FILE} "EVENT") endforeach() @@ -511,9 +526,30 @@ else() endif() endif() +if (NOT EVENT__DISABLE_THREAD_SUPPORT) + if (WIN32) + list(APPEND SRC_CORE evthread_win32.c) + else() + find_package(Threads REQUIRED) + if (NOT CMAKE_USE_PTHREADS_INIT) + message(FATAL_ERROR + "Failed to find Pthreads, set EVENT__DISABLE_THREAD_SUPPORT to disable") + endif() + + set(EVENT__HAVE_PTHREADS 1) + list(APPEND LIB_APPS ${CMAKE_THREAD_LIBS_INIT}) + + # for CHECK_SYMBOLS_EXIST() + list(APPEND CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) + + CHECK_TYPE_SIZE(pthread_t EVENT__SIZEOF_PTHREAD_T) + list(APPEND SYMBOLS_TO_CHECK pthread_mutexattr_setprotocol) + endif() +endif() + # Add stdio.h for vasprintf -set(EVENT_INCLUDES ${EVENT_INCLUDES} stdio.h) -CHECK_SYMBOLS_EXIST("${SYMBOLS_TO_CHECK}" "${EVENT_INCLUDES}" "EVENT") +list(APPEND CMAKE_EXTRA_INCLUDE_FILES ${EVENT_INCLUDES} stdio.h) +CHECK_SYMBOLS_EXIST("${SYMBOLS_TO_CHECK}" "${CMAKE_EXTRA_INCLUDE_FILES}" "EVENT") unset(SYMBOLS_TO_CHECK) set(EVENT__HAVE_EPOLL ${EVENT__HAVE_EPOLL_CREATE}) if(WIN32 AND NOT CYGWIN) @@ -545,9 +581,6 @@ if(HAVE_PORT_H AND HAVE_PORT_CREATE) set(EVENT__HAVE_EVENT_PORTS 1) endif() -# Only `CHECK_TYPE_SIZE()' will use `CMAKE_EXTRA_INCLUDE_FILES' -set(CMAKE_EXTRA_INCLUDE_FILES ${EVENT_INCLUDES}) - CHECK_TYPE_SIZE("struct sockaddr_un" EVENT__HAVE_STRUCT_SOCKADDR_UN) CHECK_TYPE_SIZE("uint8_t" EVENT__HAVE_UINT8_T) CHECK_TYPE_SIZE("uint16_t" EVENT__HAVE_UINT16_T) @@ -637,13 +670,6 @@ else() set(EVENT__SIZEOF_PID_T EVENT__SIZEOF_PID_T) endif() -if (NOT EVENT__DISABLE_THREAD_SUPPORT) - if (NOT WIN32) - list(APPEND CMAKE_EXTRA_INCLUDE_FILES pthread.h) - endif() - CHECK_TYPE_SIZE(pthread_t EVENT__SIZEOF_PTHREAD_T) -endif() - if(EVENT__HAVE_CLOCK_GETTIME) set(EVENT__DNS_USE_CPU_CLOCK_FOR_ID 1) endif() @@ -884,21 +910,6 @@ if (NOT EVENT__DISABLE_MBEDTLS) list(APPEND LIB_APPS ${MBEDTLS_LIBRARIES}) endif() -if (NOT EVENT__DISABLE_THREAD_SUPPORT) - if (WIN32) - list(APPEND SRC_CORE evthread_win32.c) - else() - find_package(Threads REQUIRED) - if (NOT CMAKE_USE_PTHREADS_INIT) - message(FATAL_ERROR - "Failed to find Pthreads, set EVENT__DISABLE_THREAD_SUPPORT to disable") - endif() - - set(EVENT__HAVE_PTHREADS 1) - list(APPEND LIB_APPS ${CMAKE_THREAD_LIBS_INIT}) - endif() -endif() - if (NOT EVENT__DISABLE_TESTS) # Zlib is only used for testing. find_package(ZLIB) diff --git a/configure.ac b/configure.ac index 2599c225..a143224e 100644 --- a/configure.ac +++ b/configure.ac @@ -786,6 +786,8 @@ if test x$bwin32 != xtrue && test "$enable_thread_support" != "no"; then [AC_INCLUDES_DEFAULT() #include ] ) + + AC_CHECK_FUNCS([pthread_mutexattr_setprotocol]) fi AM_CONDITIONAL(THREADS, [test "$enable_thread_support" != "no"]) AM_CONDITIONAL(PTHREADS, [test "$have_pthreads" != "no" && test "$enable_thread_support" != "no"]) diff --git a/event-config.h.cmake b/event-config.h.cmake index 1e7adb1c..752e7890 100644 --- a/event-config.h.cmake +++ b/event-config.h.cmake @@ -238,6 +238,9 @@ /* Define if we have pthreads on this system */ #cmakedefine EVENT__HAVE_PTHREADS 1 +/* Define to 1 if you have the `pthread_mutexattr_setprotocol' function. */ +#cmakedefine EVENT__HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL 1 + /* Define to 1 if you have the `putenv' function. */ #cmakedefine EVENT__HAVE_PUTENV 1 diff --git a/evthread_pthread.c b/evthread_pthread.c index a40f5b02..94e7da95 100644 --- a/evthread_pthread.c +++ b/evthread_pthread.c @@ -190,11 +190,15 @@ evthread_use_pthreads_with_flags(int flags) return -1; if (flags & EVTHREAD_PTHREAD_PRIO_INHERIT) { +#ifdef EVENT__HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL /* Set up priority inheritance */ if (pthread_mutexattr_setprotocol(&attr_default, PTHREAD_PRIO_INHERIT)) return -1; if (pthread_mutexattr_setprotocol(&attr_recursive, PTHREAD_PRIO_INHERIT)) return -1; +#else + return -1; +#endif } evthread_set_lock_callbacks(&cbs); diff --git a/include/event2/thread.h b/include/event2/thread.h index 519b4d30..481a02fe 100644 --- a/include/event2/thread.h +++ b/include/event2/thread.h @@ -213,7 +213,8 @@ int evthread_use_windows_threads(void); EVENT2_EXPORT_SYMBOL int evthread_use_pthreads(void); -/* Enables posix mutex priority inheritance. */ +/* Enables posix mutex priority inheritance + * (if pthread_mutexattr_setprotocol() is supported). */ #define EVTHREAD_PTHREAD_PRIO_INHERIT 0x01 /**