Let's look at sem_lock(). I never looked at this code before, I can beI would agree:
easily wrong. Manfred will correct me. But at first glance we can write
the oversimplified pseudo-code:
spinlock_t local, global;
bool my_lock(bool try_local)
{
if (try_local) {
spin_lock(&local);
if (!spin_is_locked(&global))
return true;
spin_unlock(&local);
}
spin_lock(&global);
spin_unlock_wait(&local);
return false;
}
void my_unlock(bool drop_local)
{
if (drop_local)
spin_unlock(&local);
else
spin_unlock(&global);
}
it assumes that the "local" lock is cheaper than "global", the usage is
bool xxx = my_lock(condition);
/* CRITICAL SECTION */
my_unlock(xxx);
Now. Unless I missed something, my_lock() does NOT need a barrier BEFORE
spin_unlock_wait() (or spin_is_locked()). Either my_lock(true) should see
spin_is_locked(global) == T, or my_lock(false)->spin_unlock_wait() should
see that "local" is locked and wait.
spinlock_t local, global;
bool force_global;
bool my_lock(bool try_local)
{
if (try_local) {
spin_lock(&local);
if (!spin_is_locked(&global)) {
if (!force_global) {
return true;
}
}
spin_unlock(&local);
spin_lock(&global);
spin_unlock_wait(&local);
return false;
}
void my_unlock(bool drop_local)
{
if (drop_local)
spin_unlock(&local);
else
spin_unlock(&global);
}
}
Another question is do we need a barrier AFTER spin_unlock_wait(). I do notHow could that happen?
know what ipc/sem.c actually needs, but in general (I think) this does need
mb(). Otherwise my_lock / my_unlock itself does not have the proper acq/rel
semantics. For example, my_lock(false) can miss the changes which were done
under my_lock(true).