Equivalent of withForeignPtr for System.Mem.Weak?

I have an object to which I have added one or more finalizers via addFinalizer from System.Mem.Weak. I would like to have a function that allows me to make use of the object within a block of IO code, and guarantee that the finalizer(s) will not be called during the code block -- sort of like withForeignPtr, but for arbitrary objects. Does such a function exist, and if not, how could I write one? I can imagine writing something like: \begin{code} withObject :: a -> IO b -> IO b withObject obj action = do result <- action touchObject obj -- touchObject :: a -> IO () return result \end{code} However, I don't know how I should implement touchObject and be confident that it won't ever be optimized out. Any suggestions? Cheers, -Matt

On Sun, Oct 31, 2010 at 10:14 PM, Matthew Steele
I have an object to which I have added one or more finalizers via addFinalizer from System.Mem.Weak. I would like to have a function that allows me to make use of the object within a block of IO code, and guarantee that the finalizer(s) will not be called during the code block -- sort of like withForeignPtr, but for arbitrary objects. Does such a function exist, and if not, how could I write one?
I can imagine writing something like:
\begin{code} withObject :: a -> IO b -> IO b withObject obj action = do result <- action touchObject obj -- touchObject :: a -> IO () return result \end{code}
CC'ing GHC-users list. The 'primitive' package has a general purpose 'touch' function: http://hackage.haskell.org/packages/archive/primitive/0.3.1/doc/html/Control... But it doesn't state what guarantees are provided by the function. Antoine
participants (2)
-
Antoine Latter
-
Matthew Steele