 
   override func selectGet(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 ws = self.writerQueue.dequeue()
-      {
-        switch dispatch_get_context(ws)
-        {
-        case nil:
-          preconditionFailure(__FUNCTION__)
-
-        case waitSelect:
-          if let s = semaphore.get()
-          {
-            OSSpinLockUnlock(&self.lock)
-
-            // we're talking to a selectPut(); it needs a semaphore with a buffer attached
-            let buffer = UnsafeMutablePointer<T>.alloc(1)
-            dispatch_set_context(threadLock, buffer)
-            dispatch_set_context(ws, UnsafeMutablePointer<Void>(Unmanaged.passRetained(threadLock).toOpaque()))
-            dispatch_semaphore_signal(ws)
-            dispatch_semaphore_wait(threadLock, DISPATCH_TIME_FOREVER)
-
-            // got awoken
-            switch dispatch_get_context(threadLock)
-            {
-            case buffer:
-              // thread awoken by insert(); buffer is now initialized with a copy of the message data.
-              // pass threadLock on to extract(), mimicking a queued put()
-              let selection = Selection(id: selectionID, semaphore: threadLock)
-              dispatch_set_context(s, UnsafeMutablePointer<Void>(Unmanaged.passRetained(selection).toOpaque()))
-              dispatch_semaphore_signal(s)
-
-              // wait, and then clean up.
-              dispatch_semaphore_wait(threadLock, DISPATCH_TIME_FOREVER)
-              switch dispatch_get_context(threadLock)
-              {
-              case buffer, nil: break
-              case let context:
-                preconditionFailure("Unknown context value (\(context)) in waitSelect case of \(__FUNCTION__)")
-              }
-              buffer.destroy()
-              buffer.dealloc(1)
-
-            case nil:
-              // the counterpart selectPut couldn't obtain its own semaphore.
-              buffer.dealloc(1)
-              dispatch_semaphore_signal(s)
-
-            case let context: // default
-              preconditionFailure("Unknown context value (\(context)) in waitSelect case of \(__FUNCTION__)")
-            }
-          }
-          else
-          {
-            self.writerQueue.undequeue(ws)
-            // 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 ws on to extract()
-            OSSpinLockUnlock(&self.lock)
-            let selection = Selection(id: selectionID, semaphore: ws)
-            dispatch_set_context(s, UnsafeMutablePointer<Void>(Unmanaged.passRetained(selection).toOpaque()))
-            dispatch_semaphore_signal(s)
-          }
-          else
-          {
-            self.writerQueue.undequeue(ws)
-            // 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)
-        }
-        return
-      }
-
-      dispatch_set_context(threadLock, waitSelect)
-      self.readerQueue.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(): no more data on the channel
-        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 ws = Unmanaged<dispatch_semaphore_t>.fromOpaque(COpaquePointer(context)).takeRetainedValue()
-        if let s = semaphore.get()
-        {
-          switch dispatch_get_context(ws)
-          {
-          case waitSelect:
-            // we're talking to a selectPut(); it needs a semaphore with a buffer attached
-            let buffer = UnsafeMutablePointer<T>.alloc(1)
-            dispatch_set_context(threadLock, buffer)
-            // the thread waiting on ws already has a reference to our threadLock
-            dispatch_semaphore_signal(ws)
-            dispatch_semaphore_wait(threadLock, DISPATCH_TIME_FOREVER)
-
-            // thread awoken by insert()
-            assert(dispatch_get_context(threadLock) == buffer)
-
-            // pass threadLock on to extract(), i.e. mimic a queued put()
-            let selection = Selection(id: selectionID, semaphore: threadLock)
-            dispatch_set_context(s, UnsafeMutablePointer<Void>(Unmanaged.passRetained(selection).toOpaque()))
-            dispatch_semaphore_signal(s)
-
-            // wait, and then clean up.
-            dispatch_semaphore_wait(threadLock, DISPATCH_TIME_FOREVER)
-            switch dispatch_get_context(threadLock)
-            {
-            case buffer, nil: break
-            case let context:
-              preconditionFailure("Unknown context value (\(context)) in waitSelect case of \(__FUNCTION__)")
-            }
-            buffer.destroy()
-            buffer.dealloc(1)
-
-          default:
-            // the data is attached from a put(); pass it on to extract()
-            let selection = Selection(id: selectionID, semaphore: ws)
-            dispatch_set_context(s, UnsafeMutablePointer<Void>(Unmanaged.passRetained(selection).toOpaque()))
-            dispatch_semaphore_signal(s)
-          }
-        }
-        else
-        { // signal failure to the writer
-          dispatch_set_context(ws, nil)
-          dispatch_semaphore_signal(ws)
-        }
-      }
-
-      dispatch_set_context(threadLock, nil)
-      SemaphorePool.enqueue(threadLock)
-    }
-
-    return {
-      OSMemoryBarrier()
-      if cancelable
-      {
-        OSSpinLockLock(&self.lock)
-        if self.readerQueue.remove(threadLock)
-        {
-          dispatch_set_context(threadLock, cancelSelect)
-          dispatch_semaphore_signal(threadLock)
-        }
-        OSSpinLockUnlock(&self.lock)
-      }
-    }
+    OSSpinLockLock(&lock)
+    if !writerQueue.isEmpty
+    {
+      if let s = semaphore.get()
+      {
+        if let ws = selectPutNow()
+        {
+          let selection = Selection(id: selectionID, semaphore: ws)
+          dispatch_set_context(s, UnsafeMutablePointer<Void>(Unmanaged.passRetained(selection).toOpaque()))
+        }
+        OSSpinLockUnlock(&lock)
+        dispatch_semaphore_signal(s)
+        return {}
+      }
+      else
+      {
+        OSSpinLockUnlock(&lock)
+        return {}
+      }
+    }
+
+    if closed
+    {
+      OSSpinLockUnlock(&lock)
+      if let s = semaphore.get()
+      {
+        dispatch_semaphore_signal(s)
+      }
+      return {}
+    }
+
+    // enqueue the SemaphoreChan and hope for the best.
+    readerQueue.enqueue(.selection(semaphore, selectionID))
+    OSSpinLockUnlock(&lock)
+
+    return {}
   }
 }
