-
Notifications
You must be signed in to change notification settings - Fork 1
/
cornus.hh
61 lines (47 loc) · 1.08 KB
/
cornus.hh
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
#pragma once
#include "err.hpp"
#include "MutexGuard.hpp"
#include <pthread.h>
namespace cornus {
struct GuiBits {
static const u16 bit_created = 1u << 0;
u16 bits_ = 0;
mutable pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
GuiBits() {}
~GuiBits() {}
GuiBits(const GuiBits &rhs) = delete;
inline void Broadcast() {
int status = pthread_cond_broadcast(&cond);
if (status != 0)
mtl_status(status);
}
inline int CondWait() {
return pthread_cond_wait(&cond, &mutex);
}
inline bool created() const { return bits_ & bit_created; }
inline void created(const bool flag) {
if (flag)
bits_ |= bit_created;
else
bits_ &= ~bit_created;
}
MutexGuard guard() const {
return MutexGuard(&mutex);
}
inline int Lock() {
int status = pthread_mutex_lock(&mutex);
if (status != 0)
mtl_status(status);
return status;
}
inline void Signal() {
int status = pthread_cond_signal(&cond);
if (status != 0)
mtl_status(status);
}
inline int Unlock() {
return pthread_mutex_unlock(&mutex);
}
};
}