1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // UNSUPPORTED: no-threads 10 // UNSUPPORTED: c++03, c++11 11 // 12 // ALLOW_RETRIES: 2 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 #include <mutex> 23 24 #include "test_macros.h" 25 26 bool try_lock_called = false; 27 28 struct mutex 29 { try_lock_sharedmutex30 bool try_lock_shared() 31 { 32 try_lock_called = !try_lock_called; 33 return try_lock_called; 34 } unlock_sharedmutex35 void unlock_shared() {} 36 }; 37 38 mutex m; 39 main(int,char **)40int main(int, char**) 41 { 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 #ifndef TEST_HAS_NO_EXCEPTIONS 47 try 48 { 49 TEST_IGNORE_NODISCARD lk.try_lock(); 50 assert(false); 51 } 52 catch (std::system_error& e) 53 { 54 assert(e.code().value() == EDEADLK); 55 } 56 #endif 57 lk.unlock(); 58 assert(lk.try_lock() == false); 59 assert(try_lock_called == false); 60 assert(lk.owns_lock() == false); 61 lk.release(); 62 #ifndef TEST_HAS_NO_EXCEPTIONS 63 try 64 { 65 TEST_IGNORE_NODISCARD lk.try_lock(); 66 assert(false); 67 } 68 catch (std::system_error& e) 69 { 70 assert(e.code().value() == EPERM); 71 } 72 #endif 73 74 return 0; 75 } 76