Control.Concurrent export/import weirdness

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Control.Concurrent export/import weirdness

Jason Dusek
  Why is this function exported then imported?

    foreign export ccall forkOS_entry
        :: StablePtr (IO ()) -> IO ()

    foreign import ccall "forkOS_entry" forkOS_entry_reimported
        :: StablePtr (IO ()) -> IO ()

    forkOS_entry :: StablePtr (IO ()) -> IO ()
    forkOS_entry stableAction = do
            action <- deRefStablePtr stableAction
            action


--
Jason Dusek
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Control.Concurrent export/import weirdness

Bertram Felgenhauer-2
Jason Dusek wrote:
>   Why is this function exported then imported?

It causes the RTS to create a bound thread to run code in:
(reordering the code slightly)

>     foreign import ccall "forkOS_entry" forkOS_entry_reimported
>         :: StablePtr (IO ()) -> IO ()

This is a safe call, so it suspends the currently running RTS thread,
and then calls the forkOS_entry foreign function.

>     foreign export ccall forkOS_entry
>         :: StablePtr (IO ()) -> IO ()

The generated stub creates a new, bound RTS thread, and then calls the
forkOS_entry Haskell function.

>     forkOS_entry :: StablePtr (IO ()) -> IO ()
>     forkOS_entry stableAction = do
>             action <- deRefStablePtr stableAction
>             action

And this, finally, runs an action in the new thread.

See also section 3.4 of the "Extending the Haskell FFI with Concurrency"
paper,

   http://research.microsoft.com/en-us/um/people/simonpj/Papers/conc-ffi/index.htm
   (http://tinyurl.com/cocex7)

HTH,

Bertram
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe