Class: RxRuby::AsyncLock

Inherits:
Object
  • Object
show all
Defined in:
lib/rx_ruby/concurrency/async_lock.rb

Overview

Asynchronous lock.

Instance Method Summary collapse

Constructor Details

#initializeAsyncLock

Returns a new instance of AsyncLock.



9
10
11
12
13
14
# File 'lib/rx_ruby/concurrency/async_lock.rb', line 9

def initialize
  @queue = []
  @is_acquired = false
  @has_faulted = false
  @gate = Mutex.new
end

Instance Method Details

#clearObject

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



49
50
51
52
53
54
# File 'lib/rx_ruby/concurrency/async_lock.rb', line 49

def clear
  @gate.synchronize do
    @queue = []
    @has_faulted = true
  end
end

#wait(&action) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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