Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions stl/inc/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -125,41 +125,42 @@ _INLINE_VAR constexpr try_to_lock_t try_to_lock{};

// CLASS TEMPLATE unique_lock
template <class _Mutex>
class _NODISCARD unique_lock { // whizzy class with destructor that unlocks mutex
class unique_lock { // whizzy class with destructor that unlocks mutex
public:
using mutex_type = _Mutex;

// CONSTRUCT, ASSIGN, AND DESTROY
unique_lock() noexcept : _Pmtx(nullptr), _Owns(false) {}

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

unique_lock(_Mutex& _Mtx, adopt_lock_t)
_NODISCARD_CTOR unique_lock(_Mutex& _Mtx, adopt_lock_t)
: _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

unique_lock(_Mutex& _Mtx, try_to_lock_t)
_NODISCARD_CTOR 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>
unique_lock(_Mutex& _Mtx, const chrono::duration<_Rep, _Period>& _Rel_time)
_NODISCARD_CTOR 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>
unique_lock(_Mutex& _Mtx, const chrono::time_point<_Clock, _Duration>& _Abs_time)
_NODISCARD_CTOR 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

unique_lock(_Mutex& _Mtx, const xtime* _Abs_time)
_NODISCARD_CTOR 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);
}

unique_lock(unique_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) {
_NODISCARD_CTOR unique_lock(unique_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) {
_Other._Pmtx = nullptr;
_Other._Owns = false;
}
Expand Down
14 changes: 7 additions & 7 deletions stl/inc/shared_mutex
Original file line number Diff line number Diff line change
Expand Up @@ -227,34 +227,34 @@ private:

// CLASS TEMPLATE shared_lock
template <class _Mutex>
class _NODISCARD shared_lock { // shareable lock
class shared_lock { // shareable lock
public:
using mutex_type = _Mutex;

shared_lock() noexcept : _Pmtx(nullptr), _Owns(false) {}

explicit shared_lock(mutex_type& _Mtx)
_NODISCARD_CTOR explicit shared_lock(mutex_type& _Mtx)
: _Pmtx(_STD addressof(_Mtx)), _Owns(true) { // construct with mutex and lock shared
_Mtx.lock_shared();
}

shared_lock(mutex_type& _Mtx, defer_lock_t) noexcept
: _Pmtx(_STD addressof(_Mtx)), _Owns(false) {} // construct with unlocked mutex

shared_lock(mutex_type& _Mtx, try_to_lock_t)
_NODISCARD_CTOR 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

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

template <class _Rep, class _Period>
shared_lock(mutex_type& _Mtx, const chrono::duration<_Rep, _Period>& _Rel_time)
_NODISCARD_CTOR 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>
shared_lock(mutex_type& _Mtx, const chrono::time_point<_Clock, _Duration>& _Abs_time)
_NODISCARD_CTOR 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
}
Expand All @@ -265,7 +265,7 @@ public:
}
}

shared_lock(shared_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) {
_NODISCARD_CTOR shared_lock(shared_lock&& _Other) noexcept : _Pmtx(_Other._Pmtx), _Owns(_Other._Owns) {
_Other._Pmtx = nullptr;
_Other._Owns = false;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/std/tests/VSO_0226079_mutex/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,21 @@ void test_nonmember_try_lock() {
rtOt.join();
}

// Also test VSO-1253916, in which RWC like the following broke when we annotated unique_lock with [[nodiscard]].
unique_lock<shared_mutex> do_locked_things(unique_lock<shared_mutex> lck) {
return lck;
}

shared_lock<shared_mutex> do_shared_locked_things(shared_lock<shared_mutex> lck) {
return lck;
}

void test_vso_1253916() {
shared_mutex mtx;
do_locked_things(unique_lock<shared_mutex>{mtx});
do_shared_locked_things(shared_lock<shared_mutex>{mtx});
}

int main() {
{
mutex_test_fixture<mutex> fixture;
Expand Down Expand Up @@ -537,4 +552,6 @@ int main() {

test_nonmember_lock();
test_nonmember_try_lock();

test_vso_1253916();
}