]> mj.ucw.cz Git - libucw.git/commitdiff
Libucw: Use the new temp file logic for semaphores.
authorMartin Mares <mj@ucw.cz>
Sat, 19 Jul 2008 21:54:06 +0000 (23:54 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 19 Jul 2008 21:54:06 +0000 (23:54 +0200)
(picked from Michal's dev-vorner branch)

ucw/semaphore.h

index fa2e0ee473d58318e59bc3683d22eb33d94a8895..140ba9b9f09c055926273df884655076bbfb56e2 100644 (file)
@@ -16,6 +16,9 @@
 
 #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 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;
+  do
+    {
+      temp_file_name(buf, &mode);
+      sem_t *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;
 }