[Haskell-cafe] ForeignPtrs with liveness dependencies
Simon Marlow
simonmar at microsoft.com
Tue Jul 26 04:52:05 EDT 2005
On 25 July 2005 14:44, Einar Karttunen wrote:
> What is the correct way to express liveness dependencies for
> ForeignPtrs? I am wrapping a C library and need a way to keep
> ForeignPtrs alive until the finalizer for an another ForeignPtr
> has been executed.
>
> Basically I have two types, ForeignPtr A and ForeignPtr B and a
> function associate :: ForeignPtr A -> ForeignPtr B -> IO (). I want
> to keep all of the ForeignPtr Bs associated with a given ForeignPtr A
> alive until its finalizer has been run. The relationship is M:N -
> each ForeignPtr A may be associated with multiple ForeignPtr B and
> each ForeignPtr B may be associated with multiple ForeignPtr A.
>
> GHC documentation tells that touchForeignPtr is not enough as it makes
> no guarantees about when the finalizers are run. If it helps the
> finalizers are C functions which neither block nor perform callbacks
> into Haskell.
You might be able to find more information on this in the mailing list
archives. It's true that touchForeignPtr isn't enough to enforce an
ordering on the running of finalizers, but it *can* be used to express a
liveness relationship between one ForeignPtr and another (ForeignPtr A
is alive if ForeignPtr B is alive). This should be enough if you're
dealing with pointer relationships between memory objects, for example,
where it doesn't matter which one gets freed first when they're both
unreferenced.
If you really do need ordering, maybe it would be possible to use
reference counting in your case?
Cheers,
Simon
More information about the Haskell-Cafe
mailing list