I don't think it's so hard, unless I'm missing something.

foreign export dynamic 
        mkFooCB :: (Int -> Int -> IO ()) -> IO Addr

freeHaskellFunctionPtr :: Addr -> IO ()

Now if you want the callback itself to free itself, you say

        do {
                a <- fixIO (\a -> mkFooCB (callBack a));
                register a with the window system
        }

        callBack :: Addr -> Int -> Int -> IO ()
        callBack a x y = do {  unregister a with window system
                                        ... whatever .. }

Isn't that enough?

Simon

> -----Original Message-----
> From: Sven Panne [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, April 13, 1999 10:09 AM
> To: GHC Users
> Subject: Re: Q: Threads in GHC's RTS
> 
> 
> "Sigbjorn Finne (Intl Vendor)" wrote:
> > [...]Good point, I've committed a fix for this.
> 
> Adjustor.c gets more and more impressive.  :-)   BTW: What is the
> schedule for porting (i.e. completely rewriting) Adjustor.c to
> non-Intel platforms?
> 
> > If I understand your scenario right, that only leaves getting at
> > the address of the old-Haskell-callback-to-be-nuked, but it's
> > reachable without too much bother..?
> 
> That heavily depends on the kind of API one is trying to interface
> with. In GLUT "global" callbacks (for keyboard/mouse/timer/...) are
> the easy part, because for any event there is at most one registered
> callback. In these cases, the IORef+unsafePerformIO hack suffices.
> Things get more complicated for per-window callbacks, and I think
> Manuel will have fun with the per-widget callbacks. Some kind of table
> mapping the callback to be unregistered to the Addr from f.e.d. will
> be necessary here. But this is all API-specific, so I fear it's hard
> to come up with a better general design for creating and destroying
> callbacks.
> 
> If you are willing to make the memory management visible in the
> Haskell binding of an API, there's another possibility: Every callback
> is augmented by an additional parameter. To be more concrete, let's
> assume the API has "foo" callbacks, expecting two Ints:
> 
>    foreign export ccall dynamic mkFooCB :: (Int -> Int -> IO 
> ()) -> IO Addr
>    foreign import ccall registerFoo :: Addr -> IO ()
> 
> Then you simply extend your Haskell callback by the f.e.d.-generated
> Addr and tie a knot:
> 
>    myCallback :: Addr -> Int -> Int -> IO ()
>    myCallback self x y = do
>       ...
>       freeHaskellFunctionPtr self
>       ...
> 
>    main = do
>       ...
>       let addr = mkFooCB (myCallback addr)
>       registerFoo addr
> 
> Cheers,
>    Sven
> 
> P.S.: Something completely unrelated: Why is majordomo.haskell.org
>       expanded to scarlet.cam.uk.eu.microsoft.com in the To:-field?
>       IMHO this is a bad idea, e.g. when people cut-and-paste this
>       into their address books and scarlet is renamed to e.g. 
> rhett. :-)
> 
> P.P.S.: Adding a prefix in the Subject:-field (e.g. [ghcu]) would make
>         mail filters work more reliably. Lots of other mailing lists
>         do something similar.
> -- 
> Sven Panne                                        Tel.: 
> +49/89/2178-2235
> LMU, Institut fuer Informatik                     FAX : 
> +49/89/2178-2211
> LFE Programmier- und Modellierungssprachen              
> Oettingenstr. 67
> mailto:[EMAIL PROTECTED]            
> D-80538 Muenchen
> http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne
> 

Reply via email to