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