Hi All,
I have a question about the dec_lock_cnt method in Context class under
multiple processor.
In its implementation, it checks if thread's home cpu is equal to current
cpu.
If not, it does not unset "_running_under_lock" variable, even if the
_lock_cnt is 0.
Why does it check if home cpu is equal to current cpu?
Consider the following case:
Thread A is running on core 1 holding a helping lock, but is preempted by
another high priority thread.
Thread B is running on core 2, and tries to take the same helping lock. It
finds the lock is held by A, so
it helps A to run by migrating A to core 2.
When A releases its lock, finding the current cpu is not its home cpu, so
it does not set "_running_under_lock" to false.
Then A continues to run on core 1 and tries to take that lock again. But
now it can never take the lock, as the implementation
of helping lock.
PRIVATE inline
bool NO_INSTRUMENT
Switch_lock::set_lock_owner(Context *o)
{
bool have_no_locks = o->_lock_cnt < 1;
if (have_no_locks)
{
assert_kdb (current_cpu() == o->home_cpu());
for (;;)
{
if (EXPECT_FALSE(access_once(&o->_running_under_lock)))
continue;
if (EXPECT_TRUE(mp_cas(&o->_running_under_lock, Mword(false),
Mword(true))))
break;
}
}
...
}
Do I misunderstand anything here?
Thank you very much.
Best,
Yuxin