]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/test/sync/mutual_exclusion/locks/unique_lock/cons/make_unique_lock_try_to_lock_pass.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / thread / test / sync / mutual_exclusion / locks / unique_lock / cons / make_unique_lock_try_to_lock_pass.cpp
1 // Copyright (C) 2012 Vicente J. Botet Escriba
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 // <boost/thread/locks.hpp>
7
8 // template <class Mutex> class unique_lock;
9 // unique_lock<Mutex> make_unique_lock(Mutex&, try_to_lock_t);
10
11 #define BOOST_THREAD_VERSION 4
12
13
14 #include <boost/thread/lock_factories.hpp>
15 #include <boost/thread/mutex.hpp>
16 #include <boost/thread/thread.hpp>
17 #include <boost/detail/lightweight_test.hpp>
18
19 boost::mutex m;
20
21 #if defined BOOST_THREAD_USES_CHRONO
22 typedef boost::chrono::system_clock Clock;
23 typedef Clock::time_point time_point;
24 typedef Clock::duration duration;
25 typedef boost::chrono::milliseconds ms;
26 typedef boost::chrono::nanoseconds ns;
27 #else
28 #endif
29
30 void f()
31 {
32 #if defined BOOST_THREAD_USES_CHRONO
33 {
34 time_point t0 = Clock::now();
35 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
36 auto
37 #else
38 boost::unique_lock<boost::mutex>
39 #endif
40 lk = boost::make_unique_lock(m, boost::try_to_lock);
41 BOOST_TEST(lk.owns_lock() == false);
42 time_point t1 = Clock::now();
43 ns d = t1 - t0 - ms(250);
44 // This test is spurious as it depends on the time the thread system switches the threads
45 BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
46 }
47 {
48 time_point t0 = Clock::now();
49 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
50 auto
51 #else
52 boost::unique_lock<boost::mutex>
53 #endif
54 lk = boost::make_unique_lock(m, boost::try_to_lock);
55 BOOST_TEST(lk.owns_lock() == false);
56 time_point t1 = Clock::now();
57 ns d = t1 - t0 - ms(250);
58 // This test is spurious as it depends on the time the thread system switches the threads
59 BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
60 }
61 {
62 time_point t0 = Clock::now();
63 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
64 auto
65 #else
66 boost::unique_lock<boost::mutex>
67 #endif
68 lk = boost::make_unique_lock(m, boost::try_to_lock);
69 BOOST_TEST(lk.owns_lock() == false);
70 time_point t1 = Clock::now();
71 ns d = t1 - t0 - ms(250);
72 // This test is spurious as it depends on the time the thread system switches the threads
73 BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
74 }
75 {
76 time_point t0 = Clock::now();
77 for (;;)
78 {
79 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
80 auto
81 #else
82 boost::unique_lock<boost::mutex>
83 #endif
84 lk = boost::make_unique_lock(m, boost::try_to_lock);
85 if (lk.owns_lock()) break;
86 }
87 time_point t1 = Clock::now();
88 ns d = t1 - t0 - ms(250);
89 // This test is spurious as it depends on the time the thread system switches the threads
90 BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
91 }
92 #else
93 // time_point t0 = Clock::now();
94 // {
95 // boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
96 // BOOST_TEST(lk.owns_lock() == false);
97 // }
98 // {
99 // boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
100 // BOOST_TEST(lk.owns_lock() == false);
101 // }
102 // {
103 // boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
104 // BOOST_TEST(lk.owns_lock() == false);
105 // }
106 while (true)
107 {
108 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
109 auto
110 #else
111 boost::unique_lock<boost::mutex>
112 #endif
113 lk = boost::make_unique_lock(m, boost::try_to_lock);
114 if (lk.owns_lock()) break;
115 }
116 //time_point t1 = Clock::now();
117 //ns d = t1 - t0 - ms(250);
118 // This test is spurious as it depends on the time the thread system switches the threads
119 //BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms
120 #endif
121 }
122
123 int main()
124 {
125 m.lock();
126 boost::thread t(f);
127 #if defined BOOST_THREAD_USES_CHRONO
128 boost::this_thread::sleep_for(ms(250));
129 #else
130 #endif
131 m.unlock();
132 t.join();
133
134 return boost::report_errors();
135 }