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