   {
     if closed { return }
 
+    OSSpinLockLock(&mutex)
     closed = true
+    OSSpinLockUnlock(&mutex)
+
     dispatch_semaphore_signal(empty)
     dispatch_semaphore_signal(filled)
   }

  private let filled = dispatch_semaphore_create(0)!
  private let empty =  dispatch_semaphore_create(1)!

  private var closed = false

  public var isClosed: Bool { return closed }

    if closed && elementCount <= 0 { return nil }

    dispatch_semaphore_wait(filled, DISPATCH_TIME_FOREVER)
    OSSpinLockLock(&mutex)

    if closed && elementCount <= 0
    {
      OSSpinLockUnlock(&mutex)
      dispatch_semaphore_signal(filled)
      return nil
    }
