Class: PromptObjects::Execution::ResourceLockManager
- Inherits:
-
Object
- Object
- PromptObjects::Execution::ResourceLockManager
- Defined in:
- lib/prompt_objects/execution/resource_lock_manager.rb
Overview
Atomically leases a set of resource keys. All-or-nothing acquisition avoids deadlocks when two tool calls need overlapping key sets.
Defined Under Namespace
Classes: LockTimeoutError
Instance Method Summary collapse
-
#initialize ⇒ ResourceLockManager
constructor
A new instance of ResourceLockManager.
- #locked_keys ⇒ Object
- #with(resource_keys, timeout: 30, cancellation_token: nil) ⇒ Object
Constructor Details
#initialize ⇒ ResourceLockManager
Returns a new instance of ResourceLockManager.
13 14 15 16 17 |
# File 'lib/prompt_objects/execution/resource_lock_manager.rb', line 13 def initialize @leased = Set.new @mutex = Mutex.new @condition = ConditionVariable.new end |
Instance Method Details
#locked_keys ⇒ Object
30 31 32 |
# File 'lib/prompt_objects/execution/resource_lock_manager.rb', line 30 def locked_keys @mutex.synchronize { @leased.to_a.sort } end |
#with(resource_keys, timeout: 30, cancellation_token: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/prompt_objects/execution/resource_lock_manager.rb', line 19 def with(resource_keys, timeout: 30, cancellation_token: nil) keys = Array(resource_keys).compact.map(&:to_s).uniq.sort return yield if keys.empty? acquire(keys, timeout: timeout, cancellation_token: cancellation_token) acquired = true yield ensure release(keys) if acquired end |