Jimaroid wrote:At some level, the mutex needs some shared data representing its state. So do you mutex your mutex? No because that would be infinitely recurring madness...
I thought most, if not all, mutex implementations used operations that guarantee he atomicness of the set / unset.
See `man atomic` and, for example, the implementation of include/atomic.h in the freebsd source for your platform of choice, eg
http://fxr.watson.org/fxr/source/sparc6 ... e/atomic.h
from freebsd 6 "man atomic"
Code: Select all
/* Try to obtain mtx_lock once. */
#define _obtain_lock(mp, tid) \
atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
/* Get a sleep lock, deal with recursion inline. */
#define _get_sleep_lock(mp, tid, opts, file, line) do { \
uintptr_t _tid = (uintptr_t)(tid); \
\
if (!_obtain_lock(mp, tid)) { \
if (((mp)->mtx_lock & MTX_FLAGMASK) != _tid) \
_mtx_lock_sleep((mp), _tid, (opts), (file), (line));\
else { \
atomic_set_ptr(&(mp)->mtx_lock, MTX_RECURSE); \
(mp)->mtx_recurse++; \
} \
} \
} while (0)
I believe linux uses atomic_dec_and_test() and atomic_inc_and_test() to implement mutex locking and unlocking, but it's been a while since I delved into the linux source. OSX uses OSAtomicTestAndSetBarrier and OSAtomicTestAndClearBarrier, IIRC
Simon
"massive retard™", "full of shit™", "tiny bitch™", "retarted™", "a bit of a one trick pony™", "petty™", "simple minded idealistic twat™", "
miserable humourless fucker™"