forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TQtLockGuard.h
68 lines (56 loc) · 2.82 KB
/
TQtLockGuard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef ROOT_TQtLockGuard
#define ROOT_TQtLockGuard
//////////////////////////////////////////////////////////////////////////
// //
// TQtLockGuard - Qt-based implementation of ROOT TLockGuard class //
// //
// This class provides mutex resource management in a guaranteed and //
// exception safe way. Use like this: //
// { //
// TQtLockGuard guard(mutex); //
// ... // do something //
// } //
// when guard goes out of scope the mutex is unlocked in the TLockGuard //
// destructor. The exception mechanism takes care of calling the dtors //
// of local objects so it is exception safe. //
// //
// The macro Q__LOCKGUARD2(QMutex *mutex) //
// creates first creates the QMutex object and then creates //
// TQtLockGuard as above if the QT_THREAD_SUPPORT //
// was provided //
// //
// NOTE: This class may be removed as soon as //
// ---- ROOT TThreadImp class QThread-based implemantion //
// is adopted by ROOT team (one needs to convince Fons ) //
// //
//////////////////////////////////////////////////////////////////////////
#include "qmutex.h"
class TQtLockGuard {
private:
QMutex *fMutex;
public:
TQtLockGuard(QMutex *mutex);
~TQtLockGuard();
};
//____________________________________________________________________
inline TQtLockGuard::TQtLockGuard(QMutex *mutex)
{ fMutex = mutex; if (fMutex) fMutex->lock(); }
//____________________________________________________________________
inline TQtLockGuard::~TQtLockGuard()
{ if (fMutex) fMutex->unlock(); }
// Zero overhead macros in case not compiled with thread support
#ifdef QT_THREAD_SUPPORT
#define Q__LOCKGUARD(mutex) TQtLockGuard QR__guard(mutex)
#define Q__LOCKGUARD2(mutex) { \
if (qApp && !mutex) { \
qApp->lock(); \
if (!mutex) mutex = new QMutex(true); \
qApp->unlock(); \
} \
Q__LOCKGUARD(mutex); \
}
#else
#define Q__LOCKGUARD(mutex) if (mutex) { }
#define Q__LOCKGUARD2(mutex) if (mutex) { }
#endif
#endif