Running a "final" finaliser

Simon Marlow simonmar at microsoft.com
Thu Dec 18 13:59:57 EST 2003


 
> I hope this question isn't too stupid, but I can't find
> any obvious way to do this from reading the ghc docs.
> 
> What I want to do is call a final foreign function (a
> library shutdown routine) when Haskell terminates, but
> after all ForeignPtr finalisers have been run.
>
> I suppose I could implement some kind of finaliser
> counter so the last finalizer could tell it was the
> last finaliser and call the shutdown routine, but this
> seems a little awkward, especially as these days the
> FFI requires finalisers to be foreign functions.
> 
> The other possibility that occurs to me is that I call
> performGC at the very end of the program, immediately
> before calling the library shutdown routine. But I'm
> not too sure whether that will guarantee that all
> finalizers have been run even if there are no live
> references to foreign objects at that point. (Using
> GC as described in the "non-stop Haskell" paper it
> seems possible that finalisers won't be run immediately
> in response to performGC.)

Using an explicit reference count sounds fine to me.  The runtime system
doesn't support any ordering constraints between finalizers (it's a
really hard problem in general), so the party line is "you have to code
it up yourself".

Actually, I seem to recall that we were going to disable the running of
finalizers at the end of the program completely, in which case you would
have to add your cleanup code in the main thread, with an appropriate
exception handler.

Cheers,
	Simon


More information about the Glasgow-haskell-users mailing list