class Ordered
Ordered timing strategy wrapper that preserves FIFO ordering.
This wrapper delegates to any timing strategy but ensures that tasks acquire capacity in the order they requested it, preventing starvation of high-cost operations by continuous low-cost operations.
Definitions
def initialize(delegate)
Initialize ordered timing wrapper.
Signature
-
parameter
delegate
#acquire, #wait, #maximum_cost
The timing strategy to wrap.
Implementation
def initialize(delegate)
@delegate = delegate
@mutex = Mutex.new
end
def maximum_cost
Get maximum cost from delegate strategy.
Signature
-
returns
Numeric
Maximum supported cost.
Implementation
def maximum_cost
@delegate.maximum_cost
end
def acquire(cost = 1)
Record acquisition in delegate strategy.
Signature
-
parameter
cost
Numeric
Cost of the operation.
Implementation
def acquire(cost = 1)
@delegate.acquire(cost)
end
def wait(mutex, deadline = nil, cost = 1)
Wait with FIFO ordering preserved.
Signature
-
parameter
mutex
Mutex
Mutex to release during sleep.
-
parameter
deadline
Deadline, nil
Deadline for timeout.
-
parameter
cost
Numeric
Cost of the operation.
-
returns
Boolean
True if acquired, false if timed out.
Implementation
def wait(mutex, deadline = nil, cost = 1)
@mutex.synchronize do
@delegate.wait(mutex, deadline, cost)
end
end
def statistics
Get current timing strategy statistics from delegate.
Signature
-
returns
Hash
Statistics hash with current state.
Implementation
def statistics
@delegate.statistics.tap do |statistics|
statistics[:ordered] = true
end
end