------art_91863_6009548.1181411678663
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Prodding it further I could come up with a method synchronizer.

For any class where method synchronization is required, mixin the
MethodSynchronizer and implement a class level method sync_methods providing
an array of methods to be
synchronized.

#-----------------------

module MethodSynchronizer

  def MethodSynchronizer.included(into)
    into.sync_methods.each do |m|
      MethodSynchronizer.wrap_method(into, m)
    end
  end

  def MethodSynchronizer.wrap_method(klass, meth)
    klass.class_eval do
      alias_method "__nonsync_#{meth}", "#{meth}"
      require 'thread'
      define_method(meth) do |*args|
        @__ms_lock  utex.new unless @__ms_lock
        @__ms_lock.synchronize do
          self.send("__nonsync_#{meth}",*args)
        end
      end
    end
  end
end
#-------------------------

Usage e.g.

  class D
    def self.sync_methods
      [:m1, :m2]
    end
      def m1
        puts "hello"
      end

      def m2
        puts "bye"
      end

      def m3
       puts "not synchronized"
      end
    include MethodSynchronizer
  end

Comments solicited.

- Nasir





On 6/8/07, Nasir Khan <rubylearner / gmail.com> wrote:
> Actually facets/more/synchash.rb does what I was looking for hash.
>
> Thanks
>
> On 6/8/07, Nasir Khan <rubylearner / gmail.com> wrote:
> > Yeah I see what you guys mean...I was a little delusional. Now I
> > realize that I was looking for basically something equivalent to
> > Java's -
> > Collections.synchronizedMap(new HashMap(...))
> > ...
> > Thanks
> >
> >
> >
> >
> > On 6/8/07, MenTaLguY <mental / rydia.net> wrote:
> > > On Sat, 9 Jun 2007 06:54:04 +0900, Nasir Khan <rubylearner / gmail.com>
wrote:
> > > > There is no big picture.
> > >
> > > Since you had asked for feedback on anything you'd missed, I was
trying to find out if was some mitigating factor in the specific way your
program worked (the one for which you originally wrote this code), before
telling you that it won't work.
> > >
> > > But -- unless the objects the attributes were set to were never
modified, even if they didn't have the thread safety problems you already
noted, the accessors generated still couldn't ensure thread safety.  If
thread 1 calls obj.some_accessor.foo, and thread 2 calls
obj.some_accessor.bar (where #bar is some mutating method), some_accessor
being synchronized simply _will not_ protect you.
> > >
> > > -mental
> > >
> > >
> > >
> >
>
>

------art_91863_6009548.1181411678663--