]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/thread/test/sync/conditions/condition_variable/wait_for_pred_pass.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / thread / test / sync / conditions / condition_variable / wait_for_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
29class Pred
30{
31 int& i_;
32public:
33 explicit Pred(int& i) :
34 i_(i)
35 {
36 }
37
38 bool operator()()
39 {
40 return i_ != 0;
41 }
42};
43
44boost::condition_variable cv;
45boost::mutex mut;
46
47int test1 = 0;
48int test2 = 0;
49
50int runs = 0;
51
52void f()
53{
b32b8144
FG
54 try {
55 typedef boost::chrono::system_clock Clock;
56 typedef boost::chrono::milliseconds milliseconds;
57 boost::unique_lock < boost::mutex > lk(mut);
58 assert(test2 == 0);
59 test1 = 1;
60 cv.notify_one();
61 Clock::time_point t0 = Clock::now();
62 int count=0;
63 //bool r =
64 (void)cv.wait_for(lk, milliseconds(250), Pred(test2));
65 count++;
66 Clock::time_point t1 = Clock::now();
67 if (runs == 0)
68 {
69 // This test is spurious as it depends on the time the thread system switches the threads
70 assert(t1 - t0 < milliseconds(250+1000));
71 assert(test2 != 0);
72 }
73 else
74 {
75 assert(t1 - t0 - milliseconds(250) < milliseconds(count*250+2));
76 assert(test2 == 0);
77 }
78 ++runs;
79 } catch(...) {
80 std::cout << "ERROR exception" << __LINE__ << std::endl;
81 assert(false);
7c673cae 82 }
7c673cae
FG
83}
84
85int main()
86{
b32b8144 87 try
7c673cae
FG
88 {
89 boost::unique_lock < boost::mutex > lk(mut);
90 boost::thread t(f);
91 BOOST_TEST(test1 == 0);
92 while (test1 == 0)
93 cv.wait(lk);
94 BOOST_TEST(test1 != 0);
95 test2 = 1;
96 lk.unlock();
97 cv.notify_one();
98 t.join();
b32b8144
FG
99 } catch(...) {
100 BOOST_TEST(false);
101 std::cout << "ERROR exception" << __LINE__ << std::endl;
7c673cae
FG
102 }
103 test1 = 0;
104 test2 = 0;
b32b8144 105 try
7c673cae
FG
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();
b32b8144
FG
115 } catch(...) {
116 BOOST_TEST(false);
117 std::cout << "ERROR exception" << __LINE__ << std::endl;
7c673cae
FG
118 }
119
120 return boost::report_errors();
121}
122
123#else
124#error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
125#endif