libstdc++
std_mutex.h
Go to the documentation of this file.
1 // std::mutex implementation -*- C++ -*-
2 
3 // Copyright (C) 2003-2020 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 
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 
47 #ifndef _GLIBCXX_THREAD_SAFETY_ANNOTATION
48 # ifdef _GLIBCXX_HAS_THREAD_SAFETY_ANNOTATIONS
49 # define _GLIBCXX_THREAD_SAFETY_ANNOTATION(x) __attribute__ ((x))
50 # else
51 # define _GLIBCXX_THREAD_SAFETY_ANNOTATION(x)
52 # endif
53 #endif // _GLIBCXX_THREAD_SAFETY_ANNOTATION
54 
55  /**
56  * @defgroup mutexes Mutexes
57  * @ingroup concurrency
58  *
59  * Classes for mutex support.
60  * @{
61  */
62 
63 #ifdef _GLIBCXX_HAS_GTHREADS
64  // Common base class for std::mutex and std::timed_mutex
65  class __mutex_base
66  {
67  protected:
68  typedef __gthread_mutex_t __native_type;
69 
70 #ifdef __GTHREAD_MUTEX_INIT
71  __native_type _M_mutex = __GTHREAD_MUTEX_INIT;
72 
73  constexpr __mutex_base() noexcept = default;
74 #else
75  __native_type _M_mutex;
76 
77  __mutex_base() noexcept
78  {
79  // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
80  __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
81  }
82 
83  ~__mutex_base() noexcept { __gthread_mutex_destroy(&_M_mutex); }
84 #endif
85 
86  __mutex_base(const __mutex_base&) = delete;
87  __mutex_base& operator=(const __mutex_base&) = delete;
88  };
89 
90  /// The standard mutex type.
91  class _GLIBCXX_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex
92  : private __mutex_base
93  {
94  public:
95  typedef __native_type* native_handle_type;
96 
97 #ifdef __GTHREAD_MUTEX_INIT
98  constexpr
99 #endif
100  mutex() noexcept = default;
101  ~mutex() = default;
102 
103  mutex(const mutex&) = delete;
104  mutex& operator=(const mutex&) = delete;
105 
106  void
107  lock() _GLIBCXX_THREAD_SAFETY_ANNOTATION(acquire_capability())
108  {
109  int __e = __gthread_mutex_lock(&_M_mutex);
110 
111  // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
112  if (__e)
113  __throw_system_error(__e);
114  }
115 
116  bool
117  try_lock() noexcept
118  _GLIBCXX_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true))
119  {
120  // XXX EINVAL, EAGAIN, EBUSY
121  return !__gthread_mutex_trylock(&_M_mutex);
122  }
123 
124  void
125  unlock() _GLIBCXX_THREAD_SAFETY_ANNOTATION(release_capability())
126  {
127  // XXX EINVAL, EAGAIN, EPERM
128  __gthread_mutex_unlock(&_M_mutex);
129  }
130 
131  native_handle_type
132  native_handle() noexcept
133  { return &_M_mutex; }
134  };
135 
136 #endif // _GLIBCXX_HAS_GTHREADS
137 
138  /// Do not acquire ownership of the mutex.
139  struct defer_lock_t { explicit defer_lock_t() = default; };
140 
141  /// Try to acquire ownership of the mutex without blocking.
142  struct try_to_lock_t { explicit try_to_lock_t() = default; };
143 
144  /// Assume the calling thread has already obtained mutex ownership
145  /// and manage it.
146  struct adopt_lock_t { explicit adopt_lock_t() = default; };
147 
148  /// Tag used to prevent a scoped lock from acquiring ownership of a mutex.
149  _GLIBCXX17_INLINE constexpr defer_lock_t defer_lock { };
150 
151  /// Tag used to prevent a scoped lock from blocking if a mutex is locked.
152  _GLIBCXX17_INLINE constexpr try_to_lock_t try_to_lock { };
153 
154  /// Tag used to make a scoped lock take ownership of a locked mutex.
155  _GLIBCXX17_INLINE constexpr adopt_lock_t adopt_lock { };
156 
157  /** @brief A simple scoped lock type.
158  *
159  * A lock_guard controls mutex ownership within a scope, releasing
160  * ownership in the destructor.
161  */
162  template<typename _Mutex>
163  class _GLIBCXX_THREAD_SAFETY_ANNOTATION(scoped_lockable) lock_guard
164  {
165  public:
166  typedef _Mutex mutex_type;
167 
168  explicit lock_guard(mutex_type& __m)
169  _GLIBCXX_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
170  : _M_device(__m)
171  { _M_device.lock(); }
172 
173  lock_guard(mutex_type& __m, adopt_lock_t) noexcept
174  _GLIBCXX_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
175  : _M_device(__m)
176  { } // calling thread owns mutex
177 
178  ~lock_guard() _GLIBCXX_THREAD_SAFETY_ANNOTATION(release_capability())
179  { _M_device.unlock(); }
180 
181  lock_guard(const lock_guard&) = delete;
182  lock_guard& operator=(const lock_guard&) = delete;
183 
184  private:
185  mutex_type& _M_device;
186  };
187 
188  // @} group mutexes
189 _GLIBCXX_END_NAMESPACE_VERSION
190 } // namespace
191 #endif // C++11
192 #endif // _GLIBCXX_MUTEX_H
Try to acquire ownership of the mutex without blocking.
Definition: std_mutex.h:142
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
Generic lock.
Definition: mutex:589
constexpr try_to_lock_t try_to_lock
Tag used to prevent a scoped lock from blocking if a mutex is locked.
Definition: std_mutex.h:152
A simple scoped lock type.
Definition: std_mutex.h:163
Do not acquire ownership of the mutex.
Definition: std_mutex.h:139
Assume the calling thread has already obtained mutex ownership and manage it.
Definition: std_mutex.h:146
int try_lock(_Lock1 &__l1, _Lock2 &__l2, _Lock3 &... __l3)
Generic try_lock.
Definition: mutex:568
ISO C++ entities toplevel namespace is std.
The standard mutex type.
Definition: std_mutex.h:91
constexpr adopt_lock_t adopt_lock
Tag used to make a scoped lock take ownership of a locked mutex.
Definition: std_mutex.h:155
constexpr defer_lock_t defer_lock
Tag used to prevent a scoped lock from acquiring ownership of a mutex.
Definition: std_mutex.h:149