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