Class: Async::Semaphore

Inherits:
Object
  • Object
show all
Defined in:
lib/async/semaphore.rb

Overview

A semaphore is used to control access to a common resource in a concurrent system. A useful way to think of a semaphore as used in the real-world systems is as a record of how many units of a particular resource are available, coupled with operations to adjust that record safely (i.e. to avoid race conditions) as units are required or become free, and, if necessary, wait until a unit of the resource becomes available.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit = 1) ⇒ Semaphore

Returns a new instance of Semaphore.



24
25
26
27
28
# File 'lib/async/semaphore.rb', line 24

def initialize(limit = 1)
  @count = 0
  @limit = limit
  @waiting = []
end

Instance Attribute Details

#countObject (readonly)

The current number of tasks that have acquired the semaphore.



31
32
33
# File 'lib/async/semaphore.rb', line 31

def count
  @count
end

#limitObject (readonly)

The maximum number of tasks that can acquire the semaphore.



34
35
36
# File 'lib/async/semaphore.rb', line 34

def limit
  @limit
end

Instance Method Details

#acquire { ... } ⇒ Object

Acquire the semaphore, block if we are at the limit. If no block is provided, you must call release manually.

Yields:

  • when the semaphore can be acquired

Returns:

  • the result of the block if invoked



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/async/semaphore.rb', line 67

def acquire
  wait
  
  @count += 1
  
  return unless block_given?
  
  begin
    return yield
  ensure
    self.release
  end
end

#async(*args) ⇒ Object

Run an async task. Will wait until the semaphore is ready until spawning and running the task.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/async/semaphore.rb', line 47

def async(*args)
  parent = Task.current
  
  wait
  
  parent.async do |task|
    @count += 1
    
    begin
      yield task, *args
    ensure
      self.release
    end
  end
end

#blocking?Boolean

Whether trying to acquire this semaphore would block.

Returns:

  • (Boolean)


42
43
44
# File 'lib/async/semaphore.rb', line 42

def blocking?
  @count >= @limit
end

#empty?Boolean

Is the semaphore currently acquired?

Returns:

  • (Boolean)


37
38
39
# File 'lib/async/semaphore.rb', line 37

def empty?
  @count.zero?
end

#releaseObject

Release the semaphore. Must match up with a corresponding call to acquire.



82
83
84
85
86
87
88
89
90
# File 'lib/async/semaphore.rb', line 82

def release
  @count -= 1
  
  available = @waiting.pop(@limit - @count)
  
  available.each do |fiber|
    fiber.resume if fiber.alive?
  end
end