]>
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_any> | |
15 | ||
16 | // class condition_variable_any; | |
17 | ||
18 | // condition_variable_any(const condition_variable_any&) = 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> | |
92f5a8d4 | 24 | #include "../../../timming.hpp" |
7c673cae FG |
25 | |
26 | #if defined BOOST_THREAD_USES_CHRONO | |
27 | ||
28 | boost::condition_variable_any cv; | |
29 | ||
30 | typedef boost::timed_mutex L0; | |
31 | typedef boost::unique_lock<L0> L1; | |
32 | ||
33 | L0 m0; | |
34 | ||
35 | int test1 = 0; | |
36 | int test2 = 0; | |
37 | ||
38 | int runs = 0; | |
39 | ||
11fdf7f2 TL |
40 | typedef boost::chrono::system_clock Clock; |
41 | typedef boost::chrono::milliseconds milliseconds; | |
92f5a8d4 TL |
42 | typedef boost::chrono::milliseconds ms; |
43 | typedef boost::chrono::nanoseconds ns; | |
11fdf7f2 | 44 | |
92f5a8d4 | 45 | const ms max_diff(BOOST_THREAD_TEST_TIME_MS); |
11fdf7f2 | 46 | |
7c673cae FG |
47 | void f() |
48 | { | |
7c673cae FG |
49 | L1 lk(m0); |
50 | BOOST_TEST(test2 == 0); | |
51 | test1 = 1; | |
52 | cv.notify_one(); | |
7c673cae | 53 | Clock::time_point t0 = Clock::now(); |
11fdf7f2 TL |
54 | Clock::time_point t = t0 + milliseconds(250); |
55 | while (test2 == 0 && cv.wait_for(lk, t - Clock::now()) == boost::cv_status::no_timeout) {} | |
7c673cae FG |
56 | Clock::time_point t1 = Clock::now(); |
57 | if (runs == 0) | |
58 | { | |
92f5a8d4 TL |
59 | ns d = t1 - t0; |
60 | BOOST_THREAD_TEST_IT(d, ns(max_diff)); | |
7c673cae FG |
61 | BOOST_TEST(test2 != 0); |
62 | } | |
63 | else | |
64 | { | |
92f5a8d4 TL |
65 | ns d = t1 - t0 - ms(250); |
66 | BOOST_THREAD_TEST_IT(d, ns(max_diff)); | |
7c673cae FG |
67 | BOOST_TEST(test2 == 0); |
68 | } | |
69 | ++runs; | |
70 | } | |
71 | ||
72 | int main() | |
73 | { | |
74 | { | |
75 | L1 lk(m0); | |
76 | boost::thread t(f); | |
77 | BOOST_TEST(test1 == 0); | |
78 | while (test1 == 0) | |
79 | cv.wait(lk); | |
80 | BOOST_TEST(test1 != 0); | |
81 | test2 = 1; | |
82 | lk.unlock(); | |
83 | cv.notify_one(); | |
84 | t.join(); | |
85 | } | |
86 | test1 = 0; | |
87 | test2 = 0; | |
88 | { | |
89 | L1 lk(m0); | |
90 | boost::thread t(f); | |
91 | BOOST_TEST(test1 == 0); | |
92 | while (test1 == 0) | |
93 | cv.wait(lk); | |
94 | BOOST_TEST(test1 != 0); | |
95 | lk.unlock(); | |
96 | t.join(); | |
97 | } | |
98 | ||
99 | return boost::report_errors(); | |
100 | } | |
101 | ||
102 | #else | |
103 | #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" | |
104 | #endif |