Hi mclow.lists,
The summary of the bug, provided by STL:
In shared_timed_mutex::try_lock_until() (line 195 in 3.6.0), you need to
deliver a notification. The scenario is:
* There are N threads holding the shared lock.
* One thread calls try_lock_until() to attempt to acquire the exclusive lock.
It sets the "I want to write" bool/bit, then waits for the N readers to drain
away.
* K more threads attempt to acquire the shared lock, but they notice that
someone said "I want to write", so they block on a condition_variable.
* At least one of the N readers is stubborn and doesn't release the shared lock.
* The wannabe-writer times out, gives up, and unsets the "I want to write"
bool/bit.
At this point, a notification (it needs to be notify_all) must be delivered to
the condition_variable that the K wannabe-readers are waiting on. Otherwise,
they can block forever without waking up.
http://reviews.llvm.org/D8796
Files:
include/shared_mutex
test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/stl_bug.pass.cpp
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
Index: include/shared_mutex
===================================================================
--- include/shared_mutex
+++ include/shared_mutex
@@ -170,6 +170,8 @@
shared_timed_mutex::try_lock_until(
const chrono::time_point<_Clock, _Duration>& __abs_time)
{
+ bool __write_entered_timeout = false;
+ {
unique_lock<mutex> __lk(__mut_);
if (__state_ & __write_entered_)
{
@@ -188,15 +190,22 @@
while (true)
{
cv_status __status = __gate2_.wait_until(__lk, __abs_time);
- if ((__state_ & __n_readers_) == 0)
+ if ((__state_ & __n_readers_) == 0) {
break;
+ }
if (__status == cv_status::timeout)
{
+ __write_entered_timeout = true;
__state_ &= ~__write_entered_;
- return false;
+ break;
}
}
}
+ }
+ if (__write_entered_timeout) {
+ __gate1_.notify_one();
+ return false;
+ }
return true;
}
Index: test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/stl_bug.pass.cpp
===================================================================
--- /dev/null
+++ test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/stl_bug.pass.cpp
@@ -0,0 +1,80 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// UNSUPPORTED: libcpp-has-no-threads
+// UNSUPPORTED: c++03, c++98, c++11
+
+// <shared_mutex>
+
+// class shared_timed_mutex;
+
+#include <shared_mutex>
+
+#include <atomic>
+#include <chrono>
+#include <thread>
+#include <cstdlib>
+#include <cassert>
+
+std::shared_timed_mutex m;
+
+std::atomic_bool reader_one_waits = ATOMIC_VAR_INIT(true);
+std::atomic_bool read_lock_held = ATOMIC_VAR_INIT(false);
+
+std::atomic_bool test_done = ATOMIC_VAR_INIT(false);
+
+typedef std::chrono::steady_clock Clock;
+typedef Clock::time_point time_point;
+typedef Clock::duration duration;
+typedef std::chrono::milliseconds ms;
+
+void reader_one() {
+ m.lock_shared();
+ read_lock_held = true;
+ while(reader_one_waits) {
+ }
+ m.unlock_shared();
+}
+
+// Wait until the reader_one has a read lock then attempt to get the write lock.
+void writer_one() {
+ while (read_lock_held == false) {}
+ bool b = m.try_lock_for(ms(500));
+ assert(b == false);
+}
+
+void reader_two() {
+ // Wait until writer_one is waiting for the write lock.
+ while (m.try_lock_shared()) {
+ m.unlock_shared();
+ }
+ // Attempt to get the read lock. writer_one should be blocking us because
+ // writer_one is blocked by reader_one.
+ m.lock_shared();
+ // writer_one has timed out. Tell reader_one to exit.
+ reader_one_waits = false;
+ m.unlock_shared();
+ test_done = true;
+}
+
+int main()
+{
+ std::thread t1(reader_one);
+ std::thread t2(writer_one);
+ std::thread t3(reader_two);
+ // Kill the test after 10 seconds if it hasn't completed.
+ time_point end_point = Clock::now() + std::chrono::seconds(10);
+ while (test_done == false && Clock::now() < end_point) {
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ }
+ assert(test_done);
+ t1.join();
+ t2.join();
+ t3.join();
+}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits