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#ifndef _LIBCPP___MUTEX_UNIQUE_LOCK_H
10#define _LIBCPP___MUTEX_UNIQUE_LOCK_H
11
12#include <__chrono/duration.h>
13#include <__chrono/time_point.h>
14#include <__config>
15#include <__memory/addressof.h>
16#include <__mutex/tag_types.h>
17#include <__system_error/throw_system_error.h>
18#include <__utility/move.h>
19#include <__utility/swap.h>
20#include <cerrno>
21
22#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23# pragma GCC system_header
24#endif
25
26_LIBCPP_PUSH_MACROS
27#include <__undef_macros>
28
29_LIBCPP_BEGIN_NAMESPACE_STD
30
31template <class _Mutex>
32class unique_lock {
33public:
34 typedef _Mutex mutex_type;
35
36private:
37 mutex_type* __m_;
38 bool __owns_;
39
40public:
41 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock() _NOEXCEPT : __m_(nullptr), __owns_(false) {}
42 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI explicit unique_lock(mutex_type& __m)
43 : __m_(std::addressof(__m)), __owns_(true) {
44 __m_->lock();
45 }
46
47 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, defer_lock_t) _NOEXCEPT
48 : __m_(std::addressof(__m)),
49 __owns_(false) {}
50
51 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, try_to_lock_t)
52 : __m_(std::addressof(__m)), __owns_(__m.try_lock()) {}
53
54 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, adopt_lock_t)
55 : __m_(std::addressof(__m)), __owns_(true) {}
56
57 template <class _Clock, class _Duration>
58 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, const chrono::time_point<_Clock, _Duration>& __t)
59 : __m_(std::addressof(__m)), __owns_(__m.try_lock_until(__t)) {}
60
61 template <class _Rep, class _Period>
62 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(mutex_type& __m, const chrono::duration<_Rep, _Period>& __d)
63 : __m_(std::addressof(__m)), __owns_(__m.try_lock_for(__d)) {}
64
65 _LIBCPP_HIDE_FROM_ABI ~unique_lock() {
66 if (__owns_)
67 __m_->unlock();
68 }
69
70 unique_lock(unique_lock const&) = delete;
71 unique_lock& operator=(unique_lock const&) = delete;
72
73 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI unique_lock(unique_lock&& __u) _NOEXCEPT
74 : __m_(__u.__m_),
75 __owns_(__u.__owns_) {
76 __u.__m_ = nullptr;
77 __u.__owns_ = false;
78 }
79
80 _LIBCPP_HIDE_FROM_ABI unique_lock& operator=(unique_lock&& __u) _NOEXCEPT {
81 if (this != std::addressof(__u))
82 unique_lock(std::move(__u)).swap(*this);
83 return *this;
84 }
85
86 _LIBCPP_HIDE_FROM_ABI void lock();
87 _LIBCPP_HIDE_FROM_ABI bool try_lock();
88
89 template <class _Rep, class _Period>
90 _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d);
91
92 template <class _Clock, class _Duration>
93 _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
94
95 _LIBCPP_HIDE_FROM_ABI void unlock();
96
97 _LIBCPP_HIDE_FROM_ABI void swap(unique_lock& __u) _NOEXCEPT {
98 std::swap(__m_, __u.__m_);
99 std::swap(__owns_, __u.__owns_);
100 }
101
102 _LIBCPP_HIDE_FROM_ABI mutex_type* release() _NOEXCEPT {
103 mutex_type* __m = __m_;
104 __m_ = nullptr;
105 __owns_ = false;
106 return __m;
107 }
108
109 _LIBCPP_HIDE_FROM_ABI bool owns_lock() const _NOEXCEPT { return __owns_; }
110 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __owns_; }
111 _LIBCPP_HIDE_FROM_ABI mutex_type* mutex() const _NOEXCEPT { return __m_; }
112};
113_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(unique_lock);
114
115template <class _Mutex>
116_LIBCPP_HIDE_FROM_ABI void unique_lock<_Mutex>::lock() {
117 if (__m_ == nullptr)
118 std::__throw_system_error(EPERM, what_arg: "unique_lock::lock: references null mutex");
119 if (__owns_)
120 std::__throw_system_error(EDEADLK, what_arg: "unique_lock::lock: already locked");
121 __m_->lock();
122 __owns_ = true;
123}
124
125template <class _Mutex>
126_LIBCPP_HIDE_FROM_ABI bool unique_lock<_Mutex>::try_lock() {
127 if (__m_ == nullptr)
128 std::__throw_system_error(EPERM, what_arg: "unique_lock::try_lock: references null mutex");
129 if (__owns_)
130 std::__throw_system_error(EDEADLK, what_arg: "unique_lock::try_lock: already locked");
131 __owns_ = __m_->try_lock();
132 return __owns_;
133}
134
135template <class _Mutex>
136template <class _Rep, class _Period>
137_LIBCPP_HIDE_FROM_ABI bool unique_lock<_Mutex>::try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
138 if (__m_ == nullptr)
139 std::__throw_system_error(EPERM, what_arg: "unique_lock::try_lock_for: references null mutex");
140 if (__owns_)
141 std::__throw_system_error(EDEADLK, what_arg: "unique_lock::try_lock_for: already locked");
142 __owns_ = __m_->try_lock_for(__d);
143 return __owns_;
144}
145
146template <class _Mutex>
147template <class _Clock, class _Duration>
148_LIBCPP_HIDE_FROM_ABI bool unique_lock<_Mutex>::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
149 if (__m_ == nullptr)
150 std::__throw_system_error(EPERM, what_arg: "unique_lock::try_lock_until: references null mutex");
151 if (__owns_)
152 std::__throw_system_error(EDEADLK, what_arg: "unique_lock::try_lock_until: already locked");
153 __owns_ = __m_->try_lock_until(__t);
154 return __owns_;
155}
156
157template <class _Mutex>
158_LIBCPP_HIDE_FROM_ABI void unique_lock<_Mutex>::unlock() {
159 if (!__owns_)
160 std::__throw_system_error(EPERM, what_arg: "unique_lock::unlock: not locked");
161 __m_->unlock();
162 __owns_ = false;
163}
164
165template <class _Mutex>
166inline _LIBCPP_HIDE_FROM_ABI void swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) _NOEXCEPT {
167 __x.swap(__y);
168}
169
170_LIBCPP_END_NAMESPACE_STD
171
172_LIBCPP_POP_MACROS
173
174#endif // _LIBCPP___MUTEX_UNIQUE_LOCK_H
175