diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/include/kogata/mainloop.h | 2 | ||||
-rw-r--r-- | src/lib/libkogata/mainloop.c | 20 |
2 files changed, 18 insertions, 4 deletions
diff --git a/src/lib/include/kogata/mainloop.h b/src/lib/include/kogata/mainloop.h index 2b447ef..6352916 100644 --- a/src/lib/include/kogata/mainloop.h +++ b/src/lib/include/kogata/mainloop.h @@ -12,6 +12,7 @@ typedef struct mainloop_fd mainloop_fd_t; typedef void (*buf_full_callback_t)(mainloop_fd_t *fd); typedef void (*fd_error_callback_t)(mainloop_fd_t *fd); +typedef void (*idle_callback_t)(void* data); typedef struct { size_t size, written; @@ -41,6 +42,7 @@ void mainloop_rm_fd(mainloop_fd_t* fd); void mainloop_expect(mainloop_fd_t *fd, void* buf, size_t size, buf_full_callback_t cb); bool mainloop_nonblocking_write(mainloop_fd_t *fd, void* buf, size_t size, bool must_free_buf); +void mainloop_when_idle(idle_callback_t cb, void* data); void mainloop_run(); void mainloop_exit(); diff --git a/src/lib/libkogata/mainloop.c b/src/lib/libkogata/mainloop.c index c1758a4..75fb252 100644 --- a/src/lib/libkogata/mainloop.c +++ b/src/lib/libkogata/mainloop.c @@ -7,6 +7,9 @@ mainloop_fd_t *mainloop_fds = 0; bool mainloop_fds_change = false; bool mainloop_must_exit = false; +idle_callback_t mainloop_idle_cb = 0; +void* mainloop_idle_cb_data = 0; + void mainloop_add_fd(mainloop_fd_t* fd) { mainloop_fds_change = true; @@ -53,12 +56,19 @@ bool mainloop_nonblocking_write(mainloop_fd_t *fd, void* buf, size_t size, bool return false; } +void mainloop_when_idle(idle_callback_t cb, void* data) { + mainloop_idle_cb = cb; + mainloop_idle_cb_data = data; +} + void mainloop_run() { sel_fd_t *sel_arg = 0; int nfds = 0; mainloop_fds_change = true; mainloop_must_exit = false; + + bool after_idle_step = false; while(!mainloop_must_exit) { if (mainloop_fds_change) { nfds = 0; @@ -91,11 +101,12 @@ void mainloop_run() { // ---- Do the select /*dbg_printf("(mainloop) begin select\n");*/ - bool ok = sc_select(sel_arg, nfds, -1); + bool ok = sc_select(sel_arg, nfds, (after_idle_step || mainloop_idle_cb == 0 ? -1 : 0)); if (!ok) { - dbg_printf("(mainloop) Failed to select.\n"); - free(sel_arg); - return; + // nothing happenned + if (mainloop_idle_cb != 0) mainloop_idle_cb(mainloop_idle_cb_data); + after_idle_step = true; + continue; } /*dbg_printf("(mainloop) end select\n");*/ @@ -131,6 +142,7 @@ void mainloop_run() { } i++; } + after_idle_step = false; } } } |