uloop: export the cancelled flag
[project/libubox.git] / uloop.h
1 /*
2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17 *
18 */
19
20 #ifndef _ULOOP_H__
21 #define _ULOOP_H__
22
23 #include <sys/time.h>
24 #include <stdbool.h>
25 #include <stdint.h>
26
27 #if defined(__APPLE__) || defined(__FreeBSD__)
28 #define USE_KQUEUE
29 #else
30 #define USE_EPOLL
31 #endif
32
33 struct uloop_fd;
34 struct uloop_timeout;
35
36 typedef void (*uloop_fd_handler)(struct uloop_fd *u, unsigned int events);
37 typedef void (*uloop_timeout_handler)(struct uloop_timeout *t);
38
39 #define ULOOP_READ (1 << 0)
40 #define ULOOP_WRITE (1 << 1)
41 #define ULOOP_EDGE_TRIGGER (1 << 2)
42 #define ULOOP_BLOCKING (1 << 3)
43
44 struct uloop_fd
45 {
46 uloop_fd_handler cb;
47 int fd;
48 bool eof;
49 bool error;
50 bool registered;
51 #ifdef USE_KQUEUE
52 uint8_t kqflags;
53 #endif
54 };
55
56 struct uloop_timeout
57 {
58 uloop_timeout_handler cb;
59 struct uloop_timeout *prev;
60 struct uloop_timeout *next;
61 struct timeval time;
62 bool pending;
63 };
64
65 extern bool uloop_cancelled;
66
67 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
68 int uloop_fd_delete(struct uloop_fd *sock);
69
70 int uloop_timeout_add(struct uloop_timeout *timeout);
71 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs);
72 int uloop_timeout_cancel(struct uloop_timeout *timeout);
73
74 void uloop_end(void);
75 int uloop_init(void);
76 void uloop_run(void);
77 void uloop_done(void);
78
79 #endif