-
Notifications
You must be signed in to change notification settings - Fork 41
/
SemaphoreWrapper.c
executable file
·146 lines (114 loc) · 3.19 KB
/
SemaphoreWrapper.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
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
#include "SemaphoreWrapper.h"
int semCreate(key_t key, int number_of_sem)
{
int semid;
if( (semid = semget(key, number_of_sem, 0666 | IPC_CREAT)) == -1)
return -1;
return semid;
}
/* Returns the semaphore ID for the path specified */
int semGet(key_t key, int number_of_sem)
{
int semid;
if( (semid = semget(key, number_of_sem, 0666)) == -1 )
return -1;
return semid;
}
int semDelete(int semid)
{
union semun arg;
return semctl(semid, 0, IPC_RMID, arg);
}
/* Sets semaphore_number assoc. with the semid to value */
int semSetValue(int semid, int semaphore_number, int value)
{
union semun arg;
arg.val = value;
return semctl(semid, semaphore_number, SETVAL, arg);
}
/* Gets the semaphore value from semaphore_number assoc. with the semid */
int semGetValue(int semid, int semaphore_number)
{
union semun arg;
return semctl(semid, semaphore_number, GETVAL, arg);
}
/* Increment semaphore value by one */
int semSignal(int semid, int semaphore_number)
{
struct sembuf sops;
sops.sem_num = semaphore_number;
sops.sem_op = 1;
sops.sem_flg = SEM_UNDO;
return semop(semid, &sops, 1);
}
int semIncrement(int semid, int semaphore_number, int incrementBy)
{
struct sembuf sops;
sops.sem_num = semaphore_number;
sops.sem_op = incrementBy;
sops.sem_flg = SEM_UNDO;
return semop(semid, &sops, 1);
}
/* Decrement semaphore value by one */
int semWait(int semid, int semaphore_number)
{
struct sembuf sops;
sops.sem_num = semaphore_number;
sops.sem_op = -1;
sops.sem_flg = SEM_UNDO;
return semop(semid, &sops, 1);
}
int semDecrment(int semid, int semaphore_number, int decrementBy)
{
struct sembuf sops;
sops.sem_num = semaphore_number;
sops.sem_op = decrementBy;
sops.sem_flg = SEM_UNDO;
return semop(semid, &sops, 1);
}
/* Increment semaphore value by one */
int semSignal_timeout(int semid, int semaphore_number, long usecs)
{
struct sembuf sops;
struct timespec timeout;
sops.sem_num = semaphore_number;
sops.sem_op = 1;
sops.sem_flg = SEM_UNDO;
timeout.tv_sec = 0;
timeout.tv_nsec = usecs*1000;
return semtimedop(semid, &sops, 1, &timeout);
}
int semIncrement_timeout(int semid, int semaphore_number, int incrementBy, long usecs)
{
struct sembuf sops;
struct timespec timeout;
sops.sem_num = semaphore_number;
sops.sem_op = incrementBy;
sops.sem_flg = SEM_UNDO;
timeout.tv_sec = 0;
timeout.tv_nsec = usecs*1000;
return semtimedop(semid, &sops, 1, &timeout);
}
/* Decrement semaphore value by one */
int semWait_timeout(int semid, int semaphore_number, long usecs)
{
struct sembuf sops;
struct timespec timeout;
sops.sem_num = semaphore_number;
sops.sem_op = -1;
sops.sem_flg = SEM_UNDO;
timeout.tv_sec = 0;
timeout.tv_nsec = usecs*1000;
return semtimedop(semid, &sops, 1, &timeout);
}
int semDecrement_timeout(int semid, int semaphore_number, int decrementBy, long usecs)
{
struct sembuf sops;
struct timespec timeout;
sops.sem_num = semaphore_number;
sops.sem_op = decrementBy;
sops.sem_flg = SEM_UNDO;
timeout.tv_sec = 0;
timeout.tv_nsec = usecs*1000;
return semtimedop(semid, &sops, 1, &timeout);
}