forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmutex.h
89 lines (68 loc) · 2.18 KB
/
mutex.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#ifndef UTIL_MUTEX_H
#define UTIL_MUTEX_H
// Thread annotation aware variants of locks, read-write locks and scoped
// lockers. This allows us to use Clang thread safety analysis in Mixxx.
// See: http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
#include <QMutex>
#include <QReadWriteLock>
#include <QMutexLocker>
#include "util/thread_annotations.h"
class CAPABILITY("mutex") MMutex {
public:
MMutex(QMutex::RecursionMode mode = QMutex::NonRecursive)
: m_mutex(mode) {
}
inline void lock() ACQUIRE() { m_mutex.lock(); }
inline void unlock() RELEASE() { m_mutex.unlock(); }
inline bool tryLock() TRY_ACQUIRE(true) {
return m_mutex.tryLock();
}
private:
QMutex m_mutex;
friend class MMutexLocker;
};
class CAPABILITY("mutex") MReadWriteLock {
public:
MReadWriteLock(QReadWriteLock::RecursionMode mode = QReadWriteLock::NonRecursive)
: m_lock(mode) {
}
void lockForRead() ACQUIRE_SHARED() { m_lock.lockForRead(); }
bool tryLockForRead() TRY_ACQUIRE_SHARED(true) {
return m_lock.tryLockForRead();
}
void lockForWrite() ACQUIRE() { m_lock.lockForWrite(); }
bool tryLockForWrite() TRY_ACQUIRE(true) {
return m_lock.tryLockForWrite();
}
inline void unlock() RELEASE() { m_lock.unlock(); }
private:
QReadWriteLock m_lock;
friend class MWriteLocker;
friend class MReadLocker;
};
class SCOPED_CAPABILITY MMutexLocker {
public:
MMutexLocker(MMutex* mu) ACQUIRE(mu) : m_locker(&mu->m_mutex) {}
~MMutexLocker() RELEASE() {}
inline void unlock() RELEASE() { m_locker.unlock(); }
private:
QMutexLocker m_locker;
};
class SCOPED_CAPABILITY MWriteLocker {
public:
MWriteLocker(MReadWriteLock* mu) ACQUIRE(mu) : m_locker(&mu->m_lock) {}
~MWriteLocker() RELEASE() {}
inline void unlock() RELEASE() { m_locker.unlock(); }
private:
QWriteLocker m_locker;
};
class SCOPED_CAPABILITY MReadLocker {
public:
MReadLocker(MReadWriteLock* mu) ACQUIRE_SHARED(mu)
: m_locker(&mu->m_lock) {}
~MReadLocker() RELEASE() {}
inline void unlock() RELEASE() { m_locker.unlock(); }
private:
QReadLocker m_locker;
};
#endif /* UTIL_MUTEX_H */