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