Version française
Home     About     Download     Resources     Contact us    
Browse thread
Custom blocks and finalization
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] Custom blocks and finalization
Markus Mottl wrote:

>> Let's have a look at byterun/globroots.c:
> [snip]
>> So... how could caml_remove_global_root() actually trigger a heap GC?
> 
> It certainly doesn't, but it obviously manipulates runtime
> datastructures which may not necessarily be in a state during
> finalization where this is safe.  I'd have to study the runtime code
> in detail to learn more, but maybe the OCaml team can clarify this
> issue more quickly?

I foresee absolutely no problems with registering/unregistering global
roots from a C finalizer.  As the manual states, the big no-no in
C functions attached to custom blocks is allocating in the heap,
either directly or via a callback into Caml or by releasing the global
lock.  Within a finalizer, you should also refrain from raising an
exception, as this would leave the GC is a bizarre state.  But global
roots operations are OK.

As for not using the CAMLparam, etc macros in these functions: since
these functions must not allocate, the macros are certainly not
needed.  I don't think that actually using them could cause a problem,
but that would be silly anyway, so don't use them in this context.

Hope this answers your question.

- Xavier Leroy