Re: the new read/write semaphores in 2.3.30

Benjamin C.R. LaHaise (blah@kvack.org)
Wed, 8 Dec 1999 17:02:29 -0500 (EST)


On Wed, 8 Dec 1999, Dimitris Michailidis wrote:

> If I understood the code right sem->count cannot be >0 at that point because
> the writer that is executing that code has been granted the semaphore, so
> this should trigger BUG(). OTOH, sem->count can be 0 and in that case we
> should wake up at most one process on sem->wait. If we wake up several only
> one of them will branch to one of the *failed_biased() functions and the
> others will end up where they are right now, so no reason to wake them up.
> Instead, these processes should be kicked in down_read_failed_biased() (in
> fact we need to wake up all the readers and at most one writer there).

That is correct -- sem->count should be zero or less at that point. If it
is > 0, something has gone wrong somewhere.

> The way it is right now I don't see how we wake up all the waiting readers.
> Suppose a writer has been granted the semaphore and is about to leave
> down_write_failed_biased(), and that two readers are on sem->wait having
> executed down_read_failed() (the count was <0 when they first arrived
> because other readers had the lock at the time and the writer was sleeping).
> The writer wakes up the readers, one of them goes to
> down_read_failed_biased() and the other to down_read_failed(). When the
> writer up()s it will wake up the biased reader, but who wakes up the other
> one?

All readers, regardless if they own the bias or not wait on sem->wait, not
sem->write_bias_wait. When someone sets read_bias_granted, they do a
wakeup on sem->wait, not sem->write_bias_wait, which wakes up all readers
since they're not exclusive. Of course, this will also wake one writer
who is waiting, leading to a small stampede type race if the writer gets
in before the readers. So adding a count of sleeping readers is probably
the right thing.

I'd like to combine the two wait queues, but I haven't convinced myself
that playing the game of putting an exclusive task at the head of
rwsem->wait is safe yet.

> Is my reading of the code correct?

Pretty close! =)

-ben

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/