 
   final override func close()
   {
-    if closed { return }
-
-    OSSpinLockLock(&wlock)
-    closed = true
-    OSSpinLockUnlock(&wlock)
-
-    filled.signal()
-    empty.signal()
+    if OSAtomicCompareAndSwapLongBarrier(0, 1, &closed)
+    {
+      filled.signal()
+      empty.signal()
+    }
   }
 
   /**


  final override func put(newElement: T) -> Bool
  {
    if closed { return false }

    empty.wait()
    OSSpinLockLock(&wlock)

    if !closed
    {
      buffer.advancedBy(tail&mask).initialize(newElement)
      tail = tail &+ 1

      OSSpinLockUnlock(&wlock)
      filled.signal()
      return true
    }
    else
    {
      OSSpinLockUnlock(&wlock)
      empty.signal()
      return false
    }

  {
    while tail &- head > 0
    {
      OSAtomicIncrementLongBarrier(&head)
      buffer.advancedBy(head&mask).destroy()
      empty.signal()
    }
    buffer.dealloc(mask+1)
    empty.destroy()
    filled.destroy()
  }
