Skip to content

Non-blocking foreign IO operations on file descriptors#456

Open
Rewbert wants to merge 9 commits intoaugustss:masterfrom
Rewbert:epoll-foreign-io
Open

Non-blocking foreign IO operations on file descriptors#456
Rewbert wants to merge 9 commits intoaugustss:masterfrom
Rewbert:epoll-foreign-io

Conversation

@Rewbert
Copy link
Copy Markdown
Contributor

@Rewbert Rewbert commented Apr 26, 2026

This PR adds support for non-blocking IO operations. GHC has an IO manager that (to my understanding) makes sure that

  1. sockets are non-blocking and that
  2. blocking operations on them are done in separate threads that will be awoken by the IO manager when their IO events are ready.

Here I've modified the code so that a simpler machinery can be slotted in via the MHS_IO_POLL configuration. With this configuration enabled we add

  1. a new thread state thread_io_wait
  2. new thread fields int fd and int events
    and operations on them.

As we discussed on Friday @augustss, I have tried to lift the thread and queue-specific operations out of the polling code. Since we don't know how many events might be ready, we pass in a callback to io_poll that schedules a ready thread if any exist. Please check whether you like this approach. If io_poll were to return some structure showing what threads are ready, we would have to loop over it outside of the polling machinery. Perhaps that is fine, but it is not what I did here.

I also followed your advice regarding the macos variant. I did this for Linux, but have no mac to try it on. I asked Gemini to propose an implementation for macos, and it dutifully obliged.

I have added a test that spawns a server in a separate thread, and then blocks on it. The main thread issues three heartbeats, showing that it is still runnable, before connecting to the web server. This test is run in CI and pass on both Linux and Macos.

I have written a little light-weight networking package that implements basic socket-based network communications. It works with this addition, and as soon as this (or anything like it) is merged, I will put it on hackage. It compiles and works with both GHC and MHS. (https://github.com/Rewbert/network-light/tree/main). I am already using this for some cool projects, as we've already discussed!

Please advise on any changes you might want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant