]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/test/sync/conditions/condition_variable/wait_for_pred_pass.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / thread / test / sync / conditions / condition_variable / wait_for_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 #include <cassert>
25 #include <iostream>
26
27 #if defined BOOST_THREAD_USES_CHRONO
28
29 class Pred
30 {
31 int& i_;
32 public:
33 explicit Pred(int& i) :
34 i_(i)
35 {
36 }
37
38 bool operator()()
39 {
40 return i_ != 0;
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 typedef boost::chrono::system_clock Clock;
53 typedef boost::chrono::milliseconds milliseconds;
54
55 #ifdef BOOST_THREAD_PLATFORM_WIN32
56 const milliseconds max_diff(250);
57 #else
58 const milliseconds max_diff(75);
59 #endif
60
61 void f()
62 {
63 try {
64 boost::unique_lock < boost::mutex > lk(mut);
65 assert(test2 == 0);
66 test1 = 1;
67 cv.notify_one();
68 Clock::time_point t0 = Clock::now();
69 cv.wait_for(lk, milliseconds(250), Pred(test2));
70 Clock::time_point t1 = Clock::now();
71 if (runs == 0)
72 {
73 assert(t1 - t0 < max_diff);
74 assert(test2 != 0);
75 }
76 else
77 {
78 assert(t1 - t0 - milliseconds(250) < max_diff);
79 assert(test2 == 0);
80 }
81 ++runs;
82 } catch(...) {
83 std::cout << "ERROR exception" << __LINE__ << std::endl;
84 assert(false);
85 }
86 }
87
88 int main()
89 {
90 try
91 {
92 boost::unique_lock < boost::mutex > lk(mut);
93 boost::thread t(f);
94 BOOST_TEST(test1 == 0);
95 while (test1 == 0)
96 cv.wait(lk);
97 BOOST_TEST(test1 != 0);
98 test2 = 1;
99 lk.unlock();
100 cv.notify_one();
101 t.join();
102 } catch(...) {
103 BOOST_TEST(false);
104 std::cout << "ERROR exception" << __LINE__ << std::endl;
105 }
106 test1 = 0;
107 test2 = 0;
108 try
109 {
110 boost::unique_lock < boost::mutex > lk(mut);
111 boost::thread t(f);
112 BOOST_TEST(test1 == 0);
113 while (test1 == 0)
114 cv.wait(lk);
115 BOOST_TEST(test1 != 0);
116 lk.unlock();
117 t.join();
118 } catch(...) {
119 BOOST_TEST(false);
120 std::cout << "ERROR exception" << __LINE__ << std::endl;
121 }
122
123 return boost::report_errors();
124 }
125
126 #else
127 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
128 #endif