• 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 // <shared_mutex>
11 
12 // template <class Mutex> class shared_lock;
13 
14 // template <class Rep, class Period>
15 //   bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
16 
17 #include <shared_mutex>
18 #include <cassert>
19 
20 #if _LIBCPP_STD_VER > 11
21 
22 bool try_lock_for_called = false;
23 
24 typedef std::chrono::milliseconds ms;
25 
26 struct mutex
27 {
28     template <class Rep, class Period>
try_lock_shared_formutex29         bool try_lock_shared_for(const std::chrono::duration<Rep, Period>& rel_time)
30     {
31         assert(rel_time == ms(5));
32         try_lock_for_called = !try_lock_for_called;
33         return try_lock_for_called;
34     }
unlock_sharedmutex35     void unlock_shared() {}
36 };
37 
38 mutex m;
39 
40 #endif  // _LIBCPP_STD_VER > 11
41 
main()42 int main()
43 {
44 #if _LIBCPP_STD_VER > 11
45     std::shared_lock<mutex> lk(m, std::defer_lock);
46     assert(lk.try_lock_for(ms(5)) == true);
47     assert(try_lock_for_called == true);
48     assert(lk.owns_lock() == true);
49     try
50     {
51         lk.try_lock_for(ms(5));
52         assert(false);
53     }
54     catch (std::system_error& e)
55     {
56         assert(e.code().value() == EDEADLK);
57     }
58     lk.unlock();
59     assert(lk.try_lock_for(ms(5)) == false);
60     assert(try_lock_for_called == false);
61     assert(lk.owns_lock() == false);
62     lk.release();
63     try
64     {
65         lk.try_lock_for(ms(5));
66         assert(false);
67     }
68     catch (std::system_error& e)
69     {
70         assert(e.code().value() == EPERM);
71     }
72 #endif  // _LIBCPP_STD_VER > 11
73 }
74