Allow adding an event callback

This commit is contained in:
2024-04-12 00:00:27 +02:00
parent 86611c7e3b
commit 176fa2d02b
2 changed files with 68 additions and 24 deletions
+39 -3
View File
@@ -198,6 +198,7 @@ static void clear_res(struct io_resource *res)
{ {
res->type = IO_RES_VOID; res->type = IO_RES_VOID;
res->pending = 0; res->pending = 0;
res->callback = NULL;
#if IO_PLATFORM_WINDOWS #if IO_PLATFORM_WINDOWS
res->os_handle = INVALID_HANDLE_VALUE; res->os_handle = INVALID_HANDLE_VALUE;
@@ -818,7 +819,7 @@ op_from_ov(struct io_os_overlap *ov)
#if IO_PLATFORM_WINDOWS #if IO_PLATFORM_WINDOWS
static void static void
io_wait_windows(struct io_context *ioc, io_wait_internal_windows(struct io_context *ioc,
struct io_event *ev) struct io_event *ev)
{ {
int timeout = -1; int timeout = -1;
@@ -928,7 +929,7 @@ io_wait_windows(struct io_context *ioc,
#if IO_PLATFORM_LINUX #if IO_PLATFORM_LINUX
static void static void
io_wait_linux(struct io_context *ioc, io_wait_internal_linux(struct io_context *ioc,
struct io_event *ev) struct io_event *ev)
{ {
/* --- Read barrier --- */ /* --- Read barrier --- */
@@ -984,7 +985,8 @@ io_wait_linux(struct io_context *ioc,
} }
#endif #endif
void io_wait(struct io_context *ioc, static void
io_wait_internal(struct io_context *ioc,
struct io_event *ev) struct io_event *ev)
{ {
#if IO_PLATFORM_WINDOWS #if IO_PLATFORM_WINDOWS
@@ -995,3 +997,37 @@ void io_wait(struct io_context *ioc,
io_wait_linux(ioc, ev); io_wait_linux(ioc, ev);
#endif #endif
} }
void io_wait(struct io_context *ioc,
struct io_event *ev)
{
for (;;) {
io_wait_internal(ioc, ev);
if (ev->handle == IO_INVALID)
break;
assert(ev->handle != IO_INVALID);
struct io_resource *res;
res = res_from_handle(ioc, ev->handle);
assert(res);
if (res->callback == NULL)
break;
res->callback(*ev);
}
}
void io_set_callback(struct io_context *ioc,
io_handle handle,
io_callback callback)
{
struct io_resource *res;
res = res_from_handle(ioc, handle);
if (res == NULL)
return;
res->callback = callback;
}
+26 -18
View File
@@ -63,18 +63,40 @@ struct io_operation {
#endif #endif
}; };
enum io_evtype {
IO_ERROR,
IO_ABORT,
IO_COMPLETE,
};
struct io_event {
enum io_evtype evtype;
enum io_optype optype;
io_handle handle;
void *user;
union {
uint32_t num;
io_handle accepted;
};
};
enum io_restype { enum io_restype {
IO_RES_VOID, IO_RES_VOID,
IO_RES_FILE, IO_RES_FILE,
IO_RES_SOCKET, IO_RES_SOCKET,
}; };
typedef void (*io_callback)(struct io_event);
struct io_resource { struct io_resource {
enum io_restype type; enum io_restype type;
io_os_handle os_handle; io_os_handle os_handle;
uint16_t pending; uint16_t pending;
uint16_t gen; uint16_t gen;
io_callback callback;
#if IO_PLATFORM_WINDOWS #if IO_PLATFORM_WINDOWS
void *acceptfn; void *acceptfn;
char accept_buffer[2 * (IO_SOCKADDR_IN_SIZE + 16)]; char accept_buffer[2 * (IO_SOCKADDR_IN_SIZE + 16)];
@@ -121,24 +143,6 @@ struct io_context {
#endif #endif
}; };
enum io_evtype {
IO_ERROR,
IO_ABORT,
IO_COMPLETE,
};
struct io_event {
enum io_evtype evtype;
enum io_optype optype;
io_handle handle;
void *user;
union {
uint32_t num;
io_handle accepted;
};
};
bool io_global_init(void); bool io_global_init(void);
void io_global_free(void); void io_global_free(void);
@@ -185,3 +189,7 @@ io_handle io_create_file(struct io_context *ioc,
io_handle io_start_server(struct io_context *ioc, io_handle io_start_server(struct io_context *ioc,
const char *addr, int port); const char *addr, int port);
void io_set_callback(struct io_context *ioc,
io_handle handle,
io_callback callback);