I'm pretty sure the mutex_lock and mutex_unlock are bugged, as they take a mutex int by value. Would be unfortunate if somone copy pasted those.
If it's passed by value it will never change inside the function and whatever is supposed to happen when it changes will never get executed. The integers should be passed by reference so when they change the function receiving them is aware of the update.
To my eye, the code is fundamentally broken in a way that's surprising for an article trying to teach threading, and it's likely confuse someone unfamiliar with these concepts.
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.> We use &mutex as the first argument