Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #513

Re: mutex and condition variable implementation using futex

From Alexander Terekhov <terekhov@web.de>
Newsgroups comp.programming.threads
Subject Re: mutex and condition variable implementation using futex
Date 2011-08-15 11:31 +0200
Organization A noiseless patient Spider
Message-ID <4E48E78F.5A0B6048@web.de> (permalink)
References <6752ab19-9142-471e-8b83-14fdc3784567@glegroupsg2000goo.googlegroups.com>

Show all headers | View raw


Sudhanshu Shukla wrote:
[...]
> Any hints for further optimizing the mutex and condition variables 
> would be greatly appreciated.

Linux's futex interface isn't really optimal for mutexes and condvars
because the kernel doesn't maintain 'waiters bit' (e.g. 0x80000000)
indicating that there are queued waiters.

With waiters bit, mutex would be simply

  WHILE atomic_bit_test_set_hlb(&lock, 1) // efficient 'acquire' 
    lock_queue_wait(&lock, 1) // wait if locked bit is set

for lock() and

   uintptr_t lock_queue;
   IF atomic_decrement_rel(lock_queue = &lock)
     THEN lock_queue_wake(lock_queue, 1) 

for unlock.

For condvar, it is better to let the kernel set waiters bit and then
unlock the associated mutex.

The signal would be simply

   IF cv_queue 
     THEN cv_queue_wake(&cv_queue, 1) 

and broadcast

   IF cv_queue 
     THEN cv_queue_wake(&cv_queue, UINT_MAX) 

regards,
alexander.

Back to comp.programming.threads | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

mutex and condition variable implementation using futex Sudhanshu Shukla <sudhan345@gmail.com> - 2011-08-09 14:06 -0700
  Re: mutex and condition variable implementation using futex Alexander Terekhov <terekhov@web.de> - 2011-08-15 11:31 +0200

csiph-web