3 #ifndef INCLUDE_NN_NLIB_THREADING_CRITICALSECTION_H_ 4 #define INCLUDE_NN_NLIB_THREADING_CRITICALSECTION_H_ 7 #include "nn/nlib/Swap.h" 16 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 30 void lock() NLIB_NOEXCEPT NLIB_ACQUIRE() {
35 void unlock() NLIB_NOEXCEPT NLIB_RELEASE() {
42 if (e == 0)
return true;
43 NLIB_ASSERT(e == EBUSY);
47 native_handle_type
native_handle() NLIB_NOEXCEPT NLIB_RETURN_CAPABILITY(mutex_) {
52 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 59 #ifndef NLIB_CRITICALSECTION_USE_FALLBACK 62 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 72 void lock() NLIB_NOEXCEPT NLIB_ACQUIRE() {
77 void unlock() NLIB_NOEXCEPT NLIB_RELEASE() {
84 if (e == 0)
return true;
85 NLIB_ASSERT(e == EBUSY);
89 native_handle_type
native_handle() NLIB_NOEXCEPT NLIB_RETURN_CAPABILITY(mutex_) {
94 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 102 #ifndef NLIB_TIMEDCRITICALSECTION_USE_FALLBACK 105 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 110 NLIB_ASSERT_NOERR(e);
117 NLIB_NOEXCEPT NLIB_TRY_ACQUIRE(
true);
118 void lock() NLIB_NOEXCEPT NLIB_ACQUIRE() {
120 NLIB_ASSERT_NOERR(e);
123 void unlock() NLIB_NOEXCEPT NLIB_RELEASE() {
125 NLIB_ASSERT_NOERR(e);
130 if (e == 0)
return true;
131 NLIB_ASSERT(e == EBUSY);
135 native_handle_type
native_handle() NLIB_NOEXCEPT NLIB_RETURN_CAPABILITY(mutex_) {
140 #if defined(NLIB_CXX11_CONSTEXPR) && defined(NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS) 152 namespace threading {
154 class DummyCriticalSection {
164 native_handle_type native_handle()
NLIB_NOEXCEPT {
return 0; }
170 class CriticalSectionFallback {
182 native_handle_type native_handle() {
return lock_.native_handle(); }
190 #ifdef NLIB_CRITICALSECTION_USE_FALLBACK 194 class TimedCriticalSectionFallback {
198 void lock()
NLIB_NOEXCEPT NLIB_NO_THREAD_SAFETY_ANALYSIS { lock_.lock(); }
199 void unlock()
NLIB_NOEXCEPT NLIB_NO_THREAD_SAFETY_ANALYSIS { lock_.unlock(); }
204 native_handle_type native_handle() {
return lock_.native_handle(); }
207 CriticalSection lock_;
210 #ifdef NLIB_TIMEDCRITICALSECTION_USE_FALLBACK 218 namespace threading {
233 explicit ScopedLock(mutex_type& m) NLIB_ACQUIRE(lock_) : lock_(m) {
240 return lock_.native_handle();
249 class NLIB_SCOPED_CAPABILITY ScopedLock<SimpleCriticalSection> {
253 explicit ScopedLock(mutex_type& m) NLIB_ACQUIRE(lock_) : lock_(m) {
259 ~ScopedLock()
NLIB_NOEXCEPT NLIB_RELEASE(lock_) { lock_.unlock(); }
260 native_handle_type native_handle() NLIB_RETURN_CAPABILITY(lock_) {
261 return lock_.native_handle();
270 class NLIB_SCOPED_CAPABILITY ScopedLock<nlib_mutex> {
274 explicit ScopedLock(mutex_type& m) NLIB_ACQUIRE(lock_) : lock_(m) {
277 NLIB_ASSERT_NOERR(e);
285 NLIB_ASSERT_NOERR(e);
287 native_handle_type native_handle() NLIB_RETURN_CAPABILITY(lock_) {
return &lock_; }
301 locker_(&rhs), is_locked_(true) {
305 NLIB_REQUIRES(locker_) : locker_(&rhs), is_locked_(true) {}
308 : locker_(&rhs), is_locked_(false) {}
311 is_locked_(rhs.try_lock()) {}
312 NLIB_MOVE_MEMBER_HELPER_2(
UniqueLock, locker_, is_locked_)
314 if (is_locked_) locker_->unlock();
318 NLIB_ASSERT(locker_ && !is_locked_);
319 if (!locker_ || is_locked_) {
326 NLIB_ASSERT(locker_ && is_locked_);
327 if (!locker_ || !is_locked_) {
334 NLIB_ASSERT(locker_ && !is_locked_);
335 if (!locker_ || is_locked_) {
338 is_locked_ = locker_->try_lock();
343 NLIB_ASSERT(locker_ && !is_locked_);
344 if (!locker_ || is_locked_) {
347 is_locked_ = locker_->try_lock_for(timeout);
352 NLIB_ASSERT(locker_ && !is_locked_);
353 if (!locker_ || is_locked_) {
356 is_locked_ = locker_->try_lock_until(abstime);
361 swap(locker_, rhs.locker_);
362 swap(is_locked_, rhs.is_locked_);
365 mutex_type* p = locker_;
374 return locker_->native_handle();
389 return obj.native_handle();
393 NLIB_ALWAYS_INLINE nlib_mutex* GetRawMutex(nlib_mutex& obj) NLIB_RETURN_CAPABILITY(obj) {
402 #ifndef NLIB_STD_SWAP_WORKAROUND 406 NLIB_DEFINE_STD_SWAP_T_BEGIN1(threading)
407 NLIB_DEFINE_STD_SWAP_T1(T, NLIB_NS::threading::UniqueLock)
408 NLIB_DEFINE_STD_SWAP_T_END1(threading)
410 #ifndef NLIB_STD_SWAP_WORKAROUND 414 #ifdef NLIB_CXX11_DEFAULTED_AND_DELETED_FUNCTIONS 416 bool nlib_is_error(NLIB_NS::threading::UniqueLock<T>&) =
delete;
422 #endif // INCLUDE_NN_NLIB_THREADING_CRITICALSECTION_H_
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_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.
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.
NLIB_CHECK_RESULT 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.
void swap(UniqueLock &rhs) noexcept
Attempts to swap the associated CriticalSection or other object.
native_handle_type native_handle() NLIB_RETURN_CAPABILITY(*locker_)
Returns an implementation-specific handle representing a lock.
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.
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.
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.
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.
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 ...
NLIB_CHECK_RESULT bool try_lock() noexcept NLIB_TRY_ACQUIRE(true)
Attempts to lock the associated object like CriticalSection.