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

Torvald Riegel triegel@redhat.com
Wed Jun 10 19:45:00 GMT 2015


On Mon, 2015-06-08 at 22:42 +0200, Torvald Riegel wrote:
> 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".
> 
> This is a revision of
> https://sourceware.org/ml/libc-alpha/2015-01/msg00215.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.
> * 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.
> 
> Adhemerval, is this API compatible with the new cancellation scheme you
> are working on?
> 
> Roland, okay for NaCl?
> 
> Tested on x86_64-linux.
> 
> 
> 2015-06-08  Torvald Riegel  <triegel@redhat.com>
> 
> 	* nptl/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 comment.
> 	* nptl/sem_init.c (futex_private_if_supported): Remove.
> 	* 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_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.
> 

I've revised this patch to address Roland's feedback regarding the
"ignore_value (futex_wait ())" occurrences.  I've added a
futex_wait_simple call that returns void and explains the use case.
There are comments in cases where the pattern is not visible in the
immediate surroundings.

futex_wait_simple was the best name I could come up with.  It's meant to
be for the simplest futex_wait use there is: Just using it as an
additional point of potential blocking in what otherwise looks like a
simple busy-waiting loop.  See the comments for futex_wait_simple.

Otherwise, no changes except a comment on some synchronization code that
looked weird and that we should look at in detail in the future.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: futex-api.patch
Type: text/x-patch
Size: 53229 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20150610/31d875d9/attachment.bin>


More information about the Libc-alpha mailing list