• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "../../../timming.hpp"
25 
26 #if defined BOOST_THREAD_USES_CHRONO
27 typedef boost::chrono::milliseconds ms;
28 typedef boost::chrono::nanoseconds ns;
29 struct Clock
30 {
31   typedef boost::chrono::milliseconds duration;
32   typedef duration::rep rep;
33   typedef duration::period period;
34   typedef boost::chrono::time_point<Clock> time_point;
35   static const bool is_steady = true;
36 
nowClock37   static time_point now()
38   {
39     using namespace boost::chrono;
40     return time_point(duration_cast<duration> (steady_clock::now().time_since_epoch()));
41   }
42 };
43 
44 boost::condition_variable_any cv;
45 
46 typedef boost::timed_mutex L0;
47 typedef boost::unique_lock<L0> L1;
48 
49 L0 m0;
50 
51 int test1 = 0;
52 int test2 = 0;
53 
54 int runs = 0;
55 
56 const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
57 
f()58 void f()
59 {
60   L1 lk(m0);
61   BOOST_TEST(test2 == 0);
62   test1 = 1;
63   cv.notify_one();
64   Clock::time_point t0 = Clock::now();
65   Clock::time_point t = t0 + Clock::duration(250);
66   while (test2 == 0 && cv.wait_until(lk, t) == boost::cv_status::no_timeout) {}
67   Clock::time_point t1 = Clock::now();
68   if (runs == 0)
69   {
70       ns d = t1 - t0;
71       BOOST_THREAD_TEST_IT(d, ns(max_diff));
72       BOOST_TEST(test2 != 0);
73   }
74   else
75   {
76       ns d = t1 - t0 - ms(250);
77       BOOST_THREAD_TEST_IT(d, ns(max_diff));
78       BOOST_TEST(test2 == 0);
79   }
80   ++runs;
81 }
82 
main()83 int main()
84 {
85   {
86     L1 lk(m0);
87     boost::thread t(f);
88     BOOST_TEST(test1 == 0);
89     while (test1 == 0)
90       cv.wait(lk);
91     BOOST_TEST(test1 != 0);
92     test2 = 1;
93     lk.unlock();
94     cv.notify_one();
95     t.join();
96   }
97   test1 = 0;
98   test2 = 0;
99   {
100     L1 lk(m0);
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();
108   }
109 
110   return boost::report_errors();
111 }
112 
113 #else
114 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
115 #endif
116