diff --git a/libcxx/src/shared_mutex.cpp b/libcxx/src/shared_mutex.cpp index 1a346dda027f8..6180833736956 100644 --- a/libcxx/src/shared_mutex.cpp +++ b/libcxx/src/shared_mutex.cpp @@ -38,8 +38,10 @@ bool __shared_mutex_base::try_lock() { } void __shared_mutex_base::unlock() { - lock_guard _(__mut_); - __state_ = 0; + { + lock_guard _(__mut_); + __state_ = 0; + } __gate1_.notify_all(); } @@ -67,16 +69,20 @@ bool __shared_mutex_base::try_lock_shared() { } void __shared_mutex_base::unlock_shared() { - lock_guard _(__mut_); + unique_lock lk(__mut_); unsigned num_readers = (__state_ & __n_readers_) - 1; __state_ &= ~__n_readers_; __state_ |= num_readers; if (__state_ & __write_entered_) { - if (num_readers == 0) + if (num_readers == 0) { + lk.unlock(); __gate2_.notify_one(); + } } else { - if (num_readers == __n_readers_ - 1) + if (num_readers == __n_readers_ - 1) { + lk.unlock(); __gate1_.notify_one(); + } } }