lib/synchronizable.rb in synchronizable-0.0.1 vs lib/synchronizable.rb in synchronizable-0.0.2
- old
+ new
@@ -1,6 +1,6 @@
-require 'thread'
+require 'monitor'
require 'synchronizable/version'
# Synchronizable is intended to be injected into objects via Object#extend.
# After Object#extend(Synchronizable) is performed, the object's original
# methods will become synchronized via a per-instance lock.
@@ -18,13 +18,21 @@
__lock.synchronize do
super(*args, &block)
end
end
end
+
+ # define synchronize method that executes a block
+ # protected with the internal instance lock
+ obj.define_singleton_method(:synchronize) do |&block|
+ __lock.synchronize(&block)
+ end
end
private
def __lock
- @__lock ||= Mutex.new
+ # Monitor is used instead of Mutex in order to support
+ # re-entrant locking
+ @__lock ||= Monitor.new
end
end