• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // UNSUPPORTED: libcpp-has-no-threads
11 // UNSUPPORTED: c++98, c++03, c++11
12 
13 // <shared_mutex>
14 
15 // template <class Mutex> class shared_lock;
16 
17 // bool try_lock();
18 
19 #include <shared_mutex>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 
24 bool try_lock_called = false;
25 
26 struct mutex
27 {
try_lock_sharedmutex28     bool try_lock_shared()
29     {
30         try_lock_called = !try_lock_called;
31         return try_lock_called;
32     }
unlock_sharedmutex33     void unlock_shared() {}
34 };
35 
36 mutex m;
37 
main()38 int main()
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 #ifndef TEST_HAS_NO_EXCEPTIONS
45     try
46     {
47         lk.try_lock();
48         assert(false);
49     }
50     catch (std::system_error& e)
51     {
52         assert(e.code().value() == EDEADLK);
53     }
54 #endif
55     lk.unlock();
56     assert(lk.try_lock() == false);
57     assert(try_lock_called == false);
58     assert(lk.owns_lock() == false);
59     lk.release();
60 #ifndef TEST_HAS_NO_EXCEPTIONS
61     try
62     {
63         lk.try_lock();
64         assert(false);
65     }
66     catch (std::system_error& e)
67     {
68         assert(e.code().value() == EPERM);
69     }
70 #endif
71 }
72