[OpenMP] Control KMP_CANCEL_THREADS via CMake and detect pthread_cancel (#193681)

KMP_CANCEL_THREADS was a preprocessor switch in kmp.h, with Android and
WASI explicitly #undef-ing it. Move the control to CMake:

- Detect pthread_cancel via check_symbol_exists()
- Drive KMP_CANCEL_THREADS from that result, emitted as a 0/1 #define in
kmp_config.h.cmake (overridable with -DLIBOMP_USE_CANCEL_THREADS=OFF).
- Drop the Android/WASM special-casing
This commit is contained in:
Mark Zhuang 2026-06-18 11:04:01 -05:00 committed by GitHub
parent 74a146dd9c
commit d81069d7c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 13 additions and 18 deletions

View file

@ -327,6 +327,11 @@ if(LIBOMP_STATS)
set(LIBOMP_USE_STDCPPLIB TRUE)
endif()
# Thread cancellation via pthread_cancel. Defaults to whether the symbol is
# available, but can be overridden via -DLIBOMP_USE_CANCEL_THREADS=OFF.
set(LIBOMP_USE_CANCEL_THREADS "${LIBOMP_HAVE_PTHREAD_CANCEL}" CACHE BOOL
"Enable thread cancellation via pthread_cancel?")
# Shared library can be switched to a static library
set(LIBOMP_ENABLE_SHARED TRUE CACHE BOOL
"Shared library instead of static library?")

View file

@ -100,6 +100,7 @@ endif()
# Check non-posix pthread API here before CMAKE_REQUIRED_DEFINITIONS gets messed up
check_symbol_exists(pthread_setname_np "pthread.h" LIBOMP_HAVE_PTHREAD_SETNAME_NP)
check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" LIBOMP_HAVE_PTHREAD_SET_NAME_NP)
check_symbol_exists(pthread_cancel "pthread.h" LIBOMP_HAVE_PTHREAD_CANCEL)
# Check for Unix shared memory
check_symbol_exists(shm_open "sys/mman.h" LIBOMP_HAVE_SHM_OPEN_NO_LRT)

View file

@ -45,21 +45,8 @@
#define TASK_DETACHABLE 1
#define TASK_UNDETACHABLE 0
#define KMP_CANCEL_THREADS
#define KMP_THREAD_ATTR
// Android does not have pthread_cancel. Undefine KMP_CANCEL_THREADS if being
// built on Android
#if defined(__ANDROID__)
#undef KMP_CANCEL_THREADS
#endif
// Some WASI targets (e.g., wasm32-wasi-threads) do not support thread
// cancellation.
#if KMP_OS_WASI
#undef KMP_CANCEL_THREADS
#endif
#if !KMP_OS_WASI
#include <signal.h>
#endif

View file

@ -96,6 +96,8 @@
#define KMP_HAVE_POSIX_MEMALIGN LIBOMP_HAVE_POSIX_MEMALIGN
#cmakedefine01 LIBOMP_HAVE__ALIGNED_MALLOC
#define KMP_HAVE__ALIGNED_MALLOC LIBOMP_HAVE__ALIGNED_MALLOC
#cmakedefine01 LIBOMP_USE_CANCEL_THREADS
#define KMP_CANCEL_THREADS LIBOMP_USE_CANCEL_THREADS
#cmakedefine01 OPENMP_ENABLE_LIBOMPTARGET
#define ENABLE_LIBOMPTARGET OPENMP_ENABLE_LIBOMPTARGET

View file

@ -446,7 +446,7 @@ void __kmp_terminate_thread(int gtid) {
if (!th)
return;
#ifdef KMP_CANCEL_THREADS
#if KMP_CANCEL_THREADS
KA_TRACE(10, ("__kmp_terminate_thread: kill (%d)\n", gtid));
status = pthread_cancel(th->th.th_info.ds.ds_thread);
if (status != 0 && status != ESRCH) {
@ -558,7 +558,7 @@ static void *__kmp_launch_worker(void *thr) {
__kmp_affinity_bind_init_mask(gtid);
#endif
#ifdef KMP_CANCEL_THREADS
#if KMP_CANCEL_THREADS
status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
// josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
@ -636,7 +636,7 @@ static void *__kmp_launch_monitor(void *thr) {
__kmp_check_stack_overlap((kmp_info_t *)thr);
#ifdef KMP_CANCEL_THREADS
#if KMP_CANCEL_THREADS
status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_type);
KMP_CHECK_SYSFAIL("pthread_setcanceltype", status);
// josh todo: isn't PTHREAD_CANCEL_ENABLE default for newly-created threads?
@ -1274,7 +1274,7 @@ void __kmp_remove_signals(void) {
#endif // KMP_HANDLE_SIGNALS
void __kmp_enable(int new_state) {
#ifdef KMP_CANCEL_THREADS
#if KMP_CANCEL_THREADS
int status, old_state;
status = pthread_setcancelstate(new_state, &old_state);
KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);
@ -1283,7 +1283,7 @@ void __kmp_enable(int new_state) {
}
void __kmp_disable(int *old_state) {
#ifdef KMP_CANCEL_THREADS
#if KMP_CANCEL_THREADS
int status;
status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, old_state);
KMP_CHECK_SYSFAIL("pthread_setcancelstate", status);