Skip to content

Commit

Permalink
Add precondition annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
NN--- committed Mar 7, 2023
1 parent 33108e2 commit 8ed4013
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
15 changes: 7 additions & 8 deletions stl/inc/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -178,31 +178,30 @@ public:
unique_lock() noexcept = default;

_NODISCARD_CTOR_LOCK
explicit unique_lock(_Mutex& _Mtx)
: _Pmtx(_STD addressof(_Mtx)), _Owns(false) { // construct and lock
explicit unique_lock(_Mutex& _Mtx) : _Pmtx(_STD addressof(_Mtx)), _Owns(false) { // construct and lock
_Pmtx->lock();
_Owns = true;
}

_NODISCARD_CTOR_LOCK
unique_lock(_Mutex& _Mtx, adopt_lock_t) noexcept // strengthened
unique_lock(_Mutex& _Mtx, adopt_lock_t) noexcept // strengthened
: _Pmtx(_STD addressof(_Mtx)), _Owns(true) {} // construct and assume already locked

unique_lock(_Mutex& _Mtx, defer_lock_t) noexcept
: _Pmtx(_STD addressof(_Mtx)), _Owns(false) {} // construct but don't lock

_NODISCARD_CTOR_LOCK
unique_lock(_Mutex& _Mtx, try_to_lock_t)
unique_lock(_Mutex& _Mtx, try_to_lock_t)
: _Pmtx(_STD addressof(_Mtx)), _Owns(_Pmtx->try_lock()) {} // construct and try to lock

template <class _Rep, class _Period>
_NODISCARD_CTOR_LOCK
unique_lock(_Mutex& _Mtx, const chrono::duration<_Rep, _Period>& _Rel_time)
unique_lock(_Mutex& _Mtx, const chrono::duration<_Rep, _Period>& _Rel_time)
: _Pmtx(_STD addressof(_Mtx)), _Owns(_Pmtx->try_lock_for(_Rel_time)) {} // construct and lock with timeout

template <class _Clock, class _Duration>
_NODISCARD_CTOR_LOCK
unique_lock(_Mutex& _Mtx, const chrono::time_point<_Clock, _Duration>& _Abs_time)
unique_lock(_Mutex& _Mtx, const chrono::time_point<_Clock, _Duration>& _Abs_time)
: _Pmtx(_STD addressof(_Mtx)), _Owns(_Pmtx->try_lock_until(_Abs_time)) {
// construct and lock with timeout
#if _HAS_CXX20
Expand All @@ -211,13 +210,13 @@ public:
}

_NODISCARD_CTOR_LOCK
unique_lock(_Mutex& _Mtx, const xtime* _Abs_time)
unique_lock(_Mutex& _Mtx, const xtime* _Abs_time)
: _Pmtx(_STD addressof(_Mtx)), _Owns(false) { // try to lock until _Abs_time
_Owns = _Pmtx->try_lock_until(_Abs_time);
}

_NODISCARD_CTOR_LOCK
unique_lock(unique_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) {
unique_lock(unique_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) {
_Other._Pmtx = nullptr;
_Other._Owns = false;
}
Expand Down
50 changes: 26 additions & 24 deletions stl/inc/shared_mutex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public:
shared_timed_mutex(const shared_timed_mutex&) = delete;
shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;

_Acquires_shared_lock_(this)
_Acquires_exclusive_lock_(this)
_Acquires_nonreentrant_lock_(this)
void lock() { // lock exclusive
unique_lock<mutex> _Lock(_Mymtx);
Expand All @@ -106,7 +106,7 @@ public:
}

_NODISCARD_TRY_CHANGE_STATE
_When_(return != false, _Acquires_shared_lock_(this))
_When_(return != false, _Acquires_exclusive_lock_(this))
_When_(return != false, _Acquires_nonreentrant_lock_(this))
bool try_lock() { // try to lock exclusive
lock_guard<mutex> _Lock(_Mymtx);
Expand All @@ -120,15 +120,15 @@ public:

template <class _Rep, class _Period>
_NODISCARD_TRY_CHANGE_STATE
_When_(return != false, _Acquires_shared_lock_(this))
_When_(return != false, _Acquires_exclusive_lock_(this))
_When_(return != false, _Acquires_nonreentrant_lock_(this))
bool try_lock_for(const chrono::duration<_Rep, _Period>& _Rel_time) { // try to lock for duration
return try_lock_until(_To_absolute_time(_Rel_time));
}

template <class _Clock, class _Duration>
_NODISCARD_TRY_CHANGE_STATE
_When_(return != false, _Acquires_shared_lock_(this))
_When_(return != false, _Acquires_exclusive_lock_(this))
_When_(return != false, _Acquires_nonreentrant_lock_(this))
bool try_lock_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
// try to lock until time point
Expand All @@ -155,7 +155,7 @@ public:
return true;
}

_Releases_shared_lock_(this)
_Releases_exclusive_lock_(this)
_Releases_nonreentrant_lock_(this)
void unlock() { // unlock exclusive
{ // unlock before notifying, for efficiency
Expand Down Expand Up @@ -273,6 +273,8 @@ public:
_NODISCARD_CTOR_LOCK
_Acquires_shared_lock_(_Mtx)
_Acquires_nonreentrant_lock_(_Mtx)
_Acquires_shared_lock_(*this->_Pmtx)
_Acquires_nonreentrant_lock_(*this->_Pmtx)
explicit shared_lock(mutex_type& _Mtx)
: _Pmtx(_STD addressof(_Mtx)), _Owns(true) { // construct with mutex and lock shared
_Mtx.lock_shared();
Expand All @@ -282,46 +284,46 @@ public:
: _Pmtx(_STD addressof(_Mtx)), _Owns(false) {} // construct with unlocked mutex

_NODISCARD_CTOR_LOCK
shared_lock(mutex_type& _Mtx, try_to_lock_t)
shared_lock(mutex_type& _Mtx, try_to_lock_t)
: _Pmtx(_STD addressof(_Mtx)), _Owns(_Mtx.try_lock_shared()) {} // construct with mutex and try to lock shared

_NODISCARD_CTOR_LOCK
shared_lock(mutex_type& _Mtx, adopt_lock_t) noexcept // strengthened
shared_lock(mutex_type& _Mtx, adopt_lock_t) noexcept // strengthened
: _Pmtx(_STD addressof(_Mtx)), _Owns(true) {} // construct with mutex and adopt ownership

template <class _Rep, class _Period>
_NODISCARD_CTOR_LOCK
shared_lock(mutex_type& _Mtx, const chrono::duration<_Rep, _Period>& _Rel_time)
shared_lock(mutex_type& _Mtx, const chrono::duration<_Rep, _Period>& _Rel_time)
: _Pmtx(_STD addressof(_Mtx)), _Owns(_Mtx.try_lock_shared_for(_Rel_time)) {
// construct with mutex and try to lock for relative time
}

template <class _Clock, class _Duration>
_NODISCARD_CTOR_LOCK
shared_lock(mutex_type& _Mtx, const chrono::time_point<_Clock, _Duration>& _Abs_time)
shared_lock(mutex_type& _Mtx, const chrono::time_point<_Clock, _Duration>& _Abs_time)
: _Pmtx(_STD addressof(_Mtx)), _Owns(_Mtx.try_lock_shared_until(_Abs_time)) {
// construct with mutex and try to lock until absolute time
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
}

_Releases_shared_lock_(this)
_Releases_nonreentrant_lock_(this)
_Releases_shared_lock_(*this->_Pmtx)
_Releases_nonreentrant_lock_(*this->_Pmtx)
~shared_lock() noexcept {
if (_Owns) {
_Pmtx->unlock_shared();
}
}

_NODISCARD_CTOR_LOCK
shared_lock(shared_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) {
shared_lock(shared_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) {
_Other._Pmtx = nullptr;
_Other._Owns = false;
}

_Releases_shared_lock_(this)
_Releases_nonreentrant_lock_(this)
_Releases_shared_lock_(*this->_Pmtx)
_Releases_nonreentrant_lock_(*this->_Pmtx)
shared_lock& operator=(shared_lock&& _Right) noexcept {
if (_Owns) {
_Pmtx->unlock_shared();
Expand All @@ -337,17 +339,17 @@ public:
shared_lock(const shared_lock&) = delete;
shared_lock& operator=(const shared_lock&) = delete;

_Acquires_shared_lock_(this)
_Acquires_nonreentrant_lock_(this)
_Acquires_shared_lock_(*this->_Pmtx)
_Acquires_nonreentrant_lock_(*this->_Pmtx)
void lock() { // lock the mutex
_Validate();
_Pmtx->lock_shared();
_Owns = true;
}

_NODISCARD_TRY_CHANGE_STATE
_When_(return != false, _Acquires_shared_lock_(this))
_When_(return != false, _Acquires_nonreentrant_lock_(this))
_When_(return != false, _Acquires_shared_lock_(*this->_Pmtx))
_When_(return != false, _Acquires_nonreentrant_lock_(*this->_Pmtx))
bool try_lock() { // try to lock the mutex
_Validate();
_Owns = _Pmtx->try_lock_shared();
Expand All @@ -356,8 +358,8 @@ public:

template <class _Rep, class _Period>
_NODISCARD_TRY_CHANGE_STATE
_When_(return != false, _Acquires_shared_lock_(this))
_When_(return != false, _Acquires_nonreentrant_lock_(this))
_When_(return != false, _Acquires_shared_lock_(*this->_Pmtx))
_When_(return != false, _Acquires_nonreentrant_lock_(*this->_Pmtx))
bool try_lock_for(const chrono::duration<_Rep, _Period>& _Rel_time) {
// try to lock the mutex for _Rel_time
_Validate();
Expand All @@ -367,8 +369,8 @@ public:

template <class _Clock, class _Duration>
_NODISCARD_TRY_CHANGE_STATE
_When_(return != false, _Acquires_shared_lock_(this))
_When_(return != false, _Acquires_nonreentrant_lock_(this))
_When_(return != false, _Acquires_shared_lock_(*this->_Pmtx))
_When_(return != false, _Acquires_nonreentrant_lock_(*this->_Pmtx))
bool try_lock_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
// try to lock the mutex until _Abs_time
#if _HAS_CXX20
Expand All @@ -379,8 +381,8 @@ public:
return _Owns;
}

_Releases_shared_lock_(this)
_Releases_nonreentrant_lock_(this)
_Releases_shared_lock_(*this->_Pmtx)
_Releases_nonreentrant_lock_(*this->_Pmtx)
void unlock() { // try to unlock the mutex
if (!_Pmtx || !_Owns) {
_Throw_system_error(errc::operation_not_permitted);
Expand Down

0 comments on commit 8ed4013

Please sign in to comment.