-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex.h
59 lines (43 loc) · 1.25 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
/*
* Mutex.h
* Copyright (C) 2021 youfa.song <[email protected]>
*
* Distributed under terms of the GPLv2 license.
*/
#ifndef AVE_MUTEX_H
#define AVE_MUTEX_H
#include <pthread.h>
#include "thread_annotation.h"
#include "types.h"
namespace ave {
class Condition;
class CAPABILITY("mutex") Mutex {
public:
Mutex() { pthread_mutex_init(&mutex_, nullptr); }
virtual ~Mutex() { pthread_mutex_destroy(&mutex_); }
status_t Lock() ACQUIRE() { return -pthread_mutex_lock(&mutex_); }
status_t Unlock() RELEASE() { return -pthread_mutex_unlock(&mutex_); }
status_t TryLock() TRY_ACQUIRE(0) { return -pthread_mutex_trylock(&mutex_); }
class SCOPED_CAPABILITY LockGuard {
public:
inline explicit LockGuard(Mutex& mutex) ACQUIRE(mutex) : mLock(mutex) {
mLock.Lock();
}
inline explicit LockGuard(Mutex* mutex) ACQUIRE(mutex) : mLock(*mutex) {
mLock.Lock();
}
inline ~LockGuard() RELEASE() { mLock.Unlock(); }
private:
Mutex& mLock;
LockGuard(const LockGuard&);
LockGuard& operator=(const LockGuard&);
};
private:
friend class Condition;
Mutex(const Mutex&);
Mutex& operator=(const Mutex&);
pthread_mutex_t mutex_;
};
using lock_guard = Mutex::LockGuard;
} // namespace ave
#endif /* !AVE_MUTEX_H */