Waiting on a file descriptor without blocking the event loop #1320
-
Hi, I'm writing a small gui library experiment in janet and C, and on the C side I connect to wayland compositor using wl_display_connect() and then get the corresponding fd with wl_display_get_fd() and need to call wl_display_dispatch() whenever its possible to read from the fd. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
Beta Was this translation helpful? Give feedback.
-
As far as the original issue (100% cpu issue aside), the new code to do this in C is as follows: static void callback(JanetFiber *fiber, JanetAsyncEvent event) {
switch(event) {
default:
return;
case JANET_ASYNC_EVENT_READ:
case JANET_ASYNC_EVENT_HUP:
case JANET_ASYNC_EVENT_ERR:
break;
}
janet_schedule(fiber, janet_wrap_nil());
janet_async_end(fiber);
}
JANET_FN(cfun_wayland_wait,
"(wayland/wait stream)",
"Wait a stream to be ready for reading, but do not actually read from the stream.") {
janet_fixarity(argc, 1);
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
// Optionally do an early check here to see if the file descriptor is ready using a single call to poll or similar.
janet_async_start(stream, JANET_ASYNC_LISTEN_READ, callback, NULL);
} This is essentially what Then, in your script, it would look something like: # Get my stream somehow using wl_display_get_fd
(def my-stream (wayland/connect ...))
(wayland/wait my-stream)
(wayland/display-dispatch) |
Beta Was this translation helpful? Give feedback.
As far as the original issue (100% cpu issue aside), the new code to do this in C is as follows: