13#include <config-libkleo.h>
15#include "uniquelock.h"
17#include <libkleo_debug.h>
22UniqueLock::UniqueLock() noexcept
28UniqueLock::UniqueLock(
QMutex &mutex)
29 : mMutex{std::addressof(mutex)}
36UniqueLock::UniqueLock(
QMutex &mutex, DeferLockType) noexcept
37 : mMutex{std::addressof(mutex)}
42UniqueLock::UniqueLock(
QMutex &mutex, TryToLockType)
43 : mMutex{std::addressof(mutex)}
44 , mOwnsMutex{mMutex->try_lock()}
48UniqueLock::UniqueLock(
QMutex &mutex, AdoptLockType) noexcept
49 : mMutex{std::addressof(mutex)}
55UniqueLock::~UniqueLock()
62UniqueLock::UniqueLock(UniqueLock &&u) noexcept
64 , mOwnsMutex{u.mOwnsMutex}
70UniqueLock &UniqueLock::operator=(UniqueLock &&u)
noexcept
76 UniqueLock(std::move(u)).swap(*
this);
84void UniqueLock::lock()
87 Q_ASSERT(!mOwnsMutex);
89 qCWarning(LIBKLEO_LOG) << __func__ <<
"Error: operation not permitted";
90 }
else if (mOwnsMutex) {
91 qCWarning(LIBKLEO_LOG) << __func__ <<
"Error: resource deadlock would occur";
98bool UniqueLock::try_lock()
101 Q_ASSERT(!mOwnsMutex);
103 qCWarning(LIBKLEO_LOG) << __func__ <<
"Error: operation not permitted";
105 }
else if (mOwnsMutex) {
106 qCWarning(LIBKLEO_LOG) << __func__ <<
"Error: resource deadlock would occur";
109 mOwnsMutex = mMutex->try_lock();
114void UniqueLock::unlock()
117 qCWarning(LIBKLEO_LOG) << __func__ <<
"Error: operation not permitted";
124void UniqueLock::swap(UniqueLock &u)
noexcept
126 std::swap(mMutex, u.mMutex);
127 std::swap(mOwnsMutex, u.mOwnsMutex);
130QMutex *UniqueLock::release() noexcept
138bool UniqueLock::owns_lock() const noexcept
143UniqueLock::operator bool() const noexcept
148QMutex *UniqueLock::mutex() const noexcept