b3f268c069ae10dd39aeb78eb11e1cb27746ef21
[project/libubox.git] / uloop.h
1 /*
2 * uloop - event loop implementation
3 *
4 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #ifndef _ULOOP_H__
19 #define _ULOOP_H__
20
21 #include <sys/time.h>
22 #include <sys/types.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <signal.h>
26
27 #if defined(__APPLE__) || defined(__FreeBSD__)
28 #define USE_KQUEUE
29 #else
30 #define USE_EPOLL
31 #endif
32
33 #include "list.h"
34
35 struct uloop_fd;
36 struct uloop_timeout;
37 struct uloop_process;
38 struct uloop_interval;
39
40 typedef void (*uloop_fd_handler)(struct uloop_fd *u, unsigned int events);
41 typedef void (*uloop_timeout_handler)(struct uloop_timeout *t);
42 typedef void (*uloop_process_handler)(struct uloop_process *c, int ret);
43 typedef void (*uloop_interval_handler)(struct uloop_interval *t);
44
45 #define ULOOP_READ (1 << 0)
46 #define ULOOP_WRITE (1 << 1)
47 #define ULOOP_EDGE_TRIGGER (1 << 2)
48 #define ULOOP_BLOCKING (1 << 3)
49
50 #define ULOOP_EVENT_MASK (ULOOP_READ | ULOOP_WRITE)
51
52 /* internal flags */
53 #define ULOOP_EVENT_BUFFERED (1 << 4)
54 #ifdef USE_KQUEUE
55 #define ULOOP_EDGE_DEFER (1 << 5)
56 #endif
57
58 #define ULOOP_ERROR_CB (1 << 6)
59
60 struct uloop_fd
61 {
62 uloop_fd_handler cb;
63 int fd;
64 bool eof;
65 bool error;
66 bool registered;
67 uint8_t flags;
68 };
69
70 struct uloop_timeout
71 {
72 struct list_head list;
73 bool pending;
74
75 uloop_timeout_handler cb;
76 struct timeval time;
77 };
78
79 struct uloop_process
80 {
81 struct list_head list;
82 bool pending;
83
84 uloop_process_handler cb;
85 pid_t pid;
86 };
87
88 struct uloop_interval
89 {
90 uloop_interval_handler cb;
91 uint64_t expirations;
92
93 union {
94 struct uloop_fd ufd;
95 struct {
96 int64_t fired;
97 unsigned int msecs;
98 } time;
99 } private;
100 };
101
102 extern bool uloop_cancelled;
103 extern bool uloop_handle_sigchld;
104 extern uloop_fd_handler uloop_fd_set_cb;
105
106 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
107 int uloop_fd_delete(struct uloop_fd *sock);
108
109 int uloop_get_next_timeout(void);
110 int uloop_timeout_add(struct uloop_timeout *timeout);
111 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs);
112 int uloop_timeout_cancel(struct uloop_timeout *timeout);
113 int uloop_timeout_remaining(struct uloop_timeout *timeout) __attribute__((deprecated("use uloop_timeout_remaining64")));
114 int64_t uloop_timeout_remaining64(struct uloop_timeout *timeout);
115
116 int uloop_process_add(struct uloop_process *p);
117 int uloop_process_delete(struct uloop_process *p);
118
119 int uloop_interval_set(struct uloop_interval *timer, unsigned int msecs);
120 int uloop_interval_cancel(struct uloop_interval *timer);
121 int64_t uloop_interval_remaining(struct uloop_interval *timer);
122
123 bool uloop_cancelling(void);
124
125 static inline void uloop_end(void)
126 {
127 uloop_cancelled = true;
128 }
129
130 int uloop_init(void);
131 int uloop_run_timeout(int timeout);
132 static inline int uloop_run(void)
133 {
134 return uloop_run_timeout(-1);
135 }
136 void uloop_done(void);
137
138 #endif