aboutsummaryrefslogtreecommitdiff
path: root/src/lib/include/kogata/mainloop.h
blob: 6352916de8c44cd2f1ef721625d18a5b8fef3948 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once

// These functions are not thread safe, their purpose
// is to multiplex several IO operations on a
// single thread.

#include <kogata/syscall.h>

#define MAINLOOP_MAX_WR_BUFS 4

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;
	void* buf;
	bool must_free;
} mainloop_wr_buf_t;

typedef struct mainloop_fd {
	fd_t fd;
	
	size_t rd_buf_expect_size, rd_buf_filled;
	void* rd_buf;

	mainloop_wr_buf_t wr_bufs[MAINLOOP_MAX_WR_BUFS];

	void* data;

	buf_full_callback_t rd_on_full;
	fd_error_callback_t on_error;

	mainloop_fd_t *next;
} mainloop_fd_t;

void mainloop_add_fd(mainloop_fd_t* fd);
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();

/* vim: set ts=4 sw=4 tw=0 noet :*/