class ObjectFramework::Cancellation
Thread-safe cooperative cancellation and a monotonic-clock deadline. Work must call check! at suitable boundaries to observe either condition.
Public Class Methods
Source
# File lib/object_framework/execution.rb, line 42 def initialize(deadline: nil, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }) @deadline, @clock, @mutex, @cancelled = deadline, clock, Mutex.new, false end
deadline is an absolute monotonic timestamp, or nil for no deadline.
Public Instance Methods
Source
# File lib/object_framework/execution.rb, line 47 def cancel @mutex.synchronize { @cancelled = true } end
Requests cancellation without interrupting or terminating the worker.
Source
# File lib/object_framework/execution.rb, line 61 def check! raise Cancelled, "Operation cancelled" if @mutex.synchronize { @cancelled } limit = deadline raise DeadlineExceeded, "Deadline exceeded" if limit && @clock.call >= limit end
Raises Cancelled if cancellation was requested, or DeadlineExceeded if the monotonic deadline has passed.
Source
# File lib/object_framework/execution.rb, line 51 def deadline @mutex.synchronize { @deadline } end
Source
# File lib/object_framework/execution.rb, line 55 def deadline=(deadline) @mutex.synchronize { @deadline = deadline } end