Re: LazyLoad
- From: "Erik Veenstra" <google@xxxxxxxxxxxxxxx>
- Date: 24 Jan 2006 17:10:53 -0800
> On the other end of things -- one issue that I've still got
> to address for myself in lazy.rb is threadsafety. So you've
> improved on mine in that respect.
Well, there is a problem with my locking. Do you remember that
a set both Branch@items and Branch@snapshots in Branch#load,
like this?:
@snapshots = lazy {load; @snapshots}
@items = lazy {load; @items}
Both Lazy objects don't share the same Mutex object, so,
effectively, under these circumstances, there's no locking.
So we can get of those parms thing and introduce an external
Mutex object:
mutex = Mutex.new
@snapshots = lazy(mutex) {load; @snapshots}
@items = lazy(mutex) {load; @items}
Thoughts?
gegroet,
Erik V. - http://www.erikveen.dds.nl/
----------------------------------------------------------------
require "thread"
module EV
class Lazy
instance_methods.each do |method|
undef_method(method) unless method =~ /^__/
end
def initialize(mutex=Mutex.new, &block)
@mutex = mutex
@block = block
@mutex = Mutex.new
@evaluated = false
@exception = nil
@real_object = nil
end
def method_missing(method_name, *parms, &block)
@mutex.synchronize do
begin
@real_object = @block.call() unless @evaluated
rescue Exception => e
@exception = e
ensure
@evaluated = true
end
end
raise @exception if @exception
@real_object.send(method_name, *parms, &block)
end
end
end
module Kernel
def lazy(*parms, &block)
EV::Lazy.new(*parms, &block)
end
end
----------------------------------------------------------------
.
- Follow-Ups:
- Re: LazyLoad
- From: mental
- Re: LazyLoad
- References:
- LazyLoad
- From: Erik Veenstra
- Re: LazyLoad
- From: Dave Burt
- Re: LazyLoad
- From: Erik Veenstra
- Re: LazyLoad
- From: mental
- Re: LazyLoad
- From: Erik Veenstra
- Re: LazyLoad
- From: mental
- LazyLoad
- Prev by Date: Re: How to access a attributes from the parent class?
- Next by Date: Re: Python / ruby command line script?
- Previous by thread: Re: LazyLoad
- Next by thread: Re: LazyLoad
- Index(es):
Relevant Pages
|