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.
29 30 31 32 33 34 35 |
# File 'lib/async/semaphore.rb', line 29 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.
38 39 40 |
# File 'lib/async/semaphore.rb', line 38 def count @count end |
#limit ⇒ Object (readonly)
The maximum number of tasks that can acquire the semaphore.
41 42 43 |
# File 'lib/async/semaphore.rb', line 41 def limit @limit end |
#waiting ⇒ Object (readonly)
The tasks waiting on this semaphore.
44 45 46 |
# File 'lib/async/semaphore.rb', line 44 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.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/async/semaphore.rb', line 75 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.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/async/semaphore.rb', line 57 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.
52 53 54 |
# File 'lib/async/semaphore.rb', line 52 def blocking? @count >= @limit end |
#empty? ⇒ Boolean
Is the semaphore currently acquired?
47 48 49 |
# File 'lib/async/semaphore.rb', line 47 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.
90 91 92 93 94 95 96 97 98 |
# File 'lib/async/semaphore.rb', line 90 def release @count -= 1 while (@limit - @count) > 0 and fiber = @waiting.shift if fiber.alive? Fiber.scheduler.resume(fiber) end end end |