-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.cpp
96 lines (78 loc) · 1.04 KB
/
monitor.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "monitor.h"
#include <pthread.h>
Mutex::Mutex()
{
pthread_mutex_init( &m_mu, NULL );
}
Mutex::~Mutex()
{
pthread_mutex_destroy( &m_mu );
}
void Mutex::lock()
{
pthread_mutex_lock( &m_mu );
}
void Mutex::unlock()
{
pthread_mutex_unlock( &m_mu );
}
bool Mutex::testlock()
{
return true;
}
SharedMutex::SharedMutex()
{
m_refCount = 1;
}
SharedMutex::~SharedMutex()
{
}
void
SharedMutex::ref()
{
m_refCount += 1;
}
void
SharedMutex::unref()
{
m_refCount -= 1;
if (m_refCount==0) {
delete this;
}
}
Monitor::Monitor()
{
m_mu = new SharedMutex();
pthread_cond_init(&m_cv, NULL);
}
Monitor::Monitor(Monitor &mon)
{
mon.m_mu->ref();
m_mu = mon.m_mu;
pthread_cond_init(&m_cv, NULL);
}
Monitor::~Monitor()
{
m_mu->unref();
pthread_cond_destroy(&m_cv);
}
void
Monitor::enter()
{
m_mu->lock();
}
void
Monitor::exit()
{
m_mu->unlock();
}
void
Monitor::signal()
{
pthread_cond_signal(&m_cv);
}
void
Monitor::wait()
{
pthread_cond_wait(&m_cv, &m_mu->m_mu);
}