Class: Async::Semaphore
- Inherits:
-
Object
- Object
- Async::Semaphore
- Defined in:
- lib/async/semaphore.rb
Overview
A synchronization primitive, which limits access to a given resource.
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
The current number of tasks that have acquired the semaphore.
-
#limit ⇒ Object
readonly
The maximum number of tasks that can acquire the semaphore.
-
#waiting ⇒ Object
readonly
The tasks waiting on this semaphore.
Instance Method Summary collapse
-
#acquire ⇒ Object
Acquire the semaphore, block if we are at the limit.
-
#async(*arguments, parent: (@parent or Task.current), **options) ⇒ Object
Run an async task.
-
#blocking? ⇒ Boolean
Whether trying to acquire this semaphore would block.
-
#empty? ⇒ Boolean
Is the semaphore currently acquired?.
-
#initialize(limit = 1, parent: nil) ⇒ Semaphore
constructor
A new instance of Semaphore.
-
#release ⇒ Object
Release the semaphore.
Constructor Details
#initialize(limit = 1, parent: nil) ⇒ Semaphore
Returns a new instance of Semaphore.
12 13 14 15 16 17 18 |
# File 'lib/async/semaphore.rb', line 12 def initialize(limit = 1, parent: nil) @count = 0 @limit = limit @waiting = [] @parent = parent end |
Instance Attribute Details
#count ⇒ Object (readonly)
The current number of tasks that have acquired the semaphore.
21 22 23 |
# File 'lib/async/semaphore.rb', line 21 def count @count end |
#limit ⇒ Object (readonly)
The maximum number of tasks that can acquire the semaphore.
24 25 26 |
# File 'lib/async/semaphore.rb', line 24 def limit @limit end |
#waiting ⇒ Object (readonly)
The tasks waiting on this semaphore.
27 28 29 |
# File 'lib/async/semaphore.rb', line 27 def waiting @waiting 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/async/semaphore.rb', line 58 def acquire wait @count += 1 return unless block_given? begin return yield ensure self.release end end |
#async(*arguments, parent: (@parent or Task.current), **options) ⇒ Object
Run an async task. Will wait until the semaphore is ready until spawning and running the task.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/async/semaphore.rb', line 40 def async(*arguments, parent: (@parent or Task.current), **) wait parent.async(**) do |task| @count += 1 begin yield task, *arguments ensure self.release end end end |
#blocking? ⇒ Boolean
Whether trying to acquire this semaphore would block.
35 36 37 |
# File 'lib/async/semaphore.rb', line 35 def blocking? @count >= @limit end |
#empty? ⇒ Boolean
Is the semaphore currently acquired?
30 31 32 |
# File 'lib/async/semaphore.rb', line 30 def empty? @count.zero? end |
#release ⇒ Object
Release the semaphore. Must match up with a corresponding call to ‘acquire`. Will release waiting fibers in FIFO order.
73 74 75 76 77 78 79 80 81 |
# File 'lib/async/semaphore.rb', line 73 def release @count -= 1 while (@limit - @count) > 0 and fiber = @waiting.shift if fiber.alive? Fiber.scheduler.resume(fiber) end end end |