class RxRuby::AsyncLock

Asynchronous lock.

Public Class Methods

new() click to toggle source
# File lib/rx_ruby/concurrency/async_lock.rb, line 9
def initialize
  @queue = []
  @is_acquired = false
  @has_faulted = false
  @gate = Mutex.new
end

Public Instance Methods

clear() click to toggle source

Clears the work items in the queue and drops further work being queued.

# File lib/rx_ruby/concurrency/async_lock.rb, line 49
def clear
  @gate.synchronize do
    @queue = []
    @has_faulted = true
  end
end
wait(&action) click to toggle source
# File lib/rx_ruby/concurrency/async_lock.rb, line 16
def wait(&action)
  @gate.synchronize do
    @queue.push action unless @has_faulted

    if @is_acquired or @has_faulted
      return
    else
      @is_acquired = true
    end
  end

  loop do
    work = nil

    @gate.synchronize do
      work = @queue.shift

      unless work
        @is_acquired = false
        return
      end
    end

    begin
      work.call
    rescue
      clear
      raise
    end
  end
end