-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMutexGuard.cpp
More file actions
45 lines (40 loc) · 764 Bytes
/
MutexGuard.cpp
File metadata and controls
45 lines (40 loc) · 764 Bytes
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
#include "MutexGuard.hpp"
namespace cornus {
MutexGuard::MutexGuard(pthread_mutex_t *mutex, const LockType lock_type):
mutex_(mutex)
{
switch(lock_type) {
case LockType::Normal:
{
const int status = pthread_mutex_lock(mutex_);
if (status != 0)
mtl_status(status);
break;
}
case LockType::TryLock: {
const int status = pthread_mutex_trylock(mutex_);
if (status != 0)
mutex_ = nullptr;
break;
}
default: {
mtl_trace();
}
}
}
MutexGuard::~MutexGuard()
{
if (mutex_ != nullptr)
{
const int status = pthread_mutex_unlock(mutex_);
if (status != 0)
mtl_status(status);
}
}
bool MutexGuard::Signal(pthread_cond_t *cond) {
int status = pthread_cond_signal(cond);
if (status != 0)
mtl_status(status);
return status == 0;
}
}