Concurrency (was: Re: [GUI] Re: events & callbacks)

Wolfgang Thaller wolfgang.thaller@gmx.net
Thu, 13 Mar 2003 14:51:13 +0100


Axel Simon wrote:

> I thought this would come for free with the FFI if we wrap all
> GUI calls with
>
> foreign ccall thread "GUI" blah...
>
> which means all calls are done from a single OS thread called "GUI". I
> suspect this didn't wind up in the FFI, or did it?

No, there's no such thing.

> I am not on the FFI list. Could you explain what threadsafe means in a
> call to C? I though you would specify a thread name each time you call 
> C
> and the Haskell RTS would make sure that you always call C with the 
> same
> OS thread.

No. Threadsafe (which is part of the FFI addendum) means that the RTS 
should do everything that's necessary so that other threads won't block 
during a lengthy foreign call.
For example:

foreign import ccall threadsafe "DoALotOfWorkInC" doALotOfWorkInC :: IO 
()

foo = forkIO doALotOfWorkInHaskell >> doALotOfWorkInC

Without the "threadsafe" attribute, doALotOfWorkInHaskell would get 
hardly any time to run. With "threadsafe", both would continue to run.

One of the most efficient ways to implement this feature is to switch 
the Haskell runtime from one thread to another. This has been 
implemented in GHC (as an optional feature, disabled by default) since 
version 5.02 or even earlier.
It still is substantially faster than using OS threads directly, but 
Haskell programmers should no longer need to be aware of the fact that 
Haskell threads are not OS threads.

Axel Simon wrote:

> 3) if the underlying Haskell RTS is single threaded, the library should
> automatically call "yield" whenever it is idle to ensure that forked
> Haskell processes continue to run.

Agreed. The idle callback will waste a little time, but I think that's 
worth it.

Cheers,

Wolfgang