]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/thread/test/sync/conditions/condition_variable/wait_until_pred_pass.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / thread / test / sync / conditions / condition_variable / wait_until_pred_pass.cpp
CommitLineData
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>
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>
b32b8144
FG
24#include <cassert>
25#include <iostream>
7c673cae
FG
26
27#if defined BOOST_THREAD_USES_CHRONO
28
29struct 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
44class Pred
45{
46 int& i_;
47public:
48 explicit Pred(int& i) :
49 i_(i)
50 {
51 }
52
53 bool operator()()
54 {
55 return i_ != 0;
56 }
57};
58
59boost::condition_variable cv;
60boost::mutex mut;
61
62int test1 = 0;
63int test2 = 0;
64
65int runs = 0;
66
67void f()
68{
b32b8144
FG
69 try {
70 boost::unique_lock<boost::mutex> lk(mut);
71 assert(test2 == 0);
72 test1 = 1;
73 cv.notify_one();
74 Clock::time_point t0 = Clock::now();
75 Clock::time_point t = t0 + Clock::duration(250);
76 bool r = cv.wait_until(lk, t, Pred(test2));
77 (void)r;
78 Clock::time_point t1 = Clock::now();
79 if (runs == 0)
80 {
81 assert(t1 - t0 < Clock::duration(250));
82 assert(test2 != 0);
83 assert(r);
84 }
85 else
86 {
87 assert(t1 - t0 - Clock::duration(250) < Clock::duration(250+2));
88 assert(test2 == 0);
89 assert(!r);
90 }
91 ++runs;
92 } catch(...) {
93 std::cout << "ERROR exception" << __LINE__ << std::endl;
94 assert(false);
7c673cae 95 }
7c673cae
FG
96}
97
98int main()
99{
b32b8144 100 try
7c673cae
FG
101 {
102 boost::unique_lock<boost::mutex> lk(mut);
103 boost::thread t(f);
104 BOOST_TEST(test1 == 0);
105 while (test1 == 0)
106 cv.wait(lk);
107 BOOST_TEST(test1 != 0);
108 test2 = 1;
109 lk.unlock();
110 cv.notify_one();
111 t.join();
b32b8144
FG
112 } catch(...) {
113 BOOST_TEST(false);
114 std::cout << "ERROR exception" << __LINE__ << std::endl;
7c673cae
FG
115 }
116 test1 = 0;
117 test2 = 0;
b32b8144 118 try
7c673cae
FG
119 {
120 boost::unique_lock<boost::mutex> lk(mut);
121 boost::thread t(f);
122 BOOST_TEST(test1 == 0);
123 while (test1 == 0)
124 cv.wait(lk);
125 BOOST_TEST(test1 != 0);
126 lk.unlock();
127 t.join();
b32b8144
FG
128 } catch(...) {
129 BOOST_TEST(false);
130 std::cout << "ERROR exception" << __LINE__ << std::endl;
7c673cae
FG
131 }
132
133 return boost::report_errors();
134}
135
136#else
137#error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
138#endif