Module: RaceGuard::SharedState::MutexStack

Defined in:
lib/race_guard/shared_state/mutex_stack.rb

Overview

Detects whether the current stack is inside MRI Mutex#synchronize (Epic 6.3).

Heuristic: caller_locations frame basename mutex.rb and label related to synchronize. This can miss alternate implementations or differ across Ruby builds.

Class Method Summary collapse

Class Method Details

.mutex_frame?(loc) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
# File 'lib/race_guard/shared_state/mutex_stack.rb', line 19

def mutex_frame?(loc)
  return false unless loc

  lab = loc.label.to_s
  # MRI often reports +Thread::Mutex#synchronize+ as the label (path may be app code,
  # not mutex.rb).
  return true if lab.include?('Mutex#synchronize')

  File.basename(loc.path.to_s) == 'mutex.rb' && synchronize_label?(loc.label)
end

.mutex_protected?(skip_frames: 2) ⇒ Boolean

Starts after this frame and MutexStack caller so any Mutex#synchronize above is visible regardless of TracePoint / watcher depth.

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/race_guard/shared_state/mutex_stack.rb', line 14

def mutex_protected?(skip_frames: 2)
  locs = caller_locations(skip_frames..) || []
  locs.any? { |loc| mutex_frame?(loc) }
end

.synchronize_label?(label) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/race_guard/shared_state/mutex_stack.rb', line 30

def synchronize_label?(label)
  lab = label.to_s
  lab == 'synchronize' || lab.include?('synchronize')
end