Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Thu, 2 Apr 2015 11:26:43 -0400
From: Rich Felker <>
Subject: Re: Resuming work on new semaphore

On Thu, Apr 02, 2015 at 10:42:16AM +0300, Alexander Monakov wrote:
> On Wed, 1 Apr 2015, Rich Felker wrote:
> > If just waiting, the negative semaphore value persists after the
> > waiter is killed. Subsequent posts will produce a wake for a waiter
> > that doesn't exist, and will thereby allow future waiters that arrive
> > when the semaphore value is zero to proceed immediately (leaving the
> > value negative) by consuming this wake. There are usage patterns where
> > trywait would never succeed again, but wait would succeed trivially.
> Interesting.  To examine the issue under a different light, consider that from
> the perspective of semaphore implementation, waiters that were killed,
> stopped, or pre-empted forever in the middle of sem_wait are
> indistinguishable.

Yes, I noticed this too. In that sense, theoretically there should be
no harm (aside from eventual overflow of pending wake counter) from
having asynchronously-killed waiters, assuming the implementation is
bug-free in the absence of async killing of waiters.

> Thus, subsequent sem_wait succeeds by effectively stealing
> a post, and to make things consistent you can teach sem_trywait to steal posts
> too (i.e. try atomic-decrement-if-positive val[1] just before returning
> EAGAIN, return 0 if that succeeds).

Hmm, perhaps that is valid. I'll have to think about it again. I was
thinking of having sem_trywait unconditionally down the value (val[0])
then immitate the exit path of sem_timedwait, but that's not valid
because another waiter could race and prevent sem_trywait from ever
being able to exit. But if it only does the down as a dec-if-positive
then it seems like it can safely dec-if-positive the wake count before
reporting failure.


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.