Module: Archipelago::Current::Synchronized

Includes:
MonitorMixin
Included in:
Lock, Disco::ServiceLocker, Hashish::BerkeleyHashish, 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



148
149
150
151
152
153
# File 'lib/archipelago/current.rb', line 148

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.



143
144
# File 'lib/archipelago/current.rb', line 143

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.



172
173
174
175
176
177
178
179
# File 'lib/archipelago/current.rb', line 172

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.



157
158
159
160
161
162
163
164
165
# File 'lib/archipelago/current.rb', line 157

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