-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsemaphor.c
139 lines (111 loc) · 2.88 KB
/
semaphor.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
/*
* SMAPI; Modified Squish MSGAPI
*
* Squish MSGAPI0 is copyright 1991 by Scott J. Dudley. All rights reserved.
* Modifications released to the public domain.
*
* Use of this file is subject to the restrictions contain in the Squish
* MSGAPI0 licence agreement. Please refer to licence.txt for complete
* details of the licencing restrictions. If you do not find the text
* of this agreement in licence.txt, or if you do not have this file,
* you should contact Scott Dudley at FidoNet node 1:249/106 or Internet
* e-mail [email protected].
*
* In no event should you proceed to use any of the source files in this
* archive without having accepted the terms of the MSGAPI0 licensing
* agreement, or such other agreement as you are able to reach with the
* author.
*/
#if defined (UNIX) || defined (__EMX__)
#include <unistd.h>
#endif
#include <stdlib.h> /* NULL */
#include "semaphor.h"
#if defined (NOSEMAPHORES)
/* No code for Non-Multitasking Systems */
#elif defined (__BEOS__)
void create_semaphore(SEMAPHORE * sem)
{
*sem = create_sem(0, 0);
release_sem(*sem);
}
void delete_semaphore(SEMAPHORE * sem)
{
delete_sem(*sem);
}
void lock_semaphore(SEMAPHORE * sem)
{
if(acquire_sem(*sem) != B_NO_ERROR)
{
snooze(10);
}
}
void unlock_semaphore(SEMAPHORE * sem)
{
release_sem(*sem);
}
#elif defined (UNIX)
#if ((defined (__FreeBSD__) || defined (__GNU_LIBRARY__)) && !defined (_SEM_SEMUN_UNDEFINED))
/* union semun is defined by including <sys/sem.h> */
#else
/* according to X/OPEN we have to define it ourselves */
union semun
{
int val; /* value for SETVAL */
struct semid_ds * buf; /* buffer for IPC_STAT, IPC_SET */
unsigned short int * array; /* array for GETALL, SETALL */
struct seminfo * __buf; /* buffer for IPC_INFO */
};
#endif
void create_semaphore(SEMAPHORE * sem)
{
union semun fl;
fl.val = 1;
*sem = semget(IPC_PRIVATE, 1, 0600 | IPC_CREAT);
semctl(*sem, 0, SETVAL, fl);
}
void delete_semaphore(SEMAPHORE * sem)
{
union semun fl;
fl.val = 0;
semctl(*sem, 0, IPC_RMID, fl);
}
void lock_semaphore(SEMAPHORE * sem)
{
struct sembuf sops;
sops.sem_num = 0;
sops.sem_op = -1;
sops.sem_flg = 0;
while(semop(*sem, &sops, 1))
{
usleep(10);
}
}
void unlock_semaphore(SEMAPHORE * sem)
{
struct sembuf sops;
sops.sem_num = 0;
sops.sem_op = 1;
sops.sem_flg = 0;
semop(*sem, &sops, 1);
}
#elif defined (OS2)
#define INCL_DOS
#include <os2.h>
void create_semaphore(SEMAPHORE * s)
{
DosCreateMutexSem(NULL, s, DC_SEM_SHARED, 0);
}
void lock_semaphore(SEMAPHORE * s)
{
DosRequestMutexSem(*s, SEM_INDEFINITE_WAIT);
}
void unlock_semaphore(SEMAPHORE * s)
{
DosReleaseMutexSem(*s);
}
void delete_semaphore(SEMAPHORE * s)
{
DosCloseMutexSem(*s);
}
#endif /* if defined (NOSEMAPHORES) */