From 120edd980969dce4624c72476d33f867efdee97f Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Mon, 11 Feb 2019 07:05:04 -0500 Subject: [PATCH] Problem: selection of condition_variable_t implementation is confusing and not configurable Solution: move configuration to build definition --- CMakeLists.txt | 38 +++++++++++++++++++++++++++++ acinclude.m4 | 41 +++++++++++++++++++++++++++++++ builds/cmake/platform.hpp.in | 5 ++++ configure.ac | 3 +++ src/condition_variable.hpp | 47 +++++++++++------------------------- 5 files changed, 101 insertions(+), 33 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a15697c0..a631d50f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -250,6 +250,7 @@ include(CheckCSourceRuns) include(CMakeDependentOption) include(CheckCXXSymbolExists) include(CheckSymbolExists) +include(FindThreads) execute_process(COMMAND getconf LEVEL1_DCACHE_LINESIZE OUTPUT_VARIABLE CACHELINE_SIZE ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) if(CACHELINE_SIZE STREQUAL "" OR CACHELINE_SIZE EQUAL 0 OR CACHELINE_SIZE EQUAL -1) @@ -307,6 +308,43 @@ if (WIN32) add_definitions(-D_WIN32_WINNT=${ZMQ_WIN32_WINNT}) endif(WIN32) +###################### BEGIN condition_variable_t selection +if(NOT ZMQ_CV_IMPL) + # prefer C++11 STL std::condition_variable implementation, if available + check_include_files(condition_variable ZMQ_HAVE_STL_CONDITION_VARIABLE LANGUAGE CXX) + + if (ZMQ_HAVE_STL_CONDITION_VARIABLE) + set(ZMQ_CV_IMPL_DEFAULT "stl11") + else() + if(WIN32 AND NOT CMAKE_SYSTEM_VERSION VERSION_LESS "6.0") + # Win32API CONDITION_VARIABLE is supported from Windows Vista only + set(ZMQ_CV_IMPL_DEFAULT "win32api") + elseif(CMAKE_USE_PTHREADS_INIT) + set(ZMQ_CV_IMPL_DEFAULT "pthreads") + else() + set(ZMQ_CV_IMPL_DEFAULT "none") + endif() + endif() + + # TODO a vxworks implementation also exists, but vxworks is not currently supported with cmake at all + set(ZMQ_CV_IMPL "${ZMQ_CV_IMPL_DEFAULT}" CACHE STRING "Choose condition_variable_t implementation. Valid values are + stl11, win32api, pthreads, none [default=autodetect]") +endif() + +message(STATUS "Using condition_variable_t implementation: ${ZMQ_CV_IMPL}") +if(ZMQ_CV_IMPL STREQUAL "stl11") + set(ZMQ_USE_CV_IMPL_STL11 1) +elseif(ZMQ_CV_IMPL STREQUAL "win32api") + set(ZMQ_USE_CV_IMPL_WIN32API 1) +elseif(ZMQ_CV_IMPL STREQUAL "pthreads") + set(ZMQ_USE_CV_IMPL_PTHREADS 1) +elseif(ZMQ_CV_IMPL STREQUAL "none") + set(ZMQ_USE_CV_IMPL_NONE 1) +else() + message(ERROR "Unknown value for ZMQ_CV_IMPL: ${ZMQ_CV_IMPL}") +endif() +###################### END condition_variable_t selection + if(CMAKE_SYSTEM_NAME STREQUAL "WindowsStore" AND CMAKE_SYSTEM_VERSION STREQUAL "10.0") set(ZMQ_HAVE_WINDOWS_UWP ON) endif() diff --git a/acinclude.m4 b/acinclude.m4 index 8e3f4d2a..733b289b 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -1189,3 +1189,44 @@ AC_DEFUN([LIBZMQ_CHECK_CACHELINE], [{ AC_MSG_NOTICE([Using "$zmq_cacheline_size" bytes alignment for lock-free data structures]) AC_DEFINE_UNQUOTED(ZMQ_CACHELINE_SIZE, $zmq_cacheline_size, [Using "$zmq_cacheline_size" bytes alignment for lock-free data structures]) }]) + +dnl ################################################################################ +dnl # LIBZMQ_CHECK_CV_IMPL([action-if-found], [action-if-not-found]) # +dnl # Choose condition variable implementation # +dnl ################################################################################ + +AC_DEFUN([LIBZMQ_CHECK_CV_IMPL], [{ + # Allow user to override condition variable autodetection + AC_ARG_WITH([cv-impl], + [AS_HELP_STRING([--with-cv-impl], + [choose condition variable implementation manually. Valid values are 'stl11', 'pthread', 'none', or 'auto'. [default=auto]])]) + + if test "x$with_cv_impl" == "x"; then + cv_impl=auto + else + cv_impl=$with_cv_impl + fi + case $host_os in + vxworks*) + cv_impl="vxworks" + AC_DEFINE(ZMQ_USE_CV_IMPL_VXWORKS, 1, [Use vxworks condition variable implementation.]) + ;; + esac + if test "$cv_impl" == "auto" || test "$cv_impl" == "stl11"; then + AC_LANG_PUSH([C++]) + AC_CHECK_HEADERS(condition_variable, [stl11="yes" + AC_DEFINE(ZMQ_USE_CV_IMPL_STL11, 1, [Use stl11 condition variable implementation.])], + [stl11="no"]) + AC_LANG_POP([C++]) + if test "$cv_impl" == "stl11" && test "x$stl11" == "xno"; then + AC_MSG_ERROR([--with-cv-impl set to stl11 but cannot find condition_variable]) + fi + fi + if test "$cv_impl" == "pthread" || test "x$stl11" == "xno"; then + AC_DEFINE(ZMQ_USE_CV_IMPL_PTHREADS, 1, [Use pthread condition variable implementation.]) + fi + if test "$cv_impl" == "none"; then + AC_DEFINE(ZMQ_USE_CV_IMPL_NONE, 1, [Use no condition variable implementation.]) + fi + AC_MSG_NOTICE([Using "$cv_impl" condition variable implementation.]) +}]) diff --git a/builds/cmake/platform.hpp.in b/builds/cmake/platform.hpp.in index 21a848fd..0a4740f0 100644 --- a/builds/cmake/platform.hpp.in +++ b/builds/cmake/platform.hpp.in @@ -1,6 +1,11 @@ #ifndef __ZMQ_PLATFORM_HPP_INCLUDED__ #define __ZMQ_PLATFORM_HPP_INCLUDED__ +#cmakedefine ZMQ_USE_CV_IMPL_STL11 +#cmakedefine ZMQ_USE_CV_IMPL_WIN32API +#cmakedefine ZMQ_USE_CV_IMPL_PTHREADS +#cmakedefine ZMQ_USE_CV_IMPL_NONE + #cmakedefine ZMQ_IOTHREAD_POLLER_USE_KQUEUE #cmakedefine ZMQ_IOTHREAD_POLLER_USE_EPOLL #cmakedefine ZMQ_IOTHREAD_POLLER_USE_EPOLL_CLOEXEC diff --git a/configure.ac b/configure.ac index 90c042ec..6bb98202 100644 --- a/configure.ac +++ b/configure.ac @@ -381,6 +381,9 @@ LIBZMQ_CHECK_POLLER # Check cacheline size, set appropriate macro in src/platform.hpp LIBZMQ_CHECK_CACHELINE +# Check condition variable implementation, set appropriate macro in src/platform.hpp +LIBZMQ_CHECK_CV_IMPL + # Checks for header files. AC_HEADER_STDC AC_CHECK_HEADERS(\ diff --git a/src/condition_variable.hpp b/src/condition_variable.hpp index 16502b94..6c0a5d86 100644 --- a/src/condition_variable.hpp +++ b/src/condition_variable.hpp @@ -35,25 +35,7 @@ // Condition variable class encapsulates OS mutex in a platform-independent way. -#ifdef ZMQ_HAVE_WINDOWS - -#include "windows.hpp" -#if defined(_MSC_VER) -#if _MSC_VER >= 1800 -#define _SUPPORT_CONDITION_VARIABLE 1 -#else -#define _SUPPORT_CONDITION_VARIABLE 0 -#endif -#else -#if _cplusplus >= 201103L -#define _SUPPORT_CONDITION_VARIABLE 1 -#else -#define _SUPPORT_CONDITION_VARIABLE 0 -#endif -#endif - -// Condition variable is supported from Windows Vista only, to use condition variable define _WIN32_WINNT to 0x0600 -#if _WIN32_WINNT < 0x0600 && !_SUPPORT_CONDITION_VARIABLE +#if defined(ZMQ_USE_CV_IMPL_NONE) namespace zmq { @@ -79,16 +61,12 @@ class condition_variable_t }; } -#else +#elif defined(ZMQ_USE_CV_IMPL_WIN32API) -#if _SUPPORT_CONDITION_VARIABLE || defined(ZMQ_HAVE_WINDOWS_TARGET_XP) -#include -#endif +#include "windows.hpp" namespace zmq { - -#if !defined(ZMQ_HAVE_WINDOWS_TARGET_XP) && _WIN32_WINNT >= 0x0600 class condition_variable_t { public: @@ -121,7 +99,14 @@ class condition_variable_t condition_variable_t (const condition_variable_t &); void operator= (const condition_variable_t &); }; -#else +} + +#elif defined(ZMQ_USE_CV_IMPL_STL11) + +#include + +namespace zmq +{ class condition_variable_t { public: @@ -158,13 +143,9 @@ class condition_variable_t condition_variable_t (const condition_variable_t &); void operator= (const condition_variable_t &); }; - -#endif } -#endif - -#elif defined ZMQ_HAVE_VXWORKS +#elif defined(ZMQ_USE_CV_IMPL_VXWORKS) #include @@ -248,7 +229,8 @@ class condition_variable_t const condition_variable_t &operator= (const condition_variable_t &); }; } -#else + +#elif defined(ZMQ_USE_CV_IMPL_PTHREADS) #include @@ -344,5 +326,4 @@ class condition_variable_t #endif - #endif