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 // XFAIL: libcpp-no-exceptions
11 // UNSUPPORTED: libcpp-has-no-threads
12 // UNSUPPORTED: c++98, c++03, c++11
13 
14 // <shared_mutex>
15 
16 // template <class Mutex> class shared_lock;
17 
18 // bool try_lock();
19 
20 #include <shared_mutex>
21 #include <cassert>
22 
23 bool try_lock_called = false;
24 
25 struct mutex
26 {
27     bool try_lock_shared()
28     {
29         try_lock_called = !try_lock_called;
30         return try_lock_called;
31     }
32     void unlock_shared() {}
33 };
34 
35 mutex m;
36 
37 int main()
38 {
39 
40     std::shared_lock<mutex> lk(m, std::defer_lock);
41     assert(lk.try_lock() == true);
42     assert(try_lock_called == true);
43     assert(lk.owns_lock() == true);
44     try
45     {
46         lk.try_lock();
47         assert(false);
48     }
49     catch (std::system_error& e)
50     {
51         assert(e.code().value() == EDEADLK);
52     }
53     lk.unlock();
54     assert(lk.try_lock() == false);
55     assert(try_lock_called == false);
56     assert(lk.owns_lock() == false);
57     lk.release();
58     try
59     {
60         lk.try_lock();
61         assert(false);
62     }
63     catch (std::system_error& e)
64     {
65         assert(e.code().value() == EPERM);
66     }
67 }
68