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



121
122
123
124
125
126
# File 'lib/archipelago/current.rb', line 121

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.



116
117
# File 'lib/archipelago/current.rb', line 116

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.



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

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.



130
131
132
133
134
135
136
137
138
# File 'lib/archipelago/current.rb', line 130

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