Skip to content

Commit

Permalink
libcore: Locking a mutex without throwing exceptions
Browse files Browse the repository at this point in the history
Most of the code has been written with the assumption that locking
a mutex will never throw an exception.
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent ceb8bea commit 4ed5d9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 2 additions & 3 deletions doomsday/libs/core/include/de/concurrency/lockable.h
Expand Up @@ -39,9 +39,8 @@ class DENG2_PUBLIC Lockable
{
public:
/// Acquire the lock. Blocks until the operation succeeds.
inline void lock() const {
_mutex.lock();
}
/// @return @c true, if the lock succeeded. @c false, if there was an error.
bool lock() const noexcept;

/// Release the lock.
inline void unlock() const {
Expand Down
14 changes: 13 additions & 1 deletion doomsday/libs/core/src/concurrency/lockable.cpp
Expand Up @@ -23,6 +23,18 @@

namespace de {

// nothing here
bool Lockable::lock() const noexcept
{
try
{
_mutex.lock();
return true;
}
catch (...)
{
qWarning("[Lockable] (%p) Failed to lock mutex!", static_cast<void const *>(this));
return false;
}
}

} // namespace de

0 comments on commit 4ed5d9e

Please sign in to comment.