1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: libcpp-no-exceptions
11 // UNSUPPORTED: libcpp-has-no-threads
13 // <condition_variable>
15 // class condition_variable_any;
25 // -----------------------------------------------------------------------------
27 // Check that std::terminate is called if wait(...) fails to meet its post
28 // conditions. This can happen when reacquiring the mutex throws
31 // The following methods are tested within this file
32 // 1. void wait(Lock& lock);
33 // 2. void wait(Lock& lock, Pred);
34 // 3. void wait_for(Lock& lock, Duration);
35 // 4. void wait_for(Lock& lock, Duration, Pred);
36 // 5. void wait_until(Lock& lock, TimePoint);
37 // 6. void wait_until(Lock& lock, TimePoint, Pred);
40 // 1 Create a mutex type, 'ThrowingMutex', that throws when the lock is acquired
41 // for the *second* time.
43 // 2 Replace the terminate handler with one that exits with a '0' exit code.
45 // 3 Create a 'condition_variable_any' object 'cv' and a 'ThrowingMutex'
46 // object 'm' and lock 'm'.
48 // 4 Start a thread 'T2' that will notify 'cv' once 'm' has been unlocked.
50 // 5 From the main thread call the specified wait method on 'cv' with 'm'.
51 // When 'T2' notifies 'cv' and the wait method attempts to re-lock
52 // 'm' an exception will be thrown from 'm.lock()'.
54 // 6 Check that control flow does not return from the wait method and that
55 // terminate is called (If the program exits with a 0 exit code we know
56 // that terminate has been called)
59 #include <condition_variable>
68 std::_Exit(0); // Use _Exit to prevent cleanup from taking place.
71 // The predicate used in the cv.wait calls.
73 bool pred_function() {
79 std::atomic_bool locked;
81 ThrowingMutex(const ThrowingMutex&) = delete;
82 ThrowingMutex& operator=(const ThrowingMutex&) = delete;
87 ~ThrowingMutex() = default;
92 assert(pred); // Check that we actually waited until we were signaled.
93 throw 1; // this throw should end up calling terminate()
97 void unlock() { locked = false; }
98 bool isLocked() const { return locked == true; }
102 std::condition_variable_any cv;
105 while (mut.isLocked()) {} // wait until T1 releases mut inside the cv.wait call.
110 typedef std::chrono::system_clock Clock;
111 typedef std::chrono::milliseconds MS;
113 int main(int argc, char** argv) {
115 int id = std::stoi(argv[1]);
116 assert(id >= 1 && id <= 6);
117 std::set_terminate(my_terminate); // set terminate after std::stoi because it can throw.
121 assert(pred == false);
122 std::thread(signal_me).detach();
124 case 1: cv.wait(mut); break;
125 case 2: cv.wait(mut, pred_function); break;
126 case 3: cv.wait_for(mut, wait); break;
127 case 4: cv.wait_for(mut, wait, pred_function); break;
128 case 5: cv.wait_until(mut, Clock::now() + wait); break;
129 case 6: cv.wait_until(mut, Clock::now() + wait, pred_function); break;
130 default: assert(false);