]> mj.ucw.cz Git - libucw.git/blob - ucw/semaphore.h
Libucw: Update path parameter handling of URL to current RFC (2396).
[libucw.git] / ucw / semaphore.h
1 /*
2  *      The UCW Library -- POSIX semaphores wrapper
3  *
4  *      (c) 2006 Robert Spalek <robert@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_SEMAPHORE_H
11 #define _UCW_SEMAPHORE_H
12
13 #include <semaphore.h>
14
15 #ifdef CONFIG_DARWIN
16
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <errno.h>
20
21 #include "ucw/fastbuf.h" // For the temp_file_name
22
23 /* In Darwin, sem_init() is unfortunately not implemented and the guide
24  * recommends emulating it using sem_open().  */
25
26 static inline sem_t *
27 sem_alloc(void)
28 {
29   char buf[TEMP_FILE_NAME_LEN];
30   int mode, retry = 10;
31   do
32     {
33       temp_file_name(buf, &mode);
34       sem_t *sem = sem_open(buf, mode | O_CREAT, 0777, 0);
35     }
36   while (sem == (sem_t*) SEM_FAILED && errno == EEXIST && retry --);
37   ASSERT(sem != (sem_t*) SEM_FAILED);
38   return sem;
39 }
40
41 static inline void
42 sem_free(sem_t *sem)
43 {
44   sem_close(sem);
45 }
46
47 #else
48
49 static inline sem_t *
50 sem_alloc(void)
51 {
52   sem_t *sem = xmalloc(sizeof(sem_t));
53   int res = sem_init(sem, 0, 0);
54   ASSERT(!res);
55   return sem;
56 }
57
58 static inline void
59 sem_free(sem_t *sem)
60 {
61   sem_destroy(sem);
62   xfree(sem);
63 }
64
65 #endif
66
67 #endif