1 // std::mutex implementation -*- C++ -*-
2
3 // Copyright (C) 2003-2022 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file bits/std_mutex.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{mutex}
28 */
29
30 #ifndef _GLIBCXX_MUTEX_H
31 #define _GLIBCXX_MUTEX_H 1
32
33 #pragma GCC system_header
34
35 #if __cplusplus < 201103L
36 # include <bits/c++0x_warning.h>
37 #else
38
39 #include <system_error>
40 #include <bits/functexcept.h>
41 #include <bits/gthr.h>
42
_GLIBCXX_VISIBILITY(default)43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 /**
48 * @defgroup mutexes Mutexes
49 * @ingroup concurrency
50 *
51 * Classes for mutex support.
52 * @{
53 */
54
55 #ifdef _GLIBCXX_HAS_GTHREADS
56 /// @cond undocumented
57
58 // Common base class for std::mutex and std::timed_mutex
59 class __mutex_base
60 {
61 protected:
62 typedef __gthread_mutex_t __native_type;
63
64 #ifdef __GTHREAD_MUTEX_INIT
65 __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
66
67 constexpr __mutex_base() noexcept = default;
68 #else
69 __native_type _M_mutex;
70
71 __mutex_base() noexcept
72 {
73 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
74 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
75 }
76
77 ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
78 #endif
79
80 __mutex_base(const __mutex_base&) = delete;
81 __mutex_base& operator=(const __mutex_base&) = delete;
82 };
83 /// @endcond
84
85 /** The standard mutex type.
86 *
87 * A simple, non-recursive, non-timed mutex.
88 *
89 * Do not call `lock()` and `unlock()` directly, use a scoped lock type
90 * such as `std::unique_lock`, `std::lock_guard`, or (since C++17)
91 * `std::scoped_lock`.
92 *
93 * @headerfile mutex
94 * @since C++11
95 */
96 class mutex : private __mutex_base
97 {
98 public:
99 typedef __native_type* native_handle_type;
100
101 #ifdef __GTHREAD_MUTEX_INIT
102 constexpr
103 #endif
104 mutex() noexcept = default;
105 ~mutex() = default;
106
107 mutex(const mutex&) = delete;
108 mutex& operator=(const mutex&) = delete;
109
110 void
111 lock()
112 {
113 int __e = __gthread_mutex_lock(&_M_mutex);
114
115 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
116 if (__e)
117 __throw_system_error(__e);
118 }
119
120 bool
121 try_lock() noexcept
122 {
123 // XXX EINVAL, EAGAIN, EBUSY
124 return !__gthread_mutex_trylock(&_M_mutex);
125 }
126
127 void
128 unlock()
129 {
130 // XXX EINVAL, EAGAIN, EPERM
131 __gthread_mutex_unlock(&_M_mutex);
132 }
133
134 native_handle_type
135 native_handle() noexcept
136 { return &_M_mutex; }
137 };
138
139 /// @cond undocumented
140
141 // Implementation details for std::condition_variable
142 class __condvar
143 {
144 using timespec = __gthread_time_t;
145
146 public:
147 __condvar() noexcept
148 {
149 #ifndef __GTHREAD_COND_INIT
150 __GTHREAD_COND_INIT_FUNCTION(&_M_cond);
151 #endif
152 }
153
154 ~__condvar()
155 {
156 int __e __attribute__((__unused__)) = __gthread_cond_destroy(&_M_cond);
157 __glibcxx_assert(__e != EBUSY); // threads are still blocked
158 }
159
160 __condvar(const __condvar&) = delete;
161 __condvar& operator=(const __condvar&) = delete;
162
163 __gthread_cond_t* native_handle() noexcept { return &_M_cond; }
164
165 // Expects: Calling thread has locked __m.
166 void
167 wait(mutex& __m)
168 {
169 int __e __attribute__((__unused__))
170 = __gthread_cond_wait(&_M_cond, __m.native_handle());
171 __glibcxx_assert(__e == 0);
172 }
173
174 void
175 wait_until(mutex& __m, timespec& __abs_time)
176 {
177 __gthread_cond_timedwait(&_M_cond, __m.native_handle(), &__abs_time);
178 }
179
180 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
181 void
182 wait_until(mutex& __m, clockid_t __clock, timespec& __abs_time)
183 {
184 pthread_cond_clockwait(&_M_cond, __m.native_handle(), __clock,
185 &__abs_time);
186 }
187 #endif
188
189 void
190 notify_one() noexcept
191 {
192 int __e __attribute__((__unused__)) = __gthread_cond_signal(&_M_cond);
193 __glibcxx_assert(__e == 0);
194 }
195
196 void
197 notify_all() noexcept
198 {
199 int __e __attribute__((__unused__)) = __gthread_cond_broadcast(&_M_cond);
200 __glibcxx_assert(__e == 0);
201 }
202
203 protected:
204 #ifdef __GTHREAD_COND_INIT
205 __gthread_cond_t _M_cond = __GTHREAD_COND_INIT;
206 #else
207 __gthread_cond_t _M_cond;
208 #endif
209 };
210 /// @endcond
211
212 #endif // _GLIBCXX_HAS_GTHREADS
213
214 /// Do not acquire ownership of the mutex.
215 struct defer_lock_t { explicit defer_lock_t() = default; };
216
217 /// Try to acquire ownership of the mutex without blocking.
218 struct try_to_lock_t { explicit try_to_lock_t() = default; };
219
220 /// Assume the calling thread has already obtained mutex ownership
221 /// and manage it.
222 struct adopt_lock_t { explicit adopt_lock_t() = default; };
223
224 /// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
225 _GLIBCXX17_INLINE constexpr defer_lock_t defer_lock { };
226
227 /// Tag used to prevent a scoped lock from blocking if a mutex is locked.
228 _GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock { };
229
230 /// Tag used to make a scoped lock take ownership of a locked mutex.
231 _GLIBCXX17_INLINE constexpr adopt_lock_t adopt_lock { };
232
233 /** @brief A simple scoped lock type.
234 *
235 * A lock_guard controls mutex ownership within a scope, releasing
236 * ownership in the destructor.
237 *
238 * @headerfile mutex
239 * @since C++11
240 */
241 template<typename _Mutex>
242 class lock_guard
243 {
244 public:
245 typedef _Mutex mutex_type;
246
247 explicit lock_guard(mutex_type& __m) : _M_device(__m)
248 { _M_device.lock(); }
249
250 lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
251 { } // calling thread owns mutex
252
253 ~lock_guard()
254 { _M_device.unlock(); }
255
256 lock_guard(const lock_guard&) = delete;
257 lock_guard& operator=(const lock_guard&) = delete;
258
259 private:
260 mutex_type& _M_device;
261 };
262
263 /// @} group mutexes
264 _GLIBCXX_END_NAMESPACE_VERSION
265 } // namespace
266 #endif // C++11
267 #endif // _GLIBCXX_MUTEX_H
268