Semaphore.xs

filter: Statementstmt 84.7% 61 / 72 Branchbran 63.8% 46 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.3% 107 / 144 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/POSIX/RT/Semaphore.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5  
6 #include <unistd.h> /* _POSIX, _SC_* */
7  
8 #if !defined(_POSIX_SEMAPHORES)
9 # error "POSIX::RT::Semaphore requires _POSIX_SEMAPHORES support."
10 #endif
11  
12 #include <semaphore.h>
13 #include <limits.h> /* _POSIX_SEM_* */
14 #include <errno.h> /* ENOSYS */
15  
16 #ifdef HAVE_SEM_TIMEDWAIT
17 # include <time.h>
18 #endif
19  
20  
21 #ifdef HAS_MMAP
22 # include <sys/mman.h>
23 # if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
24 # define MAP_ANONYMOUS MAP_ANON
25 # endif
26 # ifndef MAP_ANONYMOUS
27 # include <fcntl.h> /* open(..., O_RDWR) */
28 # endif
29 # ifndef MAP_HASSEMAPHORE
30 # define MAP_HASSEMAPHORE 0
31 # endif
32 # ifndef MAP_FAILED
33 # define MAP_FAILED ((void *)-1)
34 # endif
35 #endif
36  
37  
38  
39 typedef int SysRet;
40  
41 typedef struct PSem_C {
42 sem_t* sem;
43 char * name;
44 } * POSIX__RT__Semaphore___base;
45 typedef POSIX__RT__Semaphore___base POSIX__RT__Semaphore__Named;
46 typedef POSIX__RT__Semaphore___base POSIX__RT__Semaphore__Unnamed;
47  
48 /* _alloc_sem()
49 *
50 * Return a new sem_t in shared memory (maybe), or NULL (_not_ SEM_FAILED)
51 * on failure.
52 */
53 static sem_t *
54 13 _alloc_sem(void) {
55 13 sem_t *sem = NULL;
56 #ifdef HAS_MMAP
57 13 int fd = -1;
58 13 int prot_flags = PROT_READ|PROT_WRITE;
59 13 int map_flags = MAP_SHARED|MAP_HASSEMAPHORE;
60  
61 # ifdef MAP_ANONYMOUS
62 13 map_flags |= MAP_ANONYMOUS;
63 13 sem = (sem_t *)mmap(NULL, sizeof(sem_t), prot_flags, map_flags, fd, 0);
64 # else
65 if ((fd = open("/dev/zero", O_RDWR)) != -1) {
66 sem = (sem_t *)mmap(NULL, sizeof(sem_t), prot_flags, map_flags, fd, 0);
67 close(fd);
68 }
69 # endif /* MAP_ANONYMOUS */
70 #else
71 Newz(0, sem, 1, sem_t);
72 #endif /* HAS_MMAP */
73  
74 13 return sem;
75 }
76  
77 /* _dealloc_sem
78 *
79 * Free a sem_t we allocated.
80 */
81 static void
82 13 _dealloc_sem(sem_t *sem)
83 {
84 #ifdef HAS_MMAP
85 13 munmap((void *)sem, sizeof(sem_t));
86 #else
87 Safefree(sem);
88 #endif
89 13 }
90  
91 static int
92 0 function_not_implemented(void)
93 {
94 0 errno = ENOSYS;
95 0 return -1;
96 }
97  
98 #define sem_valid(sem) ((sem) && (sem) != SEM_FAILED)
99  
100 #define PRECOND_valid_psem(fname, psem) \
101 do { \
102 if (!psem || !sem_valid(psem->sem)) \
103 croak(fname "() method called on invalid psem"); \
104 } while (0)
105  
106  
107 MODULE = POSIX::RT::Semaphore PACKAGE = POSIX::RT::Semaphore PREFIX = psem_
108 PROTOTYPES: DISABLE
109  
110 BOOT:
111 {
112 15 char * const pkgs[] = {
113 "POSIX::RT::Semaphore::Named::ISA",
114 "POSIX::RT::Semaphore::Unnamed::ISA",
115 };
116 HV *stash;
117 int i;
118  
119 45 ▶ for (i = 0; i < sizeof(pkgs)/sizeof(*pkgs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < sizeof(pkgs)/sizeof(*pkgs); i++) { 30 15 45
120 AV *isa;
121 30 isa = get_av(pkgs[i], TRUE);
122 30 av_push(isa, newSVpv("POSIX::RT::Semaphore::_base", 0));
123 }
124  
125 15 stash = gv_stashpvn("POSIX::RT::Semaphore", 20, TRUE);
126  
127 15 newCONSTSUB(stash, "SIZEOF_SEM_T", newSViv(sizeof(sem_t)));
128 #ifdef _POSIX_SEM_VALUE_MAX
129 15 newCONSTSUB(stash, "SEM_VALUE_MAX", newSViv(_POSIX_SEM_VALUE_MAX));
130 #endif
131 #ifdef _SC_SEM_VALUE_MAX
132 15 newCONSTSUB(stash, "_SC_SEM_VALUE_MAX", newSViv(_POSIX_SEM_VALUE_MAX));
133 #endif
134 #ifdef _POSIX_SEM_NSEMS_MAX
135 15 newCONSTSUB(stash, "SEM_NSEMS_MAX", newSViv(_POSIX_SEM_NSEMS_MAX));
136 #endif
137 #ifdef _SC_SEM_NSEMS_MAX
138 15 newCONSTSUB(stash, "_SC_SEM_NSEMS_MAX", newSViv(_POSIX_SEM_NSEMS_MAX));
139 #endif
140 #ifdef SEM_NAME_LEN
141 newCONSTSUB(stash, "SEM_NAME_LEN", newSViv(SEM_NAME_LEN));
142 #endif
143 #ifdef SEM_NAME_MAX
144 newCONSTSUB(stash, "SEM_NAME_MAX", newSViv(SEM_NAME_MAX));
145 #endif
146 }
147  
148 SysRet
149 psem_unlink(pkg = "POSIX::RT::Semaphore", path)
150 char* pkg
151 char* path
152  
153 CODE:
154 #ifdef HAVE_SEM_UNLINK
155 5 RETVAL = sem_unlink(path);
156 #else
157 # older versions of Cygwin
158 RETVAL = function_not_implemented();
159 #endif
160  
161 OUTPUT:
162 RETVAL
163  
164 MODULE = POSIX::RT::Semaphore PACKAGE = POSIX::RT::Semaphore::_base PREFIX = psem_
165 PROTOTYPES: DISABLE
166  
167 void
168 psem_DESTROY(self)
169 POSIX::RT::Semaphore::_base self
170  
171 CODE:
172 32 ▶ if (self->name) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->name) { 19 13 32
173 19 ▶ if (sem_valid(self->sem))
Branch: 2 branches
BranchTrueFalseTotal
if (sem_valid(self->sem)) 10 9 19
if (sem_valid(self->sem)) 10 0 10
174 10 (void)sem_close(self->sem);
175 19 Safefree(self->name);
176 } else {
177 13 ▶ if (sem_valid(self->sem))
Branch: 2 branches
BranchTrueFalseTotal
if (sem_valid(self->sem)) 2 11 13
if (sem_valid(self->sem)) 2 0 2
178 2 _dealloc_sem(self->sem);
179 }
180 32 Safefree(self);
181  
182 SysRet
183 psem_wait(self)
184 POSIX::RT::Semaphore::_base self
185  
186 CODE:
187 5 ▶ PRECOND_valid_psem("wait", self);
Branch: 3 branches
BranchTrueFalseTotal
PRECOND_valid_psem("wait", self); 5 0 5
PRECOND_valid_psem("wait", self); 3 2 5
PRECOND_valid_psem("wait", self); 0 3 3
188 3 RETVAL = sem_wait(self->sem);
189  
190 OUTPUT:
191 RETVAL
192  
193 SysRet
194 psem_trywait(self)
195 POSIX::RT::Semaphore::_base self
196  
197 CODE:
198 6 ▶ PRECOND_valid_psem("trywait", self);
Branch: 3 branches
BranchTrueFalseTotal
PRECOND_valid_psem("trywait", self); 6 0 6
PRECOND_valid_psem("trywait", self); 6 0 6
PRECOND_valid_psem("trywait", self); 0 6 6
199 6 RETVAL = sem_trywait(self->sem);
200  
201 OUTPUT:
202 RETVAL
203  
204 SysRet
205 psem_timedwait(self, timeout)
206 POSIX::RT::Semaphore::_base self
207 NV timeout
208  
209 PREINIT:
210 #ifdef HAVE_SEM_TIMEDWAIT
211 struct timespec ts;
212 #endif
213  
214 CODE:
215 6 ▶ PRECOND_valid_psem("timedwait", self);
Branch: 3 branches
BranchTrueFalseTotal
PRECOND_valid_psem("timedwait", self); 6 0 6
PRECOND_valid_psem("timedwait", self); 4 2 6
PRECOND_valid_psem("timedwait", self); 0 4 4
216 #ifdef HAVE_SEM_TIMEDWAIT
217 4 ▶ if (timeout < 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (timeout < 0.0) 0 4 4
218 0 timeout = 0.0;
219 4 ts.tv_sec = (long)timeout;
220 4 timeout -= (NV)ts.tv_sec;
221 4 ts.tv_nsec = (long)(timeout * 1000000000.0);
222 4 RETVAL = sem_timedwait(self->sem, (const struct timespec *)&ts);
223 #else
224 RETVAL = function_not_implemented();
225 #endif
226  
227 OUTPUT:
228 RETVAL
229  
230 SysRet
231 psem_post(self)
232 POSIX::RT::Semaphore::_base self
233  
234 CODE:
235 14 ▶ PRECOND_valid_psem("post", self);
Branch: 3 branches
BranchTrueFalseTotal
PRECOND_valid_psem("post", self); 14 0 14
PRECOND_valid_psem("post", self); 12 2 14
PRECOND_valid_psem("post", self); 0 12 12
236 12 RETVAL = sem_post(self->sem);
237  
238 OUTPUT:
239 RETVAL
240  
241 int
242 psem_getvalue(self)
243 POSIX::RT::Semaphore::_base self
244  
245 CODE:
246 48 ▶ # RETVAL is an int, _not_ a SysRet, since we preserve -1
Branch: 3 branches
BranchTrueFalseTotal
# RETVAL is an int, _not_ a SysRet, since we preserve -1 48 0 48
# RETVAL is an int, _not_ a SysRet, since we preserve -1 46 2 48
# RETVAL is an int, _not_ a SysRet, since we preserve -1 0 46 46
247 46 ▶ PRECOND_valid_psem("getvalue", self);
Branch: 1 branch
BranchTrueFalseTotal
PRECOND_valid_psem("getvalue", self); 0 46 46
248 0 if (sem_getvalue(self->sem, &RETVAL) != 0) {
249 XSRETURN_UNDEF;
250 }
251  
252 OUTPUT:
253 RETVAL
254  
255 ## Obsolecent ##
256 char*
257 psem_name(self)
258 POSIX::RT::Semaphore::_base self
259  
260 CODE:
261 3 ▶ PRECOND_valid_psem("name", self);
Branch: 3 branches
BranchTrueFalseTotal
PRECOND_valid_psem("name", self); 3 0 3
PRECOND_valid_psem("name", self); 3 0 3
PRECOND_valid_psem("name", self); 0 3 3
262 3 RETVAL = self->name;
263  
264 OUTPUT:
265 RETVAL
266  
267  
268 MODULE = POSIX::RT::Semaphore PACKAGE = POSIX::RT::Semaphore::Unnamed PREFIX = psem_
269 PROTOTYPES: DISABLE
270  
271 POSIX::RT::Semaphore::Unnamed
272 psem_init(pkg = "POSIX::RT::Semaphore::Unnamed", pshared = 0, value = 1)
273 char* pkg
274 int pshared
275 unsigned value
276  
277 CODE:
278 13 Newz(0, RETVAL, 1, struct PSem_C);
279 13 RETVAL->sem = _alloc_sem();
280 13 ▶ if (NULL == RETVAL->sem) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == RETVAL->sem) { 0 13 13
281 0 Safefree(RETVAL);
282 0 croak("sem_init: failed to allocate semaphore");
283 }
284 13 ▶ if (sem_init(RETVAL->sem, pshared, value) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (sem_init(RETVAL->sem, pshared, value) == -1) { 0 13 13
285 0 _dealloc_sem(RETVAL->sem);
286 0 Safefree(RETVAL);
287 0 RETVAL = NULL;
288 }
289 #warn("xs.sem_init 0x%x (sem: 0x%x)\n", RETVAL, RETVAL->sem);
290  
291 OUTPUT:
292 RETVAL
293  
294 SysRet
295 psem_destroy(self)
296 POSIX::RT::Semaphore::Unnamed self
297  
298 CODE:
299 12 ▶ PRECOND_valid_psem("destroy", self);
Branch: 3 branches
BranchTrueFalseTotal
PRECOND_valid_psem("destroy", self); 12 0 12
PRECOND_valid_psem("destroy", self); 11 1 12
PRECOND_valid_psem("destroy", self); 0 11 11
300 11 ▶ if (0 == (RETVAL = sem_destroy(self->sem))) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == (RETVAL = sem_destroy(self->sem))) { 11 0 11
301 11 _dealloc_sem(self->sem);
302 11 self->sem = NULL;
303 }
304  
305 OUTPUT:
306 RETVAL
307  
308  
309 MODULE = POSIX::RT::Semaphore PACKAGE = POSIX::RT::Semaphore::Named PREFIX = psem_
310 PROTOTYPES: DISABLE
311  
312 POSIX::RT::Semaphore::Named
313 psem_open(pkg = "POSIX::RT::Semaphore::Named", name, flags = 0, mode = 0666, value = 1)
314 char* pkg
315 char* name
316 int flags
317 Mode_t mode
318 unsigned value
319  
320 PREINIT:
321 sem_t* sem;
322  
323 CODE:
324 19 sem = sem_open(name, flags, mode, value);
325 19 ▶ if (SEM_FAILED == sem) {
Branch: 1 branch
BranchTrueFalseTotal
if (SEM_FAILED == sem) { 0 19 19
326 0 XSRETURN_UNDEF;
327 }
328  
329 19 Newz(0, RETVAL, 1, struct PSem_C);
330 19 RETVAL->sem = sem;
331 19 RETVAL->name = savepv(name);
332 #warn("xs.sem_open 0x%x (sem: 0x%x)\n", RETVAL, RETVAL->sem);
333  
334 OUTPUT:
335 RETVAL
336  
337 SysRet
338 psem_close(self)
339 POSIX::RT::Semaphore::Named self
340  
341 CODE:
342 10 ▶ PRECOND_valid_psem("close", self);
Branch: 3 branches
BranchTrueFalseTotal
PRECOND_valid_psem("close", self); 10 0 10
PRECOND_valid_psem("close", self); 9 1 10
PRECOND_valid_psem("close", self); 0 9 9
343 9 RETVAL = sem_close(self->sem);
344 9 self->sem = NULL;
345  
346 OUTPUT:
347 RETVAL
« blib/lib/POSIX/RT/Semaphore.pm ↑ Summary