     // Unblock the threads waiting on our conditions.
     while let rs = readerQueue.dequeue()
     {
-      dispatch_set_context(rs, nil)
-      dispatch_semaphore_signal(rs)
+      switch rs
+      {
+      case .semaphore(let s):
+        dispatch_set_context(s, nil)
+        dispatch_semaphore_signal(s)
+
+      case .selection(let c, _):
+        if let s = c.get()
+        {
+          dispatch_semaphore_signal(s)
+        }
+      }
     }
     while let ws = writerQueue.dequeue()
     {
-      dispatch_set_context(ws, nil)
-      dispatch_semaphore_signal(ws)
+      switch ws
+      {
+      case .semaphore(let s):
+        dispatch_set_context(s, nil)
+        dispatch_semaphore_signal(s)
+
+      case .selection(let c, _):
+        if let s = c.get()
+        {
+          dispatch_semaphore_signal(s)
+        }
+      }
     }
     OSSpinLockUnlock(&lock)
   }


  override func selectPut(semaphore: SemaphoreChan, selectionID: Selectable) -> Signal
  {
    let threadLock = SemaphorePool.dequeue()
    var cancelable = false

    dispatch_async(dispatch_get_global_queue(qos_class_self(), 0)) {
      _ in
      OSSpinLockLock(&self.lock)
      if let rs = self.readerQueue.dequeue()
      {
        switch dispatch_get_context(rs)
        {
        case nil:
          preconditionFailure(__FUNCTION__)

        case waitSelect:
          if let s = semaphore.get()
          {
            OSSpinLockUnlock(&self.lock)
            dispatch_set_context(threadLock, waitSelect)
            dispatch_set_context(rs, UnsafeMutablePointer<Void>(Unmanaged.passRetained(threadLock).toOpaque()))
            dispatch_semaphore_signal(rs)
            dispatch_semaphore_wait(threadLock, DISPATCH_TIME_FOREVER)

            // got awoken
            switch dispatch_get_context(threadLock)
            {
            case nil:
              // the counterpart selectGet couldn't obtain its own select semaphore.
              dispatch_set_context(s, nil)
              dispatch_semaphore_signal(s)

            case waitSelect:
              // rs's context should now be a pointer to a buffer.
              assert(dispatch_get_context(rs) != waitSelect, __FUNCTION__)
              // pass the rs semaphore on to insert()
              let selection = Selection(id: selectionID, semaphore: rs)
              dispatch_set_context(s, UnsafeMutablePointer<Void>(Unmanaged.passRetained(selection).toOpaque()))
              dispatch_semaphore_signal(s)

            case let context: // default
              preconditionFailure("Context \(context) is invalid in waitSelect case of \(__FUNCTION__)")
            }
          }
          else
          {
            self.readerQueue.undequeue(rs)
            // The spinlock isn't released between dequeue() and undequeue(),
            // so this is transparent to other threads.
            OSSpinLockUnlock(&self.lock)
          }

        default:
          if let s = semaphore.get()
          { // pass the rs semaphore on to insert()
            OSSpinLockUnlock(&self.lock)
            let selection = Selection(id: selectionID, semaphore: rs)
            dispatch_set_context(s, UnsafeMutablePointer<Void>(Unmanaged.passRetained(selection).toOpaque()))
            dispatch_semaphore_signal(s)
          }
          else
          {
            self.readerQueue.undequeue(rs)
            // The spinlock isn't released between dequeue() and undequeue(),
            // so this is transparent to other threads.
            OSSpinLockUnlock(&self.lock)
          }
        }

        dispatch_set_context(threadLock, nil)
        SemaphorePool.enqueue(threadLock)
        return
      }

      if self.closed
      {
        OSSpinLockUnlock(&self.lock)
        if let s = semaphore.get()
        {
          dispatch_set_context(s, nil)
          dispatch_semaphore_signal(s)
        }
      }

      dispatch_set_context(threadLock, waitSelect)
      self.writerQueue.enqueue(threadLock)
      cancelable = true
      OSSpinLockUnlock(&self.lock)
      dispatch_semaphore_wait(threadLock, DISPATCH_TIME_FOREVER)

      // got awoken
      switch dispatch_get_context(threadLock)
      {
      case nil:
        // thread was awoken by close(): write operation fails
        if let s = semaphore.get()
        {
          dispatch_set_context(s, nil)
          dispatch_semaphore_signal(s)
        }

      case cancelSelect:
        // no need to try for the semaphore.
        break

      case let context: // default
        let rs = Unmanaged<dispatch_semaphore_t>.fromOpaque(COpaquePointer(context)).takeRetainedValue()
        if let s = semaphore.get()
        { // pass rs on to insert()
          let selection = Selection(id: selectionID, semaphore: rs)
          dispatch_set_context(s, UnsafeMutablePointer<Void>(Unmanaged.passRetained(selection).toOpaque()))
          dispatch_semaphore_signal(s)
        }
        else
        { // signal failure to the reader
          dispatch_set_context(rs, nil)
          dispatch_semaphore_signal(rs)
        }
      }

      dispatch_set_context(threadLock, nil)
      SemaphorePool.enqueue(threadLock)
    }

    return {
      OSMemoryBarrier()
      if cancelable
      { // selectPut is probably in its wait state
        OSSpinLockLock(&self.lock)
        if self.writerQueue.remove(threadLock)
        {
          dispatch_set_context(threadLock, cancelSelect)
          dispatch_semaphore_signal(threadLock)
        }
        OSSpinLockUnlock(&self.lock)
      }
    }
  }

  override func selectGetNow(selectionID: Selectable) -> Selection?
  {
    OSSpinLockLock(&lock)
    if let ws = writerQueue.dequeue()
    {
      switch dispatch_get_context(ws)
      {
      case nil, cancelSelect:
        preconditionFailure("Context is invalid in \(__FUNCTION__)")

      case waitSelect:
        // waitSelect has a significant chance of failure, but the selectGetNow() path should work "all" the time.
        writerQueue.undequeue(ws)

      default:
        OSSpinLockUnlock(&lock)
        return Selection(id: selectionID, semaphore: ws)
      }
    }
    OSSpinLockUnlock(&lock)
    return nil
  }

  override func extract(selection: Selection) -> T?
  {
    if let ws = selection.semaphore

{
  // MARK: private housekeeping

  private let readerQueue = SuperSemaphoreQueue()
  private let writerQueue = SuperSemaphoreQueue()

  private var lock = OS_SPINLOCK_INIT

