"lock_futex"
"Alexander Terekhov" <[email protected]> Fri, 24 Oct 2003 19:33:37 +0200
| Newsgroups | gmane.comp.lib.phil |
|---|---|
| Message-ID | <OFCA8DEEBF.B147DDD4-ONC1256DC9.005E517B-C1256DC9.00606F33@de.ibm.com> |
"lock_futex" is special. High-order bit is used to indicate the
presence of waiter(s) -- the kernel is supposed to maintain this
bit (atomic operation(s) on user memory inside the kernel). For
semas, the wait operation on "lock_futex" simply checks remaining
31 bits and returns (doesn't suspend the calling thread) if the
value is not zero. For mutexes, the "doesn't suspend" value is
zero. Here's the scheme (apart from timeouts and acquire/release
mnemonics for proper memory synchronization).
sema_lock:
WHILE
!atomic_decrement_if_binand_7FFFFFFF_is_not_zero(&lock_futex)
lock_futex_wait(&lock_futex, 0)
sema_unlock:
IF atomic_increment(&lock_futex) > 0x80000000
THEN lock_futex_wake(&lock_futex, 1)
mutex_lock:
WHILE
atomic_bit_test_set(&lock_futex, 1)
lock_futex_wait(&lock_futex, 1)
mutex_unlock:
IF atomic_decrement(&lock_futex)
THEN lock_futex_wake(&lock_futex, 1)
Wait-morphing (for BOTH cv-signal and cv-broadcast) will be
"no problem" as well (and without "forced unlock" ugliness).
Oder?
regards,
alexander.