Class: Tengine::Core::Mutex

Inherits:
Object
  • Object
show all
Defined in:
lib/tengine/core/mutex.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(name, ttl = 2.048) ⇒ Tengine::Core::Mutex

Returns An instance.

Parameters:

  • name (String)

    Mutex name. One process at once can gain a lock against a name.

  • ttl (Numeric) (defaults to: 2.048)

    Time to auto-release a gained lock.

Returns:

Raises:

  • (TypeError)


129
130
131
132
133
134
135
# File 'lib/tengine/core/mutex.rb', line 129

def new name, ttl=2.048
  t = 0.0 + ttl # type check
  raise TypeError, "finite numeric expected (got #{t})" unless t.finite?
  raise ArgumentError, "TTL doesn't make sense." unless t > 0

  return oldnew(Tengine::Core::Mutex::Mutex.find_or_create(name, t), Moped::BSON::ObjectId.new, 0)
end

.oldnewObject



123
# File 'lib/tengine/core/mutex.rb', line 123

alias oldnew new

Instance Method Details

#heartbeatObject

If you need to lock it longer than ttl, call this and you can refresh the ttl.



196
197
198
# File 'lib/tengine/core/mutex.rb', line 196

def heartbeat
  mutex.heartbeat self
end

#synchronize(&block) ⇒ Object

delays until you get a lock.

Raises:

  • (ArgumentError)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/tengine/core/mutex.rb', line 176

def synchronize(&block)
  raise ArgumentError, "no block given" unless block_given?

  if lock
    # OK, locked
    EM.schedule do
      begin
        heartbeat
        yield
      ensure
        unlock
      end
    end
  else
    # NG, try again later
    synchronize_internal mutex.ttl, block
  end
end