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 // template <class Rep, class Period>
20 //   bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
21 
22 #include <boost/thread/lock_types.hpp>
23 //#include <boost/thread/shared_mutex.hpp>
24 #include <boost/detail/lightweight_test.hpp>
25 
26 bool try_lock_called = false;
27 
28 struct shared_mutex
29 {
try_lock_sharedshared_mutex30   bool try_lock_shared()
31   {
32     try_lock_called = !try_lock_called;
33     return try_lock_called;
34   }
unlock_sharedshared_mutex35   void unlock_shared()
36   {
37   }
38 };
39 
40 shared_mutex m;
41 
main()42 int main()
43 {
44   boost::shared_lock<shared_mutex> lk(m, boost::defer_lock);
45   BOOST_TEST(lk.try_lock() == true);
46   BOOST_TEST(try_lock_called == true);
47   BOOST_TEST(lk.owns_lock() == true);
48   try
49   {
50     lk.try_lock();
51     BOOST_TEST(false);
52   }
53   catch (boost::system::system_error& e)
54   {
55     BOOST_TEST(e.code().value() == boost::system::errc::resource_deadlock_would_occur);
56   }
57   lk.unlock();
58   BOOST_TEST(lk.try_lock() == false);
59   BOOST_TEST(try_lock_called == false);
60   BOOST_TEST(lk.owns_lock() == false);
61   lk.release();
62   try
63   {
64     lk.try_lock();
65     BOOST_TEST(false);
66   }
67   catch (boost::system::system_error& e)
68   {
69     BOOST_TEST(e.code().value() == boost::system::errc::operation_not_permitted);
70   }
71 
72   return boost::report_errors();
73 }
74 
75