     closed = true
 
     // Unblock the threads waiting on our conditions.
-    while readerQueue.count > 0
+    if readerQueue.count > 0
     {
-      dispatch_semaphore_signal(readerQueue.dequeue()!)
+      while let rs = readerQueue.dequeue() { dispatch_semaphore_signal(rs) }
     }
-    while writerQueue.count > 0
+    if writerQueue.count > 0
     {
-      dispatch_semaphore_signal(writerQueue.dequeue()!)
+      while let ws = writerQueue.dequeue() { dispatch_semaphore_signal(ws) }
     }
     OSSpinLockUnlock(&mutex)
   }

    { // data is already available
      OSSpinLockUnlock(&mutex)

      let context = UnsafeMutablePointer<T>(dispatch_get_context(ws))
      if context != UnsafeMutablePointer.null()
      {
        let element = context.move()
        context.dealloc(1)
        dispatch_set_context(ws, nil)
        dispatch_semaphore_signal(ws)
        return element
      }
      dispatch_semaphore_signal(ws)
      return nil
    }


    OSSpinLockUnlock(&mutex)
    dispatch_semaphore_wait(threadLock, DISPATCH_TIME_FOREVER)

    // got awoken
    let context = UnsafeMutablePointer<T>(dispatch_get_context(threadLock))
    if context == UnsafeMutablePointer.null()
    { // thread was awoken by a close(), not a writer
      SemaphorePool.enqueue(threadLock)
      return nil
    }

    let element = context.move()
    context.dealloc(1)
    dispatch_set_context(threadLock, nil)
    SemaphorePool.enqueue(threadLock)
    return element
  }
}
