     if closed && elementCount <= 0 { return nil }
 
     dispatch_semaphore_wait(filled, DISPATCH_TIME_FOREVER)
+    OSSpinLockLock(&mutex)
 
     if closed && elementCount <= 0
     {
+      OSSpinLockUnlock(&mutex)
       dispatch_semaphore_signal(filled)
       return nil
     }

  {
    if closed { return }

    closed = true
    dispatch_semaphore_signal(empty)
    dispatch_semaphore_signal(filled)
  }

    let e = element
    elementCount--

    OSSpinLockUnlock(&mutex)
    dispatch_semaphore_signal(empty)

    return e
