]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/thread/test/sync/mutual_exclusion/locks/shared_lock/locking/lock_pass.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / thread / test / sync / mutual_exclusion / locks / shared_lock / locking / lock_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
10// Copyright (C) 2011 Vicente J. Botet Escriba
11//
12// Distributed under the Boost Software License, Version 1.0. (See accompanying
13// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
14
15// <boost/thread/locks.hpp>
16
17// template <class Mutex> class shared_lock;
18
19// void lock();
20
21#include <boost/thread/lock_types.hpp>
22#include <boost/thread/shared_mutex.hpp>
23#include <boost/thread/thread.hpp>
24#include <boost/detail/lightweight_test.hpp>
25#include <iostream>
26
27boost::shared_mutex m;
28
29#if defined BOOST_THREAD_USES_CHRONO
30typedef boost::chrono::system_clock Clock;
31typedef Clock::time_point time_point;
32typedef Clock::duration duration;
33typedef boost::chrono::milliseconds ms;
34typedef boost::chrono::nanoseconds ns;
35#else
36#endif
37
38void f()
39{
40#if defined BOOST_THREAD_USES_CHRONO
41 boost::shared_lock < boost::shared_mutex > lk(m, boost::defer_lock);
42 time_point t0 = Clock::now();
43 lk.lock();
44 time_point t1 = Clock::now();
45 BOOST_TEST(lk.owns_lock() == true);
46 ns d = t1 - t0 - ms(250);
47 // This test is spurious as it depends on the time the thread system switches the threads
48 BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms
49 try
50 {
51 lk.lock();
52 BOOST_TEST(false);
53 }
54 catch (boost::system::system_error& e)
55 {
56 BOOST_TEST(e.code().value() == boost::system::errc::resource_deadlock_would_occur);
57 }
58 lk.unlock();
59 lk.release();
60 try
61 {
62 lk.lock();
63 BOOST_TEST(false);
64 }
65 catch (boost::system::system_error& e)
66 {
67 BOOST_TEST(e.code().value() == boost::system::errc::operation_not_permitted);
68 }
69#else
70 boost::shared_lock < boost::shared_mutex > lk(m, boost::defer_lock);
71 //time_point t0 = Clock::now();
72 lk.lock();
73 //time_point t1 = Clock::now();
74 BOOST_TEST(lk.owns_lock() == true);
75 //ns d = t1 - t0 - ms(250);
76 // This test is spurious as it depends on the time the thread system switches the threads
77 //BOOST_TEST(d < ns(2500000)+ms(1000)); // within 2.5ms
78 try
79 {
80 lk.lock();
81 BOOST_TEST(false);
82 }
83 catch (boost::system::system_error& e)
84 {
85 BOOST_TEST(e.code().value() == boost::system::errc::resource_deadlock_would_occur);
86 }
87 lk.unlock();
88 lk.release();
89 try
90 {
91 lk.lock();
92 BOOST_TEST(false);
93 }
94 catch (boost::system::system_error& e)
95 {
96 BOOST_TEST(e.code().value() == boost::system::errc::operation_not_permitted);
97 }
98#endif
99}
100
101int main()
102{
103 m.lock();
104 boost::thread t(f);
105#if defined BOOST_THREAD_USES_CHRONO
106 boost::this_thread::sleep_for(ms(250));
107#else
108#endif
109 m.unlock();
110 t.join();
111
112 return boost::report_errors();
113}
114