[Openmp-commits] [openmp] d81069d - [OpenMP] Control KMP_CANCEL_THREADS via CMake and detect pthread_cancel (#193681)

via Openmp-commits openmp-commits at lists.llvm.org
Thu Jun 18 09:04:06 PDT 2026


Author: Mark Zhuang
Date: 2026-06-19T00:04:01+08:00
New Revision: d81069d7c3da46ea7bd000c3d6ab618e3db79bd4

URL: https://github.com/llvm/llvm-project/commit/d81069d7c3da46ea7bd000c3d6ab618e3db79bd4
DIFF: https://github.com/llvm/llvm-project/commit/d81069d7c3da46ea7bd000c3d6ab618e3db79bd4.diff

LOG: [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

Added: 
    

Modified: 
    openmp/runtime/CMakeLists.txt
    openmp/runtime/cmake/config-ix.cmake
    openmp/runtime/src/kmp.h
    openmp/runtime/src/kmp_config.h.cmake
    openmp/runtime/src/z_Linux_util.cpp

Removed: 
    


################################################################################
diff  --git a/openmp/runtime/CMakeLists.txt b/openmp/runtime/CMakeLists.txt
index be3dea7c6f946..ec140013eb625 100644
--- a/openmp/runtime/CMakeLists.txt
+++ b/openmp/runtime/CMakeLists.txt
@@ -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?")

diff  --git a/openmp/runtime/cmake/config-ix.cmake b/openmp/runtime/cmake/config-ix.cmake
index f076d4d43480d..d8d031a8c5aac 100644
--- a/openmp/runtime/cmake/config-ix.cmake
+++ b/openmp/runtime/cmake/config-ix.cmake
@@ -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)

diff  --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h
index fbeb58fab1d16..818e07c2bb23f 100644
--- a/openmp/runtime/src/kmp.h
+++ b/openmp/runtime/src/kmp.h
@@ -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

diff  --git a/openmp/runtime/src/kmp_config.h.cmake b/openmp/runtime/src/kmp_config.h.cmake
index 1f966008c60a5..c5e8cfa5154e9 100644
--- a/openmp/runtime/src/kmp_config.h.cmake
+++ b/openmp/runtime/src/kmp_config.h.cmake
@@ -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
 

diff  --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index 70a57c3c8f8a5..f42fc68488c91 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -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);


        


More information about the Openmp-commits mailing list