-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_sem.c
More file actions
45 lines (36 loc) · 869 Bytes
/
Copy pathbinary_sem.c
File metadata and controls
45 lines (36 loc) · 869 Bytes
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
#include <sys/types.h>
#include <sys/sem.h>
#include "semun.h"
#include "binary_sem.h"
int bsUseSemUndo = 0;
int bsRetryOnEintr = 1;
//initializers
int initSemAvailable(int semId, int semNum){
union semun arg;
arg.val = 1;
return semctl(semId, semNum, SETVAL, arg);
}
int initSemInUse(int semId, int semNum){
union semun arg;
arg.val = 0;
return semctl(semId, semNum, SETVAL, arg);
}
int reserveSem(int semId, int semNum){
struct sembuf sops;
sops.sem_num = semNum;
sops.sem_op = -1;
sops.sem_flg = bsUseSemUndo ? SEM_UNDO : 0;
while (semop(semId, &sops, 1) == -1){
if (errno != EINTR || !bsRetryOnEintr){
return -1;
}
return 0;
}
}
int releaseSem(int semId, int semNum){
struct sembuf sops;
sops.sem_num = semNum;
sops.sem_op = 1;
sops.sem_flg = bsUseSemUndo ? SEM_UNDO : 0;
return semop(semId, &sops, 1);
}