2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Micro event library for FreeBSD, designed for a single i/o thread
31 * using kqueue, and having events be persistent by default.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
45 #include <sys/event.h>
49 #include <pthread_np.h>
57 #define MEV_DEL_PENDING 3
61 static pthread_t mevent_tid;
62 static int mevent_timid = 43;
63 static int mevent_pipefd[2];
64 static pthread_mutex_t mevent_lmutex = PTHREAD_MUTEX_INITIALIZER;
67 void (*me_func)(int, enum ev_type, void *);
68 #define me_msecs me_fd
76 LIST_ENTRY(mevent) me_list;
79 static LIST_HEAD(listhead, mevent) global_head, change_head;
84 pthread_mutex_lock(&mevent_lmutex);
90 pthread_mutex_unlock(&mevent_lmutex);
94 mevent_pipe_read(int fd, enum ev_type type, void *param)
100 * Drain the pipe read side. The fd is non-blocking so this is
104 status = read(fd, buf, sizeof(buf));
105 } while (status == MEVENT_MAX);
114 * If calling from outside the i/o thread, write a byte on the
115 * pipe to force the i/o thread to exit the blocking kevent call.
117 if (mevent_pipefd[1] != 0 && pthread_self() != mevent_tid) {
118 write(mevent_pipefd[1], &c, 1);
123 mevent_kq_filter(struct mevent *mevp)
129 if (mevp->me_type == EVF_READ)
130 retval = EVFILT_READ;
132 if (mevp->me_type == EVF_WRITE)
133 retval = EVFILT_WRITE;
135 if (mevp->me_type == EVF_TIMER)
136 retval = EVFILT_TIMER;
138 if (mevp->me_type == EVF_SIGNAL)
139 retval = EVFILT_SIGNAL;
145 mevent_kq_flags(struct mevent *mevp)
149 switch (mevp->me_state) {
152 if (mevp->me_type == EVF_TIMER)
158 case MEV_DEL_PENDING:
167 mevent_kq_fflags(struct mevent *mevp)
169 /* XXX nothing yet, perhaps EV_EOF for reads ? */
174 mevent_build(int mfd, struct kevent *kev)
176 struct mevent *mevp, *tmpp;
183 LIST_FOREACH_SAFE(mevp, &change_head, me_list, tmpp) {
184 if (mevp->me_closefd) {
186 * A close of the file descriptor will remove the
191 if (mevp->me_type == EVF_TIMER) {
192 kev[i].ident = mevp->me_timid;
193 kev[i].data = mevp->me_msecs;
195 kev[i].ident = mevp->me_fd;
198 kev[i].filter = mevent_kq_filter(mevp);
199 kev[i].flags = mevent_kq_flags(mevp);
200 kev[i].fflags = mevent_kq_fflags(mevp);
206 LIST_REMOVE(mevp, me_list);
208 if (mevp->me_state == MEV_DEL_PENDING) {
211 LIST_INSERT_HEAD(&global_head, mevp, me_list);
214 assert(i < MEVENT_MAX);
223 mevent_handle(struct kevent *kev, int numev)
228 for (i = 0; i < numev; i++) {
231 /* XXX check for EV_ERROR ? */
233 (*mevp->me_func)(mevp->me_fd, mevp->me_type, mevp->me_param);
238 mevent_add(int tfd, enum ev_type type,
239 void (*func)(int, enum ev_type, void *), void *param)
241 struct mevent *lp, *mevp;
243 if (tfd < 0 || func == NULL) {
252 * Verify that the fd/type tuple is not present in any list
254 LIST_FOREACH(lp, &global_head, me_list) {
255 if (type != EVF_TIMER && lp->me_fd == tfd &&
256 lp->me_type == type) {
261 LIST_FOREACH(lp, &change_head, me_list) {
262 if (type != EVF_TIMER && lp->me_fd == tfd &&
263 lp->me_type == type) {
269 * Allocate an entry, populate it, and add it to the change list.
271 mevp = malloc(sizeof(struct mevent));
276 memset(mevp, 0, sizeof(struct mevent));
277 if (type == EVF_TIMER) {
278 mevp->me_msecs = tfd;
279 mevp->me_timid = mevent_timid++;
282 mevp->me_type = type;
283 mevp->me_func = func;
284 mevp->me_param = param;
286 LIST_INSERT_HEAD(&change_head, mevp, me_list);
288 mevp->me_state = MEV_ENABLE;
298 mevent_update(struct mevent *evp, int newstate)
301 * It's not possible to enable/disable a deleted event
303 if (evp->me_state == MEV_DEL_PENDING)
307 * No update needed if state isn't changing
309 if (evp->me_state == newstate)
314 evp->me_state = newstate;
317 * Place the entry onto the changed list if not already there.
319 if (evp->me_cq == 0) {
321 LIST_REMOVE(evp, me_list);
322 LIST_INSERT_HEAD(&change_head, evp, me_list);
332 mevent_enable(struct mevent *evp)
335 return (mevent_update(evp, MEV_ENABLE));
339 mevent_disable(struct mevent *evp)
342 return (mevent_update(evp, MEV_DISABLE));
346 mevent_delete_event(struct mevent *evp, int closefd)
351 * Place the entry onto the changed list if not already there, and
352 * mark as to be deleted.
354 if (evp->me_cq == 0) {
356 LIST_REMOVE(evp, me_list);
357 LIST_INSERT_HEAD(&change_head, evp, me_list);
360 evp->me_state = MEV_DEL_PENDING;
371 mevent_delete(struct mevent *evp)
374 return (mevent_delete_event(evp, 0));
378 mevent_delete_close(struct mevent *evp)
381 return (mevent_delete_event(evp, 1));
385 mevent_set_name(void)
388 pthread_set_name_np(mevent_tid, "mevent");
392 mevent_dispatch(void)
394 struct kevent changelist[MEVENT_MAX];
395 struct kevent eventlist[MEVENT_MAX];
396 struct mevent *pipev;
401 mevent_tid = pthread_self();
408 * Open the pipe that will be used for other threads to force
409 * the blocking kqueue call to exit by writing to it. Set the
410 * descriptor to non-blocking.
412 ret = pipe(mevent_pipefd);
419 * Add internal event handler for the pipe write fd
421 pipev = mevent_add(mevent_pipefd[0], EVF_READ, mevent_pipe_read, NULL);
422 assert(pipev != NULL);
426 * Build changelist if required.
427 * XXX the changelist can be put into the blocking call
428 * to eliminate the extra syscall. Currently better for
431 numev = mevent_build(mfd, changelist);
433 ret = kevent(mfd, changelist, numev, NULL, 0, NULL);
435 perror("Error return from kevent change");
440 * Block awaiting events
442 ret = kevent(mfd, NULL, 0, eventlist, MEVENT_MAX, NULL);
443 if (ret == -1 && errno != EINTR) {
444 perror("Error return from kevent monitor");
448 * Handle reported events
450 mevent_handle(eventlist, ret);