1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-07-29 11:41:21 +03:00

Fix semaphore destruction (bug 12674).

This commit fixes semaphore destruction by either using 64b atomic
operations (where available), or by using two separate fields when only
32b atomic operations are available.  In the latter case, we keep a
conservative estimate of whether there are any waiting threads in one
bit of the field that counts the number of available tokens, thus
allowing sem_post to atomically both add a token and determine whether
it needs to call futex_wake.

See:
https://sourceware.org/ml/libc-alpha/2014-12/msg00155.html
This commit is contained in:
Carlos O'Donell
2015-01-21 00:46:16 -05:00
parent a8db092ec0
commit 042e1521c7
34 changed files with 731 additions and 2102 deletions

View File

@ -18,17 +18,29 @@
#include <errno.h>
#include <semaphore.h>
#include <lowlevellock.h>
#include <shlib-compat.h>
#include "semaphoreP.h"
#include <kernel-features.h>
/* Returns FUTEX_PRIVATE if pshared is zero and private futexes are supported;
returns FUTEX_SHARED otherwise.
TODO Remove when cleaning up the futex API throughout glibc. */
static __always_inline int
futex_private_if_supported (int pshared)
{
if (pshared != 0)
return LLL_SHARED;
#ifdef __ASSUME_PRIVATE_FUTEX
return LLL_PRIVATE;
#else
return THREAD_GETMEM (THREAD_SELF, header.private_futex)
^ FUTEX_PRIVATE_FLAG;
#endif
}
int
__new_sem_init (sem, pshared, value)
sem_t *sem;
int pshared;
unsigned int value;
__new_sem_init (sem_t *sem, int pshared, unsigned int value)
{
/* Parameter sanity check. */
if (__glibc_unlikely (value > SEM_VALUE_MAX))
@ -40,16 +52,15 @@ __new_sem_init (sem, pshared, value)
/* Map to the internal type. */
struct new_sem *isem = (struct new_sem *) sem;
/* Use the values the user provided. */
isem->value = value;
#ifdef __ASSUME_PRIVATE_FUTEX
isem->private = pshared ? 0 : FUTEX_PRIVATE_FLAG;
/* Use the values the caller provided. */
#if __HAVE_64B_ATOMICS
isem->data = value;
#else
isem->private = pshared ? 0 : THREAD_GETMEM (THREAD_SELF,
header.private_futex);
isem->value = value << SEM_VALUE_SHIFT;
isem->nwaiters = 0;
#endif
isem->nwaiters = 0;
isem->private = futex_private_if_supported (pshared);
return 0;
}