Debian Codegraph @

libmowgli-2/src/libmowgli/eventloop/kqueue_pollops.c

raw
1 /*
2 * Copyright (c) 2011 William Pitcock <[email protected]>.
3 * Copyright (c) 2011 Jilles Tjoelker <[email protected]>.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
13 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
14 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
16 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
17 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
18 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
19 * POSSIBILITY OF SUCH DAMAGE.
20 */
21
22 #include "mowgli.h"
23 #include "eventloop/eventloop_internal.h"
24
25 #ifdef HAVE_KQUEUE
26
27 # include <sys/event.h>
28
29 typedef struct
30 {
31 int kqueue_fd;
32 int nevents;
33 struct kevent *events;
34 } mowgli_kqueue_eventloop_private_t;
35
36 static void
37 mowgli_kqueue_eventloop_pollsetup(mowgli_eventloop_t *eventloop)
38 {
39 mowgli_kqueue_eventloop_private_t *priv;
40
41 priv = mowgli_alloc(sizeof *priv);
42 eventloop->poller = priv;
43
44 priv->nevents = getdtablesize();
45 priv->kqueue_fd = kqueue();
46 priv->events = mowgli_alloc(sizeof(struct kevent) * priv->nevents);
47
48 /* attempt to set the fd as close-on-exec, but ignore errors */
49 fcntl(priv->kqueue_fd, F_SETFD, FD_CLOEXEC);
50
51 return;
52 }
53
54 static void
55 mowgli_kqueue_eventloop_pollshutdown(mowgli_eventloop_t *eventloop)
56 {
57 mowgli_kqueue_eventloop_private_t *priv;
58
59 return_if_fail(eventloop != NULL);
60
61 priv = eventloop->poller;
62
63 close(priv->kqueue_fd);
64
65 mowgli_free(priv->events);
66 mowgli_free(priv);
67 return;
68 }
69
70 static void
71 mowgli_kqueue_eventloop_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable)
72 {
73 mowgli_kqueue_eventloop_private_t *priv;
74 struct kevent event;
75
76 return_if_fail(eventloop != NULL);
77 return_if_fail(pollable != NULL);
78
79 priv = eventloop->poller;
80
81 EV_SET(&event, pollable->fd, EVFILT_READ | EVFILT_WRITE, EV_DELETE, 0, 0, pollable);
82
83 if (kevent(priv->kqueue_fd, &event, 1, NULL, 0,
84 &(const struct timespec) { .tv_sec = 0, .tv_nsec = 0 }
85 ) != 0)
86 mowgli_log("mowgli_kqueue_eventloop_setselect(): kevent failed: %d (%s)", errno, strerror(errno));
87 }
88
89 static void
90 mowgli_kqueue_eventloop_setselect(mowgli_eventloop_t *eventloop, mowgli_eventloop_pollable_t *pollable, mowgli_eventloop_io_dir_t dir, mowgli_eventloop_io_cb_t *event_function)
91 {
92 mowgli_kqueue_eventloop_private_t *priv;
93 mowgli_eventloop_io_cb_t **fptr;
94 struct kevent event;
95
96 int filter;
97 bool change;
98
99 return_if_fail(eventloop != NULL);
100 return_if_fail(pollable != NULL);
101
102 priv = eventloop->poller;
103 change = false;
104
105 # ifdef DEBUG
106 mowgli_log("setselect %p fd %d func %p", pollable, pollable->fd, event_function);
107 # endif
108
109 switch (dir)
110 {
111 case MOWGLI_EVENTLOOP_IO_READ:
112 fptr = &pollable->read_function;
113 filter = EVFILT_READ;
114 break;
115 case MOWGLI_EVENTLOOP_IO_WRITE:
116 fptr = &pollable->write_function;
117 filter = EVFILT_WRITE;
118 break;
119 default:
120 mowgli_log("unhandled pollable direction %d", dir);
121 return;
122 }
123
124 change = (*fptr != NULL) != (event_function != NULL);
125
126 *fptr = event_function;
127
128 if (!change)
129 return;
130
131 # ifdef DEBUG
132 mowgli_log("%p -> read %p : write %p", pollable, pollable->read_function, pollable->write_function);
133 # endif
134
135 EV_SET(&event, pollable->fd, filter,
136 event_function ? EV_ADD : EV_DELETE, 0, 0, pollable);
137
138 if (kevent(priv->kqueue_fd, &event, 1, NULL, 0,
139 &(const struct timespec) { .tv_sec = 0, .tv_nsec = 0 }
140 ) != 0)
141 mowgli_log("mowgli_kqueue_eventloop_setselect(): kevent failed: %d (%s)", errno, strerror(errno));
142 }
143
144 static void
145 mowgli_kqueue_eventloop_select(mowgli_eventloop_t *eventloop, int delay)
146 {
147 mowgli_kqueue_eventloop_private_t *priv;
148 int i, num, o_errno;
149
150 return_if_fail(eventloop != NULL);
151
152 priv = eventloop->poller;
153
154 num = kevent(priv->kqueue_fd, NULL, 0, priv->events, priv->nevents,
155 delay >= 0 ? &(const struct timespec) { .tv_sec = delay / 1000,
156 .tv_nsec = delay % 1000 * 1000000 } : NULL);
157
158 o_errno = errno;
159 mowgli_eventloop_synchronize(eventloop);
160
161 if (num < 0)
162 {
163 if (mowgli_eventloop_ignore_errno(o_errno))
164 return;
165
166 mowgli_log("mowgli_kqueue_eventloop_select(): kevent failed: %d (%s)", o_errno, strerror(o_errno));
167 return;
168 }
169
170 for (i = 0; i < num; i++)
171 {
172 mowgli_eventloop_pollable_t *pollable = priv->events[i].udata;
173
174 if (priv->events[i].filter == EVFILT_READ && !pollable->removed)
175 mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_READ);
176
177 if (priv->events[i].filter == EVFILT_WRITE && !pollable->removed)
178 mowgli_pollable_trigger(eventloop, pollable, MOWGLI_EVENTLOOP_IO_WRITE);
179
180 /* XXX Perhaps we need to recheck read_function and
181 * write_function now.
182 */
183 }
184 }
185
186 mowgli_eventloop_ops_t _mowgli_kqueue_pollops =
187 {
188 .timeout_once = mowgli_simple_eventloop_timeout_once,
189 .run_once = mowgli_simple_eventloop_run_once,
190 .pollsetup = mowgli_kqueue_eventloop_pollsetup,
191 .pollshutdown = mowgli_kqueue_eventloop_pollshutdown,
192 .setselect = mowgli_kqueue_eventloop_setselect,
193 .select = mowgli_kqueue_eventloop_select,
194 .destroy = mowgli_kqueue_eventloop_destroy,
195 };
196
197 #endif