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 // <mutex> 10 11 // template <class Mutex> class unique_lock; 12 13 // void lock(); 14 15 #include <cassert> 16 #include <mutex> 17 #include <system_error> 18 19 #include "checking_mutex.h" 20 #include "test_macros.h" 21 22 int main(int, char**) { 23 checking_mutex mux; 24 std::unique_lock<checking_mutex> lk(mux, std::defer_lock_t()); 25 assert(mux.last_try == checking_mutex::none); 26 lk.lock(); 27 assert(mux.current_state == checking_mutex::locked_via_lock); 28 mux.last_try = checking_mutex::none; 29 30 #ifndef TEST_HAS_NO_EXCEPTIONS 31 try { 32 mux.last_try = checking_mutex::none; 33 lk.lock(); 34 assert(false); 35 } catch (std::system_error& e) { 36 assert(mux.last_try == checking_mutex::none); 37 assert(e.code() == std::errc::resource_deadlock_would_occur); 38 } 39 40 lk.unlock(); 41 lk.release(); 42 43 try { 44 mux.last_try = checking_mutex::none; 45 lk.lock(); 46 assert(false); 47 } catch (std::system_error& e) { 48 assert(mux.last_try == checking_mutex::none); 49 assert(e.code() == std::errc::operation_not_permitted); 50 } 51 #endif 52 53 return 0; 54 } 55