 
     if closed && head >= tail
     {
+      if !writerQueue.isEmpty, let ws = writerQueue.dequeue()
+      {
+        dispatch_semaphore_signal(ws)
+      }
+      else if head < tail || closed
+      {
+        if !readerQueue.isEmpty, let rs = readerQueue.dequeue()
+        {
+          dispatch_semaphore_signal(rs)
+        }
+      }
       OSSpinLockUnlock(&lock)
       return nil
     }

      wait(lock: &lock, queue: writerQueue)
    }

    if closed
    {
      OSSpinLockUnlock(&lock)
      return false
    }

    buffer.advancedBy(tail&mask).initialize(newElement)
    tail += 1

    if !readerQueue.isEmpty, let rs = readerQueue.dequeue()
    {
      dispatch_semaphore_signal(rs)
    }
    else if head+capacity > tail // the channel isn't full
    {
      if !writerQueue.isEmpty, let ws = writerQueue.dequeue()
      {

    OSSpinLockLock(&lock)
    closed = true

    // Unblock waiting threads.
    if !readerQueue.isEmpty, let rs = readerQueue.dequeue()
    {
      dispatch_semaphore_signal(rs)
    }
    else if !writerQueue.isEmpty, let ws = writerQueue.dequeue()
    {
      dispatch_semaphore_signal(ws)
    }
    OSSpinLockUnlock(&lock)
  }
