[PATCH v2] Add and use new glibc-internal futex API.

Torvald Riegel triegel@redhat.com
Wed Jun 17 22:39:00 GMT 2015


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.  The latter is done by adding
lll_futex_abstimed_wait.  I expect that we will refactor this later on,
depending on how we do the lll_ parts.

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.
    
The sanity checks regarding whether shared futexes are supported abort
instead of returning errors because POSIX error specs don't really
consider that there could be no support for shared futexes.  Aborting is
better than returning an unspecified error or an error specified for a
different condition; only NaCl has no support for shared futexes.

This is a revision of
https://sourceware.org/ml/libc-alpha/2015-06/msg00284.html

I have transformed all the lll_futex_* uses that I'm aware of except the
following:
* Anything related to lowlevellock or mutexes.
* sparc-specific files: I'll send a follow-up patch so this can be
reviewed and tested separately.
* pthread condvar: I'll send a follow-up patch on top of my revised
condvar implementation.  This will use
futex_supports_exact_relative_timeouts() (as required by the
CLOCK_MONOTONIC clock setting).
* All tls.h files: Siddhesh wants to look at this area in more detail,
so I'll work with him to see how to best move this to the new API.

Interacting with futex words requires atomic accesses, which isn't done
by most of glibc's current futex callers.  I did not fix these in this
patch to keep the patch easier to review: Using the new futex API is in
most cases a pretty mechanical change, which I didn't want to obfuscate
by lots of other changes to atomics.  The core motivation behind this
patch is to add error handling and improve the internal futex API, not
to change any synchronization.
Nonetheless, using atomics where they are needed is on my list of things
to do, so don't worry :)
Specifically, this is already done in the my new condvar implementation
and in the new semaphore.  It will get done for rwlock in the new
implementation I'm working on.  I also plan to update the barrier
implementation.  For TLS, this is something Siddhesh has on his radar,
AFAIK.  Adhemerval is working on a new cancellation scheme.

I kept the old semaphore code unchanged for now because I don't have a
testing setup for this ready.

Roland, okay for NaCl?  I decided to not try to "optimize" the
shared/private setting at data structure initialization time because I
didn't see a good way to specify the error conditions for the futex_*
functions then: We do want those to sanity check shared/private and not
just rely on the shared/private initialization to do the right thing --
but if we do that, we can as well transform private into shared if
that's actually necessary.
For support of shared mutexes, I have added sanity checks (see above).
The actual __ASSUME_FUTEX_* flags can be removed later, once we tackled
all the remaining uses (lll_*, TLS, ...).
I'll leave it to you to merge NaCl lowlevellock-futex.h into
futex-internal.h because you can do the testing, and know which errors
the NaCl futex functions actually return.  I suppose doing so is a good
thing even though there might be some dupplication with
lowlevellock-futex.h as long as that one still exists.

I'll adapt the SPARC version after this patch has been ack'ed.

Tested on x86_64-linux.


2015-06-17  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/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/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_barrier_wait.c (pthread_barrier_wait): Likewise.
	* nptl/pthread_barrier_init.c (pthread_barrier_init): Add comments
	and abort if attribute initializer failed to sanitize inputs.
	* nptl/pthread_barrierattr_setpshared.c
	(pthread_barrierattr_setpshared): Add sanity check.
	* nptl/sem_init.c (futex_private_if_supported): Remove.
	(__new_sem_init): Adapt and add sanity check.
	* nptl/sem_post.c (futex_wake): Likewise.
	* nptl/sem_waitcommon.c (futex_abstimed_wait, futex_wake): Likewise.
	(do_futex_wait): Use futex wrappers with error checking.
	* nptl/sem_wait.c: Include lowlevellock.h.
	* nptl/sem_open.c (sem_open): Use FUTEX_SHARED.
	* sysdeps/nptl/lowlevellock-futex.h (lll_futex_abstimed_wait): New.
	* sysdeps/unix/sysv/linux/lowlevellock-futex.h
	(lll_futex_abstimed_wait): New.
	* sysdeps/nacl/lowlevellock-futex.h (lll_futex_abstimed_wait): New.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: futex-api.patch
Type: text/x-patch
Size: 69105 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20150617/e095ac6d/attachment.bin>


More information about the Libc-alpha mailing list