class Monitor
Monitors for concurrent access to objects across fibers.
This monitor detects when multiple fibers try to execute methods on the same object simultaneously (actual data races). Sequential access across fibers is allowed - objects can be passed between fibers freely.
Uses TracePoint to track in-flight method calls and detect concurrent access.
Definitions
def initialize
Initialize a new concurrency monitor.
Implementation
def initialize
@guards = ObjectSpace::WeakMap.new # Tracks {object => fiber} or {object => {guard => fiber}}
@mutex = Thread::Mutex.new
@trace_point = nil
end
def enable!
Enable the monitor by activating the TracePoint.
Implementation
def enable!
return if @trace_point
@trace_point = TracePoint.new(:call, :return) do |tp|
if tp.event == :call
check_call(tp)
else
check_return(tp)
end
end
@trace_point.enable
end
def disable!
Disable the monitor by deactivating the TracePoint.
Implementation
def disable!
if trace_point = @trace_point
@trace_point = nil
trace_point.disable
end
end
def transfer(*objects)
Transfer has no effect in concurrency monitoring.
Objects can move freely between fibers. This method exists for backward compatibility but does nothing.
Signature
-
parameter
objectsArray(Object) The objects to transfer (ignored).
Implementation
def transfer(*objects)
# No-op - objects move freely between fibers
end