Fix handling of an empty filename at end of a path
[official-gcc.git] / libstdc++-v3 / include / bits / std_mutex.h
blob34d22907a06ffdf8c075178c1a9b79c55321484b
1 // std::mutex implementation -*- C++ -*-
3 // Copyright (C) 2003-2018 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.
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.
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.
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/>.
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}
30 #ifndef _GLIBCXX_MUTEX_H
31 #define _GLIBCXX_MUTEX_H 1
33 #pragma GCC system_header
35 #if __cplusplus < 201103L
36 # include <bits/c++0x_warning.h>
37 #else
39 #include <system_error>
40 #include <bits/functexcept.h>
41 #include <bits/gthr.h>
42 #include <bits/move.h> // for std::swap
44 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 /**
51 * @defgroup mutexes Mutexes
52 * @ingroup concurrency
54 * Classes for mutex support.
55 * @{
58 #ifdef _GLIBCXX_HAS_GTHREADS
59 // Common base class for std::mutex and std::timed_mutex
60 class __mutex_base
62 protected:
63 typedef __gthread_mutex_t __native_type;
65 #ifdef __GTHREAD_MUTEX_INIT
66 __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
68 constexpr __mutex_base() noexcept = default;
69 #else
70 __native_type _M_mutex;
72 __mutex_base() noexcept
74 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
75 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
78 ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
79 #endif
81 __mutex_base(const __mutex_base&) = delete;
82 __mutex_base& operator=(const __mutex_base&) = delete;
85 /// The standard mutex type.
86 class mutex : private __mutex_base
88 public:
89 typedef __native_type* native_handle_type;
91 #ifdef __GTHREAD_MUTEX_INIT
92 constexpr
93 #endif
94 mutex() noexcept = default;
95 ~mutex() = default;
97 mutex(const mutex&) = delete;
98 mutex& operator=(const mutex&) = delete;
100 void
101 lock()
103 int __e = __gthread_mutex_lock(&_M_mutex);
105 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
106 if (__e)
107 __throw_system_error(__e);
110 bool
111 try_lock() noexcept
113 // XXX EINVAL, EAGAIN, EBUSY
114 return !__gthread_mutex_trylock(&_M_mutex);
117 void
118 unlock()
120 // XXX EINVAL, EAGAIN, EPERM
121 __gthread_mutex_unlock(&_M_mutex);
124 native_handle_type
125 native_handle() noexcept
126 { return &_M_mutex; }
129 #endif // _GLIBCXX_HAS_GTHREADS
131 /// Do not acquire ownership of the mutex.
132 struct defer_lock_t { explicit defer_lock_t() = default; };
134 /// Try to acquire ownership of the mutex without blocking.
135 struct try_to_lock_t { explicit try_to_lock_t() = default; };
137 /// Assume the calling thread has already obtained mutex ownership
138 /// and manage it.
139 struct adopt_lock_t { explicit adopt_lock_t() = default; };
141 /// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
142 _GLIBCXX17_INLINE constexpr defer_lock_t defer_lock { };
144 /// Tag used to prevent a scoped lock from blocking if a mutex is locked.
145 _GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock { };
147 /// Tag used to make a scoped lock take ownership of a locked mutex.
148 _GLIBCXX17_INLINE constexpr adopt_lock_t adopt_lock { };
150 /** @brief A simple scoped lock type.
152 * A lock_guard controls mutex ownership within a scope, releasing
153 * ownership in the destructor.
155 template<typename _Mutex>
156 class lock_guard
158 public:
159 typedef _Mutex mutex_type;
161 explicit lock_guard(mutex_type& __m) : _M_device(__m)
162 { _M_device.lock(); }
164 lock_guard(mutex_type& __m, adopt_lock_t) noexcept : _M_device(__m)
165 { } // calling thread owns mutex
167 ~lock_guard()
168 { _M_device.unlock(); }
170 lock_guard(const lock_guard&) = delete;
171 lock_guard& operator=(const lock_guard&) = delete;
173 private:
174 mutex_type& _M_device;
177 /** @brief A movable scoped lock type.
179 * A unique_lock controls mutex ownership within a scope. Ownership of the
180 * mutex can be delayed until after construction and can be transferred
181 * to another unique_lock by move construction or move assignment. If a
182 * mutex lock is owned when the destructor runs ownership will be released.
184 template<typename _Mutex>
185 class unique_lock
187 public:
188 typedef _Mutex mutex_type;
190 unique_lock() noexcept
191 : _M_device(0), _M_owns(false)
194 explicit unique_lock(mutex_type& __m)
195 : _M_device(std::__addressof(__m)), _M_owns(false)
197 lock();
198 _M_owns = true;
201 unique_lock(mutex_type& __m, defer_lock_t) noexcept
202 : _M_device(std::__addressof(__m)), _M_owns(false)
205 unique_lock(mutex_type& __m, try_to_lock_t)
206 : _M_device(std::__addressof(__m)), _M_owns(_M_device->try_lock())
209 unique_lock(mutex_type& __m, adopt_lock_t) noexcept
210 : _M_device(std::__addressof(__m)), _M_owns(true)
212 // XXX calling thread owns mutex
215 template<typename _Clock, typename _Duration>
216 unique_lock(mutex_type& __m,
217 const chrono::time_point<_Clock, _Duration>& __atime)
218 : _M_device(std::__addressof(__m)),
219 _M_owns(_M_device->try_lock_until(__atime))
222 template<typename _Rep, typename _Period>
223 unique_lock(mutex_type& __m,
224 const chrono::duration<_Rep, _Period>& __rtime)
225 : _M_device(std::__addressof(__m)),
226 _M_owns(_M_device->try_lock_for(__rtime))
229 ~unique_lock()
231 if (_M_owns)
232 unlock();
235 unique_lock(const unique_lock&) = delete;
236 unique_lock& operator=(const unique_lock&) = delete;
238 unique_lock(unique_lock&& __u) noexcept
239 : _M_device(__u._M_device), _M_owns(__u._M_owns)
241 __u._M_device = 0;
242 __u._M_owns = false;
245 unique_lock& operator=(unique_lock&& __u) noexcept
247 if(_M_owns)
248 unlock();
250 unique_lock(std::move(__u)).swap(*this);
252 __u._M_device = 0;
253 __u._M_owns = false;
255 return *this;
258 void
259 lock()
261 if (!_M_device)
262 __throw_system_error(int(errc::operation_not_permitted));
263 else if (_M_owns)
264 __throw_system_error(int(errc::resource_deadlock_would_occur));
265 else
267 _M_device->lock();
268 _M_owns = true;
272 bool
273 try_lock()
275 if (!_M_device)
276 __throw_system_error(int(errc::operation_not_permitted));
277 else if (_M_owns)
278 __throw_system_error(int(errc::resource_deadlock_would_occur));
279 else
281 _M_owns = _M_device->try_lock();
282 return _M_owns;
286 template<typename _Clock, typename _Duration>
287 bool
288 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
290 if (!_M_device)
291 __throw_system_error(int(errc::operation_not_permitted));
292 else if (_M_owns)
293 __throw_system_error(int(errc::resource_deadlock_would_occur));
294 else
296 _M_owns = _M_device->try_lock_until(__atime);
297 return _M_owns;
301 template<typename _Rep, typename _Period>
302 bool
303 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
305 if (!_M_device)
306 __throw_system_error(int(errc::operation_not_permitted));
307 else if (_M_owns)
308 __throw_system_error(int(errc::resource_deadlock_would_occur));
309 else
311 _M_owns = _M_device->try_lock_for(__rtime);
312 return _M_owns;
316 void
317 unlock()
319 if (!_M_owns)
320 __throw_system_error(int(errc::operation_not_permitted));
321 else if (_M_device)
323 _M_device->unlock();
324 _M_owns = false;
328 void
329 swap(unique_lock& __u) noexcept
331 std::swap(_M_device, __u._M_device);
332 std::swap(_M_owns, __u._M_owns);
335 mutex_type*
336 release() noexcept
338 mutex_type* __ret = _M_device;
339 _M_device = 0;
340 _M_owns = false;
341 return __ret;
344 bool
345 owns_lock() const noexcept
346 { return _M_owns; }
348 explicit operator bool() const noexcept
349 { return owns_lock(); }
351 mutex_type*
352 mutex() const noexcept
353 { return _M_device; }
355 private:
356 mutex_type* _M_device;
357 bool _M_owns; // XXX use atomic_bool
360 /// Swap overload for unique_lock objects.
361 template<typename _Mutex>
362 inline void
363 swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y) noexcept
364 { __x.swap(__y); }
366 // @} group mutexes
367 _GLIBCXX_END_NAMESPACE_VERSION
368 } // namespace
369 #endif // _GLIBCXX_USE_C99_STDINT_TR1
371 #endif // C++11
373 #endif // _GLIBCXX_MUTEX_H