-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreads.cpp
More file actions
196 lines (175 loc) · 4.59 KB
/
Threads.cpp
File metadata and controls
196 lines (175 loc) · 4.59 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//#include "./Threads.h"
/**************************************
* Semaphore class implementation
*************************************/
Semaphore::Semaphore() {
int res = sem_init(&semaphore, 0, 0);
if(res == -1)
switch(errno) {
case ENOSPC : throw NoResourcesException();
break;
case EPERM : throw NoPriviligesException();
break;
default : throw SemaphoreException();
}
}
Semaphore::Semaphore(int val) {
int res = sem_init(&semaphore, 0, val);
if(res == -1)
switch(errno) {
case EINVAL : throw SemaphoreValueException(SEM_VALUE_MAX);
break;
case ENOSPC : throw NoResourcesException();
break;
case EPERM : throw NoPriviligesException();
break;
default : throw SemaphoreException();
}
}
Semaphore::~Semaphore() {
int res = sem_destroy(&semaphore);
if(res == -1)
if(errno == EBUSY) throw BusySemaphoreException();
else throw SemaphoreException();
}
void Semaphore::wait() {
int res = sem_wait(&semaphore);
if(res == -1)
if(errno == EDEADLK) throw DeadlockException();
else throw SemaphoreException();
}
void Semaphore::tryWait(){
int res = sem_trywait(&semaphore);
if(res == -1)
switch(errno) {
case EDEADLK : throw DeadlockException();
break;
case EAGAIN : throw LockedSemaphoreException();
break;
default : throw SemaphoreException();
}
}
void Semaphore::post(){
sem_post(&semaphore);
}
int Semaphore::getValue(){
int ret;
sem_getvalue(&semaphore, &ret);
return ret;
}
/**************************************
* Mutex class implementation
*************************************/
Mutex::Mutex(){
int error = pthread_mutexattr_init(&attr);
if(error == 0) throw NoResourcesException();
error = pthread_mutex_init(&mutex, &attr);
if(error != 0)
switch(error) {
case EAGAIN :
case ENOMEM : throw NoResourcesException();
break;
case EPERM : throw NoPrivilegesException();
break;
case EBUSY : throw ReInitException();
break;
default : throw MutexException();
}
}
Mutex::~Mutex(){
int error = pthread_mutex_destroy(&mutex);
pthread_mutexattr_destroy(&attr);
if(error != 0)
if(error == EBUSY) throw BusySemaphoreException();
else throw MutexException();
}
void Mutex::lock() {
int error = pthread_mutex_lock(&mutex);
if(error != 0)
switch(error) {
case EDEADLK : throw DeadlockException();
break;
case EAGAIN : throw MaxLockException();
break;
default : throw MutexException();
}
}
void Mutex::tryLock() {
int error = pthread_mutex_trylock(&mutex);
if(error != 0)
switch(error) {
case EBUSY : throw LockedSemaphoreException();
break;
case EDEADLK : throw DeadlockException();
break;
case EAGAIN : throw MaxLockException();
break;
default : throw MutexException();
}
}
void Mutex::unlock() {
int error = pthread_mutex_unlock(&mutex);
if(error != 0)
if(error == EPERM) throw NoProvilegesException();
else throw MutexException();
}
/**************************************
* Thread class implementation
*************************************/
Thread::Thread() {
retVal = 0;
startRoutine = NULL;
int error = pthread_attr_init(&attributes); //default attributes
if(error != 0) throw NoResourcesException();
}
Thread::~Thread() {
startRoutine = NULL;
pthread_attr_destroy(&attributes);
exit();
}
void Thread::setStackSize(unsigned int sz) {
int error = pthread_attr_setstacksize(&attributes, sz);
if(error != 0) throw StackSizeExcleption(PTHERAD_STACK_MIN);
}
unsigned int Thread::getStackSize() {
int ret;
pthread_attr_getstacksize(&attributes, &ret);
return ret;
}
void Thread::setRoutine(void *(*start_routine)(void*)) {
startRoutine = start_routine;
}
void Thread::run() {
int error = 0;
if(startRoutine != NULL) error = pthread_create(&thread, &attributes, startRoutine, NULL); //FIXME passargli this?
else throw StartRoutineException();
if(error != 0)
switch {
case EAGAIN : throw NoResourcesException();
break;
case EINVAL : throw InvalidAttrException();
break;
case EPERM : throw NoPrivilegesException();
break;
default: ;
}
}
void Thread::kill() {
pthread_cancel(thread);
}
void Thread::exit() {
pthread_exit(&retVal);
}
int Thread::join() {
int *ret, error;
error = pthread_join(thread, &ret);
if(error != 0)
switch {
case EINVAL : throw NoJoinableException();
break;
case EDEADLK : throw DeadlockException();
break;
default : ;
}
return (*ret);
}