]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/test/sync/conditions/condition_variable/wait_until_pass.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / thread / test / sync / conditions / condition_variable / wait_until_pass.cpp
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>
24 #include <iostream>
25 #include <cassert>
26
27 #if defined BOOST_THREAD_USES_CHRONO
28
29 struct Clock
30 {
31 typedef boost::chrono::milliseconds duration;
32 typedef duration::rep rep;
33 typedef duration::period period;
34 typedef boost::chrono::time_point<Clock> time_point;
35 static const bool is_steady = true;
36
37 static time_point now()
38 {
39 using namespace boost::chrono;
40 return time_point(duration_cast<duration> (steady_clock::now().time_since_epoch()));
41 }
42 };
43
44 boost::condition_variable cv;
45 boost::mutex mut;
46
47 int test1 = 0;
48 int test2 = 0;
49
50 int runs = 0;
51
52 #ifdef BOOST_THREAD_PLATFORM_WIN32
53 const Clock::duration max_diff(250);
54 #else
55 const Clock::duration max_diff(75);
56 #endif
57
58 void f()
59 {
60 try {
61 boost::unique_lock < boost::mutex > lk(mut);
62 assert(test2 == 0);
63 test1 = 1;
64 cv.notify_one();
65 Clock::time_point t0 = Clock::now();
66 Clock::time_point t = t0 + Clock::duration(250);
67 while (test2 == 0 && cv.wait_until(lk, t) == boost::cv_status::no_timeout) {}
68 Clock::time_point t1 = Clock::now();
69 if (runs == 0)
70 {
71 assert(t1 - t0 < max_diff);
72 assert(test2 != 0);
73 }
74 else
75 {
76 assert(t1 - t0 - Clock::duration(250) < max_diff);
77 assert(test2 == 0);
78 }
79 ++runs;
80 } catch(...) {
81 assert(false);
82 std::cout << "ERROR exception" << __LINE__ << std::endl;
83 }
84 }
85
86 int main()
87 {
88 try
89 {
90 boost::unique_lock < boost::mutex > lk(mut);
91 boost::thread t(f);
92 BOOST_TEST(test1 == 0);
93 while (test1 == 0)
94 cv.wait(lk);
95 BOOST_TEST(test1 != 0);
96 test2 = 1;
97 lk.unlock();
98 cv.notify_one();
99 t.join();
100 } catch(...) {
101 BOOST_TEST(false);
102 std::cout << "ERROR exception" << __LINE__ << std::endl;
103 }
104
105 test1 = 0;
106 test2 = 0;
107 try
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();
117 } catch(...) {
118 BOOST_TEST(false);
119 std::cout << "ERROR exception" << __LINE__ << std::endl;
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