[PATCH] Improve thread safety of pthreads builds that rely on C11 atomic operations...
authorMartin Kroeker <martin@ruby.chemie.uni-freiburg.de>
Fri, 7 Mar 2025 12:48:28 +0000 (13:48 +0100)
committerMo Zhou <lumin@debian.org>
Mon, 10 Mar 2025 18:55:28 +0000 (14:55 -0400)
* Tighten memory orders for C11 atomic operations

Gbp-Pq: Name 39eb43d4410d0745ca2c78d1162d973abf3aa35b.patch

driver/others/blas_server.c

index 7306a3ecd884941f5d306a653d27d38478f72fc7..4b79136ec7c9d69935edad1406127a3bd2568f81 100644 (file)
@@ -146,8 +146,8 @@ typedef struct {
 } thread_status_t;
 
 #ifdef HAVE_C11
-#define        atomic_load_queue(p)            __atomic_load_n(p, __ATOMIC_RELAXED)
-#define        atomic_store_queue(p, v)        __atomic_store_n(p, v, __ATOMIC_RELAXED)
+#define        atomic_load_queue(p)            __atomic_load_n(p, __ATOMIC_ACQUIRE)
+#define        atomic_store_queue(p, v)        __atomic_store_n(p, v, __ATOMIC_RELEASE)
 #else
 #define        atomic_load_queue(p)            (blas_queue_t*)(*(volatile blas_queue_t**)(p))
 #define        atomic_store_queue(p, v)        (*(volatile blas_queue_t* volatile*)(p) = (v))
@@ -637,7 +637,9 @@ int exec_blas_async(BLASLONG pos, blas_queue_t *queue){
 
 #ifdef SMP_SERVER
   // Handle lazy re-init of the thread-pool after a POSIX fork
+  LOCK_COMMAND(&server_lock);
   if (unlikely(blas_server_avail == 0)) blas_thread_init();
+  UNLOCK_COMMAND(&server_lock);
 #endif
   BLASLONG i = 0;
   blas_queue_t *current = queue;