Itasca C++ Interface
Loading...
Searching...
No Matches
basemutex.h
1#pragma once
2
3#include "baseexception.h"
4
5
6// MOO NOTE: This should probably not be used any more in new code, instead use std::mutex
7// The problems it addressed have largely been solved by better runtime checking
8// and debugger support.
9
10
22public:
24 IMutexLocker(QMutex *mutex) : mutex_(mutex), locked_(true) {
25 assert(mutex_);
26 mutex_->lock();
27 }
33 IMutexLocker(QMutex *mutex,int time,int line,const char *file) : mutex_(mutex), locked_(false) {
34 assert(mutex_);
35 if (not mutex_->tryLock(time))
36 throw Exception("Mutex timeout in {} / {}.",line,file);
37 locked_ = true;
38 }
40 ~IMutexLocker() { if (mutex_ and locked_) mutex_->unlock(); }
42 QMutex *mutex() { return mutex_; }
44 void unlock() { if (mutex_ and locked_) { mutex_->unlock(); locked_ = false; } }
46 void relock(int time=30000,int line=0,const char *file=0) {
47 if (mutex_ && not locked_) {
48 if (!mutex_->tryLock(time))
49 throw Exception("Mutex timeout in %1 / %2.",line,file);
50 locked_ = true;
51 }
52 }
53private:
54 QMutex *mutex_;
55 bool locked_;
56};
57
62#define LOCK_MUTEX1(mutexpnt) IMutexLocker default_mutex_name(mutexpnt,30000,__LINE__,__FILE__)
68#define LOCK_MUTEX2(name,mutexpnt) IMutexLocker name(mutexpnt,30000,__LINE__,__FILE__)
74#define LOCK_MUTEX3(name,mutexpnt,time) IMutexLocker name(mutexpnt,time,__LINE__,__FILE__)
76#define QMutexLocker ERROR_SHOULD_NOT_CREATE_DIRECTLY
77
78// EoF
Base exception class for all Itasca code.
Definition baseexception.h:10
An automatic QMutex lock/unlock helper class.
Definition basemutex.h:21
IMutexLocker(QMutex *mutex)
Lock the mutex on construction, default simple version.
Definition basemutex.h:24
void relock(int time=30000, int line=0, const char *file=0)
Explicitly relocks the mutex after destruction, if unlock() was called.
Definition basemutex.h:46
IMutexLocker(QMutex *mutex, int time, int line, const char *file)
Definition basemutex.h:33
~IMutexLocker()
Destructor, if locked unlocks the mutex.
Definition basemutex.h:40
void unlock()
Explicitly unlocks the mutex before destruction.
Definition basemutex.h:44
QMutex * mutex()
Returns a pointer to the mutex claimed by this locker.
Definition basemutex.h:42