• Davide Libenzi's avatar
    eventfd: improve support for semaphore-like behavior · bcd0b235
    Davide Libenzi authored
    People started using eventfd in a semaphore-like way where before they
    were using pipes.
    
    That is, counter-based resource access.  Where a "wait()" returns
    immediately by decrementing the counter by one, if counter is greater than
    zero.  Otherwise will wait.  And where a "post(count)" will add count to
    the counter releasing the appropriate amount of waiters.  If eventfd the
    "post" (write) part is fine, while the "wait" (read) does not dequeue 1,
    but the whole counter value.
    
    The problem with eventfd is that a read() on the fd returns and wipes the
    whole counter, making the use of it as semaphore a little bit more
    cumbersome.  You can do a read() followed by a write() of COUNTER-1, but
    IMO it's pretty easy and cheap to make this work w/out extra steps.  This
    patch introduces a new eventfd flag that tells eventfd to only dequeue 1
    from the counter, allowing simple read/write to make it behave like a
    semaphore.  Simple test here:
    
    http://www.xmailserver.org/eventfd-sem.c
    
    To be back-compatible with earlier kernels, userspace applications should
    probe for the availability of this feature via
    
    #ifdef EFD_SEMAPHORE
    	fd = eventfd2 (CNT, EFD_SEMAPHORE);
    	if (fd == -1 && errno == EINVAL)
    		<fallback>
    #else
    		<fallback>
    #endif
    Signed-off-by: 's avatarDavide Libenzi <davidel@xmailserver.org>
    Cc: <linux-api@vger.kernel.org>
    Tested-by: 's avatarMichael Kerrisk <mtk.manpages@gmail.com>
    Cc: Ulrich Drepper <drepper@redhat.com>
    Signed-off-by: 's avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: 's avatarLinus Torvalds <torvalds@linux-foundation.org>
    bcd0b235
Name
Last commit
Last update
Documentation Loading commit data...
arch Loading commit data...
block Loading commit data...
crypto Loading commit data...
drivers Loading commit data...
firmware Loading commit data...
fs Loading commit data...
include Loading commit data...
init Loading commit data...
ipc Loading commit data...
kernel Loading commit data...
lib Loading commit data...
mm Loading commit data...
net Loading commit data...
samples Loading commit data...
scripts Loading commit data...
security Loading commit data...
sound Loading commit data...
usr Loading commit data...
virt/kvm Loading commit data...
.gitignore Loading commit data...
.mailmap Loading commit data...
COPYING Loading commit data...
CREDITS Loading commit data...
Kbuild Loading commit data...
MAINTAINERS Loading commit data...
Makefile Loading commit data...
README Loading commit data...
REPORTING-BUGS Loading commit data...