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