Class: Async::Background::Runtime::Semaphore
- Inherits:
-
Object
- Object
- Async::Background::Runtime::Semaphore
- Defined in:
- lib/async/background/runtime/semaphore.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
Instance Method Summary collapse
- #acquire ⇒ Object
- #available ⇒ Object
-
#initialize(limit) ⇒ Semaphore
constructor
A new instance of Semaphore.
- #release ⇒ Object
- #waiting ⇒ Object
Constructor Details
#initialize(limit) ⇒ Semaphore
Returns a new instance of Semaphore.
9 10 11 12 13 14 15 |
# File 'lib/async/background/runtime/semaphore.rb', line 9 def initialize(limit) @limit = Integer(limit) raise ArgumentError, 'limit must be >= 1' unless @limit.positive? @available = @limit @waiting = WaitList.new end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
7 8 9 |
# File 'lib/async/background/runtime/semaphore.rb', line 7 def limit @limit end |
Instance Method Details
#acquire ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/async/background/runtime/semaphore.rb', line 20 def acquire wait return @available unless block_given? begin yield ensure release end end |
#available ⇒ Object
17 |
# File 'lib/async/background/runtime/semaphore.rb', line 17 def available = @available |
#release ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/async/background/runtime/semaphore.rb', line 31 def release while (node = @waiting.shift) node.grant! return @available if node.resume end @available += 1 @available end |
#waiting ⇒ Object
18 |
# File 'lib/async/background/runtime/semaphore.rb', line 18 def waiting = @waiting.size |