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



151
152
153
154
155
156
# File 'lib/archipelago/current.rb', line 151

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.



146
147
# File 'lib/archipelago/current.rb', line 146

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.



175
176
177
178
179
180
181
182
# File 'lib/archipelago/current.rb', line 175

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.



160
161
162
163
164
165
166
167
168
# File 'lib/archipelago/current.rb', line 160

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