@@ -31,13 +31,31 @@ HighPriorityLockable::HighPriorityLockable(MutexTwoPriorities & parent)
3131void
3232HighPriorityLockable::lock ()
3333{
34- parent_.data_ .lock ();
34+ std::unique_lock<std::mutex> guard{parent_.cv_mutex_ };
35+ if (parent_.data_taken_ ) {
36+ ++parent_.hp_waiting_count_ ;
37+ while (parent_.data_taken_ ) {
38+ parent_.hp_cv_ .wait (guard);
39+ }
40+ --parent_.hp_waiting_count_ ;
41+ }
42+ parent_.data_taken_ = true ;
3543}
3644
3745void
3846HighPriorityLockable::unlock ()
3947{
40- parent_.data_ .unlock ();
48+ bool notify_lp{false };
49+ {
50+ std::lock_guard<std::mutex> guard{parent_.cv_mutex_ };
51+ parent_.data_taken_ = false ;
52+ notify_lp = 0u == parent_.hp_waiting_count_ ;
53+ }
54+ if (notify_lp) {
55+ parent_.lp_cv_ .notify_one ();
56+ } else {
57+ parent_.hp_cv_ .notify_one ();
58+ }
4159}
4260
4361LowPriorityLockable::LowPriorityLockable (MutexTwoPriorities & parent)
@@ -47,16 +65,27 @@ LowPriorityLockable::LowPriorityLockable(MutexTwoPriorities & parent)
4765void
4866LowPriorityLockable::lock ()
4967{
50- std::unique_lock<std::mutex> barrier_guard{parent_.barrier_ };
51- parent_.data_ .lock ();
52- barrier_guard.release ();
68+ std::unique_lock<std::mutex> guard{parent_.cv_mutex_ };
69+ while (parent_.data_taken_ || parent_.hp_waiting_count_ ) {
70+ parent_.lp_cv_ .wait (guard);
71+ }
72+ parent_.data_taken_ = true ;
5373}
5474
5575void
5676LowPriorityLockable::unlock ()
5777{
58- std::lock_guard<std::mutex> barrier_guard{parent_.barrier_ , std::adopt_lock};
59- parent_.data_ .unlock ();
78+ bool notify_lp{false };
79+ {
80+ std::lock_guard<std::mutex> guard{parent_.cv_mutex_ };
81+ parent_.data_taken_ = false ;
82+ notify_lp = 0u == parent_.hp_waiting_count_ ;
83+ }
84+ if (notify_lp) {
85+ parent_.lp_cv_ .notify_one ();
86+ } else {
87+ parent_.hp_cv_ .notify_one ();
88+ }
6089}
6190
6291HighPriorityLockable
0 commit comments