-
-/**
-  Useful fake pointers for use with dispatch_set_context()
-*/
-
-private let cancelSelect = UnsafeMutablePointer<Void>(bitPattern: 1)
-private let waitSelect   = UnsafeMutablePointer<Void>(bitPattern: 3)

    // Unblock the threads waiting on our conditions.
    while let rs = readerQueue.dequeue()
    {
      dispatch_set_context(rs, nil)
      dispatch_semaphore_signal(rs)
    }
    while let ws = writerQueue.dequeue()
    {
      dispatch_set_context(ws, nil)
      dispatch_semaphore_signal(ws)
    }
    OSSpinLockUnlock(&lock)
  }

  override func selectPutNow(selectionID: Selectable) -> Selection?
  {
    OSSpinLockLock(&lock)
    if let rs = selectPutNow()
    {
      OSSpinLockUnlock(&lock)
      return Selection(id: selectionID, semaphore: rs)
    }
    OSSpinLockUnlock(&lock)
    return nil
  }

  private func selectPutNow() -> dispatch_semaphore_t?
  {
    precondition(lock != 0, __FUNCTION__)
    while let ssema = readerQueue.dequeue()
    {
      switch ssema
      {
      case .semaphore(let rs):
        return rs

      case .selection(let c, let selectionID):
        if let s = c.get()
        {
          OSSpinLockUnlock(&lock)
          let mediator = SemaphorePool.dequeue()
          let buffer = UnsafeMutablePointer<T>.alloc(1)
          dispatch_set_context(mediator, buffer)

          // two select()s talking to each other need a 3rd thread
          dispatch_async(dispatch_get_global_queue(qos_class_self(), 0)) {
            dispatch_semaphore_wait(mediator, DISPATCH_TIME_FOREVER)
            // got awoken by insert()
            precondition(dispatch_get_context(mediator) == buffer, "Unknown context in \(__FUNCTION__)")

            let selection = Selection(id: selectionID, semaphore: mediator)
            dispatch_set_context(s, UnsafeMutablePointer<Void>(Unmanaged.passRetained(selection).toOpaque()))
            dispatch_semaphore_signal(s)
            dispatch_semaphore_wait(mediator, DISPATCH_TIME_FOREVER)
            // got awoken by extract()
            buffer.destroy(1)
            buffer.dealloc(1)
            dispatch_set_context(mediator, nil)
            SemaphorePool.enqueue(mediator)
          }
          OSSpinLockLock(&lock)
          return mediator
        }
        else
        { // try the next enqueued reader instead
          continue
        }
      }
    }
    return nil
  }

