nlib
CriticalSection.h
Go to the documentation of this file.
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_THREADING_CRITICALSECTION_H_
17 #define INCLUDE_NN_NLIB_THREADING_CRITICALSECTION_H_
18 
19 #include "nn/nlib/Config.h"
20 #include "nn/nlib/Swap.h"
21 
22 NLIB_NAMESPACE_BEGIN
23 class TimeSpan;
24 class DateTime;
25 namespace threading {
26 
27 class NLIB_CAPABILITY("mutex") SimpleCriticalSection {
28  public:
29 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
30  constexpr SimpleCriticalSection() NLIB_NOEXCEPT = default;
31 #else
32  SimpleCriticalSection() NLIB_NOEXCEPT {
33  errno_t e = nlib_mutex_init(&mutex_);
34  NLIB_ASSERT_NOERR(e);
35  NLIB_UNUSED(e);
36  }
37 #endif
38  ~SimpleCriticalSection() NLIB_NOEXCEPT {
39  errno_t e = nlib_mutex_destroy(&mutex_);
40  NLIB_UNUSED(e);
41  NLIB_ASSERT_NOERR(e);
42  }
43  void lock() NLIB_NOEXCEPT NLIB_ACQUIRE() {
44  errno_t e = nlib_mutex_lock(&mutex_);
45  NLIB_UNUSED(e);
46  NLIB_ASSERT_NOERR(e);
47  }
48  void unlock() NLIB_NOEXCEPT NLIB_RELEASE() {
49  errno_t e = nlib_mutex_unlock(&mutex_);
50  NLIB_UNUSED(e);
51  NLIB_ASSERT_NOERR(e);
52  }
53  NLIB_CHECK_RESULT bool try_lock() NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true) {
54  errno_t e = nlib_mutex_trylock(&mutex_);
55  if (e == 0) return true;
56  NLIB_ASSERT(e == EBUSY);
57  return false;
58  }
60  native_handle_type native_handle() NLIB_NOEXCEPT NLIB_RETURN_CAPABILITY(mutex_) {
61  return &mutex_;
62  }
63 
64  private:
65 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
67 #else
68  nlib_mutex mutex_;
69 #endif
70 };
71 
72 #ifndef NLIB_CRITICALSECTION_USE_FALLBACK
73 class NLIB_CAPABILITY("mutex") CriticalSection {
74  public:
75 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
76  constexpr CriticalSection() NLIB_NOEXCEPT = default;
77 #else
78  CriticalSection() NLIB_NOEXCEPT {
80  NLIB_ASSERT_NOERR(e);
81  NLIB_UNUSED(e);
82  }
83 #endif
84  ~CriticalSection() NLIB_NOEXCEPT { nlib_mutex_destroy(&mutex_); }
85  void lock() NLIB_NOEXCEPT NLIB_ACQUIRE() {
86  errno_t e = nlib_mutex_lock(&mutex_);
87  NLIB_ASSERT_NOERR(e);
88  NLIB_UNUSED(e);
89  }
90  void unlock() NLIB_NOEXCEPT NLIB_RELEASE() {
91  errno_t e = nlib_mutex_unlock(&mutex_);
92  NLIB_ASSERT_NOERR(e);
93  NLIB_UNUSED(e);
94  }
95  NLIB_CHECK_RESULT bool try_lock() NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true) {
96  errno_t e = nlib_mutex_trylock(&mutex_);
97  if (e == 0) return true;
98  NLIB_ASSERT(e == EBUSY);
99  return false;
100  }
102  native_handle_type native_handle() NLIB_NOEXCEPT NLIB_RETURN_CAPABILITY(mutex_) {
103  return &mutex_;
104  }
105 
106  private:
107 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
109 #else
110  nlib_mutex mutex_;
111 #endif
112 };
113 #endif
114 
115 #ifndef NLIB_TIMEDCRITICALSECTION_USE_FALLBACK
116 class NLIB_CAPABILITY("mutex") TimedCriticalSection {
117  public:
118 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
119  constexpr TimedCriticalSection() NLIB_NOEXCEPT = default;
120 #else
121  TimedCriticalSection() NLIB_NOEXCEPT {
123  NLIB_ASSERT_NOERR(e);
124  NLIB_UNUSED(e);
125  }
126 #endif
127  ~TimedCriticalSection() NLIB_NOEXCEPT { nlib_mutex_destroy(&mutex_); }
128  NLIB_VIS_PUBLIC bool try_lock_for(const TimeSpan& span) NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true);
129  NLIB_VIS_PUBLIC bool try_lock_until(const DateTime& abstime)
130  NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true);
131  void lock() NLIB_NOEXCEPT NLIB_ACQUIRE() {
132  errno_t e = nlib_mutex_lock(&mutex_);
133  NLIB_ASSERT_NOERR(e);
134  NLIB_UNUSED(e);
135  }
136  void unlock() NLIB_NOEXCEPT NLIB_RELEASE() {
137  errno_t e = nlib_mutex_unlock(&mutex_);
138  NLIB_ASSERT_NOERR(e);
139  NLIB_UNUSED(e);
140  }
141  NLIB_CHECK_RESULT bool try_lock() NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true) {
142  errno_t e = nlib_mutex_trylock(&mutex_);
143  if (e == 0) return true;
144  NLIB_ASSERT(e == EBUSY);
145  return false;
146  }
148  native_handle_type native_handle() NLIB_NOEXCEPT NLIB_RETURN_CAPABILITY(mutex_) {
149  return &mutex_;
150  }
151 
152  private:
153 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS)
155 #else
156  nlib_mutex mutex_;
157 #endif
158 };
159 #endif
160 
161 } // namespace threading
162 NLIB_NAMESPACE_END
163 
164 NLIB_NAMESPACE_BEGIN
165 namespace threading {
166 
167 class DummyCriticalSection {
168  public:
169  NLIB_CEXPR DummyCriticalSection() NLIB_NOEXCEPT {}
170  ~DummyCriticalSection() NLIB_NOEXCEPT {}
171  void lock() NLIB_NOEXCEPT {}
172  void unlock() NLIB_NOEXCEPT {}
173  bool try_lock() NLIB_NOEXCEPT { return true; }
174  bool try_lock_for(const TimeSpan&) NLIB_NOEXCEPT { return true; } // NOLINT
175  bool try_lock_until(const DateTime&) NLIB_NOEXCEPT { return true; } // NOLINT
176  typedef int native_handle_type;
177  native_handle_type native_handle() NLIB_NOEXCEPT { return 0; }
178 
179  private:
180  NLIB_DISALLOW_COPY_AND_ASSIGN(DummyCriticalSection);
181 };
182 
183 class CriticalSectionFallback {
184  public:
185  // NOTE:
186  // -1 must not be a thread-id
187  CriticalSectionFallback() NLIB_NOEXCEPT : counter_(0) {
188  tid_ = -1;
189  }
190  ~CriticalSectionFallback() NLIB_NOEXCEPT {}
191  NLIB_VIS_PUBLIC void lock() NLIB_NOEXCEPT;
192  NLIB_VIS_PUBLIC void unlock() NLIB_NOEXCEPT;
193  NLIB_VIS_PUBLIC bool try_lock() NLIB_NOEXCEPT;
195  native_handle_type native_handle() { return lock_.native_handle(); }
196 
197  private:
198  SimpleCriticalSection lock_;
199  int32_t counter_;
200  nlib_thread_id tid_;
201 };
202 
203 #ifdef NLIB_CRITICALSECTION_USE_FALLBACK
204 typedef CriticalSectionFallback CriticalSection;
205 #endif
206 
207 class TimedCriticalSectionFallback {
208  public:
209  TimedCriticalSectionFallback() NLIB_NOEXCEPT {}
210  ~TimedCriticalSectionFallback() NLIB_NOEXCEPT {}
211  void lock() NLIB_NOEXCEPT NLIB_NO_THREAD_SAFETY_ANALYSIS { lock_.lock(); }
212  void unlock() NLIB_NOEXCEPT NLIB_NO_THREAD_SAFETY_ANALYSIS { lock_.unlock(); }
213  bool try_lock() NLIB_NOEXCEPT { return lock_.try_lock(); }
214  NLIB_VIS_PUBLIC bool try_lock_for(const TimeSpan& span) NLIB_NOEXCEPT;
215  NLIB_VIS_PUBLIC bool try_lock_until(const DateTime& abstime) NLIB_NOEXCEPT;
217  native_handle_type native_handle() { return lock_.native_handle(); }
218 
219  private:
220  CriticalSection lock_;
221 };
222 
223 #ifdef NLIB_TIMEDCRITICALSECTION_USE_FALLBACK
224 typedef TimedCriticalSectionFallback TimedCriticalSection;
225 #endif
226 
227 } // namespace threading
228 NLIB_NAMESPACE_END
229 
230 NLIB_NAMESPACE_BEGIN
231 namespace threading {
232 
233 struct AdoptLockType {};
234 struct TryToLockType {};
235 struct DeferLockType {};
236 
240 
241 template <class T>
242 class NLIB_SCOPED_CAPABILITY ScopedLock {
243  public:
244  typedef T mutex_type;
245  typedef typename mutex_type::native_handle_type native_handle_type;
246  explicit ScopedLock(mutex_type& m) NLIB_ACQUIRE(lock_) : lock_(m) { // NOLINT
247  lock_.lock();
248  }
249  ScopedLock(mutex_type& m, AdoptLockType) NLIB_NOEXCEPT NLIB_ACQUIRE(lock_) // NOLINT
250  : lock_(m) {}
251  ~ScopedLock() NLIB_NOEXCEPT NLIB_RELEASE(lock_) { lock_.unlock(); }
252  native_handle_type native_handle() NLIB_RETURN_CAPABILITY(lock_) {
253  return lock_.native_handle();
254  }
255 
256  private:
257  mutex_type& lock_;
259 };
260 
261 template <>
262 class NLIB_SCOPED_CAPABILITY ScopedLock<SimpleCriticalSection> {
263  public:
264  typedef SimpleCriticalSection mutex_type;
265  typedef mutex_type::native_handle_type native_handle_type;
266  explicit ScopedLock(mutex_type& m) NLIB_ACQUIRE(lock_) : lock_(m) { // NOLINT
267  lock_.lock();
268  }
269  ScopedLock(mutex_type& m, AdoptLockType) NLIB_NOEXCEPT NLIB_REQUIRES(m) // NOLINT
270  NLIB_ACQUIRE(lock_)
271  : lock_(m) {}
272  ~ScopedLock() NLIB_NOEXCEPT NLIB_RELEASE(lock_) { lock_.unlock(); }
273  native_handle_type native_handle() NLIB_RETURN_CAPABILITY(lock_) {
274  return lock_.native_handle();
275  }
276 
277  private:
278  mutex_type& lock_;
279  NLIB_DISALLOW_COPY_AND_ASSIGN(ScopedLock);
280 };
281 
282 template<>
283 class NLIB_SCOPED_CAPABILITY ScopedLock<nlib_mutex> {
284  public:
285  typedef nlib_mutex mutex_type;
286  typedef nlib_mutex* native_handle_type;
287  explicit ScopedLock(mutex_type& m) NLIB_ACQUIRE(lock_) : lock_(m) { // NOLINT
288  errno_t e = nlib_mutex_lock(&lock_);
289  NLIB_UNUSED(e);
290  NLIB_ASSERT_NOERR(e);
291  }
292  ScopedLock(mutex_type& m, AdoptLockType) NLIB_NOEXCEPT NLIB_REQUIRES(lock_) // NOLINT
293  NLIB_ACQUIRE(lock_)
294  : lock_(m) {}
295  ~ScopedLock() NLIB_NOEXCEPT NLIB_RELEASE(lock_) {
296  errno_t e = nlib_mutex_unlock(&lock_);
297  NLIB_UNUSED(e);
298  NLIB_ASSERT_NOERR(e);
299  }
300  native_handle_type native_handle() NLIB_RETURN_CAPABILITY(lock_) { return &lock_; }
301 
302  private:
303  nlib_mutex& lock_;
304  NLIB_DISALLOW_COPY_AND_ASSIGN(ScopedLock);
305 };
306 
307 template <class T>
308 class NLIB_SCOPED_CAPABILITY UniqueLock {
309  public:
310  typedef T mutex_type;
311  typedef typename mutex_type::native_handle_type native_handle_type;
312  UniqueLock() NLIB_NOEXCEPT : locker_(NULL), is_locked_(false) {}
313  explicit UniqueLock(mutex_type& rhs) NLIB_ACQUIRE(locker_) :
314  locker_(&rhs), is_locked_(true) { // NOLINT
315  rhs.lock();
316  }
317  UniqueLock(mutex_type& rhs, AdoptLockType) // NOLINT
318  NLIB_REQUIRES(locker_) : locker_(&rhs), is_locked_(true) {}
319  UniqueLock(mutex_type& rhs, DeferLockType) // NOLINT
320  NLIB_NOEXCEPT NLIB_EXCLUDES(locker_)
321  : locker_(&rhs), is_locked_(false) {}
322  UniqueLock(mutex_type& rhs, TryToLockType) // NOLINT
323  : locker_(&rhs),
324  is_locked_(rhs.try_lock()) {}
325  NLIB_MOVE_MEMBER_HELPER_2(UniqueLock, locker_, is_locked_)
326  ~UniqueLock() NLIB_NOEXCEPT NLIB_RELEASE() {
327  if (is_locked_) locker_->unlock();
328  }
329  void lock() NLIB_ACQUIRE() {
330  // no NLIB_NOEXCEPT
331  NLIB_ASSERT(locker_ && !is_locked_);
332  if (!locker_ || is_locked_) {
333  return;
334  }
335  locker_->lock();
336  is_locked_ = true;
337  }
338  void unlock() NLIB_NOEXCEPT NLIB_RELEASE() {
339  NLIB_ASSERT(locker_ && is_locked_);
340  if (!locker_ || !is_locked_) {
341  return;
342  }
343  locker_->unlock();
344  is_locked_ = false;
345  }
346  NLIB_CHECK_RESULT bool try_lock() NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true) {
347  NLIB_ASSERT(locker_ && !is_locked_);
348  if (!locker_ || is_locked_) {
349  return false;
350  }
351  is_locked_ = locker_->try_lock();
352  return is_locked_;
353  }
355  NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true) {
356  NLIB_ASSERT(locker_ && !is_locked_);
357  if (!locker_ || is_locked_) {
358  return false;
359  }
360  is_locked_ = locker_->try_lock_for(timeout);
361  return is_locked_;
362  }
364  NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(true) {
365  NLIB_ASSERT(locker_ && !is_locked_);
366  if (!locker_ || is_locked_) {
367  return false;
368  }
369  is_locked_ = locker_->try_lock_until(abstime);
370  return is_locked_;
371  }
372  void swap(UniqueLock& rhs) NLIB_NOEXCEPT { // NOLINT
373  using std::swap;
374  swap(locker_, rhs.locker_);
375  swap(is_locked_, rhs.is_locked_);
376  }
377  mutex_type* release() NLIB_NOEXCEPT NLIB_RETURN_CAPABILITY(*locker_) {
378  mutex_type* p = locker_;
379  locker_ = NULL;
380  is_locked_ = false;
381  return p;
382  }
383  mutex_type* mutex() const NLIB_NOEXCEPT NLIB_RETURN_CAPABILITY(*locker_) {
384  return locker_;
385  }
386  native_handle_type native_handle() NLIB_RETURN_CAPABILITY(*locker_) {
387  return locker_->native_handle();
388  }
389  bool owns_lock() const NLIB_NOEXCEPT { return is_locked_; }
390  NLIB_SAFE_BOOL(UniqueLock, owns_lock());
391 
392  private:
393  mutex_type* locker_;
394  bool is_locked_;
396 };
397 
398 namespace detail {
399 
400 template<class T>
401 NLIB_ALWAYS_INLINE nlib_mutex* GetRawMutex(T& obj) NLIB_RETURN_CAPABILITY(obj) { // NOLINT
402  return obj.native_handle();
403 }
404 
405 template<>
406 NLIB_ALWAYS_INLINE nlib_mutex* GetRawMutex(nlib_mutex& obj) NLIB_RETURN_CAPABILITY(obj) { // NOLINT
407  return &obj;
408 }
409 
410 } // namespace detail
411 
412 } // namespace threading
413 NLIB_NAMESPACE_END
414 
415 #ifndef NLIB_STD_SWAP_WORKAROUND
416 NLIB_NAMESPACE_BEGIN
417 #endif
418 
419 NLIB_DEFINE_STD_SWAP_T_BEGIN1(threading) // NOLINT
420 NLIB_DEFINE_STD_SWAP_T1(T, NLIB_NS::threading::UniqueLock) // NOLINT
421 NLIB_DEFINE_STD_SWAP_T_END1(threading) // NOLINT
422 
423 #ifndef NLIB_STD_SWAP_WORKAROUND
424 NLIB_NAMESPACE_END
425 #endif
426 
427 #ifdef NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS
428 template<class T>
429 bool nlib_is_error(NLIB_NS::threading::UniqueLock<T>&) = delete;
430 #else
431 template<class T>
432 bool nlib_is_error(NLIB_NS::threading::UniqueLock<T>&);
433 #endif
434 
435 #endif // INCLUDE_NN_NLIB_THREADING_CRITICALSECTION_H_
#define NLIB_RECURSIVE_TIMED_MUTEX_INITIALIZER
A macro for statically initializing nlib_mutex. Makes it a recursive mutex that can time out...
native_handle_type native_handle() noexcept NLIB_RETURN_CAPABILITY(mutex_)
Gets a pointer to a native type mutex.
NLIB_CHECK_RESULT bool try_lock() noexcept NLIB_TRY_ACQUIRE(true)
Gets a lock, and attempts to enter the critical section.
UniqueLock() noexcept
Initializes an object without creating an association.
#define NLIB_ALWAYS_INLINE
Indicates that the compiler is forced to perform inline expansion of functions.
Definition: Platform_unix.h:97
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:163
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:178
Simplest critical section. Not reentrant.
mutex_type * mutex() const noexcept NLIB_RETURN_CAPABILITY(*locker_)
Gets the pointer to the associated object like CriticalSection.
mutex_type * release() noexcept NLIB_RETURN_CAPABILITY(*locker_)
Removes the association. Does not unlock.
errno_t nlib_mutex_recursive_timed_init(nlib_mutex *mutex) NLIB_EXCLUDES(*mutex)
Initializes a mutex that is recursive and can time out.
NLIB_CHECK_RESULT bool try_lock_until(const DateTime &abstime) noexcept NLIB_TRY_ACQUIRE(true)
Attempts to lock with timeout the associated CriticalSection or other object.
native_handle_type native_handle() NLIB_RETURN_CAPABILITY(lock_)
Returns an implementation-specific handle representing a lock.
errno_t nlib_mutex_unlock(nlib_mutex *mutex) NLIB_RELEASE(*mutex)
Unlocks the specified mutex.
NLIB_CHECK_RESULT bool try_lock() noexcept NLIB_TRY_ACQUIRE(true)
Gets a lock, and attempts to enter the critical section.
#define NLIB_MUTEX_INITIALIZER
A macro for statically initializing nlib_mutex.
#define NLIB_CHECK_RESULT
Indicates that the caller of the function must check the returned value.
mutex_type::native_handle_type native_handle_type
mutex_type::native_handle_type.
bool owns_lock() const noexcept
Returns true if the lock associated with UniqueLock is locked.
mutex_type::native_handle_type native_handle_type
mutex_type::native_handle_type.
UniqueLock(mutex_type &rhs, AdoptLockType) NLIB_REQUIRES(locker_)
Assumes an object like CriticalSection is already locked, and initializes the object without locking ...
errno_t nlib_mutex_init(nlib_mutex *mutex) NLIB_EXCLUDES(*mutex)
Initializes a mutex.
The class for representing the date and time.
Definition: DateTime.h:261
void swap(UniqueLock &rhs) noexcept
Attempts to swap the associated CriticalSection or other object.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
#define NLIB_RECURSIVE_MUTEX_INITIALIZER
A macro for statically initializing nlib_mutex. Makes it a recursive mutex.
native_handle_type native_handle() NLIB_RETURN_CAPABILITY(*locker_)
Returns an implementation-specific handle representing a lock.
errno_t nlib_mutex_lock(nlib_mutex *mutex) NLIB_ACQUIRE(*mutex)
Locks the specified mutex.
Used in ScopedLock and UniqueLock by tag type.
Class to wrap objects like CriticalSection.
nlib_mutex * native_handle_type
typedef to a pointer type to a native mutex.
void lock() noexcept NLIB_ACQUIRE()
Gets a lock, and enters the critical section. Blocks until it can get a lock.
NLIB_CHECK_RESULT bool try_lock_for(const TimeSpan &timeout) noexcept NLIB_TRY_ACQUIRE(true)
Attempts to lock with timeout the associated CriticalSection or other object.
ScopedLock(mutex_type &m) NLIB_ACQUIRE(lock_)
Locks objects like CriticalSection (calls lock).
UniqueLock(mutex_type &rhs, DeferLockType) noexcept NLIB_EXCLUDES(locker_)
Initializes an object without locking.
void unlock() noexcept NLIB_RELEASE()
Releases the lock, and exits the critical section.
Used in ScopedLock and UniqueLock by tag type.
nlib_mutex * native_handle_type
typedef to a pointer type to a native mutex.
nlib_mutex * native_handle_type
typedef to a pointer type to a native mutex.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:99
constexpr const DeferLockType deferLock
A DeferLockType-type value.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:93
A file that contains the configuration information for each development environment.
constexpr const AdoptLockType adoptLock
An AdoptLockType-type value.
Used in ScopedLock and UniqueLock by tag type.
void unlock() noexcept NLIB_RELEASE()
Unlocks the associated CriticalSection or other object.
NLIB_CHECK_RESULT errno_t nlib_mutex_trylock(nlib_mutex *mutex) NLIB_TRY_ACQUIRE(0
Locks mutex, but only if it is not locked.
UniqueLock(mutex_type &rhs, TryToLockType)
Attempts to lock using try_lock when initializing.
native_handle_type native_handle() noexcept NLIB_RETURN_CAPABILITY(mutex_)
Gets a pointer to a native type mutex.
int nlib_thread_id
A unique integer value for each thread.
Definition: Platform.h:1224
Wraps objects like CriticalSection. Locks with a constructor, and unlocks with a destructor.
native_handle_type native_handle() noexcept NLIB_RETURN_CAPABILITY(mutex_)
Gets a pointer to a native type mutex.
void lock() NLIB_ACQUIRE()
Locks the associated CriticalSection or other object.
pthread_mutex_t nlib_mutex
The type for mutex variables.
void lock() noexcept NLIB_ACQUIRE()
Gets a lock, and enters the critical section. Blocks until it can get a lock.
void lock() noexcept NLIB_ACQUIRE()
Gets a lock, and enters the critical section. Blocks until it can get a lock.
NLIB_CHECK_RESULT bool try_lock() noexcept NLIB_TRY_ACQUIRE(true)
Gets a lock, and attempts to enter the critical section.
~ScopedLock() noexcept NLIB_RELEASE(lock_)
Unlocks objects like CriticalSection (calls unlock).
The class for representing the time.
Definition: DateTime.h:106
void unlock() noexcept NLIB_RELEASE()
Releases the lock, and exits the critical section.
UniqueLock(mutex_type &rhs) NLIB_ACQUIRE(locker_)
Locks the object like CriticalSection and associates it with this object.
Critical section that can timeout in reentrant.
constexpr const TryToLockType tryToLock
A TryToLockType-type value.
void unlock() noexcept NLIB_RELEASE()
Releases the lock, and exits the critical section.
errno_t nlib_mutex_destroy(nlib_mutex *mutex) NLIB_EXCLUDES(*mutex)
Destroys the specified mutex object and frees any associated resources.
bool nlib_is_error(const T &obj) noexcept
Returns true when the process result or object status is in an erroneous condition.
Definition: Config.h:768
ScopedLock(mutex_type &m, AdoptLockType) noexcept NLIB_ACQUIRE(lock_)
Assumes an object like CriticalSection is already locked, and initializes the object without locking ...
errno_t nlib_mutex_recursive_init(nlib_mutex *mutex) NLIB_EXCLUDES(*mutex)
Initializes a recursive mutex.
NLIB_CHECK_RESULT bool try_lock() noexcept NLIB_TRY_ACQUIRE(true)
Attempts to lock the associated object like CriticalSection.
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37