uloop: add a clock_gettime() implementation for mac os x
[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 <sys/types.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <signal.h>
28
29 #if defined(__APPLE__) || defined(__FreeBSD__)
30 #define USE_KQUEUE
31 #else
32 #define USE_EPOLL
33 #endif
34
35 #include "list.h"
36
37 struct uloop_fd;
38 struct uloop_timeout;
39 struct uloop_process;
40
41 typedef void (*uloop_fd_handler)(struct uloop_fd *u, unsigned int events);
42 typedef void (*uloop_timeout_handler)(struct uloop_timeout *t);
43 typedef void (*uloop_process_handler)(struct uloop_process *c, int ret);
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 #ifdef USE_KQUEUE
50 #define ULOOP_EDGE_DEFER (1 << 4)
51 #endif
52
53 struct uloop_fd
54 {
55 uloop_fd_handler cb;
56 int fd;
57 bool eof;
58 bool error;
59 bool registered;
60 #ifdef USE_KQUEUE
61 bool flags;
62 #endif
63 };
64
65 struct uloop_timeout
66 {
67 struct list_head list;
68 bool pending;
69
70 uloop_timeout_handler cb;
71 struct timeval time;
72 };
73
74 struct uloop_process
75 {
76 struct list_head list;
77 bool pending;
78
79 uloop_process_handler cb;
80 pid_t pid;
81 };
82
83 extern bool uloop_cancelled;
84 extern bool uloop_handle_sigchld;
85
86 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
87 int uloop_fd_delete(struct uloop_fd *sock);
88
89 int uloop_timeout_add(struct uloop_timeout *timeout);
90 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs);
91 int uloop_timeout_cancel(struct uloop_timeout *timeout);
92
93 int uloop_process_add(struct uloop_process *p);
94 int uloop_process_delete(struct uloop_process *p);
95
96 static inline void uloop_end(void)
97 {
98 uloop_cancelled = true;
99 }
100
101 int uloop_init(void);
102 void uloop_run(void);
103 void uloop_done(void);
104
105 #endif