]> mj.ucw.cz Git - libucw.git/commitdiff
added a wrapper for POSIX semaphores that works also on Darwin
authorRobert Spalek <robert@ucw.cz>
Fri, 27 Oct 2006 03:05:16 +0000 (20:05 -0700)
committerRobert Spalek <robert@ucw.cz>
Fri, 27 Oct 2006 03:05:16 +0000 (20:05 -0700)
lib/semaphore.h [new file with mode: 0644]

diff --git a/lib/semaphore.h b/lib/semaphore.h
new file mode 100644 (file)
index 0000000..ad160b1
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ *     The UCW Library -- POSIX semaphores wrapper
+ *
+ *     (c) 2006 Robert Spalek <robert@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#ifndef _UCW_SEMAPHORE_H
+#define _UCW_SEMAPHORE_H
+
+#include <semaphore.h>
+
+#ifdef CONFIG_DARWIN
+
+#include <unistd.h>
+
+/* In Darwin, sem_init() is unfortunately not implemented and the guide
+ * recommends emulating it using sem_open().  */
+
+static inline sem_t *
+sem_alloc(void)
+{
+  static uns cnt = 0;
+  byte buf[20];
+  sprintf(buf, "tmp/sem-%d-%d", getpid(), cnt++);
+  sem_t *sem = sem_open(buf, O_CREAT, 0777, 0);
+  ASSERT(sem != (sem_t*) SEM_FAILED);
+  return sem;
+}
+
+static inline void
+sem_free(sem_t *sem)
+{
+  sem_close(sem);
+}
+
+#else
+
+static inline sem_t *
+sem_alloc(void)
+{
+  sem_t *sem = xmalloc(sizeof(sem_t));
+  int res = sem_init(sem, 0, 0);
+  ASSERT(!res);
+  return sem;
+}
+
+static inline void
+sem_free(sem_t *sem)
+{
+  sem_destroy(sem);
+  xfree(sem);
+}
+
+#endif
+
+#endif