Paul Brannan wrote:
> On Wed, Nov 10, 2004 at 09:23:04PM +0900, Michael Neumann wrote:
> 
>>Are finalizers (ObjectSpace.define_finalizer) executed atomicly? Or can
>>they be interrupt by other finalizers (or threads)?
> 
> 
> I believe finalizers are only called from the garbage collector, and the
> garbage collector doesn't do anything if called re-entrantly.

hm, if you allocate objects inside the finalizer, the garbage collector 
could be invoked while we're *in* the finalizer.

   def run
     p2 = proc { puts "fin" }

     obj = Object.new
     ObjectSpace.define_finalizer(obj, proc {
       puts "enter obj"
       loop do
         x = Array.new
         ObjectSpace.define_finalizer(x, p2)
       end
       puts "leave obj"
     })
   end

   run

outputs:

  enter obj
  fin
  fin
   .
   .
   .

This usually will not happen using "real" finalizers, but maybe in some 
rare cases... who knows.

So, do I really have to invoke "GC.disable" at the top of my finalizer, 
to be 100% sure, that no other finalizers are invoked while I'm in the 
finalizer?

Regards,

   Michael