-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mutex.cpp
62 lines (52 loc) · 1.06 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
61
62
/*
* File: Mutex.cpp
* Class: ICS 451
* Project #: 3
* Team Members: Bryce Groff, Brandon Grant, Emiliano Miranda
* Author: Bryce Groff
* Created Date: 04-19-09
* Desc: Represents a Lock.
*/
#include "Mutex.h"
Mutex::Mutex()
{
pthread_mutex_init(&mLock, NULL);
pthread_cond_init(&mCond, NULL);
}
Mutex::Mutex(int type)
{
pthread_mutexattr_init(&mAttr);
pthread_mutexattr_settype(&mAttr, type);
pthread_mutex_init(&mLock, &mAttr);
pthread_cond_init(&mCond, NULL);
}
Mutex::~Mutex()
{
pthread_mutex_destroy(&mLock);
pthread_mutexattr_destroy(&mAttr);
pthread_cond_destroy(&mCond);
}
int Mutex::Lock()
{
return pthread_mutex_lock(&mLock);
}
int Mutex::TryLock()
{
return pthread_mutex_trylock(&mLock);
}
int Mutex::Unlock()
{
return pthread_mutex_unlock(&mLock);
}
int Mutex::TimedWait(timespec &waitTime)
{
return pthread_cond_timedwait(&mCond, &mLock, &waitTime);
}
int Mutex::Wait()
{
return pthread_cond_wait(&mCond, &mLock);
}
int Mutex::Signal()
{
return pthread_cond_signal(&mCond);
}