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_pipefd[2];
63 static pthread_mutex_t mevent_lmutex = PTHREAD_MUTEX_INITIALIZER;
66 void (*me_func)(int, enum ev_type, void *);
73 LIST_ENTRY(mevent) me_list;
76 static LIST_HEAD(listhead, mevent) global_head, change_head;
81 pthread_mutex_lock(&mevent_lmutex);
87 pthread_mutex_unlock(&mevent_lmutex);
91 mevent_pipe_read(int fd, enum ev_type type, void *param)
97 * Drain the pipe read side. The fd is non-blocking so this is
101 status = read(fd, buf, sizeof(buf));
102 } while (status == MEVENT_MAX);
111 * If calling from outside the i/o thread, write a byte on the
112 * pipe to force the i/o thread to exit the blocking kevent call.
114 if (mevent_pipefd[1] != 0 && pthread_self() != mevent_tid) {
115 write(mevent_pipefd[1], &c, 1);
120 mevent_kq_filter(struct mevent *mevp)
126 if (mevp->me_type == EVF_READ)
127 retval = EVFILT_READ;
129 if (mevp->me_type == EVF_WRITE)
130 retval = EVFILT_WRITE;
136 mevent_kq_flags(struct mevent *mevp)
140 switch (mevp->me_state) {
147 case MEV_DEL_PENDING:
156 mevent_kq_fflags(struct mevent *mevp)
158 /* XXX nothing yet, perhaps EV_EOF for reads ? */
163 mevent_build(int mfd, struct kevent *kev)
165 struct mevent *mevp, *tmpp;
172 LIST_FOREACH_SAFE(mevp, &change_head, me_list, tmpp) {
173 if (mevp->me_closefd) {
175 * A close of the file descriptor will remove the
180 kev[i].ident = mevp->me_fd;
181 kev[i].filter = mevent_kq_filter(mevp);
182 kev[i].flags = mevent_kq_flags(mevp);
183 kev[i].fflags = mevent_kq_fflags(mevp);
190 LIST_REMOVE(mevp, me_list);
192 if (mevp->me_state == MEV_DEL_PENDING) {
195 LIST_INSERT_HEAD(&global_head, mevp, me_list);
198 assert(i < MEVENT_MAX);
207 mevent_handle(struct kevent *kev, int numev)
212 for (i = 0; i < numev; i++) {
215 /* XXX check for EV_ERROR ? */
217 (*mevp->me_func)(mevp->me_fd, mevp->me_type, mevp->me_param);
222 mevent_add(int fd, enum ev_type type,
223 void (*func)(int, enum ev_type, void *), void *param)
225 struct mevent *lp, *mevp;
227 if (fd < 0 || func == NULL) {
236 * Verify that the fd/type tuple is not present in any list
238 LIST_FOREACH(lp, &global_head, me_list) {
239 if (lp->me_fd == fd && lp->me_type == type) {
244 LIST_FOREACH(lp, &change_head, me_list) {
245 if (lp->me_fd == fd && lp->me_type == type) {
251 * Allocate an entry, populate it, and add it to the change list.
253 mevp = malloc(sizeof(struct mevent));
258 memset(mevp, 0, sizeof(struct mevent));
260 mevp->me_type = type;
261 mevp->me_func = func;
262 mevp->me_param = param;
264 LIST_INSERT_HEAD(&change_head, mevp, me_list);
266 mevp->me_state = MEV_ENABLE;
276 mevent_update(struct mevent *evp, int newstate)
279 * It's not possible to enable/disable a deleted event
281 if (evp->me_state == MEV_DEL_PENDING)
285 * No update needed if state isn't changing
287 if (evp->me_state == newstate)
292 evp->me_state = newstate;
295 * Place the entry onto the changed list if not already there.
297 if (evp->me_cq == 0) {
299 LIST_REMOVE(evp, me_list);
300 LIST_INSERT_HEAD(&change_head, evp, me_list);
310 mevent_enable(struct mevent *evp)
313 return (mevent_update(evp, MEV_ENABLE));
317 mevent_disable(struct mevent *evp)
320 return (mevent_update(evp, MEV_DISABLE));
324 mevent_delete_event(struct mevent *evp, int closefd)
329 * Place the entry onto the changed list if not already there, and
330 * mark as to be deleted.
332 if (evp->me_cq == 0) {
334 LIST_REMOVE(evp, me_list);
335 LIST_INSERT_HEAD(&change_head, evp, me_list);
338 evp->me_state = MEV_DEL_PENDING;
349 mevent_delete(struct mevent *evp)
352 return (mevent_delete_event(evp, 0));
356 mevent_delete_close(struct mevent *evp)
359 return (mevent_delete_event(evp, 1));
363 mevent_set_name(void)
365 char tname[MAXCOMLEN + 1];
367 snprintf(tname, sizeof(tname), "%s mevent", vmname);
368 pthread_set_name_np(mevent_tid, tname);
372 mevent_dispatch(void)
374 struct kevent changelist[MEVENT_MAX];
375 struct kevent eventlist[MEVENT_MAX];
376 struct mevent *pipev;
381 mevent_tid = pthread_self();
388 * Open the pipe that will be used for other threads to force
389 * the blocking kqueue call to exit by writing to it. Set the
390 * descriptor to non-blocking.
392 ret = pipe(mevent_pipefd);
399 * Add internal event handler for the pipe write fd
401 pipev = mevent_add(mevent_pipefd[0], EVF_READ, mevent_pipe_read, NULL);
402 assert(pipev != NULL);
406 * Build changelist if required.
407 * XXX the changelist can be put into the blocking call
408 * to eliminate the extra syscall. Currently better for
411 numev = mevent_build(mfd, changelist);
413 ret = kevent(mfd, changelist, numev, NULL, 0, NULL);
415 perror("Error return from kevent change");
420 * Block awaiting events
422 ret = kevent(mfd, NULL, 0, eventlist, MEVENT_MAX, NULL);
424 perror("Error return from kevent monitor");
428 * Handle reported events
430 mevent_handle(eventlist, ret);