Module: RaceGuard::MethodWatch

Defined in:
lib/race_guard/method_watch.rb

Overview

Prepends a thin wrapper around public methods so calls run inside protect. Idempotent per class/method/owner (see registry).

Constant Summary collapse

REGISTRY_MUTEX =
Mutex.new
REGISTRY =
Set.new

Class Method Summary collapse

Class Method Details

.reset_registry!Object

Test helper: clears idempotency keys only (does not remove prepended modules).



32
33
34
# File 'lib/race_guard/method_watch.rb', line 32

def self.reset_registry!
  REGISTRY_MUTEX.synchronize { REGISTRY.clear }
end

.watch(klass, method_name, scope: :auto) ⇒ Object

Raises:

  • (TypeError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/race_guard/method_watch.rb', line 16

def watch(klass, method_name, scope: :auto)
  raise TypeError, 'klass must be a Class or Module' unless klass.is_a?(Module)

  m = method_name.to_sym
  REGISTRY_MUTEX.synchronize do
    owner = MethodResolution.resolve_owner!(klass, m, scope)
    key = [klass.__id__, m, owner]
    return RaceGuard if REGISTRY.include?(key)

    install!(klass, m, owner)
    REGISTRY.add(key)
  end
  RaceGuard
end