Skip to content

Commit

Permalink
Cleanups for <mutex> etc. (#3759)
Browse files Browse the repository at this point in the history
  • Loading branch information
achabense authored Jun 15, 2023
1 parent 64941ce commit 641dd9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 45 deletions.
46 changes: 12 additions & 34 deletions stl/inc/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -762,18 +762,6 @@ private:
}
}

template <class _Predicate>
bool _Wait_until1(unique_lock<mutex>& _Lck, const _timespec64* _Abs_time, _Predicate& _Pred) {
// wait for signal with timeout and check predicate
while (!_Pred()) {
if (_Wait_until_sys_time(_Lck, _Abs_time) == cv_status::timeout) {
return _Pred();
}
}

return true;
}

template <class _Clock, class _Duration, class _Predicate>
bool _Wait_until1(
unique_lock<mutex>& _Lck, const chrono::time_point<_Clock, _Duration>& _Abs_time, _Predicate& _Pred) {
Expand Down Expand Up @@ -843,8 +831,12 @@ public:
return try_lock_until(_To_absolute_time(_Rel_time));
}

template <class _Time>
bool _Try_lock_until(_Time _Abs_time) { // try to lock the mutex with timeout
template <class _Clock, class _Duration>
_NODISCARD_TRY_CHANGE_STATE bool try_lock_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
// try to lock the mutex with timeout
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
unique_lock<mutex> _Lock(_My_mutex);
if (!_My_cond.wait_until(_Lock, _Abs_time, _UInt_is_zero{_My_locked})) {
return false;
Expand All @@ -854,15 +846,6 @@ public:
return true;
}

template <class _Clock, class _Duration>
_NODISCARD_TRY_CHANGE_STATE bool try_lock_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
// try to lock the mutex with timeout
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
return _Try_lock_until(_Abs_time);
}

private:
mutex _My_mutex;
condition_variable _My_cond;
Expand Down Expand Up @@ -944,8 +927,12 @@ public:
return try_lock_until(_To_absolute_time(_Rel_time));
}

template <class _Time>
bool _Try_lock_until(_Time _Abs_time) { // try to lock the mutex with timeout
template <class _Clock, class _Duration>
_NODISCARD_TRY_CHANGE_STATE bool try_lock_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
// try to lock the mutex with timeout
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
const thread::id _Tid = this_thread::get_id();

unique_lock<mutex> _Lock(_My_mutex);
Expand All @@ -967,15 +954,6 @@ public:
return true;
}

template <class _Clock, class _Duration>
_NODISCARD_TRY_CHANGE_STATE bool try_lock_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
// try to lock the mutex with timeout
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
return _Try_lock_until(_Abs_time);
}

private:
mutex _My_mutex;
condition_variable _My_cond;
Expand Down
17 changes: 6 additions & 11 deletions stl/inc/shared_mutex
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,12 @@ public:
return try_lock_shared_until(_To_absolute_time(_Rel_time));
}

template <class _Time>
bool _Try_lock_shared_until(_Time _Abs_time) { // try to lock non-exclusive until absolute time
template <class _Clock, class _Duration>
_NODISCARD_TRY_CHANGE_STATE bool try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
// try to lock non-exclusive until absolute time
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
const auto _Can_acquire = [this] { return !_Writing && _Readers < _Max_readers; };

unique_lock<mutex> _Lock(_Mymtx);
Expand All @@ -180,15 +184,6 @@ public:
return true;
}

template <class _Clock, class _Duration>
_NODISCARD_TRY_CHANGE_STATE bool try_lock_shared_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) {
// try to lock non-exclusive until absolute time
#if _HAS_CXX20
static_assert(chrono::is_clock_v<_Clock>, "Clock type required");
#endif // _HAS_CXX20
return _Try_lock_shared_until(_Abs_time);
}

void unlock_shared() { // unlock non-exclusive
_Read_cnt_t _Local_readers;
bool _Local_writing;
Expand Down

0 comments on commit 641dd9b

Please sign in to comment.