[libdispatch-changes] [84] trunk/src/semaphore.c

source_changes at macosforge.org source_changes at macosforge.org
Mon Nov 2 03:38:46 PST 2009


Revision: 84
          http://trac.macosforge.org/projects/libdispatch/changeset/84
Author:   robert at fledge.watson.org
Date:     2009-11-02 03:38:43 -0800 (Mon, 02 Nov 2009)
Log Message:
-----------
Unconditionally initialize the POSIX semaphore supporting a dispatch
semaphore, as the type sem_t may not be a scalar on all platforms.

Even if it were, the current code was racey as it failed to do an
atomic test-and-set to make sure that two threads hadn't triggered
on-demand initialization of the dispatch semaphore at the same time.

In practice, this should remain fairly efficient as non-pshared POSIX
semaphores will generally be implemented entirely with libpthread,
and likely not require kernel involvement during allocation (as is
required for Mach semaphores, hence lazy allocation optimizations).

Spotted by:	Kevin Van Vechten <kvv at apple.com>

Modified Paths:
--------------
    trunk/src/semaphore.c

Modified: trunk/src/semaphore.c
===================================================================
--- trunk/src/semaphore.c	2009-11-02 11:27:33 UTC (rev 83)
+++ trunk/src/semaphore.c	2009-11-02 11:38:43 UTC (rev 84)
@@ -86,6 +86,9 @@
 dispatch_semaphore_create(long value)
 {
 	dispatch_semaphore_t dsema;
+#if USE_POSIX_SEM
+	int ret;
+#endif
 	
 	// If the internal value is negative, then the absolute of the value is
 	// equal to the number of waiting threads. Therefore it is bogus to
@@ -104,6 +107,10 @@
 		dsema->do_targetq = dispatch_get_global_queue(0, 0);
 		dsema->dsema_value = value;
 		dsema->dsema_orig = value;
+#if USE_POSIX_SEM
+		ret = sem_init(&dsema->dsema_sem, 0, 0);
+		dispatch_assume_zero(ret);
+#endif
 	}
 	
 	return dsema;
@@ -140,21 +147,7 @@
 	_dispatch_safe_fork = false;
 }
 #endif
-#if USE_POSIX_SEM
-static void
-_dispatch_posix_semaphore_create(sem_t *s4)
-{
-	int ret;
 
-	if (*s4) {
-		return;
-	}
-
-	ret = sem_init(s4, 0, 0);
-	dispatch_assume_zero(ret);
-}
-#endif
-
 DISPATCH_NOINLINE
 static long
 _dispatch_semaphore_wait_slow(dispatch_semaphore_t dsema, dispatch_time_t timeout)
@@ -183,9 +176,6 @@
 #if USE_MACH_SEM
 	_dispatch_semaphore_create_port(&dsema->dsema_port);
 #endif
-#if USE_POSIX_SEM
-	_dispatch_posix_semaphore_create(&dsema->dsema_sem);
-#endif
 
 	// From xnu/osfmk/kern/sync_sema.c:
 	// wait_semaphore->count = -1;  /* we don't keep an actual count */
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/libdispatch-changes/attachments/20091102/421297b8/attachment.html>


More information about the libdispatch-changes mailing list