From 98897b26f8c63f848562cb4d8401bbe2521dffd3 Mon Sep 17 00:00:00 2001 From: Martchus Date: Thu, 10 Mar 2016 22:15:16 +0100 Subject: [PATCH] added locker classes intended to be used with QMutex --- CMakeLists.txt | 2 ++ misc/adoptlocker.h | 38 +++++++++++++++++++++++++++++++ misc/trylocker.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++ qtutilities.pro | 4 +++- 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 misc/adoptlocker.h create mode 100644 misc/trylocker.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0730d5d..4fc632c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,8 @@ set(HEADER_FILES misc/desktoputils.h misc/xmlparsermacros.h misc/undefxmlparsermacros.h + misc/trylocker.h + misc/adoptlocker.h models/checklistmodel.h resources/qtconfigarguments.h resources/resources.h diff --git a/misc/adoptlocker.h b/misc/adoptlocker.h new file mode 100644 index 0000000..4bc860e --- /dev/null +++ b/misc/adoptlocker.h @@ -0,0 +1,38 @@ +#ifndef ADOPTLOCKER_H +#define ADOPTLOCKER_H + +#include + +QT_FORWARD_DECLARE_CLASS(QMutex) + +namespace ThreadingUtils { + +/*! + * \brief Like QMutexLocker, but assumes that the mutex has already been locked. + */ +template +class AdoptLocker +{ +public: + /*! + * \brief Constructs the locker for the specified \a mutex. + */ + AdoptLocker(Mutex &mutex) : + m_mutex(mutex) + {} + + /*! + * \brief Unlocks the mutex specified when constructing the instance. + */ + ~AdoptLocker() + { + m_mutex.unlock(); + } + +private: + Mutex &m_mutex; +}; + +} + +#endif // ADOPTLOCKER_H diff --git a/misc/trylocker.h b/misc/trylocker.h new file mode 100644 index 0000000..7d432ae --- /dev/null +++ b/misc/trylocker.h @@ -0,0 +1,57 @@ +#ifndef TRYLOCKER_H +#define TRYLOCKER_H + +#include + +QT_FORWARD_DECLARE_CLASS(QMutex) + +namespace ThreadingUtils { + +/*! + * \brief Like QMutexLocker, but it just tries to lock the mutex. + */ +template +class TryLocker +{ +public: + /*! + * \brief Tries to lock the specified mutex. + */ + TryLocker(Mutex &mutex) : + m_mutex(mutex.tryLock() ? &mutex : nullptr) + {} + + /*! + * \brief Unlocks the mutex specified when constructing. + * \remarks Does nothing if the mutex couldn't be locked in the first place. + */ + ~TryLocker() + { + if(m_mutex) { + m_mutex->unlock(); + } + } + + /*! + * \brief Returns whether the mutex could be locked. + */ + bool isLocked() const + { + return m_mutex != nullptr; + } + + /*! + * \brief Returns whether the mutex could be locked. + */ + operator bool() const + { + return m_mutex != nullptr; + } + +private: + Mutex *m_mutex; +}; + +} + +#endif // TRYLOCKER_H diff --git a/qtutilities.pro b/qtutilities.pro index 0a40e87..c4174f2 100644 --- a/qtutilities.pro +++ b/qtutilities.pro @@ -32,7 +32,9 @@ HEADERS += \ misc/dialogutils.h \ misc/desktoputils.h \ misc/xmlparsermacros.h \ - misc/undefxmlparsermacros.h + misc/undefxmlparsermacros.h \ + misc/trylocker.h \ + misc/adoptlocker.h SOURCES += resources/resources.cpp \ models/checklistmodel.cpp \