lib/finite_machine/threadable.rb in finite_machine-0.1.0 vs lib/finite_machine/threadable.rb in finite_machine-0.2.0
- old
+ new
@@ -1,8 +1,10 @@
# encoding: utf-8
module FiniteMachine
+
+ # A mixin to allow instance methods to be synchronized
module Threadable
module InstanceMethods
@@sync = Sync.new
def sync_exclusive(&block)
@@ -25,11 +27,15 @@
include InstanceMethods
def attr_threadsafe(*attrs)
attrs.flatten.each do |attr|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
- def #{attr}
- sync_shared { @#{attr} }
+ def #{attr}(*args)
+ if args.empty?
+ sync_shared { @#{attr} }
+ else
+ self.#{attr} = args.shift
+ end
end
alias_method '#{attr}?', '#{attr}'
def #{attr}=(value)
sync_exclusive { @#{attr} = value }