blob: f9a35cc905c70d929d6f90c6f60ebb33bbb228db [file] [log] [blame]
Eric Fiselier286e0a42015-08-18 23:29:59 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Asiri Rathnayakecc2e93c2015-11-10 11:41:22 +000010// XFAIL: libcpp-no-exceptions
Eric Fiselier286e0a42015-08-18 23:29:59 +000011// UNSUPPORTED: libcpp-has-no-threads
12
13// <condition_variable>
14
15// class condition_variable_any;
16
17// RUN: %build
18// RUN: %run 1
19// RUN: %run 2
20// RUN: %run 3
21// RUN: %run 4
22// RUN: %run 5
23// RUN: %run 6
24
25// -----------------------------------------------------------------------------
26// Overview
27// Check that std::terminate is called if wait(...) fails to meet it's post
28// conditions. This can happens when reacquiring the mutex throws
29// an exception.
30//
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);
38//
39// Plan
40// 1 Create a mutex type, 'ThrowingMutex', that throws when the lock is aquired
41// for the *second* time.
42//
43// 2 Replace the terminate handler with one that exits with a '0' exit code.
44//
45// 3 Create a 'condition_variable_any' object 'cv' and a 'ThrowingMutex'
46// object 'm' and lock 'm'.
47//
48// 4 Start a thread 'T2' that will notify 'cv' once 'm' has been unlocked.
49//
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()'.
53//
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)
57
58
59#include <condition_variable>
Eric Fiselier85b66612015-08-19 17:37:34 +000060#include <atomic>
Eric Fiselier286e0a42015-08-18 23:29:59 +000061#include <thread>
62#include <chrono>
63#include <string>
64#include <cstdlib>
65#include <cassert>
66
Eric Fiselier286e0a42015-08-18 23:29:59 +000067void my_terminate() {
68 std::_Exit(0); // Use _Exit to prevent cleanup from taking place.
69}
70
71// The predicate used in the cv.wait calls.
72bool pred = false;
73bool pred_function() {
74 return pred == true;
75}
76
77class ThrowingMutex
78{
Eric Fiselier85b66612015-08-19 17:37:34 +000079 std::atomic_bool locked;
Eric Fiselier286e0a42015-08-18 23:29:59 +000080 unsigned state = 0;
81 ThrowingMutex(const ThrowingMutex&) = delete;
82 ThrowingMutex& operator=(const ThrowingMutex&) = delete;
83public:
Eric Fiselier85b66612015-08-19 17:37:34 +000084 ThrowingMutex() {
85 locked = false;
86 }
Eric Fiselier286e0a42015-08-18 23:29:59 +000087 ~ThrowingMutex() = default;
88
89 void lock() {
90 locked = true;
91 if (++state == 2) {
92 assert(pred); // Check that we actually waited until we were signaled.
93 throw 1; // this throw should end up calling terminate()
94 }
95 }
96
97 void unlock() { locked = false; }
98 bool isLocked() const { return locked == true; }
99};
100
101ThrowingMutex mut;
102std::condition_variable_any cv;
103
104void signal_me() {
105 while (mut.isLocked()) {} // wait until T1 releases mut inside the cv.wait call.
106 pred = true;
107 cv.notify_one();
108}
109
110typedef std::chrono::system_clock Clock;
111typedef std::chrono::milliseconds MS;
112
113int main(int argc, char** argv) {
114 assert(argc == 2);
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.
118 MS wait(250);
119 try {
120 mut.lock();
121 assert(pred == false);
122 std::thread(signal_me).detach();
123 switch (id) {
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);
131 }
132 } catch (...) {}
133 assert(false);
134}