]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/test/sync/conditions/condition_variable/wait_until_pred_pass.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / thread / test / sync / conditions / condition_variable / wait_until_pred_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
25 #if defined BOOST_THREAD_USES_CHRONO
26
27 struct Clock
28 {
29 typedef boost::chrono::milliseconds duration;
30 typedef duration::rep rep;
31 typedef duration::period period;
32 typedef boost::chrono::time_point<Clock> time_point;
33 static const bool is_steady = true;
34
35 static time_point now()
36 {
37 using namespace boost::chrono;
38 return time_point(duration_cast<duration> (steady_clock::now().time_since_epoch()));
39 }
40 };
41
42 class Pred
43 {
44 int& i_;
45 public:
46 explicit Pred(int& i) :
47 i_(i)
48 {
49 }
50
51 bool operator()()
52 {
53 return i_ != 0;
54 }
55 };
56
57 boost::condition_variable cv;
58 boost::mutex mut;
59
60 int test1 = 0;
61 int test2 = 0;
62
63 int runs = 0;
64
65 void f()
66 {
67 boost::unique_lock<boost::mutex> lk(mut);
68 BOOST_TEST(test2 == 0);
69 test1 = 1;
70 cv.notify_one();
71 Clock::time_point t0 = Clock::now();
72 Clock::time_point t = t0 + Clock::duration(250);
73 bool r = cv.wait_until(lk, t, Pred(test2));
74 Clock::time_point t1 = Clock::now();
75 if (runs == 0)
76 {
77 BOOST_TEST(t1 - t0 < Clock::duration(250));
78 BOOST_TEST(test2 != 0);
79 BOOST_TEST(r);
80 }
81 else
82 {
83 BOOST_TEST(t1 - t0 - Clock::duration(250) < Clock::duration(250+2));
84 BOOST_TEST(test2 == 0);
85 BOOST_TEST(!r);
86 }
87 ++runs;
88 }
89
90 int main()
91 {
92 {
93 boost::unique_lock<boost::mutex> lk(mut);
94 boost::thread t(f);
95 BOOST_TEST(test1 == 0);
96 while (test1 == 0)
97 cv.wait(lk);
98 BOOST_TEST(test1 != 0);
99 test2 = 1;
100 lk.unlock();
101 cv.notify_one();
102 t.join();
103 }
104 test1 = 0;
105 test2 = 0;
106 {
107 boost::unique_lock<boost::mutex> lk(mut);
108 boost::thread t(f);
109 BOOST_TEST(test1 == 0);
110 while (test1 == 0)
111 cv.wait(lk);
112 BOOST_TEST(test1 != 0);
113 lk.unlock();
114 t.join();
115 }
116
117 return boost::report_errors();
118 }
119
120 #else
121 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
122 #endif