Le 17/03/13 11:32, Fredrik Orderud a
écrit :
I have started today.
I'm not confident with this refactoring
void unlock_shared()
{
boost::unique_lock<boost::mutex>
lk(state_change);
- bool const last_reader=!--state.shared_count;
- if(last_reader)
- {
- if(state.upgrade)
- {
- state.upgrade=false;
- state.exclusive=true;
- upgrade_cond.notify_one();
- }
- else
- {
- state.exclusive_waiting_blocked=false;
- }
+ bool const last_reader = !state.unlock_shared();
+
+ if (last_reader) {
+ upgrade_cond.notify_one();
where
unsigned unlock_shared () {
bool const last_reader = !--shared_count;
if (last_reader) {
if (upgrade) {
upgrade=false;
exclusive=true;
} else {
exclusive_waiting_blocked=false;
}
}
return shared_count;
}
In the current code
upgrade_cond.notify_one();
is done if !--state.shared_count and state.upgrade
After the pacth
upgrade_cond.notify_one();
is done if !--state.shared_count independently of the state of
state.upgrade.
I guess that unlock_shared () should return whether the mutex is
downgraded
bool unlock_shared () {
bool const last_reader = !--shared_count;
if (last_reader) {
if (upgrade) {
upgrade=false;
exclusive=true;
return true;
} else {
exclusive_waiting_blocked=false;
}
}
return false;
}
and the code is replaced by
+ bool const downgraded = state.unlock_shared();
+
+ if (downgraded) {
+ upgrade_cond.notify_one();
What do you think?
I'll post more a I try to merge your patch.
Best,
Vicente