Re: [PATCH] Futexes IV (Fast Lightweight Userspace Semaphores)

Martin Wirth
2002-03-13T09:13:43Z

> > > On Tue, 12 Mar 2002, Rusty Russell wrote:
> > > > > > > You've convinced me.
> > > > > Damn. Because now I've been playing with a different approach.
> > > I don't think your current patch is very useful.

> I agree. But your "Applied" EMail rushed me into posting it.

The normal way to use multithreading under UNIX is the pthread
library. Here the condition variables are the equivalent to kernel
wait queues. So I think to really implement a fast pthread lib based
on futexes one needs some means to implement condition variables
(with synchronous futex release to implement pthread_cond_wait(..)!).

This could either be done with the exported waitqueue approach or a bit
easier (but less general) by associating a second hashed waitqueue with
each futex (maybe marked by the odd offset+1?). Then we would have two
additional variants of sys_futex (with parameters FUTEX_WAIT,
FUTEX_SIGNAL).

The principle implementation is:

FUTEX_WAIT:
add_to_cond_queue
current->state=INTERRUPTIBLE
futex_up
schedule
remove_from_cond_queue
futex_down

FUTEX_SIGNAL
wake_up_all on cond_queue

Later we may also want FUTEX_SIGNAL_ONE and FUTEX_WAIT_TIMEOUT.

The user space code for pthread_cond_wait then of course needs a
chaining of the protecting pthread_mutex and the futex used as condition
variable.

Martin