diff options
| author | jan.nijtmans <nijtmans@users.sourceforge.net> | 2025-09-18 09:15:02 (GMT) |
|---|---|---|
| committer | jan.nijtmans <nijtmans@users.sourceforge.net> | 2025-09-18 09:15:02 (GMT) |
| commit | 8e9b212e23cbdd947070dbac3d35dbe5b21d3a8f (patch) | |
| tree | d8c5b777c3d2e9dc24db2f8621a2dcc779428b59 /unix/tclUnixThrd.c | |
| parent | 866aec41d6d17c05d6260437e4a73628a8c0e109 (diff) | |
| download | tcl-8e9b212e23cbdd947070dbac3d35dbe5b21d3a8f.zip tcl-8e9b212e23cbdd947070dbac3d35dbe5b21d3a8f.tar.gz tcl-8e9b212e23cbdd947070dbac3d35dbe5b21d3a8f.tar.bz2 | |
Use C11 atomic functions. (WIP this is still UNIX-only)
Diffstat (limited to 'unix/tclUnixThrd.c')
| -rw-r--r-- | unix/tclUnixThrd.c | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/unix/tclUnixThrd.c b/unix/tclUnixThrd.c index 219d0c6..c33640f 100644 --- a/unix/tclUnixThrd.c +++ b/unix/tclUnixThrd.c @@ -12,6 +12,7 @@ */ #include "tclInt.h" +#include <stdatomic.h> #if TCL_THREADS @@ -68,8 +69,8 @@ typedef struct PMutex { pthread_mutex_t mutex; - pthread_t thread; - int counter; + volatile pthread_t thread; + volatile int counter; } PMutex; static void @@ -92,21 +93,24 @@ static void PMutexLock( PMutex *pmutexPtr) { - if (pmutexPtr->thread != pthread_self() || pmutexPtr->counter == 0) { + pthread_t mythread = pthread_self(); + if (__atomic_load_n(&pmutexPtr->counter, __ATOMIC_RELAXED) == 0) { pthread_mutex_lock(&pmutexPtr->mutex); - pmutexPtr->thread = pthread_self(); - pmutexPtr->counter = 0; + __atomic_store_n(&pmutexPtr->thread, mythread, __ATOMIC_RELAXED); + } else if (__atomic_load_n (&pmutexPtr->thread, __ATOMIC_RELAXED) != mythread) { + pthread_mutex_lock(&pmutexPtr->mutex); + __atomic_store_n(&pmutexPtr->thread, mythread, __ATOMIC_RELAXED); + __atomic_store_n(&pmutexPtr->counter, 0, __ATOMIC_RELAXED); } - pmutexPtr->counter++; + __atomic_fetch_add(&pmutexPtr->counter, 1, __ATOMIC_RELAXED); } static void PMutexUnlock( PMutex *pmutexPtr) { - pmutexPtr->counter--; - if (pmutexPtr->counter == 0) { - pmutexPtr->thread = 0; + if (__atomic_fetch_add(&pmutexPtr->counter, -1, __ATOMIC_RELAXED) == 1) { + __atomic_store_n(&pmutexPtr->thread, 0, __ATOMIC_RELAXED); pthread_mutex_unlock(&pmutexPtr->mutex); } } @@ -116,13 +120,11 @@ PCondWait( pthread_cond_t *pcondPtr, PMutex *pmutexPtr) { - int counter = pmutexPtr->counter; - - pmutexPtr->thread = 0; - pmutexPtr->counter = 0; + int counter =__atomic_exchange_n(&pmutexPtr->counter, 0, __ATOMIC_RELAXED); + __atomic_store_n(&pmutexPtr->thread, 0, __ATOMIC_RELAXED); pthread_cond_wait(pcondPtr, &pmutexPtr->mutex); - pmutexPtr->thread = pthread_self(); - pmutexPtr->counter = counter; + __atomic_store_n(&pmutexPtr->thread, pthread_self(), __ATOMIC_RELAXED); + __atomic_store_n(&pmutexPtr->counter, counter, __ATOMIC_RELAXED); } static void @@ -131,13 +133,11 @@ PCondTimedWait( PMutex *pmutexPtr, struct timespec *ptime) { - int counter = pmutexPtr->counter; - - pmutexPtr->thread = 0; - pmutexPtr->counter = 0; + int counter =__atomic_exchange_n(&pmutexPtr->counter, 0, __ATOMIC_RELAXED); + __atomic_store_n(&pmutexPtr->thread, 0, __ATOMIC_RELAXED); pthread_cond_timedwait(pcondPtr, &pmutexPtr->mutex, ptime); - pmutexPtr->thread = pthread_self(); - pmutexPtr->counter = counter; + __atomic_store_n(&pmutexPtr->thread, pthread_self(), __ATOMIC_RELAXED); + __atomic_store_n(&pmutexPtr->counter, counter, __ATOMIC_RELAXED); } /* |
