-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutex.cpp
60 lines (51 loc) · 1.24 KB
/
Mutex.cpp
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
//
// Mutex.cpp for plazza in /home/jowett_j//pj/plazza/svn
//
// Made by james jowett
// Login <jowett_j@epitech.net>
//
// Started on Wed Apr 11 02:33:22 2012 james jowett
// Last update Wed Jan 23 23:07:59 2013 WILMOT Pierre
//
#include "Mutex.hpp"
Mutex::Mutex(Mutex::e_type t)
: m_t(t)
{
pthread_mutexattr_t attr;
if (m_t != Normal)
{
errno = 0;
if (pthread_mutexattr_init(&attr) != 0)
throw std::runtime_error(std::string("pthread_mutexattr_init(3): ") + strerror(errno));
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
}
errno = 0;
if (pthread_mutex_init(&m_mutex, (m_t == Normal) ? NULL : &attr) != 0)
throw std::runtime_error(std::string("pthread_mutex_init(3): ") + strerror(errno));
}
pthread_mutex_t *Mutex::getCMutex()
{
return (&m_mutex);
}
bool Mutex::lock()
{
return (pthread_mutex_lock(&m_mutex) == 0);
}
bool Mutex::trylock(bool& gotLock)
{
gotLock = true;
errno = 0;
if (pthread_mutex_trylock(&m_mutex) == 0)
return true;
gotLock = false;
return (errno == EBUSY);
}
bool Mutex::unlock()
{
return (pthread_mutex_unlock(&m_mutex) == 0);
}
Mutex::~Mutex()
{
if (pthread_mutex_destroy(&m_mutex) != 0)
std::cerr << "!! Failed to destroy mutex !!" << std::endl;
}