diff options
| author | Torvald Riegel <triegel@redhat.com> | 2014-12-04 14:12:23 +0100 |
|---|---|---|
| committer | Torvald Riegel <triegel@redhat.com> | 2015-07-10 13:47:09 +0200 |
| commit | a2f0363f817a58c4d3f439aa515a3b1d73efde36 (patch) | |
| tree | 6204a1208c1421d573e8c91e4523376a18e1dd60 | |
| parent | 203c1a898dd2e281eae30f3c57ceb8d4a50b00f4 (diff) | |
| download | glibc-a2f0363f817a58c4d3f439aa515a3b1d73efde36.tar.xz glibc-a2f0363f817a58c4d3f439aa515a3b1d73efde36.zip | |
Add and use new glibc-internal futex API.
This adds new functions for futex operations, starting with wait,
abstimed_wait, reltimed_wait, wake. They add documentation and error
checking according to the current draft of the Linux kernel futex manpage.
Waiting with absolute or relative timeouts is split into separate functions.
This allows for removing a few cases of code duplication in pthreads code,
which uses absolute timeouts; also, it allows us to put platform-specific
code to go from an absolute to a relative timeout into the platform-specific
futex abstractions..
Futex operations that can be canceled are also split out into separate
functions suffixed by "_cancelable".
There are separate versions for both Linux and NaCl; while they currently
differ only slightly, my expectation is that the separate versions of
lowlevellock-futex.h will eventually be merged into futex-internal.h
when we get to move the lll_ functions over to the new futex API.
40 files changed, 963 insertions, 492 deletions
@@ -1,3 +1,73 @@ +2015-07-10 Torvald Riegel <triegel@redhat.com> + + * sysdeps/nptl/futex-internal.h: New file. + * sysdeps/nacl/futex-internal.h: New file. + * sysdeps/unix/sysv/linux/futex-internal.h: New file. + * nptl/allocatestack.c (setxid_mark_thread): Use futex wrappers with + error checking. + (setxid_unmark_thread): Likewise. + (__nptl_setxid): Likewise. + (__wait_lookup_done): Likewise. + * nptl/cancellation.c (__pthread_disable_asynccancel): Likewise. + * nptl/nptl-init.c (sighandler_setxid): Likewise. + * nptl/pthread_create.c (START_THREAD_DEFN): Likewise. + * nptl/pthread_once.c (clear_once_control): Likewise. + (__pthread_once_slow): Likewise. + * nptl/unregister-atfork.c (__unregister_atfork): Likewise. + * sysdeps/nacl/exit-thread.h (__exit_thread): Likewise. + * sysdeps/nptl/aio_misc.h (AIO_MISC_NOTIFY, AIO_MISC_WAIT): Likewise. + * sysdeps/nptl/fork.c (__libc_fork): Likewise. + * sysdeps/nptl/gai_misc.h (GAI_MISC_NOTIFY, GAI_MISC_WAIT): Likewise. + * nptl/pthread_rwlock_rdlock.c (__pthread_rwlock_rdlock_slow): + Likewise. + (__pthread_rwlock_rdlock): Likewise. + * nptl/pthread_rwlock_timedrdlock.c (pthread_rwlock_timedrdlock): + Likewise. + * nptl/pthread_rwlock_timedwrlock.c (pthread_rwlock_timedwrlock): + Likewise. + * nptl/pthread_rwlock_tryrdlock.c (__pthread_rwlock_tryrdlock): + Likewise. + * nptl/pthread_rwlock_unlock.c (__pthread_rwlock_unlock): Likewise. + * nptl/pthread_rwlock_wrlock.c (__pthread_rwlock_wrlock_slow: + Likewise. + * nptl/pthread_rwlock_init (__pthread_rwlock_init): Remove + __ASSUME_PRIVATE_FUTEX check. + * nptl/pthread_rwlockattr_setpshared (pthread_rwlockattr_setpshared): + Check that shared futexes are supported. + * nptl/pthread_barrier_wait.c (pthread_barrier_wait): Use futex + wrappers with error checking. + * nptl/pthread_barrier_init.c (pthread_barrier_init): Add comments, + remove attribute sanity check and __ASSUME_PRIVATE_FUTEX check. + * nptl/pthread_barrierattr_setpshared.c + (pthread_barrierattr_setpshared): Check that shared futexes are + supported. + * nptl/pthread_condattr_setpshared.c (pthread_condattr_setpshared): + Likewise. + * nptl/pthread_mutexattr_setpshared.c (pthread_mutexattr_setpshared): + Likewise. + * nptl/sem_init.c (futex_private_if_supported): Remove. + (__new_sem_init): Adapt and check that shared futexes are supported. + * nptl/sem_open.c (sem_open): Likewise. + * nptl/sem_post.c (futex_wake): Remove. + * nptl/sem_waitcommon.c (futex_abstimed_wait, futex_wake): Remove. + (do_futex_wait): Use futex wrappers with error checking. + * nptl/sem_wait.c: Include lowlevellock.h. + * sysdeps/sparc/nptl/pthread_barrier_init.c (__pthread_barrier_init): + Use futex_supports_pshared. + * sysdeps/sparc/nptl/pthread_barrier_wait.c (pthread_barrier_wait): + Use futex wrappers with error checking. + * sysdeps/sparc/sparc32/pthread_barrier_wait.c (pthread_barrier_wait): + Likewise. + * sysdeps/sparc/sparc32/sem_init.c (futex_private_if_supported): Remove. + * sysdeps/sparc/sparc32/sem_post.c (futex_wake): Likewise. + * sysdeps/sparc/sparc32/sem_open.c (sem_open): Use FUTEX_SHARED. + * sysdeps/sparc/sparc32/sem_waitcommon.c (futex_abstimed_wait): Remove. + (futex_wake): Likewise. + (sem_assume_only_signals_cause_futex_EINTR): Likewise. + (do_futex_wait): Use futex wrappers with error checking. + (__new_sem_wait_slow): Update EINTR handling. + * sysdeps/sparc/sparc32/sem_wait.c: Include lowlevellock.h. + 2015-07-09 Martin Sebor <msebor@redhat.com> [BZ #18435] diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c index 8e620c46e9..c56a4df12c 100644 --- a/nptl/allocatestack.c +++ b/nptl/allocatestack.c @@ -29,6 +29,7 @@ #include <tls.h> #include <list.h> #include <lowlevellock.h> +#include <futex-internal.h> #include <kernel-features.h> #include <stack-aliasing.h> @@ -987,7 +988,7 @@ setxid_mark_thread (struct xid_command *cmdp, struct pthread *t) if (t->setxid_futex == -1 && ! atomic_compare_and_exchange_bool_acq (&t->setxid_futex, -2, -1)) do - lll_futex_wait (&t->setxid_futex, -2, LLL_PRIVATE); + futex_wait_simple (&t->setxid_futex, -2, FUTEX_PRIVATE); while (t->setxid_futex == -2); /* Don't let the thread exit before the setxid handler runs. */ @@ -1005,7 +1006,7 @@ setxid_mark_thread (struct xid_command *cmdp, struct pthread *t) if ((ch & SETXID_BITMASK) == 0) { t->setxid_futex = 1; - lll_futex_wake (&t->setxid_futex, 1, LLL_PRIVATE); + futex_wake (&t->setxid_futex, 1, FUTEX_PRIVATE); } return; } @@ -1032,7 +1033,7 @@ setxid_unmark_thread (struct xid_command *cmdp, struct pthread *t) /* Release the futex just in case. */ t->setxid_futex = 1; - lll_futex_wake (&t->setxid_futex, 1, LLL_PRIVATE); + futex_wake (&t->setxid_futex, 1, FUTEX_PRIVATE); } @@ -1141,7 +1142,8 @@ __nptl_setxid (struct xid_command *cmdp) int cur = cmdp->cntr; while (cur != 0) { - lll_futex_wait (&cmdp->cntr, cur, LLL_PRIVATE); + futex_wait_simple ((unsigned int *) &cmdp->cntr, cur, + FUTEX_PRIVATE); cur = cmdp->cntr; } } @@ -1251,7 +1253,8 @@ __wait_lookup_done (void) continue; do - lll_futex_wait (gscope_flagp, THREAD_GSCOPE_FLAG_WAIT, LLL_PRIVATE); + futex_wait_simple ((unsigned int *) gscope_flagp, + THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE); while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT); } @@ -1273,7 +1276,8 @@ __wait_lookup_done (void) continue; do - lll_futex_wait (gscope_flagp, THREAD_GSCOPE_FLAG_WAIT, LLL_PRIVATE); + futex_wait_simple ((unsigned int *) gscope_flagp, + THREAD_GSCOPE_FLAG_WAIT, FUTEX_PRIVATE); while (*gscope_flagp == THREAD_GSCOPE_FLAG_WAIT); } diff --git a/nptl/cancellation.c b/nptl/cancellation.c index deac1ebb3e..2bd31686fd 100644 --- a/nptl/cancellation.c +++ b/nptl/cancellation.c @@ -19,6 +19,7 @@ #include <setjmp.h> #include <stdlib.h> #include "pthreadP.h" +#include <futex-internal.h> /* The next two functions are similar to pthread_setcanceltype() but @@ -93,7 +94,8 @@ __pthread_disable_asynccancel (int oldtype) while (__builtin_expect ((newval & (CANCELING_BITMASK | CANCELED_BITMASK)) == CANCELING_BITMASK, 0)) { - lll_futex_wait (&self->cancelhandling, newval, LLL_PRIVATE); + futex_wait_simple ((unsigned int *) &self->cancelhandling, newval, + FUTEX_PRIVATE); newval = THREAD_GETMEM (self, cancelhandling); } } diff --git a/nptl/nptl-init.c b/nptl/nptl-init.c index 8a511610cd..c043fb50ab 100644 --- a/nptl/nptl-init.c +++ b/nptl/nptl-init.c @@ -34,6 +34,7 @@ #include <shlib-compat.h> #include <smp.h> #include <lowlevellock.h> +#include <futex-internal.h> #include <kernel-features.h> #include <libc-internal.h> #include <pthread-pids.h> @@ -279,10 +280,10 @@ sighandler_setxid (int sig, siginfo_t *si, void *ctx) /* And release the futex. */ self->setxid_futex = 1; - lll_futex_wake (&self->setxid_futex, 1, LLL_PRIVATE); + futex_wake (&self->setxid_futex, 1, FUTEX_PRIVATE); if (atomic_decrement_val (&__xidcmd->cntr) == 0) - lll_futex_wake (&__xidcmd->cntr, 1, LLL_PRIVATE); + futex_wake ((unsigned int *) &__xidcmd->cntr, 1, FUTEX_PRIVATE); } #endif diff --git a/nptl/pthread_barrier_init.c b/nptl/pthread_barrier_init.c index 5ea9fad291..8fe15ecba5 100644 --- a/nptl/pthread_barrier_init.c +++ b/nptl/pthread_barrier_init.c @@ -36,6 +36,7 @@ __pthread_barrier_init (barrier, attr, count) { struct pthread_barrier *ibarrier; + /* XXX EINVAL is not specified by POSIX as a possible error code. */ if (__glibc_unlikely (count == 0)) return EINVAL; @@ -44,11 +45,6 @@ __pthread_barrier_init (barrier, attr, count) ? iattr = (struct pthread_barrierattr *) attr : &default_barrierattr); - if (iattr->pshared != PTHREAD_PROCESS_PRIVATE - && __builtin_expect (iattr->pshared != PTHREAD_PROCESS_SHARED, 0)) - /* Invalid attribute. */ - return EINVAL; - ibarrier = (struct pthread_barrier *) barrier; /* Initialize the individual fields. */ @@ -57,14 +53,10 @@ __pthread_barrier_init (barrier, attr, count) ibarrier->init_count = count; ibarrier->curr_event = 0; -#ifdef __ASSUME_PRIVATE_FUTEX + /* XXX Don't use FUTEX_SHARED or FUTEX_PRIVATE as long as there are still + assembly implementations that expect the value determined below. */ ibarrier->private = (iattr->pshared != PTHREAD_PROCESS_PRIVATE ? 0 : FUTEX_PRIVATE_FLAG); -#else - ibarrier->private = (iattr->pshared != PTHREAD_PROCESS_PRIVATE - ? 0 : THREAD_GETMEM (THREAD_SELF, - header.private_futex)); -#endif return 0; } diff --git a/nptl/pthread_barrier_wait.c b/nptl/pthread_barrier_wait.c index d69a929ef6..2b34e3097b 100644 --- a/nptl/pthread_barrier_wait.c +++ b/nptl/pthread_barrier_wait.c @@ -19,6 +19,7 @@ #include <errno.h> #include <sysdep.h> #include <lowlevellock.h> +#include <futex-internal.h> #include <pthreadP.h> @@ -29,9 +30,12 @@ __pthread_barrier_wait (barrier) { struct pthread_barrier *ibarrier = (struct pthread_barrier *) barrier; int result = 0; + int lll_private = ibarrier->private ^ FUTEX_PRIVATE_FLAG; + int futex_private = (lll_private == LLL_PRIVATE + ? FUTEX_PRIVATE : FUTEX_SHARED); /* Make sure we are alone. */ - lll_lock (ibarrier->lock, ibarrier->private ^ FUTEX_PRIVATE_FLAG); + lll_lock (ibarrier->lock, lll_private); /* One more arrival. */ --ibarrier->left; @@ -44,8 +48,7 @@ __pthread_barrier_wait (barrier) ++ibarrier->curr_event; /* Wake up everybody. */ - lll_futex_wake (&ibarrier->curr_event, INT_MAX, - ibarrier->private ^ FUTEX_PRIVATE_FLAG); + futex_wake (&ibarrier->curr_event, INT_MAX, futex_private); /* This is the thread which finished the serialization. */ result = PTHREAD_BARRIER_SERIAL_THREAD; @@ -57,12 +60,11 @@ __pthread_barrier_wait (barrier) unsigned int event = ibarrier->curr_event; /* Before suspending, make the barrier available to others. */ - lll_unlock (ibarrier->lock, ibarrier->private ^ FUTEX_PRIVATE_FLAG); + lll_unlock (ibarrier->lock, lll_private); /* Wait for the event counter of the barrier to change. */ do - lll_futex_wait (&ibarrier->curr_event, event, - ibarrier->private ^ FUTEX_PRIVATE_FLAG); + futex_wait_simple (&ibarrier->curr_event, event, futex_private); while (event == ibarrier->curr_event); } @@ -72,7 +74,7 @@ __pthread_barrier_wait (barrier) /* If this was the last woken thread, unlock. */ if (atomic_increment_val (&ibarrier->left) == init_count) /* We are done. */ - lll_unlock (ibarrier->lock, ibarrier->private ^ FUTEX_PRIVATE_FLAG); + lll_unlock (ibarrier->lock, lll_private); return result; } diff --git a/nptl/pthread_barrierattr_setpshared.c b/nptl/pthread_barrierattr_setpshared.c index 86d72c5d87..eeaee5d430 100644 --- a/nptl/pthread_barrierattr_setpshared.c +++ b/nptl/pthread_barrierattr_setpshared.c @@ -18,6 +18,7 @@ #include <errno.h> #include "pthreadP.h" +#include <futex-internal.h> int @@ -27,9 +28,9 @@ pthread_barrierattr_setpshared (attr, pshared) { struct pthread_barrierattr *iattr; - if (pshared != PTHREAD_PROCESS_PRIVATE - && __builtin_expect (pshared != PTHREAD_PROCESS_SHARED, 0)) - return EINVAL; + int err = futex_supports_pshared (pshared); + if (err != 0) + return err; iattr = (struct pthread_barrierattr *) attr; diff --git a/nptl/pthread_condattr_setpshared.c b/nptl/pthread_condattr_setpshared.c index 3a760cfdf1..a015403cf5 100644 --- a/nptl/pthread_condattr_setpshared.c +++ b/nptl/pthread_condattr_setpshared.c @@ -18,15 +18,16 @@ #include <errno.h> #include <pthreadP.h> +#include <futex-internal.h> int pthread_condattr_setpshared (attr, pshared) pthread_condattr_t *attr; int pshared; { - if (pshared != PTHREAD_PROCESS_PRIVATE - && __builtin_expect (pshared != PTHREAD_PROCESS_SHARED, 0)) - return EINVAL; + int err = futex_supports_pshared (pshared); + if (err != 0) + return err; int *valuep = &((struct pthread_condattr *) attr)->value; diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index 71a56193e6..d10f4ea800 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -31,6 +31,7 @@ #include <kernel-features.h> #include <exit-thread.h> #include <default-sched.h> +#include <futex-internal.h> #include <shlib-compat.h> @@ -269,7 +270,7 @@ START_THREAD_DEFN /* Allow setxid from now onwards. */< |
