Sha256: 09b03be5c2a38f3e8f8d2b02239f665d3a550e7654f481dac8c2d4d0e3f3a8a2

Contents?: true

Size: 1.53 KB

Versions: 8

Compression:

Stored size: 1.53 KB

Contents

# Author:: Joel Friedman and Patrick Farley
#
# This module goes hand in hand with +Mailbox+.
# It simplifies concurrecncy within your 
# JRuby applications.
module Synchronized
  
  private
  
  def __mutex__
    @mutex ||= Mutex.new
  end
  
  def self.included(base)
    base.extend(Synchronized::ClassMethods)
  end
  
  module ClassMethods
    
    # Notify +Mailbox+ that the next method added
    # will be +synchronized+. 
    #
    # This guarentees:
    #     1. Two invocations of this method will not interleave and 
    #     2. a happens-before relationship is established with any subsequent invocation.
    # http://java.sun.com/docs/books/tutorial/essential/concurrency/syncmeth.html
    def synchronized
      @synchronized = true
    end
    
    private
    
    def method_added(method_name, &block)
      return super unless __synchronized__ == true
      @synchronized = false
      __synchronize__(method_name)
      super
    end
    
    
    def __synchronize__(method_name)
      return super if __is_adding_synchronized_to_method__
      
      alias_method :"__#{method_name}__", method_name
      @is_adding_synchronized_to_method = true
      

      define_method( method_name, lambda do |*args| 
        __mutex__.synchronize { self.send(:"__#{method_name}__", *args ) }  
      end )
      
      @is_adding_synchronized_to_method = false
    end
    
    def __synchronized__
      @synchronized ||= false
    end

    def __is_adding_synchronized_to_method__
      @is_adding_synchronized_to_method ||= false
    end
    
  end
  
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mailbox-0.2.4 lib/synchronized.rb
mailbox-0.2.3 lib/synchronized.rb
mailbox-0.2.2 lib/synchronized.rb
mailbox-0.2.1 lib/synchronized.rb
mailbox-0.2.0 lib/synchronized.rb
mailbox-0.1.7 lib/synchronized.rb
mailbox-0.1.6 lib/synchronized.rb
mailbox-0.1.4 lib/synchronized.rb