]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/semaphore.h
tableprinter: code cleanup
[libucw.git] / ucw / semaphore.h
index fa2e0ee473d58318e59bc3683d22eb33d94a8895..7b211f8e1c9d1d3a0b746bad68531074b87c9c41 100644 (file)
 
 #include <unistd.h>
 #include <stdio.h>
+#include <errno.h>
+
+#include <ucw/fastbuf.h> // For the temp_file_name
 
 /* 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 inline sem_t *sem_alloc(void)
 {
-  static uns cnt = 0;
-  char buf[20];
-  sprintf(buf, "tmp/sem-%d-%d", getpid(), cnt++);
-  sem_t *sem = sem_open(buf, O_CREAT, 0777, 0);
+  char buf[TEMP_FILE_NAME_LEN];
+  int mode, retry = 10;
+  sem_t *sem;
+  do
+    {
+      temp_file_name(buf, &mode);
+      sem = sem_open(buf, mode | O_CREAT, 0777, 0);
+    }
+  while (sem == (sem_t*) SEM_FAILED && errno == EEXIST && retry --);
   ASSERT(sem != (sem_t*) SEM_FAILED);
   return sem;
 }
 
-static inline void
-sem_free(sem_t *sem)
+static inline void sem_free(sem_t *sem)
 {
   sem_close(sem);
 }
 
 #else
 
-static inline sem_t *
-sem_alloc(void)
+static inline sem_t *sem_alloc(void)
 {
   sem_t *sem = xmalloc(sizeof(sem_t));
   int res = sem_init(sem, 0, 0);
@@ -48,8 +53,7 @@ sem_alloc(void)
   return sem;
 }
 
-static inline void
-sem_free(sem_t *sem)
+static inline void sem_free(sem_t *sem)
 {
   sem_destroy(sem);
   xfree(sem);