-
Notifications
You must be signed in to change notification settings - Fork 1
/
semaphore.c
executable file
·67 lines (57 loc) · 1.66 KB
/
semaphore.c
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
#include <pthread.h>
#include <sched.h>
#include <malloc.h>
#include "semaphore.h"
#define DEBUG
semaphore *sem;
int num_sems = 0;
/* Local function prototypes */
semaphore *lookup(int);
void *P(int sem_ref) {
semaphore *this_sem;
this_sem = lookup(sem_ref);
pthread_mutex_lock(&this_sem->mtx);
this_sem->value--;
if(this_sem->value < 0) { /* Block this thread */
pthread_mutex_unlock(&this_sem->mtx);
pthread_mutex_lock(&this_sem->hold); // This should have 0 val initially
}
pthread_mutex_unlock(&this_sem->mtx);
}
void *V(int sem_ref) {
semaphore *this_sem;
this_sem = lookup(sem_ref);
pthread_mutex_lock(&this_sem->mtx);
this_sem->value++;
if(this_sem->value > 0) pthread_mutex_unlock(&this_sem->hold);
pthread_mutex_unlock(&this_sem->mtx);
}
int create_semaphore(int iVal) {
semaphore *new_sem;
new_sem = (semaphore *) malloc(sizeof(semaphore));
new_sem->id = ++num_sems;
new_sem->value = (iVal < 0)? -iVal: iVal;
pthread_mutex_init(&new_sem->mtx, NULL);
pthread_mutex_init(&new_sem->hold, NULL);
/* This won't block. It is a hack to get hold into the right state.
* We want a general semaphore, and we are using a mutex to do it.
*/
pthread_mutex_lock(&new_sem->hold);
new_sem->next = sem;
sem = new_sem;
#ifdef DEBUG
printf("created semaphore number %d with initial value = %d\n",
new_sem->id, new_sem->value);
#endif
return new_sem->id;
}
semaphore *lookup(int sid) {
semaphore *next_sem;
next_sem = sem;
while(next_sem != NULL) {
if(next_sem->id == sid) return next_sem;
next_sem = next_sem->next;
}
/* Semaphore not found */
return NULL;
}