Call pthread's recursive mutex on Linux (Issue #245)

Add CMake test for PTHREAD_MUTEX_RECURSIVE

Add autoconf/configure compile test for PTHREAD_MUTEX_RECURSIVE

Replace "#ifdef PTHREAD_MUTEX_RECURSIVE"
   with "#ifdef HAVE_PTHREAD_MUTEX_RECURSIVE"
and define HAVE_PTHREAD_MUTEX_RECURSIVE in config.h
This commit is contained in:
Albrecht Schlosser 2021-07-21 12:12:26 +02:00
parent 15f1b6b5c4
commit c6eccf6137
6 changed files with 57 additions and 8 deletions

View File

@ -0,0 +1,7 @@
/*
FLTK feature test: do we have PTHREAD_MUTEX_RECURSIVE ?
*/
#include <pthread.h>
int main() {
return PTHREAD_MUTEX_RECURSIVE;
}

View File

@ -71,6 +71,17 @@ else ()
fl_find_header (HAVE_PTHREAD_H pthread.h)
endif (WIN32 AND NOT CYGWIN)
# Do we have PTHREAD_MUTEX_RECURSIVE ?
if (HAVE_PTHREAD_H)
try_compile(HAVE_PTHREAD_MUTEX_RECURSIVE
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/CMake/pthread_mutex_recursive.c
)
else ()
set (HAVE_PTHREAD_MUTEX_RECURSIVE 0)
endif ()
# Special case for Microsoft Visual Studio generator (MSVC):
#
# The header files <GL/glu.h> and <locale.h> are located in the SDK's
@ -139,7 +150,8 @@ endif (NOT HAVE_DIRENT_H)
mark_as_advanced (HAVE_ALSA_ASOUNDLIB_H HAVE_DIRENT_H HAVE_DLFCN_H)
mark_as_advanced (HAVE_GL_GLU_H)
mark_as_advanced (HAVE_LIBPNG_PNG_H HAVE_LOCALE_H HAVE_NDIR_H)
mark_as_advanced (HAVE_OPENGL_GLU_H HAVE_PNG_H HAVE_PTHREAD_H)
mark_as_advanced (HAVE_OPENGL_GLU_H HAVE_PNG_H)
mark_as_advanced (HAVE_PTHREAD_H HAVE_PTHREAD_MUTEX_RECURSIVE)
mark_as_advanced (HAVE_STDIO_H HAVE_STRINGS_H HAVE_SYS_DIR_H)
mark_as_advanced (HAVE_SYS_NDIR_H HAVE_SYS_SELECT_H)
mark_as_advanced (HAVE_SYS_STDTYPES_H HAVE_XDBE_H)

View File

@ -344,6 +344,12 @@
#cmakedefine HAVE_PTHREAD 1
#cmakedefine HAVE_PTHREAD_H 1
/*
* Do we have PTHREAD_MUTEX_RECURSIVE?
*/
#cmakedefine HAVE_PTHREAD_MUTEX_RECURSIVE 1
/*
* Do we have the ALSA library?
*/

View File

@ -344,6 +344,12 @@
#undef HAVE_PTHREAD
#undef HAVE_PTHREAD_H
/*
* Do we have PTHREAD_MUTEX_RECURSIVE?
*/
#undef HAVE_PTHREAD_MUTEX_RECURSIVE
/*
* Do we have the ALSA library?
*/

View File

@ -870,10 +870,28 @@ AS_IF([test "x$enable_threads" != xno -a x$check_pthread = xyes], [
])
])
done
dnl Check if we have PTHREAD_MUTEX_RECURSIVE
AC_CACHE_CHECK([whether we have PTHREAD_MUTEX_RECURSIVE], ac_cv_pthread_mutex_recursive,[
AC_LANG_PUSH([C])
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM(
[[#include <pthread.h>
]], [[
return PTHREAD_MUTEX_RECURSIVE;
]])],
[ac_cv_pthread_mutex_recursive=yes],
[ac_cv_pthread_mutex_recursive=no])
AC_LANG_POP([])
])
AS_IF([test x$ac_cv_pthread_mutex_recursive = xyes], [
AC_DEFINE([HAVE_PTHREAD_MUTEX_RECURSIVE])
])
])
])
AC_SUBST([PTHREAD_FLAGS])
AC_SUBST([HAVE_PTHREAD_MUTEX_RECURSIVE])
dnl Define OS-specific stuff...

View File

@ -193,7 +193,7 @@ static void* quadruple_dlopen(const char *libname)
}
return ptr;
}
#endif
#endif // HAVE_DLSYM && HAVE_DLFCN_H && !defined (__APPLE_CC__)
/**
@ -332,7 +332,7 @@ static void unlock_function_std() {
if (!--counter) pthread_mutex_unlock(&fltk_mutex);
}
# ifdef PTHREAD_MUTEX_RECURSIVE
# ifdef HAVE_PTHREAD_MUTEX_RECURSIVE
static bool lock_function_init_rec() {
pthread_mutexattr_t attrib;
pthread_mutexattr_init(&attrib);
@ -352,7 +352,7 @@ static void lock_function_rec() {
static void unlock_function_rec() {
pthread_mutex_unlock(&fltk_mutex);
}
# endif // PTHREAD_MUTEX_RECURSIVE
# endif // HAVE_PTHREAD_MUTEX_RECURSIVE
void Fl_Posix_System_Driver::awake(void* msg) {
if (thread_filedes[1]) {
@ -402,18 +402,18 @@ int Fl_Posix_System_Driver::lock() {
// Set lock/unlock functions for this system, using a system-supplied
// recursive mutex if supported...
# ifdef PTHREAD_MUTEX_RECURSIVE
# ifdef HAVE_PTHREAD_MUTEX_RECURSIVE
if (!lock_function_init_rec()) {
fl_lock_function = lock_function_rec;
fl_unlock_function = unlock_function_rec;
} else {
# endif // PTHREAD_MUTEX_RECURSIVE
# endif // HAVE_PTHREAD_MUTEX_RECURSIVE
lock_function_init_std();
fl_lock_function = lock_function_std;
fl_unlock_function = unlock_function_std;
# ifdef PTHREAD_MUTEX_RECURSIVE
# ifdef HAVE_PTHREAD_MUTEX_RECURSIVE
}
# endif // PTHREAD_MUTEX_RECURSIVE
# endif // HAVE_PTHREAD_MUTEX_RECURSIVE
}
fl_lock_function();