make the blobmsg format endian agnostic (stick to big-endian)
[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 #include <signal.h>
27
28 #if defined(__APPLE__) || defined(__FreeBSD__)
29 #define USE_KQUEUE
30 #else
31 #define USE_EPOLL
32 #endif
33
34 #include "list.h"
35
36 struct uloop_fd;
37 struct uloop_timeout;
38 struct uloop_process;
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
44 #define ULOOP_READ              (1 << 0)
45 #define ULOOP_WRITE             (1 << 1)
46 #define ULOOP_EDGE_TRIGGER      (1 << 2)
47 #define ULOOP_BLOCKING          (1 << 3)
48
49 struct uloop_fd
50 {
51         uloop_fd_handler cb;
52         int fd;
53         bool eof;
54         bool error;
55         bool registered;
56 #ifdef USE_KQUEUE
57         uint8_t kqflags;
58 #endif
59 };
60
61 struct uloop_timeout
62 {
63         struct list_head list;
64         bool pending;
65
66         uloop_timeout_handler cb;
67         struct timeval time;
68 };
69
70 struct uloop_process
71 {
72         struct list_head list;
73         bool pending;
74
75         uloop_process_handler cb;
76         pid_t pid;
77 };
78
79 extern bool uloop_cancelled;
80 extern bool uloop_handle_sigchld;
81
82 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
83 int uloop_fd_delete(struct uloop_fd *sock);
84
85 int uloop_timeout_add(struct uloop_timeout *timeout);
86 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs);
87 int uloop_timeout_cancel(struct uloop_timeout *timeout);
88
89 int uloop_process_add(struct uloop_process *p);
90 int uloop_process_delete(struct uloop_process *p);
91
92 static inline void uloop_end(void)
93 {
94         uloop_cancelled = true;
95 }
96
97 int uloop_init(void);
98 void uloop_run(void);
99 void uloop_done(void);
100
101 #endif