Class: RequireHooks::KernelPatch::Features::Locker

Inherits:
Object
  • Object
show all
Defined in:
lib/require-hooks/mode/kernel_patch.rb

Defined Under Namespace

Classes: PathLock

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLocker

Returns a new instance of Locker.



95
96
97
98
# File 'lib/require-hooks/mode/kernel_patch.rb', line 95

def initialize
  @mu = Mutex.new
  @features = {}
end

Instance Attribute Details

#featuresObject (readonly)

Returns the value of attribute features.



93
94
95
# File 'lib/require-hooks/mode/kernel_patch.rb', line 93

def features
  @features
end

#muObject (readonly)

Returns the value of attribute mu.



93
94
95
# File 'lib/require-hooks/mode/kernel_patch.rb', line 93

def mu
  @mu
end

Instance Method Details

#lock_feature(fname) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/require-hooks/mode/kernel_patch.rb', line 100

def lock_feature(fname)
  lock = mu.synchronize do
    features[fname] ||= PathLock.new
  end

  # Can this even happen?
  return yield(true) if lock.resolved?

  # Recursive require
  if lock.owned? && lock.locked?
    warn "loading in progress, circular require considered harmful: #{fname}" if RequireHooks.print_warnings
    return yield(true)
  end

  lock.lock!
  begin
    yield(lock.resolved?).tap do
      lock.resolve!
    end
  ensure
    lock.unlock!

    mu.synchronize do
      features.delete(fname)
    end
  end
end

#locked_feature?(fname) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/require-hooks/mode/kernel_patch.rb', line 128

def locked_feature?(fname)
  mu.synchronize { features.key?(fname) }
end