]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/thread/test/sync/mutual_exclusion/timed_mutex/try_lock_pass.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / thread / test / sync / mutual_exclusion / timed_mutex / try_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/timed_mutex.hpp>
16
17// class timed_mutex;
18
19// bool try_lock();
20
21#include <boost/thread/mutex.hpp>
22#include <boost/thread/thread.hpp>
23#include <boost/detail/lightweight_test.hpp>
24
25
26boost::timed_mutex m;
27
28#if defined BOOST_THREAD_USES_CHRONO
29typedef boost::chrono::system_clock Clock;
30typedef Clock::time_point time_point;
31typedef Clock::duration duration;
32typedef boost::chrono::milliseconds ms;
33typedef boost::chrono::nanoseconds ns;
34#else
35#endif
36
11fdf7f2
TL
37#ifdef BOOST_THREAD_PLATFORM_WIN32
38const ms max_diff(250);
39#else
40const ms max_diff(75);
41#endif
42
7c673cae
FG
43void f()
44{
45#if defined BOOST_THREAD_USES_CHRONO
46 time_point t0 = Clock::now();
47 BOOST_TEST(!m.try_lock());
48 BOOST_TEST(!m.try_lock());
49 BOOST_TEST(!m.try_lock());
50 while (!m.try_lock())
51 ;
52 time_point t1 = Clock::now();
53 m.unlock();
54 ns d = t1 - t0 - ms(250);
11fdf7f2 55 BOOST_TEST(d < max_diff);
7c673cae
FG
56#else
57 //time_point t0 = Clock::now();
58 //BOOST_TEST(!m.try_lock());
59 //BOOST_TEST(!m.try_lock());
60 //BOOST_TEST(!m.try_lock());
61 while (!m.try_lock())
62 ;
63 //time_point t1 = Clock::now();
64 m.unlock();
65 //ns d = t1 - t0 - ms(250);
11fdf7f2 66 //BOOST_TEST(d < max_diff);
7c673cae
FG
67#endif
68}
69
70int main()
71{
72 m.lock();
73 boost::thread t(f);
74#if defined BOOST_THREAD_USES_CHRONO
75 boost::this_thread::sleep_for(ms(250));
76#else
77#endif
78 m.unlock();
79 t.join();
80
81 return boost::report_errors();
82}
83
84