   {
     let kr = semaphore_destroy(mach_task_self_, semp)
     assert(kr == KERN_SUCCESS, __FUNCTION__)
+  }
+
+  final private func initSemaphorePort()
+  {
+    var semaphore = semaphore_t()
+    let kr = semaphore_create(mach_task_self_, &semaphore, SYNC_POLICY_FIFO, 0)
+    assert(kr == KERN_SUCCESS, __FUNCTION__)
+
+    func atomicAssignPort(port: semaphore_t, assignee: UnsafeMutablePointer<Void>) -> Bool
+    { // succeed only if 'semp' is still zero
+      return OSAtomicCompareAndSwap32Barrier(0, unsafeBitCast(port, Int32.self),
+                                             UnsafeMutablePointer<Int32>(assignee))
+    }
+
+    if !atomicAssignPort(semaphore, &semp)
+    { // another initialization attempt succeeded concurrently. Don't leak the port; return it.
+      let kr = semaphore_destroy(mach_task_self_, semaphore)
+      assert(kr == KERN_SUCCESS, __FUNCTION__)
+    }
   }
 
   final var isSelected: Bool { return selected != 0 }


import Darwin.Mach
import Dispatch.time

struct SemaphorePool
{

    {
      return true
    }

    if semp == 0 { initSemaphorePort() }

    var kr: kern_return_t
    switch timeout
