diff options
-rw-r--r-- | Modules/CheckForPthreads.c | 30 | ||||
-rw-r--r-- | Modules/FindThreads.cmake | 53 |
2 files changed, 69 insertions, 14 deletions
diff --git a/Modules/CheckForPthreads.c b/Modules/CheckForPthreads.c new file mode 100644 index 0000000..0f57245 --- /dev/null +++ b/Modules/CheckForPthreads.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <pthread.h> +#include <unistd.h> + +void* runner(void*); + +int res = 0; +int main() +{ + pthread_t tid[2]; + pthread_create(&tid[0], 0, runner, (void*)1); + pthread_create(&tid[1], 0, runner, (void*)2); + + usleep(1); // for strange behavior on single-processor sun + pthread_join(tid[0], 0); + pthread_join(tid[1], 0); + + return res; +} + +void* runner(void* args) +{ + int cc; + for ( cc = 0; cc < 10; cc ++ ) + { + printf("%d CC: %d\n", (int)args, cc); + } + res ++; + return 0; +} diff --git a/Modules/FindThreads.cmake b/Modules/FindThreads.cmake index d5cab19..f3050e8 100644 --- a/Modules/FindThreads.cmake +++ b/Modules/FindThreads.cmake @@ -16,20 +16,45 @@ ENDIF(CMAKE_SYSTEM MATCHES IRIX) CHECK_INCLUDE_FILE("pthread.h" CMAKE_HAVE_PTHREAD_H) IF(CMAKE_HAVE_PTHREAD_H) IF(NOT CMAKE_HAVE_SPROC_H) - CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE) - IF(CMAKE_HAVE_PTHREADS_CREATE) - SET(CMAKE_THREAD_LIBS_INIT "-lpthreads") - ENDIF(CMAKE_HAVE_PTHREADS_CREATE) - CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE) - IF(CMAKE_HAVE_PTHREAD_CREATE) - SET(CMAKE_THREAD_LIBS_INIT "-lpthread") - ENDIF(CMAKE_HAVE_PTHREAD_CREATE) - IF(CMAKE_SYSTEM MATCHES "SunOS.*") - CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE) - IF(CMAKE_HAVE_THR_CREATE) - SET(CMAKE_THREAD_LIBS_INIT "-lthread") - ENDIF(CMAKE_HAVE_THR_CREATE) - ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*") + IF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG") + MESSAGE(STATUS "Check if compiler accepts -pthread") + TRY_RUN(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG + ${CMAKE_BINARY_DIR} + ${CMAKE_ROOT}/Modules/CheckForPthreads.c + CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread + OUTPUT_VARIABLE OUTPUT) + IF(THREADS_HAVE_PTHREAD_ARG) + IF(THREADS_PTHREAD_ARG MATCHES "^2$") + MESSAGE(STATUS "Check if compiler accepts -pthread - yes") + ELSE(THREADS_PTHREAD_ARG MATCHES "^2$") + MESSAGE(STATUS "Check if compiler accepts -pthread - no") + FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeError.log + "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n") + ENDIF(THREADS_PTHREAD_ARG MATCHES "^2$") + ELSE(THREADS_HAVE_PTHREAD_ARG) + MESSAGE(STATUS "Check if compiler accepts -pthread - no") + FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeError.log + "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n") + ENDIF(THREADS_HAVE_PTHREAD_ARG) + ENDIF("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG") + IF(THREADS_HAVE_PTHREAD_ARG) + SET(CMAKE_THREAD_LIBS_INIT "-pthread") + ELSE(THREADS_HAVE_PTHREAD_ARG) + CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE) + IF(CMAKE_HAVE_PTHREADS_CREATE) + SET(CMAKE_THREAD_LIBS_INIT "-lpthreads") + ENDIF(CMAKE_HAVE_PTHREADS_CREATE) + CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE) + IF(CMAKE_HAVE_PTHREAD_CREATE) + SET(CMAKE_THREAD_LIBS_INIT "-lpthread") + ENDIF(CMAKE_HAVE_PTHREAD_CREATE) + IF(CMAKE_SYSTEM MATCHES "SunOS.*") + CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE) + IF(CMAKE_HAVE_THR_CREATE) + SET(CMAKE_THREAD_LIBS_INIT "-lthread") + ENDIF(CMAKE_HAVE_THR_CREATE) + ENDIF(CMAKE_SYSTEM MATCHES "SunOS.*") + ENDIF(THREADS_HAVE_PTHREAD_ARG) ENDIF(NOT CMAKE_HAVE_SPROC_H) ENDIF(CMAKE_HAVE_PTHREAD_H) |