CMake: Improved detection of pthreads.

This commit is contained in:
Cameron Cawley 2018-01-26 22:36:44 +00:00 committed by endrift
parent 59db2a1946
commit 19747ea21d
2 changed files with 38 additions and 14 deletions

View File

@ -257,15 +257,10 @@ if(WIN32)
endif() endif()
elseif(UNIX) elseif(UNIX)
set(USE_PTHREADS ON) set(USE_PTHREADS ON)
add_definitions(-DUSE_PTHREADS)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux") if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_definitions(-D_GNU_SOURCE) add_definitions(-D_GNU_SOURCE)
endif() endif()
if(NOT APPLE AND NOT HAIKU)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()
list(APPEND CORE_VFS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-fd.c ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-dirent.c) list(APPEND CORE_VFS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-fd.c ${CMAKE_CURRENT_SOURCE_DIR}/src/util/vfs/vfs-dirent.c)
file(GLOB OS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/posix/*.c) file(GLOB OS_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/platform/posix/*.c)
@ -351,6 +346,7 @@ if(3DS OR WII)
add_definitions(-D_GNU_SOURCE) add_definitions(-D_GNU_SOURCE)
endif() endif()
include(CheckCCompilerFlag)
include(CheckFunctionExists) include(CheckFunctionExists)
include(CheckIncludeFiles) include(CheckIncludeFiles)
check_function_exists(strdup HAVE_STRDUP) check_function_exists(strdup HAVE_STRDUP)
@ -402,6 +398,27 @@ endif()
check_function_exists(chmod HAVE_CHMOD) check_function_exists(chmod HAVE_CHMOD)
check_function_exists(umask HAVE_UMASK) check_function_exists(umask HAVE_UMASK)
if(USE_PTHREADS)
check_include_files("pthread.h" HAVE_PTHREAD_H)
if(HAVE_PTHREAD_H)
check_c_compiler_flag(-pthread HAVE_PTHREAD)
if(HAVE_PTHREAD AND NOT APPLE AND NOT HAIKU)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()
check_function_exists(pthread_create HAVE_PTHREAD_CREATE)
if(HAVE_PTHREAD_CREATE)
add_definitions(-DUSE_PTHREADS)
check_include_files("pthread_np.h" HAVE_PTHREAD_NP_H)
check_function_exists(pthread_setname_np HAVE_PTHREAD_SETNAME_NP)
check_function_exists(pthread_set_name_np HAVE_PTHREAD_SET_NAME_NP)
endif()
endif()
endif()
set(FUNCTION_DEFINES) set(FUNCTION_DEFINES)
if(HAVE_STRDUP) if(HAVE_STRDUP)
@ -443,6 +460,18 @@ if(HAVE_UMASK)
list(APPEND FUNCTION_DEFINES HAVE_UMASK) list(APPEND FUNCTION_DEFINES HAVE_UMASK)
endif() endif()
if(HAVE_PTHREAD_NP_H)
list(APPEND FUNCTION_DEFINES HAVE_PTHREAD_NP_H)
endif()
if(HAVE_PTHREAD_SETNAME_NP)
list(APPEND FUNCTION_DEFINES HAVE_PTHREAD_SETNAME_NP)
endif()
if(HAVE_PTHREAD_SET_NAME_NP)
list(APPEND FUNCTION_DEFINES HAVE_PTHREAD_SET_NAME_NP)
endif()
# Feature dependencies # Feature dependencies
set(FEATURE_DEFINES) set(FEATURE_DEFINES)
set(FEATURE_FLAGS) set(FEATURE_FLAGS)

View File

@ -12,7 +12,7 @@ CXX_GUARD_START
#include <pthread.h> #include <pthread.h>
#include <sys/time.h> #include <sys/time.h>
#if defined(__FreeBSD__) || defined(__OpenBSD__) #ifdef HAVE_PTHREAD_NP_H
#include <pthread_np.h> #include <pthread_np.h>
#elif defined(__HAIKU__) #elif defined(__HAIKU__)
#include <OS.h> #include <OS.h>
@ -85,20 +85,15 @@ static inline int ThreadJoin(Thread thread) {
} }
static inline int ThreadSetName(const char* name) { static inline int ThreadSetName(const char* name) {
#ifdef __APPLE__ #if defined(__APPLE__) && defined(HAVE_PTHREAD_SETNAME_NP)
#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060
return pthread_setname_np(name); return pthread_setname_np(name);
#else #elif defined(HAVE_PTHREAD_SET_NAME_NP)
UNUSED(name);
return 0;
#endif
#elif defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(pthread_self(), name); pthread_set_name_np(pthread_self(), name);
return 0; return 0;
#elif defined(__HAIKU__) #elif defined(__HAIKU__)
rename_thread(find_thread(NULL), name); rename_thread(find_thread(NULL), name);
return 0; return 0;
#elif !defined(BUILD_PANDORA) // Pandora's glibc is too old #elif defined(HAVE_PTHREAD_SETNAME_NP)
return pthread_setname_np(pthread_self(), name); return pthread_setname_np(pthread_self(), name);
#else #else
UNUSED(name); UNUSED(name);