forked from larsjuhljensen/tagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex.h
54 lines (43 loc) · 796 Bytes
/
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
#ifndef __REFLECT_MUTEX_HEADER__
#define __REFLECT_MUTEX_HEADER__
#include <cassert>
#include <errno.h>
#include <pthread.h>
class Mutex
{
private:
pthread_mutex_t mutex_t;
public:
Mutex();
~Mutex();
public:
void lock();
bool trylock();
void unlock();
};
////////////////////////////////////////////////////////////////////////////////
Mutex::Mutex()
{
pthread_mutex_init(&this->mutex_t, NULL);
}
Mutex::~Mutex()
{
pthread_mutex_destroy(&this->mutex_t);
}
void Mutex::lock()
{
int rc = pthread_mutex_lock(&this->mutex_t);
assert(rc == 0);
}
bool Mutex::trylock()
{
int rc = pthread_mutex_trylock(&this->mutex_t);
assert(rc == 0 or rc == EBUSY);
return !rc;
}
void Mutex::unlock()
{
int rc = pthread_mutex_unlock(&this->mutex_t);
assert(rc == 0);
}
#endif