]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //===----------------------------------------------------------------------===// |
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 | // Copyright (C) 2011 Vicente J. Botet Escriba | |
10 | // | |
11 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
12 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
13 | ||
14 | // <boost/thread/condition_variable> | |
15 | ||
16 | // class condition_variable; | |
17 | ||
18 | // condition_variable(const condition_variable&) = delete; | |
19 | ||
20 | #include <boost/thread/condition_variable.hpp> | |
21 | #include <boost/thread/mutex.hpp> | |
22 | #include <boost/thread/thread.hpp> | |
23 | #include <boost/detail/lightweight_test.hpp> | |
b32b8144 FG |
24 | #include <cassert> |
25 | #include <iostream> | |
92f5a8d4 | 26 | #include "../../../timming.hpp" |
7c673cae FG |
27 | |
28 | #if defined BOOST_THREAD_USES_CHRONO | |
29 | ||
30 | class Pred | |
31 | { | |
32 | int& i_; | |
33 | public: | |
34 | explicit Pred(int& i) : | |
35 | i_(i) | |
36 | { | |
37 | } | |
38 | ||
39 | bool operator()() | |
40 | { | |
41 | return i_ != 0; | |
42 | } | |
43 | }; | |
44 | ||
45 | boost::condition_variable cv; | |
46 | boost::mutex mut; | |
47 | ||
48 | int test1 = 0; | |
49 | int test2 = 0; | |
50 | ||
51 | int runs = 0; | |
52 | ||
11fdf7f2 TL |
53 | typedef boost::chrono::system_clock Clock; |
54 | typedef boost::chrono::milliseconds milliseconds; | |
92f5a8d4 TL |
55 | typedef boost::chrono::milliseconds ms; |
56 | typedef boost::chrono::nanoseconds ns; | |
11fdf7f2 | 57 | |
92f5a8d4 | 58 | const ms max_diff(BOOST_THREAD_TEST_TIME_MS); |
11fdf7f2 | 59 | |
7c673cae FG |
60 | void f() |
61 | { | |
b32b8144 | 62 | try { |
b32b8144 FG |
63 | boost::unique_lock < boost::mutex > lk(mut); |
64 | assert(test2 == 0); | |
65 | test1 = 1; | |
66 | cv.notify_one(); | |
67 | Clock::time_point t0 = Clock::now(); | |
11fdf7f2 | 68 | cv.wait_for(lk, milliseconds(250), Pred(test2)); |
b32b8144 FG |
69 | Clock::time_point t1 = Clock::now(); |
70 | if (runs == 0) | |
71 | { | |
11fdf7f2 | 72 | assert(t1 - t0 < max_diff); |
b32b8144 FG |
73 | assert(test2 != 0); |
74 | } | |
75 | else | |
76 | { | |
11fdf7f2 | 77 | assert(t1 - t0 - milliseconds(250) < max_diff); |
b32b8144 FG |
78 | assert(test2 == 0); |
79 | } | |
80 | ++runs; | |
81 | } catch(...) { | |
82 | std::cout << "ERROR exception" << __LINE__ << std::endl; | |
83 | assert(false); | |
7c673cae | 84 | } |
7c673cae FG |
85 | } |
86 | ||
87 | int main() | |
88 | { | |
b32b8144 | 89 | try |
7c673cae FG |
90 | { |
91 | boost::unique_lock < boost::mutex > lk(mut); | |
92 | boost::thread t(f); | |
93 | BOOST_TEST(test1 == 0); | |
94 | while (test1 == 0) | |
95 | cv.wait(lk); | |
96 | BOOST_TEST(test1 != 0); | |
97 | test2 = 1; | |
98 | lk.unlock(); | |
99 | cv.notify_one(); | |
100 | t.join(); | |
b32b8144 FG |
101 | } catch(...) { |
102 | BOOST_TEST(false); | |
103 | std::cout << "ERROR exception" << __LINE__ << std::endl; | |
7c673cae FG |
104 | } |
105 | test1 = 0; | |
106 | test2 = 0; | |
b32b8144 | 107 | try |
7c673cae FG |
108 | { |
109 | boost::unique_lock < boost::mutex > lk(mut); | |
110 | boost::thread t(f); | |
111 | BOOST_TEST(test1 == 0); | |
112 | while (test1 == 0) | |
113 | cv.wait(lk); | |
114 | BOOST_TEST(test1 != 0); | |
115 | lk.unlock(); | |
116 | t.join(); | |
b32b8144 FG |
117 | } catch(...) { |
118 | BOOST_TEST(false); | |
119 | std::cout << "ERROR exception" << __LINE__ << std::endl; | |
7c673cae FG |
120 | } |
121 | ||
122 | return boost::report_errors(); | |
123 | } | |
124 | ||
125 | #else | |
126 | #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" | |
127 | #endif |