Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LWG-2309 mutex::lock() should not throw device_or_resource_busy #3469

Merged
merged 4 commits into from
Feb 23, 2023
Merged
Changes from 1 commit
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
31 changes: 28 additions & 3 deletions stl/inc/mutex
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,30 @@ public:
_Mutex_base& operator=(const _Mutex_base&) = delete;

void lock() {
_Check_C_return(_Mtx_lock(_Mymtx()));
if (_Mtx_lock(_Mymtx()) != _Thrd_success) {
// undefined behavior, only occurs for plain mutexes (N4928 [thread.mutex.requirements.mutex.general]/6)
_STD _Throw_Cpp_error(_RESOURCE_DEADLOCK_WOULD_OCCUR);
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
}
if (_Ownership_levels() == INT_MAX) {
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
// only occurs for recursive mutexes (N4928 [thread.mutex.recursive]/3)
--_Ownership_levels();
// POSIX specifies EAGAIN in the corresponding situation:
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_lock.html
_STD _Throw_Cpp_error(_RESOURCE_UNAVAILABLE_TRY_AGAIN);
}
}

_NODISCARD_TRY_CHANGE_STATE bool try_lock() noexcept /* strengthened */ {
return _Mtx_trylock(_Mymtx()) == _Thrd_success;
if (_Mtx_trylock(_Mymtx()) != _Thrd_success) {
// undefined behavior, only occurs for plain mutexes (N4928 [thread.mutex.requirements.mutex.general]/6)
return false;
}
if (_Ownership_levels() == INT_MAX) {
// only occurs for recursive mutexes (N4928 [thread.mutex.recursive]/3)
--_Ownership_levels();
return false;
}
return true;
}

void unlock() noexcept /* strengthened */ {
Expand All @@ -71,6 +90,10 @@ private:
_Mtx_t _Mymtx() noexcept { // get pointer to _Mtx_internal_imp_t inside _Mtx_storage
return reinterpret_cast<_Mtx_t>(&_Mtx_storage);
}

int& _Ownership_levels() noexcept { // the count is denoted by an int subobject at the end of _Mtx_storage
return *reinterpret_cast<int*>(reinterpret_cast<unsigned char*>(&_Mtx_storage + 1) - sizeof(int));
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
}
};

_EXPORT_STD class mutex : public _Mutex_base { // class for mutual exclusion
Expand Down Expand Up @@ -852,7 +875,9 @@ public:
if (_My_locked < UINT_MAX) {
++_My_locked;
} else {
_Throw_system_error(errc::device_or_resource_busy);
// POSIX specifies EAGAIN in the corresponding situation:
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_lock.html
_STD _Throw_system_error(errc::resource_unavailable_try_again);
}
} else {
while (_My_locked != 0) {
Expand Down