| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "miscellaneous/mutex.h" |
| 4 | |
| 5 | #include "definitions/definitions.h" |
| 6 | |
| 7 | Mutex::Mutex(QObject* parent) : QObject(parent), m_mutex(new QMutex()), m_isLocked(false) {} |
| 8 | |
| 9 | Mutex::~Mutex() { |
| 10 | qDebugNN << LOGSEC_CORE << ("Destroying Mutex instance."); |
| 11 | } |
| 12 | |
| 13 | void Mutex::lock() { |
| 14 | m_mutex->lock(); |
| 15 | setLocked(); |
| 16 | } |
| 17 | |
| 18 | bool Mutex::tryLock() { |
| 19 | bool result; |
| 20 | |
| 21 | if ((result = m_mutex->tryLock())) { |
| 22 | setLocked(); |
| 23 | } |
| 24 | |
| 25 | return result; |
| 26 | } |
| 27 | |
| 28 | bool Mutex::tryLock(int timeout) { |
| 29 | bool result; |
| 30 | |
| 31 | if ((result = m_mutex->tryLock(timeout))) { |
| 32 | setLocked(); |
| 33 | } |
| 34 | |
| 35 | return result; |
| 36 | } |
| 37 | |
| 38 | void Mutex::unlock() { |
| 39 | m_mutex->unlock(); |
| 40 | setUnlocked(); |
| 41 | } |
| 42 | |
| 43 | void Mutex::setLocked() { |
| 44 | m_isLocked = true; |
| 45 | emit locked(); |
| 46 | } |
| 47 | |
| 48 | void Mutex::setUnlocked() { |
| 49 | m_isLocked = false; |
| 50 | emit unlocked(); |
| 51 | } |
| 52 | |
| 53 | bool Mutex::isLocked() const { |
| 54 | return m_isLocked; |
| 55 | } |
| 56 | |
| 57 | Mutex::operator QMutex*() const { |
| 58 | return m_mutex.data(); |
| 59 | } |