Handling multiple fds with GHC

Markus Ongyerth ongy44 at gmail.com
Wed Oct 7 07:49:16 UTC 2015


the last few days, I tried to get an IO-Event system running with GHC i.e.
trigger an IO action when there is data to read from a fd.
I looked at a few different implementations, but all of them have some

 * using select package
   - This uses the select syscall. select is rather limited (fd cannot be

 * using GHC.Event
   - GHC.Event is broken in 7.10.1 (unless unsafeCoerce and a hacky trick
are used)
   - GHC.Event is GHC internal according to hackage
   - Both Network libraries I looked at (networking (Network.Socket) and
socket (System.Socket)) crash the application with GHC.Event
    - with 7.8+ I didn't see a way to create your own EventManager, so it
only works with -threaded

 * using forkIO and threadWaitRead for each fd in a loop
    - needs some kind of custom control structure around it
    - uses a separate thread for each fd
    - might become pretty awkward to handle multiple events

 * using poll package
   - blocks in a safe foreign call
   - needs some kind of wrapper

>From the above list, GHC.Event isn't usable (for me) right now. It would
require some work for my usecase.
The select option is usable, but suffers from the same problems as poll +
the limitation mentioned, so it is strictly worse.

This leaves me with two options: poll and forkIO + blocking.

Those are based on two completely different approaches to event handling..

poll can be used in a rather classic event handling system with a main loop
that blocks until an event occurs (or a timeout triggers) and handles the
event in the loop.
forkIO + blocking is closer to registering an action later that should be
triggered by an event.

My main questions right now are:
1. How bad is it for the (non-threaded) runtime to be blocking in a foreign
call most of the time?
2. How significant will the overhead be for the forkIO version?
3. Is there a *good* way to use something like threadWaitRead that allows
to wake up on other events as well?
4. Is there a better way to handle multiple fds that may get readable data
at any time, in Haskell/with GHC right now?

Thanks in advance,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/glasgow-haskell-users/attachments/20151007/e70380e5/attachment.html>

More information about the Glasgow-haskell-users mailing list