[GUI] Re: events & callbacks

Daan Leijen daanleijen@xs4all.nl
Tue, 11 Mar 2003 17:02:56 +0100


On Tue, 11 Mar 2003 16:07:59 +0100, George Russell <ger@tzi.de> wrote:

> Daan Leijen wrote:
>> It has nothing to do with the Haskell "callback" model. It is how most GUI libraries just work on the OS level: win32, X, GTK, etc.
>
> I just don't understand this at all.  Either IO actions provoked by
> callbacks run concurrently with other Haskell actions or they do not.  If
> they run concurrently, the "argument/example/supposed bug" is going to
> occur.

On the primitive level, nothing runs concurrent. This means that you
can register other handlers etc. since no other event will be handled while
you are doing haskell work. On the primitive level there is an eventloop
that gets an event from the (OS maintained) event queue, calls haskell
with the event as argument, and when haskell returns it waits again.

One can implement all other models on top of this:
- you just keep this model and use callbacks. This is what GIO, HsGTK and TkGofer do.
- you make the callbacks behave concurrently (this would lead to the
 bug in the example though). How could you do that? well, when in Haskell, you fork
a thread to handle an event and return directly. Would it work? No! because
the eventloop will immediately wait for the next event. As this is a C call,
the haskell runtime system will not run any haskell threads.

Are there solutions? yes! You can for example run haskell threads in idle time or wait until all haskell threads are in a waiting state, before returning to the event loop. Another solution is to run the event loop and Haskell as OS co-threads.
Different solutions are probably appropiate in different situations.

Bottom line though is that all the concurrent stuff can (and must!) be implemented on top of simple non-concurrent callbacks.

In the end, whether events are concurrent or not, becomes a property
of a specific library. A library should therefore be clear about the
semantics of events and whether they can be concurrent of not.
(Examples of concurrent libraries are Haggis and Fran.)

-- Daan.




>
>