]>
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
66f6433931e2ae18a4360cad8c9453c0d174a77b
1 // Copyright (C) 2012 Vicente J. Botet Escriba
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)
6 // <boost/thread/locks.hpp>
8 // template <class Mutex> class unique_lock;
9 // unique_lock<Mutex> make_unique_lock(Mutex&, try_to_lock_t);
11 #define BOOST_THREAD_VERSION 4
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>
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
;
32 #if defined BOOST_THREAD_USES_CHRONO
34 time_point t0
= Clock::now();
35 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
38 boost::unique_lock
<boost::mutex
>
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
48 time_point t0
= Clock::now();
49 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
52 boost::unique_lock
<boost::mutex
>
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
62 time_point t0
= Clock::now();
63 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
66 boost::unique_lock
<boost::mutex
>
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
76 time_point t0
= Clock::now();
79 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
82 boost::unique_lock
<boost::mutex
>
84 lk
= boost::make_unique_lock(m
, boost::try_to_lock
);
85 if (lk
.owns_lock()) break;
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
93 // time_point t0 = Clock::now();
95 // boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
96 // BOOST_TEST(lk.owns_lock() == false);
99 // boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
100 // BOOST_TEST(lk.owns_lock() == false);
103 // boost::unique_lock<boost::mutex> lk(m, boost::try_to_lock);
104 // BOOST_TEST(lk.owns_lock() == false);
108 #if ! defined(BOOST_NO_CXX11_AUTO_DECLARATIONS)
111 boost::unique_lock
<boost::mutex
>
113 lk
= boost::make_unique_lock(m
, boost::try_to_lock
);
114 if (lk
.owns_lock()) break;
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
127 #if defined BOOST_THREAD_USES_CHRONO
128 boost::this_thread::sleep_for(ms(250));
134 return boost::report_errors();