[Haskell-cafe] Re: FFI and callbacks

Duncan Coutts duncan.coutts at worcester.oxford.ac.uk
Mon Jul 25 11:05:15 EDT 2005


On Mon, 2005-07-25 at 13:47 +0000, John Goerzen wrote:
> On 2005-07-25, Simon Marlow <simonmar at microsoft.com> wrote:
> > On 23 July 2005 03:38, Duncan Coutts wrote:
> > Thanks for describing the problem in detail, I understand it better now.
> > I think it comes down to this conflict:
> >
> >  - you want to take advantage of the fact that GHC has lightweight
> >    "green" threads in order to do multithreading within a single OS
> >    thread, but
> >
> >  - our "bound threads" design does not require the implementation
> >    to support lightweight threads, and hence doesn't let the
> >    programmer take advantage of them.
> 
> In my particular case, the C library isn't aware of threading at all.
> While it may be safe to call it from any arbitrary Haskell thread, it
> would not be safe to call it from two threads simultaneously.

That's ok. Without the threaded rts, you get just one OS thread being
used to make the foreign calls.

> It seems that some sort of global lock -- implemented as a global
> variable of some sort, I guess -- is necessary.  I'm not sure exactly
> how to accomodate that.
> 
> It seems the current model doesn't work all that well in this scenario,
> unless I'm missing something?

So long as you can arrange for your C lib to call back to Haskell
reasonably often you can use the same solution as Gtk2Hs uses.

I've outlined the technique here (mostly on page 2 for implementation
notes):

http://haskell.org/gtk2hs/archives/2005/07/24/writing-multi-threaded-guis/

Duncan



More information about the Haskell-Cafe mailing list