]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/test/sync/conditions/condition_variable/wait_for_pass.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / thread / test / sync / conditions / condition_variable / wait_for_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 <iostream>
21 #include <boost/thread/condition_variable.hpp>
22 #include <boost/thread/mutex.hpp>
23 #include <boost/thread/thread.hpp>
24 #include <boost/detail/lightweight_test.hpp>
25 #include <cassert>
26
27 #if defined BOOST_THREAD_USES_CHRONO
28
29 boost::condition_variable cv;
30 boost::mutex mut;
31
32 int test1 = 0;
33 int test2 = 0;
34
35 int runs = 0;
36
37 void f()
38 {
39 try {
40 typedef boost::chrono::steady_clock Clock;
41 typedef boost::chrono::milliseconds milliseconds;
42 boost::unique_lock<boost::mutex> lk(mut);
43 assert(test2 == 0);
44 test1 = 1;
45 cv.notify_one();
46 Clock::time_point t0 = Clock::now();
47 int count=0;
48 while (test2 == 0 && cv.wait_for(lk, milliseconds(250)) == boost::cv_status::no_timeout)
49 count++;
50 Clock::time_point t1 = Clock::now();
51 if (runs == 0)
52 {
53 assert(t1 - t0 < milliseconds(250));
54 assert(test2 != 0);
55 }
56 else
57 {
58 // This test is spurious as it depends on the time the thread system switches the threads
59 assert(t1 - t0 - milliseconds(250) < milliseconds(count*250+5+1000));
60 assert(test2 == 0);
61 }
62 ++runs;
63 } catch(...) {
64 std::cout << "ERROR exception" << __LINE__ << std::endl;
65 assert(false);
66 }
67 }
68
69 int main()
70 {
71 try
72 {
73 boost::unique_lock<boost::mutex> lk(mut);
74 boost::thread t(f);
75 BOOST_TEST(test1 == 0);
76 while (test1 == 0)
77 cv.wait(lk);
78 BOOST_TEST(test1 != 0);
79 test2 = 1;
80 lk.unlock();
81 cv.notify_one();
82 t.join();
83 } catch(...) {
84 std::cout << "ERROR exception" << __LINE__ << std::endl;
85 BOOST_TEST(false);
86 }
87 test1 = 0;
88 test2 = 0;
89 try
90 {
91 boost::unique_lock<boost::mutex> lk(mut);
92 boost::thread t(f);
93 BOOST_TEST(test1 == 0);
94 while (test1 == 0)
95 cv.wait(lk);
96 BOOST_TEST(test1 != 0);
97 lk.unlock();
98 t.join();
99 } catch(...) {
100 BOOST_TEST(false);
101 std::cout << "ERROR exception" << __LINE__ << std::endl;
102 }
103 return boost::report_errors();
104 }
105 #else
106 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
107 #endif
108