root/src/eventer/eventer_epoll_impl.c

Revision 8fa2f7461cad23707774137d82e4b2b9e6e01c57, 8.6 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

whence is invalid on fd events. We may use this for something clever later, so we want to explode if anyone is accidentally use it now.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34 #include "eventer/eventer.h"
35 #include "utils/noit_atomic.h"
36 #include "utils/noit_skiplist.h"
37 #include "utils/noit_log.h"
38
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <sys/epoll.h>
43 #include <signal.h>
44 #include <pthread.h>
45 #include <assert.h>
46
47 struct _eventer_impl eventer_epoll_impl;
48 #define LOCAL_EVENTER eventer_epoll_impl
49 #define LOCAL_EVENTER_foreach_fdevent eventer_epoll_impl_foreach_fdevent
50 #define maxfds LOCAL_EVENTER.maxfds
51 #define master_fds LOCAL_EVENTER.master_fds
52
53 #include "eventer/eventer_impl_private.h"
54
55 static pthread_t master_thread;
56 static int *masks;
57 static int epoll_fd = -1;
58
59 static int eventer_epoll_impl_init() {
60   struct rlimit rlim;
61   int rv;
62
63   /* super init */
64   if((rv = eventer_impl_init()) != 0) return rv;
65
66   master_thread = pthread_self();
67   signal(SIGPIPE, SIG_IGN);
68   epoll_fd = epoll_create(1024);
69   if(epoll_fd == -1) {
70     return -1;
71   }
72   getrlimit(RLIMIT_NOFILE, &rlim);
73   maxfds = rlim.rlim_cur;
74   master_fds = calloc(maxfds, sizeof(*master_fds));
75   masks = calloc(maxfds, sizeof(*masks));
76   return 0;
77 }
78 static int eventer_epoll_impl_propset(const char *key, const char *value) {
79   if(eventer_impl_propset(key, value)) {
80     /* Do our epoll local properties here */
81     return -1;
82   }
83   return 0;
84 }
85 static void eventer_epoll_impl_add(eventer_t e) {
86   struct epoll_event _ev;
87   ev_lock_state_t lockstate;
88   assert(e->mask);
89
90   if(e->mask & EVENTER_ASYNCH) {
91     eventer_add_asynch(NULL, e);
92     return;
93   }
94
95   /* Recurrent delegation */
96   if(e->mask & EVENTER_RECURRENT) {
97     eventer_add_recurrent(e);
98     return;
99   }
100
101   /* Timed events are simple */
102   if(e->mask & EVENTER_TIMER) {
103     eventer_add_timed(e);
104     return;
105   }
106
107   /* file descriptor event */
108   assert(e->whence.tv_sec == 0 && e->whence.tv_usec == 0);
109   memset(&_ev, 0, sizeof(_ev));
110   _ev.data.fd = e->fd;
111   if(e->mask & EVENTER_READ) _ev.events |= (EPOLLIN|EPOLLPRI);
112   if(e->mask & EVENTER_WRITE) _ev.events |= (EPOLLOUT);
113   if(e->mask & EVENTER_EXCEPTION) _ev.events |= (EPOLLERR|EPOLLHUP);
114
115   lockstate = acquire_master_fd(e->fd);
116   master_fds[e->fd].e = e;
117
118   epoll_ctl(epoll_fd, EPOLL_CTL_ADD, e->fd, &_ev);
119
120   release_master_fd(e->fd, lockstate);
121 }
122 static eventer_t eventer_epoll_impl_remove(eventer_t e) {
123   eventer_t removed = NULL;
124   if(e->mask & EVENTER_ASYNCH) {
125     abort();
126   }
127   if(e->mask & (EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION)) {
128     ev_lock_state_t lockstate;
129     struct epoll_event _ev;
130     memset(&_ev, 0, sizeof(_ev));
131     _ev.data.fd = e->fd;
132     lockstate = acquire_master_fd(e->fd);
133     if(e == master_fds[e->fd].e) {
134       removed = e;
135       master_fds[e->fd].e = NULL;
136       epoll_ctl(epoll_fd, EPOLL_CTL_DEL, e->fd, &_ev);
137     }
138     release_master_fd(e->fd, lockstate);
139   }
140   else if(e->mask & EVENTER_TIMER) {
141     removed = eventer_remove_timed(e);
142   }
143   else if(e->mask & EVENTER_RECURRENT) {
144     removed = eventer_remove_recurrent(e);
145   }
146   else {
147     abort();
148   }
149   return removed;
150 }
151 static void eventer_epoll_impl_update(eventer_t e, int mask) {
152   struct epoll_event _ev;
153   if(e->mask & EVENTER_TIMER) {
154     eventer_update_timed(e,mask);
155     return;
156   }
157   memset(&_ev, 0, sizeof(_ev));
158   _ev.data.fd = e->fd;
159   e->mask = mask;
160   if(e->mask & (EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION)) {
161     if(e->mask & EVENTER_READ) _ev.events |= (EPOLLIN|EPOLLPRI);
162     if(e->mask & EVENTER_WRITE) _ev.events |= (EPOLLOUT);
163     if(e->mask & EVENTER_EXCEPTION) _ev.events |= (EPOLLERR|EPOLLHUP);
164     epoll_ctl(epoll_fd, EPOLL_CTL_MOD, e->fd, &_ev);
165   }
166 }
167 static eventer_t eventer_epoll_impl_remove_fd(int fd) {
168   eventer_t eiq = NULL;
169   ev_lock_state_t lockstate;
170   if(master_fds[fd].e) {
171     struct epoll_event _ev;
172     memset(&_ev, 0, sizeof(_ev));
173     _ev.data.fd = fd;
174     lockstate = acquire_master_fd(fd);
175     eiq = master_fds[fd].e;
176     master_fds[fd].e = NULL;
177     epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &_ev);
178     release_master_fd(fd, lockstate);
179   }
180   return eiq;
181 }
182 static eventer_t eventer_epoll_impl_find_fd(int fd) {
183   return master_fds[fd].e;
184 }
185
186 static void eventer_epoll_impl_trigger(eventer_t e, int mask) {
187   struct timeval __now;
188   int fd, oldmask, newmask;
189   const char *cbname;
190   ev_lock_state_t lockstate;
191
192   fd = e->fd;
193   if(e != master_fds[fd].e) return;
194   lockstate = acquire_master_fd(fd);
195   if(lockstate == EV_ALREADY_OWNED) return;
196   assert(lockstate == EV_OWNED);
197
198   gettimeofday(&__now, NULL);
199   oldmask = e->mask;
200   cbname = eventer_name_for_callback(e->callback);
201   noitLT(eventer_deb, &__now, "epoll: fire on %d/%x to %s(%p)\n",
202          fd, mask, cbname?cbname:"???", e->callback);
203   newmask = e->callback(e, mask, e->closure, &__now);
204
205   if(newmask) {
206     struct epoll_event _ev;
207     memset(&_ev, 0, sizeof(_ev));
208     _ev.data.fd = fd;
209     if(newmask & EVENTER_READ) _ev.events |= (EPOLLIN|EPOLLPRI);
210     if(newmask & EVENTER_WRITE) _ev.events |= (EPOLLOUT);
211     if(newmask & EVENTER_EXCEPTION) _ev.events |= (EPOLLERR|EPOLLHUP);
212     epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &_ev);
213     /* Set our mask */
214     e->mask = newmask;
215   }
216   else {
217     /* see kqueue implementation for details on the next line */
218     if(master_fds[fd].e == e) master_fds[fd].e = NULL;
219     eventer_free(e);
220   }
221   release_master_fd(fd, lockstate);
222 }
223 static int eventer_epoll_impl_loop() {
224   int is_master_thread = 0;
225   struct epoll_event *epev;
226   pthread_t self;
227
228   self = pthread_self();
229   if(pthread_equal(self, master_thread)) is_master_thread = 1;
230
231   epev = malloc(sizeof(*epev) * maxfds);
232
233   while(1) {
234     struct timeval __now, __sleeptime;
235     int fd_cnt = 0;
236
237     __sleeptime = eventer_max_sleeptime;
238
239     eventer_dispatch_timed(&__now, &__sleeptime);
240
241     /* Handle recurrent events */
242     eventer_dispatch_recurrent(&__now);
243
244     /* Now we move on to our fd-based events */
245     fd_cnt = epoll_wait(epoll_fd, epev, maxfds,
246                         __sleeptime.tv_sec * 1000 + __sleeptime.tv_usec / 1000);
247     noitLT(eventer_deb, &__now, "debug: epoll_wait(%d, [], %d) => %d\n", epoll_fd, maxfds, fd_cnt);
248     if(fd_cnt < 0) {
249       noitLT(eventer_err, &__now, "epoll_wait: %s\n", strerror(errno));
250     }
251     else {
252       int idx;
253       /* loop once to clear */
254       for(idx = 0; idx < fd_cnt; idx++) {
255         struct epoll_event *ev;
256         eventer_t e;
257         int fd, mask = 0;
258
259         ev = &epev[idx];
260
261         if(ev->events & (EPOLLIN | EPOLLPRI)) mask |= EVENTER_READ;
262         if(ev->events & (EPOLLOUT)) mask |= EVENTER_WRITE;
263         if(ev->events & (EPOLLERR|EPOLLHUP)) mask |= EVENTER_EXCEPTION;
264
265         fd = ev->data.fd;
266
267         e = master_fds[fd].e;
268         /* It's possible that someone removed the event and freed it
269          * before we got here.
270          */
271         if(!e) continue;
272
273         eventer_epoll_impl_trigger(e, mask);
274       }
275     }
276   }
277   /* NOTREACHED */
278   return 0;
279 }
280
281 struct _eventer_impl eventer_epoll_impl = {
282   "epoll",
283   eventer_epoll_impl_init,
284   eventer_epoll_impl_propset,
285   eventer_epoll_impl_add,
286   eventer_epoll_impl_remove,
287   eventer_epoll_impl_update,
288   eventer_epoll_impl_remove_fd,
289   eventer_epoll_impl_find_fd,
290   eventer_epoll_impl_trigger,
291   eventer_epoll_impl_loop,
292   eventer_epoll_impl_foreach_fdevent,
293   { 0, 200000 },
294   0,
295   NULL
296 };
Note: See TracBrowser for help on using the browser.