]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/thread/test/sync/conditions/condition_variable_any/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_any / wait_for_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_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>
92f5a8d4 24#include "../../../timming.hpp"
7c673cae
FG
25
26#if defined BOOST_THREAD_USES_CHRONO
27
28boost::condition_variable_any cv;
29
30typedef boost::timed_mutex L0;
31typedef boost::unique_lock<L0> L1;
32
33L0 m0;
34
35int test1 = 0;
36int test2 = 0;
37
38int runs = 0;
39
11fdf7f2
TL
40typedef boost::chrono::system_clock Clock;
41typedef boost::chrono::milliseconds milliseconds;
92f5a8d4
TL
42typedef boost::chrono::milliseconds ms;
43typedef boost::chrono::nanoseconds ns;
11fdf7f2 44
92f5a8d4 45const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
11fdf7f2 46
7c673cae
FG
47void f()
48{
7c673cae
FG
49 L1 lk(m0);
50 BOOST_TEST(test2 == 0);
51 test1 = 1;
52 cv.notify_one();
7c673cae 53 Clock::time_point t0 = Clock::now();
11fdf7f2
TL
54 Clock::time_point t = t0 + milliseconds(250);
55 while (test2 == 0 && cv.wait_for(lk, t - Clock::now()) == boost::cv_status::no_timeout) {}
7c673cae
FG
56 Clock::time_point t1 = Clock::now();
57 if (runs == 0)
58 {
92f5a8d4
TL
59 ns d = t1 - t0;
60 BOOST_THREAD_TEST_IT(d, ns(max_diff));
7c673cae
FG
61 BOOST_TEST(test2 != 0);
62 }
63 else
64 {
92f5a8d4
TL
65 ns d = t1 - t0 - ms(250);
66 BOOST_THREAD_TEST_IT(d, ns(max_diff));
7c673cae
FG
67 BOOST_TEST(test2 == 0);
68 }
69 ++runs;
70}
71
72int main()
73{
74 {
75 L1 lk(m0);
76 boost::thread t(f);
77 BOOST_TEST(test1 == 0);
78 while (test1 == 0)
79 cv.wait(lk);
80 BOOST_TEST(test1 != 0);
81 test2 = 1;
82 lk.unlock();
83 cv.notify_one();
84 t.join();
85 }
86 test1 = 0;
87 test2 = 0;
88 {
89 L1 lk(m0);
90 boost::thread t(f);
91 BOOST_TEST(test1 == 0);
92 while (test1 == 0)
93 cv.wait(lk);
94 BOOST_TEST(test1 != 0);
95 lk.unlock();
96 t.join();
97 }
98
99 return boost::report_errors();
100}
101
102#else
103#error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
104#endif