Sha256: e186a11461eef3bf27bd35bb55b1b141496e4cbef80a27a8a48e17ed2d3af734

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

module Elevate
  class IOCoordinator
    def self.for_thread
      Thread.current[:io_coordinator]
    end

    def initialize
      @lock = NSLock.alloc.init
      @blocking_operation = nil
      @cancelled = false
    end

    def cancel
      blocking_operation = nil

      @lock.lock()
      @cancelled = true
      blocking_operation = @blocking_operation
      @lock.unlock()

      if blocking_operation
        blocking_operation.cancel()
      end
    end

    def cancelled?
      cancelled = nil

      @lock.lock()
      cancelled = @cancelled
      @lock.unlock()

      cancelled
    end

    def install
      Thread.current[:io_coordinator] = self
    end

    def signal_blocked(operation)
      check_for_cancellation

      @lock.lock()
      @blocking_operation = operation
      @lock.unlock()
    end

    def signal_unblocked(operation)
      @lock.lock()
      @blocking_operation = nil
      @lock.unlock()

      check_for_cancellation
    end

    def uninstall
      Thread.current[:io_coordinator] = nil
    end

    private

    def check_for_cancellation
      raise CancelledError if cancelled?
    end
  end

  class CancelledError < StandardError
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
elevate-0.5.0 lib/elevate/io_coordinator.rb
elevate-0.4.0 lib/elevate/io_coordinator.rb
elevate-0.3.3 lib/elevate/io_coordinator.rb
elevate-0.3.2 lib/elevate/io_coordinator.rb
elevate-0.3.1 lib/elevate/io_coordinator.rb
elevate-0.3 lib/elevate/io_coordinator.rb