[Openmp-commits] [openmp] 19f66fb - [libomp] Atomically claim library unregistration (#216713)
via Openmp-commits
openmp-commits at lists.llvm.org
Tue Sep 1 00:40:27 PDT 2026
Author: Robert Imschweiler
Date: 2026-09-01T09:40:22+02:00
New Revision: 19f66fb00411832bd0059d4c7c2b97d987915841
URL: https://github.com/llvm/llvm-project/commit/19f66fb00411832bd0059d4c7c2b97d987915841
DIFF: https://github.com/llvm/llvm-project/commit/19f66fb00411832bd0059d4c7c2b97d987915841.diff
LOG: [libomp] Atomically claim library unregistration (#216713)
Claude assisted with this patch.
Follow-up to https://github.com/llvm/llvm-project/pull/215988.
Added:
Modified:
openmp/runtime/src/kmp_runtime.cpp
Removed:
################################################################################
diff --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp
index f16bcf5e2cbbb..22ec2605718a3 100644
--- a/openmp/runtime/src/kmp_runtime.cpp
+++ b/openmp/runtime/src/kmp_runtime.cpp
@@ -6927,12 +6927,17 @@ void __kmp_register_library_startup(void) {
void __kmp_unregister_library(void) {
- // The library can be torn down before it ever registered itself, e.g. when
- // __kmp_abort_process() runs for a fatal error raised during environment
- // parsing. There is nothing to unregister then, and __kmp_registration_str
- // is still NULL, so the strcmp() below would dereference it.
- if (__kmp_registration_flag == 0)
+ // Claim the unregistration. Teardown can be entered concurrently from
+ // library shutdown, __kmp_abort_process() and the signal handler, none of
+ // which share a lock, and the library may never have registered itself at
+ // all (e.g. a fatal error raised before registration). Atomically take
+ // ownership of __kmp_registration_str so exactly one caller runs the
+ // teardown below; the others return without touching the freed string.
+ char *reg_str = __kmp_registration_str;
+ if (reg_str == NULL ||
+ !KMP_COMPARE_AND_STORE_PTR(&__kmp_registration_str, reg_str, NULL))
return;
+ __kmp_registration_flag = 0;
char *name = __kmp_reg_status_name();
char *value = NULL;
@@ -6968,9 +6973,7 @@ void __kmp_unregister_library(void) {
value = __kmp_env_get(name);
#endif
- KMP_DEBUG_ASSERT(__kmp_registration_flag != 0);
- KMP_DEBUG_ASSERT(__kmp_registration_str != NULL);
- if (value != NULL && strcmp(value, __kmp_registration_str) == 0) {
+ if (value != NULL && strcmp(value, reg_str) == 0) {
// Ok, this is our variable. Delete it.
#if defined(KMP_USE_SHM)
if (__kmp_shm_available) {
@@ -6992,13 +6995,10 @@ void __kmp_unregister_library(void) {
KMP_INTERNAL_FREE(temp_reg_status_file_name);
#endif
- KMP_INTERNAL_FREE(__kmp_registration_str);
+ KMP_INTERNAL_FREE(reg_str);
KMP_INTERNAL_FREE(value);
KMP_INTERNAL_FREE(name);
- __kmp_registration_flag = 0;
- __kmp_registration_str = NULL;
-
} // __kmp_unregister_library
// End of Library registration stuff.
More information about the Openmp-commits
mailing list