3 ms·
The articles code for mutex_lock is this mutex_lock(unsigned int mutex) { while (!cmpxchg(&mutex, UNLOCKED, LOCKED)) futex(&mutex, FUTEX_WAIT, LO
by im3w1l 5y ago
The articles code for mutex_lock is this
mutex_lock(unsigned int mutex)
{
while (!cmpxchg(&mutex, UNLOCKED, LOCKED))
futex(&mutex, FUTEX_WAIT, LOCKED);
}
Now consider what happens if someone calls it
volatile unsigned int my_mutex = UNLOCKED;
mutex_lock(my_mutex);
...
mutex_unlock(my_mutex);
my_mutex is copied into the mutex parameter. The mutex parameter is updated to locked. mutex_lock exits, which destroys the mutex variable. my_mutex still has the variable UNLOCKED.
- mfilion 5y agoGood catch, corrected!
- waynesonfire 5y agoOne more... > We use &mutex as the first argument
- andrealmeid 5y agoops, fixed, thanks!