Skip to content

Commit

Permalink
Call pthread's recursive mutex on Linux (Issue #245)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Albrecht Schlosser committed Jul 21, 2021
1 parent 15f1b6b commit c6eccf6
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CMake/pthread_mutex_recursive.c
@@ -0,0 +1,7 @@
/*
FLTK feature test: do we have PTHREAD_MUTEX_RECURSIVE ?
*/
#include <pthread.h>
int main() {
return PTHREAD_MUTEX_RECURSIVE;
}
14 changes: 13 additions & 1 deletion CMake/resources.cmake
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions configh.cmake.in
Expand Up @@ -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?
*/
Expand Down
6 changes: 6 additions & 0 deletions configh.in
Expand Up @@ -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?
*/
Expand Down
18 changes: 18 additions & 0 deletions configure.ac
Expand Up @@ -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...
Expand Down
14 changes: 7 additions & 7 deletions src/drivers/Posix/Fl_Posix_System_Driver.cxx
Expand Up @@ -193,7 +193,7 @@ static void* quadruple_dlopen(const char *libname)
}
return ptr;
}
#endif
#endif // HAVE_DLSYM && HAVE_DLFCN_H && !defined (__APPLE_CC__)


/**
Expand Down Expand Up @@ -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);
Expand All @@ -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]) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c6eccf6

Please sign in to comment.