4 * Copyright (c) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
5 * Copyright (c) 2011 University of Tuebingen
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * This provides a basic event loop similar to those provided by glib etc.
30 * It uses linux specific features like signalfd so it may not be easy to port
31 * it to other platforms.
41 #include <sys/signalfd.h>
42 #include <sys/types.h>
52 * @file: Source code file where the log message originated or NULL
53 * @line: Line number in source code or 0
54 * @func: C function name or NULL
55 * @subs: Subsystem where the message came from or NULL
56 * @sev: Kernel-style severity between 0=FATAL and 7=DEBUG
57 * @format: printf-formatted message
58 * @args: arguments for printf-style @format
60 * This is the type of a logging callback function. You can always pass NULL
61 * instead of such a function to disable logging.
63 typedef void (*ev_log_t) (const char *file,
73 * @fd: File descriptor event source
74 * @mask: Mask of @EV_READABLE, @EV_WRITEABLE, etc.
75 * @data: user-supplied data
77 * This is the callback-type for file-descriptor event sources.
79 typedef void (*ev_fd_cb) (struct ev_fd *fd, int mask, void *data);
83 * @timer: Timer source
84 * @num: Number of times the timer fired since last wakeup
85 * @data: user-supplied data
87 * This is the callback-type for timer event sources. If the process was too
88 * busy to be woken up as fast as possible, then @num may be bigger than 1.
90 typedef void (*ev_timer_cb)
91 (struct ev_timer *timer, uint64_t num, void *data);
95 * @cnt: Counter source
96 * @num: Current counter state
97 * @data: user-supplied data
99 * This is the callback-type for counter event sources. @num is at least 1 but
100 * may be bigger if the timer was increased multiple times or by bigger values.
101 * The counter is reset to 0 before this callback is called.
103 typedef void (*ev_counter_cb)
104 (struct ev_counter *cnt, uint64_t num, void *data);
107 * ev_signal_shared_cb:
108 * @eloop: event loop object
109 * @info: signalfd info for this event, see "man signalfd"
110 * @data: user-supplied data
112 * This is the callback-type for shared signal events.
114 typedef void (*ev_signal_shared_cb)
115 (struct ev_eloop *eloop, struct signalfd_siginfo *info, void *data);
120 * @status: status of child
122 * This contains the payload for @ev_child_cb events. The data is simply copied
123 * from the waitpid() system-call.
125 struct ev_child_data {
132 * @eloop: event loop object
133 * @chld: chld pid/status payload
134 * @data: user-supplied data
136 * This is the callback-type for child-reaper events.
138 typedef void (*ev_child_cb)
139 (struct ev_eloop *eloop, struct ev_child_data *chld, void *data);
143 * @eloop: event loop object
144 * @unused: Unused parameter which is required internally
145 * @data: user-supplied data
147 * This is the callback-type for idle-source events.
149 typedef void (*ev_idle_cb) (struct ev_eloop *eloop, void *unused, void *data);
153 * @EV_READABLE: file-descriptor is readable
154 * @EV_WRITEABLE: file-desciprotr is writeable
155 * @EV_HUP: Hang-up on file-descriptor
156 * @EV_ERR: I/O error on file-descriptor
157 * @EV_ET: Edge-triggered mode
159 * These flags are used for events on file-descriptors. You can combine them
160 * with binary-operators like @EV_READABLE | @EV_WRITEABLE.
161 * @EV_HUP and @EV_ERR are always raised for file-descriptors, even if not
162 * requested explicitly.
163 * @EV_ET enables edge-triggered mode for the operation
165 enum ev_eloop_flags {
173 int ev_eloop_new(struct ev_eloop **out, ev_log_t log);
174 void ev_eloop_ref(struct ev_eloop *loop);
175 void ev_eloop_unref(struct ev_eloop *loop);
177 void ev_eloop_flush_fd(struct ev_eloop *loop, struct ev_fd *fd);
178 int ev_eloop_dispatch(struct ev_eloop *loop, int timeout);
179 int ev_eloop_run(struct ev_eloop *loop, int timeout);
180 void ev_eloop_exit(struct ev_eloop *loop);
181 int ev_eloop_get_fd(struct ev_eloop *loop);
185 int ev_eloop_new_eloop(struct ev_eloop *loop, struct ev_eloop **out);
186 int ev_eloop_add_eloop(struct ev_eloop *loop, struct ev_eloop *add);
187 void ev_eloop_rm_eloop(struct ev_eloop *rm);
191 int ev_fd_new(struct ev_fd **out, int fd, int mask, ev_fd_cb cb, void *data,
193 void ev_fd_ref(struct ev_fd *fd);
194 void ev_fd_unref(struct ev_fd *fd);
196 int ev_fd_enable(struct ev_fd *fd);
197 void ev_fd_disable(struct ev_fd *fd);
198 bool ev_fd_is_enabled(struct ev_fd *fd);
199 bool ev_fd_is_bound(struct ev_fd *fd);
200 void ev_fd_set_cb_data(struct ev_fd *fd, ev_fd_cb cb, void *data);
201 int ev_fd_update(struct ev_fd *fd, int mask);
203 int ev_eloop_new_fd(struct ev_eloop *loop, struct ev_fd **out, int rfd,
204 int mask, ev_fd_cb cb, void *data);
205 int ev_eloop_add_fd(struct ev_eloop *loop, struct ev_fd *fd);
206 void ev_eloop_rm_fd(struct ev_fd *fd);
210 int ev_timer_new(struct ev_timer **out, const struct itimerspec *spec,
211 ev_timer_cb cb, void *data, ev_log_t log);
212 void ev_timer_ref(struct ev_timer *timer);
213 void ev_timer_unref(struct ev_timer *timer);
215 int ev_timer_enable(struct ev_timer *timer);
216 void ev_timer_disable(struct ev_timer *timer);
217 bool ev_timer_is_enabled(struct ev_timer *timer);
218 bool ev_timer_is_bound(struct ev_timer *timer);
219 void ev_timer_set_cb_data(struct ev_timer *timer, ev_timer_cb cb, void *data);
220 int ev_timer_update(struct ev_timer *timer, const struct itimerspec *spec);
221 int ev_timer_drain(struct ev_timer *timer, uint64_t *expirations);
223 int ev_eloop_new_timer(struct ev_eloop *loop, struct ev_timer **out,
224 const struct itimerspec *spec, ev_timer_cb cb,
226 int ev_eloop_add_timer(struct ev_eloop *loop, struct ev_timer *timer);
227 void ev_eloop_rm_timer(struct ev_timer *timer);
229 /* counter sources */
231 int ev_counter_new(struct ev_counter **out, ev_counter_cb, void *data,
233 void ev_counter_ref(struct ev_counter *cnt);
234 void ev_counter_unref(struct ev_counter *cnt);
236 int ev_counter_enable(struct ev_counter *cnt);
237 void ev_counter_disable(struct ev_counter *cnt);
238 bool ev_counter_is_enabled(struct ev_counter *cnt);
239 bool ev_counter_is_bound(struct ev_counter *cnt);
240 void ev_counter_set_cb_data(struct ev_counter *cnt, ev_counter_cb cb,
242 int ev_counter_inc(struct ev_counter *cnt, uint64_t val);
244 int ev_eloop_new_counter(struct ev_eloop *eloop, struct ev_counter **out,
245 ev_counter_cb cb, void *data);
246 int ev_eloop_add_counter(struct ev_eloop *eloop, struct ev_counter *cnt);
247 void ev_eloop_rm_counter(struct ev_counter *cnt);
251 int ev_eloop_register_signal_cb(struct ev_eloop *loop, int signum,
252 ev_signal_shared_cb cb, void *data);
253 void ev_eloop_unregister_signal_cb(struct ev_eloop *loop, int signum,
254 ev_signal_shared_cb cb, void *data);
256 /* child reaper sources */
258 int ev_eloop_register_child_cb(struct ev_eloop *loop, ev_child_cb cb,
260 void ev_eloop_unregister_child_cb(struct ev_eloop *loop, ev_child_cb cb,
269 EV_IDLE_ALL = EV_ONESHOT | EV_SINGLE,
272 int ev_eloop_register_idle_cb(struct ev_eloop *eloop, ev_idle_cb cb,
273 void *data, unsigned int flags);
274 void ev_eloop_unregister_idle_cb(struct ev_eloop *eloop, ev_idle_cb cb,
275 void *data, unsigned int flags);
277 /* pre dispatch callbacks */
279 int ev_eloop_register_pre_cb(struct ev_eloop *eloop, ev_idle_cb cb,
281 void ev_eloop_unregister_pre_cb(struct ev_eloop *eloop, ev_idle_cb cb,
284 /* post dispatch callbacks */
286 int ev_eloop_register_post_cb(struct ev_eloop *eloop, ev_idle_cb cb,
288 void ev_eloop_unregister_post_cb(struct ev_eloop *eloop, ev_idle_cb cb,
291 #endif /* EV_ELOOP_H */