16 #ifndef INCLUDE_NN_NLIB_THREADING_CRITICALSECTION_H_ 17 #define INCLUDE_NN_NLIB_THREADING_CRITICALSECTION_H_ 20 #include "nn/nlib/Swap.h" 29 #if defined(__cpp_constexpr) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 55 if (e == 0)
return true;
56 NLIB_ASSERT(e == EBUSY);
65 #if defined(__cpp_constexpr) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 73 #ifndef NLIB_CRITICALSECTION_USE_FALLBACK 76 #if defined(__cpp_constexpr) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 98 if (e == 0)
return true;
99 NLIB_ASSERT(e == EBUSY);
108 #if defined(__cpp_constexpr) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 117 #ifndef NLIB_TIMEDCRITICALSECTION_USE_FALLBACK 120 #if defined(__cpp_constexpr) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 125 NLIB_ASSERT_NOERR(e);
132 NLIB_TRY_ACQUIRE(
true);
135 NLIB_ASSERT_NOERR(e);
140 NLIB_ASSERT_NOERR(e);
145 if (e == 0)
return true;
146 NLIB_ASSERT(e == EBUSY);
155 #if defined(__cpp_constexpr) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 168 namespace threading {
177 bool try_lock_for(
const TimeSpan&)
NLIB_NOEXCEPT {
return true; }
178 bool try_lock_until(
const DateTime&)
NLIB_NOEXCEPT {
return true; }
179 typedef int native_handle_type;
180 native_handle_type native_handle()
NLIB_NOEXCEPT {
return 0; }
190 CriticalSectionFallback()
NLIB_NOEXCEPT : counter_(0) { tid_ = -1; }
195 typedef SimpleCriticalSection::native_handle_type native_handle_type;
196 native_handle_type native_handle() {
return lock_.native_handle(); }
199 SimpleCriticalSection lock_;
204 #ifdef NLIB_CRITICALSECTION_USE_FALLBACK 205 typedef CriticalSectionFallback CriticalSection;
208 class TimedCriticalSectionFallback
NLIB_FINAL {
212 void lock()
NLIB_NOEXCEPT NLIB_NO_THREAD_SAFETY_ANALYSIS { lock_.lock(); }
213 void unlock()
NLIB_NOEXCEPT NLIB_NO_THREAD_SAFETY_ANALYSIS { lock_.unlock(); }
217 typedef CriticalSection::native_handle_type native_handle_type;
218 native_handle_type native_handle() {
return lock_.native_handle(); }
221 CriticalSection lock_;
224 #ifdef NLIB_TIMEDCRITICALSECTION_USE_FALLBACK 225 typedef TimedCriticalSectionFallback TimedCriticalSection;
232 namespace threading {
251 return lock_.native_handle();
260 class NLIB_SCOPED_CAPABILITY ScopedLock<SimpleCriticalSection>
NLIB_FINAL {
262 typedef SimpleCriticalSection mutex_type;
263 typedef mutex_type::native_handle_type native_handle_type;
264 explicit ScopedLock(mutex_type& m) NLIB_ACQUIRE(lock_) : lock_(m) { lock_.lock(); }
265 ScopedLock(mutex_type& m, AdoptLockType)
NLIB_NOEXCEPT NLIB_REQUIRES(m) NLIB_ACQUIRE(lock_) :
267 ~ScopedLock()
NLIB_NOEXCEPT NLIB_RELEASE(lock_) { lock_.unlock(); }
268 native_handle_type native_handle() NLIB_RETURN_CAPABILITY(lock_) {
269 return lock_.native_handle();
282 explicit ScopedLock(mutex_type& m) NLIB_ACQUIRE(lock_) : lock_(m) {
285 NLIB_ASSERT_NOERR(e);
287 ScopedLock(mutex_type& m, AdoptLockType)
NLIB_NOEXCEPT NLIB_REQUIRES(lock_)
288 NLIB_ACQUIRE(lock_) :
293 NLIB_ASSERT_NOERR(e);
295 native_handle_type native_handle() NLIB_RETURN_CAPABILITY(lock_) {
return &lock_; }
319 if (is_locked_) locker_->unlock();
321 #ifdef __cpp_rvalue_references 323 rhs.locker_ =
nullptr;
324 rhs.is_locked_ =
false;
327 locker_ = rhs.locker_;
328 is_locked_ = rhs.is_locked_;
329 rhs.locker_ =
nullptr;
330 rhs.is_locked_ =
false;
335 is_locked_(rhs.is_locked_) {
336 rhs.locker_ =
nullptr;
337 rhs.is_locked_ =
false;
340 locker_ = rhs.locker_;
341 is_locked_ = rhs.is_locked_;
342 rhs.locker_ =
nullptr;
343 rhs.is_locked_ =
false;
348 NLIB_ASSERT(locker_ && !is_locked_);
349 if (!locker_ || is_locked_) {
356 NLIB_ASSERT(locker_ && is_locked_);
357 if (!locker_ || !is_locked_) {
364 NLIB_ASSERT(locker_ && !is_locked_);
365 if (!locker_ || is_locked_) {
368 is_locked_ = locker_->try_lock();
372 NLIB_TRY_ACQUIRE(
true) {
373 NLIB_ASSERT(locker_ && !is_locked_);
374 if (!locker_ || is_locked_) {
377 is_locked_ = locker_->try_lock_for(timeout);
381 NLIB_TRY_ACQUIRE(
true) {
382 NLIB_ASSERT(locker_ && !is_locked_);
383 if (!locker_ || is_locked_) {
386 is_locked_ = locker_->try_lock_until(abstime);
397 return locker_->native_handle();
412 return obj.native_handle();
425 NLIB_DEFINE_STD_SWAP_T_BEGIN3(
nn, nlib, threading)
426 NLIB_DEFINE_STD_SWAP_T1(T, NLIB_NS::threading::UniqueLock)
427 NLIB_DEFINE_STD_SWAP_T_END3(
nn, nlib, threading)
429 #if defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) && !defined(NLIB_DOXYGEN) 431 bool nlib_is_error(NLIB_NS::threading::UniqueLock<T>&) =
delete;
437 #endif // INCLUDE_NN_NLIB_THREADING_CRITICALSECTION_H_ UniqueLock & assign(UniqueLock &rhs, move_tag) noexcept
Corresponds to a move assignment operator.
UniqueLock(UniqueLock &rhs, move_tag) noexcept
Corresponds to a move constructor.
native_handle_type native_handle() noexcept NLIB_RETURN_CAPABILITY(mutex_)
Gets a pointer to a native type mutex.
bool try_lock() noexcept NLIB_TRY_ACQUIRE(true)
Gets a lock, and attempts to enter the critical section.
UniqueLock() noexcept
Instantiates the object with default parameters (default constructor). Initializes an object without ...
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
#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...
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.
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.
bool try_lock() noexcept NLIB_TRY_ACQUIRE(true)
Gets a lock, and attempts to enter the critical section.
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 ...
The class for representing the date and time.
native_handle_type native_handle() NLIB_RETURN_CAPABILITY(*locker_)
Returns an implementation-specific handle representing a lock.
An empty structure indicating that an argument to a function needs to be moved.
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.
UniqueLock & operator=(UniqueLock &&rhs) noexcept
Move assignment operator.
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.
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.
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.
UniqueLock(UniqueLock &&rhs) noexcept
Instantiates the object (move constructor).
void unlock() noexcept NLIB_RELEASE()
Unlocks the associated CriticalSection or other object.
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.
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.
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.
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).
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
The class for representing the time.
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.
ScopedLock(mutex_type &m, AdoptLockType) noexcept NLIB_ACQUIRE(lock_)
Assumes an object like CriticalSection is already locked, and initializes the object without locking ...
bool try_lock() noexcept NLIB_TRY_ACQUIRE(true)
Attempts to lock the associated object like CriticalSection.