# 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)
        @@sync.synchronize(:EX, &block)
      end

      def sync_shared(&block)
        @@sync.synchronize(:SH, &block)
      end
    end

    def self.included(base)
      base.extend ClassMethods
      base.module_eval do
        include InstanceMethods
      end
    end

    module ClassMethods
      include InstanceMethods

      def attr_threadsafe(*attrs)
        attrs.flatten.each do |attr|
          class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
            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 }
            end
          RUBY_EVAL
        end
      end
    end

  end # Threadable
end # FiniteMachine

# encoding: utf-8

module FiniteMachine

  # A mixin to allow instance methods to be synchronized
  module Threadable
    module InstanceMethods
      @@sync = Sync.new

      # Exclusive lock
      #
      # @return [nil]
      #
      # @api public
      def sync_exclusive(&block)
        @@sync.synchronize(:EX, &block)
      end

      # Shared lock
      #
      # @return [nil]
      #
      # @api public
      def sync_shared(&block)
        @@sync.synchronize(:SH, &block)
      end
    end

    # Module hook
    #
    # @return [nil]
    #
    # @api private
    def self.included(base)
      base.extend ClassMethods
      base.module_eval do
        include InstanceMethods
      end
    end

    private_class_method :included

    module ClassMethods
      include InstanceMethods

      # Defines threadsafe attributes for a class
      #
      # @example
      #   attr_threadable :errors, :events
      #
      # @example
      #   attr_threadable :errors, default: []
      #
      # @return [nil]
      #
      # @api public
      def attr_threadsafe(*attrs)
        opts    = attrs.last.is_a?(::Hash) ? attrs.pop : {}
        default = opts.fetch(:default, nil)
        attrs.flatten.each do |attr|
          class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
            def #{attr}(*args)
              value = args.shift
              if value
                self.#{attr} = value
              elsif instance_variables.include?(:@#{attr})
                sync_shared { @#{attr} }
              elsif #{!default.nil?}
                sync_shared { instance_variable_set(:@#{attr}, #{default}) }
              end
            end
            alias_method '#{attr}?', '#{attr}'

            def #{attr}=(value)
              sync_exclusive { @#{attr} = value }
            end
          RUBY_EVAL
        end
      end
    end

  end # Threadable
end # FiniteMachine

Change to allow for default value, add docs.
Change to allow for default value, add docs.
