Class: PromptObjects::Execution::PermitPool
- Inherits:
-
Object
- Object
- PromptObjects::Execution::PermitPool
- Defined in:
- lib/prompt_objects/execution/permit_pool.rb
Overview
A small counting semaphore used for scheduler resources that must remain distinct (workers, providers, and later resource leases).
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
Instance Method Summary collapse
- #acquire ⇒ Object
- #available ⇒ Object
-
#initialize(limit) ⇒ PermitPool
constructor
A new instance of PermitPool.
- #release ⇒ Object
- #with ⇒ Object
Constructor Details
#initialize(limit) ⇒ PermitPool
Returns a new instance of PermitPool.
8 9 10 11 12 13 14 15 |
# File 'lib/prompt_objects/execution/permit_pool.rb', line 8 def initialize(limit) raise ArgumentError, "limit must be positive" unless limit.positive? @limit = limit @available = limit @mutex = Mutex.new @condition = ConditionVariable.new end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
17 18 19 |
# File 'lib/prompt_objects/execution/permit_pool.rb', line 17 def limit @limit end |
Instance Method Details
#acquire ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/prompt_objects/execution/permit_pool.rb', line 26 def acquire @mutex.synchronize do @condition.wait(@mutex) until @available.positive? @available -= 1 end true end |
#available ⇒ Object
44 45 46 |
# File 'lib/prompt_objects/execution/permit_pool.rb', line 44 def available @mutex.synchronize { @available } end |
#release ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/prompt_objects/execution/permit_pool.rb', line 34 def release @mutex.synchronize do raise ThreadError, "permit pool released without an acquisition" if @available >= @limit @available += 1 @condition.signal end true end |
#with ⇒ Object
19 20 21 22 23 24 |
# File 'lib/prompt_objects/execution/permit_pool.rb', line 19 def with acquire yield ensure release end |