   the first successful write operation closes the channel.
 */
 
-public final class SingletonChan<T>: pthreadChan<T>
+final public class SingletonChan<T>: Chan<T>
 {
   public class func Make() -> (tx: Sender<T>, rx: Receiver<T>)
   {

    return Make()
  }

  private var element: T?

  // housekeeping variables

  private var writerCount: Int64 = -1
  private var readerCount: Int64 = -1

  private var elementsWritten: Int64 = -1
  private var elementsRead: Int64 = -1

  // Initialization and destruction

  public override init()
  {
    element = nil
    super.init()
  }

  // Computed property accessors

  final override var isEmpty: Bool
  {
    return elementsWritten <= elementsRead
  }

  final override var isFull: Bool
  {
    return elementsWritten > elementsRead
  }

  /**

  {
    let writer = OSAtomicIncrement64Barrier(&writerCount)

    if writer > 1
    { // if this is not the first writer, the call is happening too late.
      return
    }

    element = newElement
    close() // also increments the 'barrier' semaphore
  }

  /**
    Return the element from the channel.

    If the channel is empty, this call will block.
    If the channel is empty and closed, this will return nil.

    :return: the element transmitted through the channel.
  */

  override func get() -> T?
  {
    if !closed
    {
      dispatch_semaphore_wait(barrier, DISPATCH_TIME_FOREVER)
    }

    let reader = OSAtomicIncrement64Barrier(&readerCount)

    if reader == 1
    {
      if let e = element
      {
        element = nil
        dispatch_semaphore_signal(barrier)
        return e
      }
    }

    // if this is not the first reader, too late.
    dispatch_semaphore_signal(barrier)
    return nil
  }
}
