Skip to content

Commit

Permalink
Detect existence of pthread_mutexattr_setprotocol()
Browse files Browse the repository at this point in the history
  • Loading branch information
azat committed Aug 28, 2020
1 parent e3e7bb2 commit 972289f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
73 changes: 42 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,8 @@ if test x$bwin32 != xtrue && test "$enable_thread_support" != "no"; then
[AC_INCLUDES_DEFAULT()
#include <pthread.h> ]
)

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"])
Expand Down
3 changes: 3 additions & 0 deletions event-config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions evthread_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion include/event2/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down

0 comments on commit 972289f

Please sign in to comment.