Running a "final" finaliser

Adrian Hey ahey at iee.org
Fri Dec 19 11:16:46 EST 2003


Thanks for your reply. I'm afraid it's left me even
more confused about which way to go with this :-(

If it's possible that future Haskell FFI's don't guarantee
that all finalisers are run then this more or less rules
out the use of the reference counting solution (which
wasn't particularly attractive anyway because it needs to
be done in C AFAICS :-). If users who want this behaviour
are required to code it themselves, it seems to require that
they maintain a global list of all allocated ForeignPtrs.
But doing that naively will stop them being garbage collected
at all, unless it's possible to do something clever using weak
pointers. Perhaps it is possible (or maybe some tricks at the
C level could be used) but I think it's a significant extra
burden for FFI users.

Also, while we're talking about this, maybe the semantics
of performGC should be clarified. Does it block until
all GC (and finalisation of garbage ForeignPtrs) is complete?
I would guess this was the original intention, but this
doesn't seem to be consistent with non-stop Haskell. If
it does block, are all Haskell threads blocked, or just
the calling thread?

Also, I could you explain what you mean by a suitable
exception handler? I don't really understand this at all.
I'd expected I may well end up using bracket or similar,
but I'm not sure how exception handling is relevant to
this problem.   
 
Thanks
--
Adrian Hey

On Thursday 18 Dec 2003 1:59 pm, Simon Marlow wrote:
> > 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
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



More information about the Glasgow-haskell-users mailing list