Module: Archipelago::Current::Synchronized

Includes:
MonitorMixin
Included in:
Lock, Hashish::CachedHashish, Tranny::Transaction
Defined in:
lib/archipelago/current.rb

Overview

A module that will allow any class to synchronize over any other object.

Instance Method Summary collapse

Instance Method Details

#lock_on(object) ⇒ Object

Get a lock for this object



186
187
188
189
190
191
# File 'lib/archipelago/current.rb', line 186

def lock_on(object)
  Thread.exclusive do
    this_lock = @archipelago_current_synchronized_lock_by_object[object] ||= Lock.new
    this_lock.lock
  end
end

#mon_check_ownerObject

We dont care about lock ownership.



181
182
# File 'lib/archipelago/current.rb', line 181

def mon_check_owner
end

#synchronize_on(object, actually = true, &block) ⇒ Object

Makes sure the given block is only run once at a time for this instance and the given object

Optionally do NOT lock, if you dont actually want to.



210
211
212
213
214
215
216
217
# File 'lib/archipelago/current.rb', line 210

def synchronize_on(object, actually = true, &block)
  lock_on(object) if actually
  begin
    return yield
  ensure
    unlock_on(object) if actually
  end
end

#unlock_on(object) ⇒ Object

Release any lock on this object.



195
196
197
198
199
200
201
202
203
# File 'lib/archipelago/current.rb', line 195

def unlock_on(object)
  Thread.exclusive do
    this_lock = @archipelago_current_synchronized_lock_by_object[object] ||= Lock.new
    this_lock.unlock
    if this_lock.mon_entering_queue.empty?
      @archipelago_current_synchronized_lock_by_object.delete(object)
    end
  end
end