Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 11 | |
| 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 Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 21 | #if _LIBCPP_STD_VER > 11 |
| 22 | |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 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 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 37 | #endif // _LIBCPP_STD_VER > 11 |
| 38 | |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 39 | int main() |
| 40 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 41 | #if _LIBCPP_STD_VER > 11 |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 42 | 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 Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 69 | #endif // _LIBCPP_STD_VER > 11 |
Howard Hinnant | ba898e4 | 2013-09-21 01:49:28 +0000 | [diff] [blame] | 70 | } |