]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/test/sync/conditions/condition_variable/wait_for_pass.cpp
import new upstream nautilus stable release 14.2.8
[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 #include "../../../timming.hpp"
27
28 #if defined BOOST_THREAD_USES_CHRONO
29
30 boost::condition_variable cv;
31 boost::mutex mut;
32
33 int test1 = 0;
34 int test2 = 0;
35
36 int runs = 0;
37
38 typedef boost::chrono::steady_clock Clock;
39 typedef boost::chrono::milliseconds milliseconds;
40 typedef boost::chrono::nanoseconds nanoseconds;
41 typedef boost::chrono::milliseconds ms;
42 typedef boost::chrono::nanoseconds ns;
43
44 const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
45
46 void f()
47 {
48 try {
49 boost::unique_lock<boost::mutex> lk(mut);
50 assert(test2 == 0);
51 test1 = 1;
52 cv.notify_one();
53 Clock::time_point t0 = Clock::now();
54 Clock::time_point t = t0 + milliseconds(250);
55 while (test2 == 0 && cv.wait_for(lk, t - Clock::now()) == boost::cv_status::no_timeout) {}
56 Clock::time_point t1 = Clock::now();
57 if (runs == 0)
58 {
59 assert(t1 - t0 < max_diff);
60 assert(test2 != 0);
61 }
62 else
63 {
64 nanoseconds d = t1 - t0 - milliseconds(250);
65 std::cout << "diff= " << d.count() << std::endl;
66 std::cout << "max_diff= " << max_diff.count() << std::endl;
67 assert( d < max_diff);
68 assert(test2 == 0);
69 }
70 ++runs;
71 } catch(...) {
72 std::cout << "ERROR exception" << __LINE__ << std::endl;
73 assert(false);
74 }
75 }
76
77 int main()
78 {
79 try
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 } catch(...) {
92 std::cout << "ERROR exception" << __LINE__ << std::endl;
93 BOOST_TEST(false);
94 }
95 test1 = 0;
96 test2 = 0;
97 try
98 {
99 boost::unique_lock<boost::mutex> lk(mut);
100 boost::thread t(f);
101 BOOST_TEST(test1 == 0);
102 while (test1 == 0)
103 cv.wait(lk);
104 BOOST_TEST(test1 != 0);
105 lk.unlock();
106 t.join();
107 } catch(...) {
108 BOOST_TEST(false);
109 std::cout << "ERROR exception" << __LINE__ << std::endl;
110 }
111 return boost::report_errors();
112 }
113 #else
114 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
115 #endif
116