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 11 // <mutex> 12 13 // template <class Mutex> class unique_lock; 14 15 // void unlock(); 16 17 #include <mutex> 18 #include <cassert> 19 20 #include "test_macros.h" 21 22 bool unlock_called = false; 23 24 struct mutex 25 { lockmutex26 void lock() {} unlockmutex27 void unlock() {unlock_called = true;} 28 }; 29 30 mutex m; 31 main(int,char **)32int main(int, char**) 33 { 34 std::unique_lock<mutex> lk(m); 35 lk.unlock(); 36 assert(unlock_called == true); 37 assert(lk.owns_lock() == false); 38 #ifndef TEST_HAS_NO_EXCEPTIONS 39 try 40 { 41 lk.unlock(); 42 assert(false); 43 } 44 catch (std::system_error& e) 45 { 46 assert(e.code().value() == EPERM); 47 } 48 #endif 49 lk.release(); 50 #ifndef TEST_HAS_NO_EXCEPTIONS 51 try 52 { 53 lk.unlock(); 54 assert(false); 55 } 56 catch (std::system_error& e) 57 { 58 assert(e.code().value() == EPERM); 59 } 60 #endif 61 62 return 0; 63 } 64