jshn: properly support JSON "null" type
[project/libubox.git] / uloop-kqueue.c
1 /*
2 * uloop - event loop implementation
3 *
4 * Copyright (C) 2010-2016 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 static int uloop_init_pollfd(void)
19 {
20 struct timespec timeout = { 0, 0 };
21 struct kevent ev = {};
22
23 if (poll_fd >= 0)
24 return 0;
25
26 poll_fd = kqueue();
27 if (poll_fd < 0)
28 return -1;
29
30 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
31 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
32
33 return 0;
34 }
35
36
37 static uint16_t get_flags(unsigned int flags, unsigned int mask)
38 {
39 uint16_t kflags = 0;
40
41 if (!(flags & mask))
42 return EV_DELETE;
43
44 kflags = EV_ADD;
45 if (flags & ULOOP_EDGE_TRIGGER)
46 kflags |= EV_CLEAR;
47
48 return kflags;
49 }
50
51 static struct kevent events[ULOOP_MAX_EVENTS];
52
53 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
54 {
55 struct timespec timeout = { 0, 0 };
56 struct kevent ev[2];
57 int nev = 0;
58 unsigned int fl = 0;
59 unsigned int changed;
60 uint16_t kflags;
61
62 if (flags & ULOOP_EDGE_DEFER)
63 flags &= ~ULOOP_EDGE_TRIGGER;
64
65 changed = flags ^ fd->flags;
66 if (changed & ULOOP_EDGE_TRIGGER)
67 changed |= flags;
68
69 if (changed & ULOOP_READ) {
70 kflags = get_flags(flags, ULOOP_READ);
71 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
72 }
73
74 if (changed & ULOOP_WRITE) {
75 kflags = get_flags(flags, ULOOP_WRITE);
76 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
77 }
78
79 if (!flags)
80 fl |= EV_DELETE;
81
82 fd->flags = flags;
83 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
84 return -1;
85
86 return 0;
87 }
88
89 static int register_poll(struct uloop_fd *fd, unsigned int flags)
90 {
91 if (flags & ULOOP_EDGE_TRIGGER)
92 flags |= ULOOP_EDGE_DEFER;
93 else
94 flags &= ~ULOOP_EDGE_DEFER;
95
96 return register_kevent(fd, flags);
97 }
98
99 static int __uloop_fd_delete(struct uloop_fd *fd)
100 {
101 return register_poll(fd, 0);
102 }
103
104 static int uloop_fetch_events(int timeout)
105 {
106 struct timespec ts;
107 int nfds, n;
108
109 if (timeout >= 0) {
110 ts.tv_sec = timeout / 1000;
111 ts.tv_nsec = (timeout % 1000) * 1000000;
112 }
113
114 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
115 for (n = 0; n < nfds; n++) {
116 struct uloop_fd_event *cur = &cur_fds[n];
117 struct uloop_fd *u = events[n].udata;
118 unsigned int ev = 0;
119
120 cur->fd = u;
121 if (!u)
122 continue;
123
124 if (events[n].flags & EV_ERROR) {
125 u->error = true;
126 if (!(u->flags & ULOOP_ERROR_CB))
127 uloop_fd_delete(u);
128 }
129
130 if(events[n].filter == EVFILT_READ)
131 ev |= ULOOP_READ;
132 else if (events[n].filter == EVFILT_WRITE)
133 ev |= ULOOP_WRITE;
134
135 if (events[n].flags & EV_EOF)
136 u->eof = true;
137 else if (!ev)
138 cur->fd = NULL;
139
140 cur->events = ev;
141 if (u->flags & ULOOP_EDGE_DEFER) {
142 u->flags &= ~ULOOP_EDGE_DEFER;
143 u->flags |= ULOOP_EDGE_TRIGGER;
144 register_kevent(u, u->flags);
145 }
146 }
147 return nfds;
148 }