ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_kqueue.c
Revision: 1.32
Committed: Mon Dec 17 11:54:29 2007 UTC (16 years, 5 months ago) by ayin
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_0, rel-1_86
Changes since 1.31: +0 -4 lines
Log Message:
Remove unused variables.

File Contents

# User Rev Content
1 root 1.1 /*
2 root 1.26 * libev kqueue backend
3     *
4     * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
5 root 1.1 * All rights reserved.
6     *
7     * Redistribution and use in source and binary forms, with or without
8 root 1.26 * modification, are permitted provided that the following conditions are
9     * met:
10     *
11     * * Redistributions of source code must retain the above copyright
12     * notice, this list of conditions and the following disclaimer.
13 root 1.1 *
14 root 1.26 * * Redistributions in binary form must reproduce the above
15     * copyright notice, this list of conditions and the following
16     * disclaimer in the documentation and/or other materials provided
17     * with the distribution.
18 root 1.1 *
19 root 1.26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23     * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 root 1.1 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 root 1.26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 root 1.1 */
31    
32     #include <sys/types.h>
33     #include <sys/time.h>
34     #include <sys/queue.h>
35     #include <sys/event.h>
36     #include <string.h>
37     #include <errno.h>
38    
39 root 1.27 void inline_speed
40 root 1.6 kqueue_change (EV_P_ int fd, int filter, int flags, int fflags)
41 root 1.1 {
42 ayin 1.16 ++kqueue_changecnt;
43 root 1.17 array_needsize (struct kevent, kqueue_changes, kqueue_changemax, kqueue_changecnt, EMPTY2);
44 root 1.1
45 root 1.23 EV_SET (&kqueue_changes [kqueue_changecnt - 1], fd, filter, flags, fflags, 0, 0);
46 root 1.1 }
47    
48 root 1.4 #ifndef NOTE_EOF
49     # define NOTE_EOF 0
50     #endif
51    
52 root 1.1 static void
53 root 1.6 kqueue_modify (EV_P_ int fd, int oev, int nev)
54 root 1.1 {
55 root 1.22 if (oev != nev)
56     {
57     if (oev & EV_READ)
58     kqueue_change (EV_A_ fd, EVFILT_READ , EV_DELETE, 0);
59 root 1.14
60 root 1.22 if (oev & EV_WRITE)
61     kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_DELETE, 0);
62     }
63 root 1.14
64 root 1.22 /* to detect close/reopen reliably, we have to re-add */
65     /* event requests even when oev == nev */
66 root 1.14
67     if (nev & EV_READ)
68 root 1.22 kqueue_change (EV_A_ fd, EVFILT_READ , EV_ADD, NOTE_EOF);
69 root 1.1
70 root 1.14 if (nev & EV_WRITE)
71     kqueue_change (EV_A_ fd, EVFILT_WRITE, EV_ADD, NOTE_EOF);
72 root 1.1 }
73    
74     static void
75 root 1.6 kqueue_poll (EV_P_ ev_tstamp timeout)
76 root 1.1 {
77     int res, i;
78     struct timespec ts;
79    
80 root 1.14 /* need to resize so there is enough space for errors */
81     if (kqueue_changecnt > kqueue_eventmax)
82     {
83     ev_free (kqueue_events);
84 root 1.28 kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_changecnt);
85 root 1.15 kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
86 root 1.14 }
87    
88 root 1.1 ts.tv_sec = (time_t)timeout;
89 root 1.18 ts.tv_nsec = (long)((timeout - (ev_tstamp)ts.tv_sec) * 1e9);
90 root 1.25 res = kevent (backend_fd, kqueue_changes, kqueue_changecnt, kqueue_events, kqueue_eventmax, &ts);
91 root 1.6 kqueue_changecnt = 0;
92 root 1.1
93 root 1.29 if (expect_false (res < 0))
94 root 1.9 {
95     if (errno != EINTR)
96 root 1.10 syserr ("(libev) kevent");
97 root 1.9
98     return;
99     }
100 root 1.1
101     for (i = 0; i < res; ++i)
102     {
103 root 1.14 int fd = kqueue_events [i].ident;
104    
105 root 1.24 if (expect_false (kqueue_events [i].flags & EV_ERROR))
106 root 1.1 {
107 root 1.14 int err = kqueue_events [i].data;
108    
109     /* we are only interested in errors for fds that we are interested in :) */
110     if (anfds [fd].events)
111     {
112     if (err == ENOENT) /* resubmit changes on ENOENT */
113     kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
114     else if (err == EBADF) /* on EBADF, we re-check the fd */
115     {
116     if (fd_valid (fd))
117     kqueue_modify (EV_A_ fd, 0, anfds [fd].events);
118     else
119     fd_kill (EV_A_ fd);
120     }
121     else /* on all other errors, we error out on the fd */
122     fd_kill (EV_A_ fd);
123     }
124 root 1.1 }
125     else
126 root 1.3 fd_event (
127 root 1.6 EV_A_
128 root 1.14 fd,
129 root 1.6 kqueue_events [i].filter == EVFILT_READ ? EV_READ
130     : kqueue_events [i].filter == EVFILT_WRITE ? EV_WRITE
131 root 1.1 : 0
132     );
133     }
134    
135 root 1.6 if (expect_false (res == kqueue_eventmax))
136 root 1.1 {
137 root 1.9 ev_free (kqueue_events);
138 root 1.28 kqueue_eventmax = array_nextsize (sizeof (struct kevent), kqueue_eventmax, kqueue_eventmax + 1);
139 root 1.15 kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
140 root 1.1 }
141     }
142    
143 root 1.27 int inline_size
144 root 1.6 kqueue_init (EV_P_ int flags)
145 root 1.1 {
146     /* Initalize the kernel queue */
147 root 1.25 if ((backend_fd = kqueue ()) < 0)
148 root 1.6 return 0;
149 root 1.1
150 root 1.25 fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
151 root 1.7
152 root 1.31 backend_fudge = 0.;
153 root 1.21 backend_modify = kqueue_modify;
154     backend_poll = kqueue_poll;
155 root 1.1
156 root 1.26 kqueue_eventmax = 64; /* initial number of events receivable per poll */
157 root 1.15 kqueue_events = (struct kevent *)ev_malloc (sizeof (struct kevent) * kqueue_eventmax);
158 root 1.6
159 root 1.7 kqueue_changes = 0;
160     kqueue_changemax = 0;
161     kqueue_changecnt = 0;
162    
163 root 1.20 return EVBACKEND_KQUEUE;
164 root 1.1 }
165    
166 root 1.27 void inline_size
167 root 1.7 kqueue_destroy (EV_P)
168     {
169 root 1.9 ev_free (kqueue_events);
170     ev_free (kqueue_changes);
171 root 1.7 }
172    
173 root 1.27 void inline_size
174 root 1.7 kqueue_fork (EV_P)
175     {
176 root 1.25 close (backend_fd);
177 root 1.10
178 root 1.25 while ((backend_fd = kqueue ()) < 0)
179 root 1.11 syserr ("(libev) kqueue");
180 root 1.10
181 root 1.25 fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
182 root 1.7
183     /* re-register interest in fds */
184 root 1.8 fd_rearm_all (EV_A);
185 root 1.7 }
186