-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsem.h
37 lines (32 loc) · 867 Bytes
/
sem.h
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
/*************************************************************
*
Author: Denny Abraham Cheriyan, Adrin Peter Fernandes
Contains functions to initialize, block and unblock semaphores.
Since we are dealing with non-preemptive scheduling we don't
have to ensure that the P(sem) and V(sem) are atomic operations
*/
#include "threads.h"
typedef struct Semaphore_t {
int count;
TCB_t *SemQ;
} Semaphore_t;
Semaphore_t CreateSem(int InputValue) {
Semaphore_t *sem = (Semaphore_t *) malloc (sizeof (Semaphore_t));
sem->count = InputValue;
sem->SemQ = 0;
return *sem;
}
void P(Semaphore_t * sem) {
sem->count--;
if(sem->count < 0){
AddQ(&(sem->SemQ),DelQ(&RunQ));
swapcontext(&(sem->SemQ->prev->context),&(RunQ->context));
}
}
void V(Semaphore_t * sem) {
sem->count++;
if(sem->count <= 0){
AddQ(&RunQ,DelQ(&(sem->SemQ)));
}
yield();
}