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 #include "../../../timming.hpp"
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 
40 typedef boost::chrono::system_clock Clock;
41 typedef boost::chrono::milliseconds milliseconds;
42 typedef boost::chrono::milliseconds ms;
43 typedef boost::chrono::nanoseconds ns;
44 
45 const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
46 
f()47 void f()
48 {
49     L1 lk(m0);
50     BOOST_TEST(test2 == 0);
51     test1 = 1;
52     cv.notify_one();
53     Clock::time_point t0 = Clock::now();
54     Clock::time_point t = t0 + milliseconds(250);
55     while (test2 == 0 && cv.wait_for(lk, t - Clock::now()) == boost::cv_status::no_timeout) {}
56     Clock::time_point t1 = Clock::now();
57     if (runs == 0)
58     {
59         ns d = t1 - t0;
60         BOOST_THREAD_TEST_IT(d, ns(max_diff));
61         BOOST_TEST(test2 != 0);
62     }
63     else
64     {
65         ns d = t1 - t0 - ms(250);
66         BOOST_THREAD_TEST_IT(d, ns(max_diff));
67         BOOST_TEST(test2 == 0);
68     }
69     ++runs;
70 }
71 
main()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
105