diff options
author | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2020-12-13 15:14:40 +0000 |
---|---|---|
committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2020-12-16 01:58:33 +0100 |
commit | c8f9421298f5f973b31a7cbbc76e61b06eca03bc (patch) | |
tree | 86bf4c058624d76b6e35fd44f1f506c3ad9b3d94 /sysdeps/htl/sem-post.c | |
parent | 644d98ec4d8405e9b721ecb715483ea1983e116f (diff) | |
download | glibc-c8f9421298f5f973b31a7cbbc76e61b06eca03bc.tar.gz glibc-c8f9421298f5f973b31a7cbbc76e61b06eca03bc.tar.xz glibc-c8f9421298f5f973b31a7cbbc76e61b06eca03bc.zip |
htl: Add pshared semaphore support
The implementation is extremely similar to the nptl implementation, but with slight differences in the futex interface. This fixes some of BZ 25521.
Diffstat (limited to 'sysdeps/htl/sem-post.c')
-rw-r--r-- | sysdeps/htl/sem-post.c | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/sysdeps/htl/sem-post.c b/sysdeps/htl/sem-post.c index 720b73a059..83a3279c84 100644 --- a/sysdeps/htl/sem-post.c +++ b/sysdeps/htl/sem-post.c @@ -19,48 +19,50 @@ #include <semaphore.h> #include <assert.h> +#include <hurdlock.h> + #include <pt-internal.h> int __sem_post (sem_t *sem) { - struct __pthread *wakeup; + struct new_sem *isem = (struct new_sem *) sem; + int flags = isem->pshared ? GSYNC_SHARED : 0; + +#if __HAVE_64B_ATOMICS + uint64_t d = atomic_load_relaxed (&isem->data); - __pthread_spin_wait (&sem->__lock); - if (sem->__value > 0) - /* Do a quick up. */ + do { - if (sem->__value == SEM_VALUE_MAX) + if ((d & SEM_VALUE_MASK) == SEM_VALUE_MAX) { - __pthread_spin_unlock (&sem->__lock); errno = EOVERFLOW; return -1; } - - assert (sem->__queue == NULL); - sem->__value++; - __pthread_spin_unlock (&sem->__lock); - return 0; } + while (!atomic_compare_exchange_weak_release (&isem->data, &d, d + 1)); - if (sem->__queue == NULL) - /* No one waiting. */ + if ((d >> SEM_NWAITERS_SHIFT) != 0) + /* Wake one waiter. */ + __lll_wake (((unsigned int *) &isem->data) + SEM_VALUE_OFFSET, flags); +#else + unsigned int v = atomic_load_relaxed (&isem->value); + + do { - sem->__value = 1; - __pthread_spin_unlock (&sem->__lock); - return 0; + if ((v >> SEM_VALUE_SHIFT) == SEM_VALUE_MAX) + { + errno = EOVERFLOW; + return -1; + } } + while (!atomic_compare_exchange_weak_release + (&isem->value, &v, v + (1 << SEM_VALUE_SHIFT))); - /* Wake someone up. */ - - /* First dequeue someone. */ - wakeup = sem->__queue; - __pthread_dequeue (wakeup); - - /* Then drop the lock and transfer control. */ - __pthread_spin_unlock (&sem->__lock); - - __pthread_wakeup (wakeup); + if ((v & SEM_NWAITERS_MASK) != 0) + /* Wake one waiter. */ + __lll_wake (&isem->value, flags); +#endif return 0; } |