[libcxx] r233944 - [libcxx] Fix bug in shared_timed_mutex that could cause a program to hang.

Eric Fiselier eric at efcs.ca
Thu Apr 2 14:02:06 PDT 2015


Author: ericwf
Date: Thu Apr  2 16:02:06 2015
New Revision: 233944

URL: http://llvm.org/viewvc/llvm-project?rev=233944&view=rev
Log:
[libcxx] Fix bug in shared_timed_mutex that could cause a program to hang.

Summary:
The summary of the bug, provided by Stephan T. Lavavej:

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.



Reviewers: mclow.lists, jyasskin

Reviewed By: jyasskin

Subscribers: jyasskin, cfe-commits

Differential Revision: http://reviews.llvm.org/D8796

Added:
    libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until_deadlock_bug.pass.cpp
Modified:
    libcxx/trunk/include/shared_mutex

Modified: libcxx/trunk/include/shared_mutex
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/shared_mutex?rev=233944&r1=233943&r2=233944&view=diff
==============================================================================
--- libcxx/trunk/include/shared_mutex (original)
+++ libcxx/trunk/include/shared_mutex Thu Apr  2 16:02:06 2015
@@ -193,6 +193,7 @@ shared_timed_mutex::try_lock_until(
             if (__status == cv_status::timeout)
             {
                 __state_ &= ~__write_entered_;
+                __gate1_.notify_all();
                 return false;
             }
         }

Added: libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until_deadlock_bug.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until_deadlock_bug.pass.cpp?rev=233944&view=auto
==============================================================================
--- libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until_deadlock_bug.pass.cpp (added)
+++ libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until_deadlock_bug.pass.cpp Thu Apr  2 16:02:06 2015
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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;
+
+const int total_readers = 2;
+std::atomic<int> readers_started(0);
+std::atomic<int> readers_finished(0);
+
+// Wait for the readers to start then try and acquire the write lock.
+void writer_one() {
+  while (readers_started != total_readers) {}
+  bool b = m.try_lock_for(std::chrono::milliseconds(500));
+  assert(b == false);
+}
+
+void blocked_reader() {
+  ++readers_started;
+  // 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 main.
+  m.lock_shared();
+  ++readers_finished;
+  m.unlock_shared();
+}
+
+int main()
+{
+  typedef std::chrono::steady_clock Clock;
+
+  m.lock_shared();
+  std::thread t1(writer_one);
+  // create some readers
+  std::thread t2(blocked_reader);
+  std::thread t3(blocked_reader);
+  // Kill the test after 10 seconds if it hasn't completed.
+  auto end_point = Clock::now() + std::chrono::seconds(10);
+  while (readers_finished != total_readers && Clock::now() < end_point) {
+    std::this_thread::sleep_for(std::chrono::seconds(1));
+  }
+  assert(readers_finished == total_readers);
+  m.unlock_shared();
+  t1.join();
+  t2.join();
+  t3.join();
+}





More information about the cfe-commits mailing list