]> mj.ucw.cz Git - libucw.git/blob - lib/semaphore.h
Readding line of makefile lost in merge
[libucw.git] / lib / 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
20 /* In Darwin, sem_init() is unfortunately not implemented and the guide
21  * recommends emulating it using sem_open().  */
22
23 static inline sem_t *
24 sem_alloc(void)
25 {
26   static uns cnt = 0;
27   char buf[20];
28   sprintf(buf, "tmp/sem-%d-%d", getpid(), cnt++);
29   sem_t *sem = sem_open(buf, O_CREAT, 0777, 0);
30   ASSERT(sem != (sem_t*) SEM_FAILED);
31   return sem;
32 }
33
34 static inline void
35 sem_free(sem_t *sem)
36 {
37   sem_close(sem);
38 }
39
40 #else
41
42 static inline sem_t *
43 sem_alloc(void)
44 {
45   sem_t *sem = xmalloc(sizeof(sem_t));
46   int res = sem_init(sem, 0, 0);
47   ASSERT(!res);
48   return sem;
49 }
50
51 static inline void
52 sem_free(sem_t *sem)
53 {
54   sem_destroy(sem);
55   xfree(sem);
56 }
57
58 #endif
59
60 #endif