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

source code of libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/unlock.pass.cpp