blob: 82b1ff86505337660fe15bd307041e60802d1963 [file] [log] [blame]
Howard Hinnantba898e42013-09-21 01:49:28 +00001//===----------------------------------------------------------------------===//
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//===----------------------------------------------------------------------===//
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +00009//
10// UNSUPPORTED: libcpp-has-no-threads
Howard Hinnantba898e42013-09-21 01:49:28 +000011
12// <shared_mutex>
13
14// template <class Mutex> class shared_lock;
15
16// bool try_lock();
17
18#include <shared_mutex>
19#include <cassert>
20
Dan Albert1d4a1ed2016-05-25 22:36:09 -070021#if _LIBCPP_STD_VER > 11
22
Howard Hinnantba898e42013-09-21 01:49:28 +000023bool try_lock_called = false;
24
25struct 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
35mutex m;
36
Dan Albert1d4a1ed2016-05-25 22:36:09 -070037#endif // _LIBCPP_STD_VER > 11
38
Howard Hinnantba898e42013-09-21 01:49:28 +000039int main()
40{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070041#if _LIBCPP_STD_VER > 11
Howard Hinnantba898e42013-09-21 01:49:28 +000042 std::shared_lock<mutex> lk(m, std::defer_lock);
43 assert(lk.try_lock() == true);
44 assert(try_lock_called == true);
45 assert(lk.owns_lock() == true);
46 try
47 {
48 lk.try_lock();
49 assert(false);
50 }
51 catch (std::system_error& e)
52 {
53 assert(e.code().value() == EDEADLK);
54 }
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 try
61 {
62 lk.try_lock();
63 assert(false);
64 }
65 catch (std::system_error& e)
66 {
67 assert(e.code().value() == EPERM);
68 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070069#endif // _LIBCPP_STD_VER > 11
Howard Hinnantba898e42013-09-21 01:49:28 +000070}