src/server/daemon/event_linux.c

changeset 541
1e1fca11aaff
parent 537
ad44e72fbf50
child 542
1327febf99c4
equal deleted inserted replaced
540:d9c3c23c635b 541:1e1fca11aaff
48 ev->current = 0; 48 ev->current = 0;
49 ev->instances = calloc(cfg->nthreads, sizeof(void*)); 49 ev->instances = calloc(cfg->nthreads, sizeof(void*));
50 ev->numins = cfg->nthreads; 50 ev->numins = cfg->nthreads;
51 51
52 for(int i=0;i<cfg->nthreads;i++) { 52 for(int i=0;i<cfg->nthreads;i++) {
53 EventHandler *handler = malloc(sizeof(EventHandler)); 53 EventHandlerLinux *handler = malloc(sizeof(EventHandlerLinux));
54 ev->instances[i] = handler; 54 memset(handler, 0, sizeof(EventHandlerLinux));
55 ev->instances[i] = (EventHandler*)handler;
55 56
56 handler->ep = epoll_create(64); 57 handler->ep = epoll_create(64);
57 if(handler->ep < 0) { 58 if(handler->ep < 0) {
58 log_ereport(LOG_FAILURE, "evhandler_create: epoll_create: %s", strerror(errno)); 59 log_ereport(LOG_FAILURE, "evhandler_create: epoll_create: %s", strerror(errno));
59 return NULL; 60 return NULL;
84 } 85 }
85 86
86 return ev; 87 return ev;
87 } 88 }
88 89
89 void ev_handle_events(EventHandler *ev) { 90 void ev_handle_events(EventHandlerLinux *ev) {
91 EventHandler *h = (EventHandler*)ev;
90 int ep = ev->ep; 92 int ep = ev->ep;
91 93
92 struct epoll_event events[EV_MAX_EVENTS]; 94 struct epoll_event events[EV_MAX_EVENTS];
93 Event* finished[EV_MAX_EVENTS]; 95 Event* finished[EV_MAX_EVENTS];
94 96
99 log_ereport(LOG_FAILURE, "epoll_wait failed: %s", strerror(errno)); 101 log_ereport(LOG_FAILURE, "epoll_wait failed: %s", strerror(errno));
100 continue; 102 continue;
101 } 103 }
102 104
103 int numfinished = 0; 105 int numfinished = 0;
104 ev->numret = 0; 106 ev->base.numret = 0;
105 for(int i=0;i<ret;i++) { 107 for(int i=0;i<ret;i++) {
106 Event *event = events[i].data.ptr; 108 Event *event = events[i].data.ptr;
107 if(!event) { 109 if(!event) {
108 char ebuf[sizeof(Event*)]; 110 char ebuf[sizeof(Event*)];
109 int ebufpos = 0; 111 int ebufpos = 0;
129 } 131 }
130 } 132 }
131 133
132 if(event->fn) { 134 if(event->fn) {
133 int saved_ev = event->events; 135 int saved_ev = event->events;
134 if(!event->fn(ev, event)) { 136 if(!event->fn(h, event)) {
135 // event fn returned 0 -> remove event from epoll 137 // event fn returned 0 -> remove event from epoll
136 if(epoll_ctl(ep, EPOLL_CTL_DEL, event->object, NULL)) { 138 if(epoll_ctl(ep, EPOLL_CTL_DEL, event->object, NULL)) {
137 event->error = 1; 139 event->error = 1;
138 log_ereport( 140 log_ereport(
139 LOG_FAILURE, 141 LOG_FAILURE,
171 // call event finish handlers 173 // call event finish handlers
172 for(int i=0;i<numfinished;i++) { 174 for(int i=0;i<numfinished;i++) {
173 Event *event = finished[i]; 175 Event *event = finished[i];
174 // check again if the finish callback is set 176 // check again if the finish callback is set
175 if(finished[i]->finish) { 177 if(finished[i]->finish) {
176 finished[i]->finish(ev, event); 178 finished[i]->finish(h, event);
177 } 179 }
178 } 180 }
179 // execute return calls 181 // execute return calls
180 for(int i=0;i<ev->numret;i++) { 182 for(int i=0;i<ev->base.numret;i++) {
181 EVReturn ret = ev->fnreturn[i]; 183 EVReturn ret = ev->base.fnreturn[i];
182 nsapi_saf_return(ret.sn, ret.rq, ret.ret); 184 nsapi_saf_return(ret.sn, ret.rq, ret.ret);
183 } 185 }
184 } 186 }
185 } 187 }
186 188
194 } 196 }
195 return e; 197 return e;
196 } 198 }
197 199
198 int ev_pollin(EventHandler *h, int fd, Event *event) { 200 int ev_pollin(EventHandler *h, int fd, Event *event) {
201 EventHandlerLinux *ev = (EventHandlerLinux*)h;
199 event->object = (intptr_t)fd; 202 event->object = (intptr_t)fd;
200 event->events = EVENT_POLLIN; 203 event->events = EVENT_POLLIN;
201 struct epoll_event epev; 204 struct epoll_event epev;
202 epev.events = EPOLLIN | EPOLLET; // input event, edge triggered 205 epev.events = EPOLLIN | EPOLLET; // input event, edge triggered
203 epev.data.ptr = event; 206 epev.data.ptr = event;
204 return epoll_ctl(h->ep, EPOLL_CTL_ADD, fd, &epev); 207 return epoll_ctl(ev->ep, EPOLL_CTL_ADD, fd, &epev);
205 } 208 }
206 209
207 int ev_pollout(EventHandler *h, int fd, Event *event) { 210 int ev_pollout(EventHandler *h, int fd, Event *event) {
211 EventHandlerLinux *ev = (EventHandlerLinux*)h;
208 event->object = (intptr_t)fd; 212 event->object = (intptr_t)fd;
209 event->events = EVENT_POLLOUT; 213 event->events = EVENT_POLLOUT;
210 struct epoll_event epev; 214 struct epoll_event epev;
211 epev.events = EPOLLOUT | EPOLLET; // input event, edge triggered 215 epev.events = EPOLLOUT | EPOLLET; // input event, edge triggered
212 epev.data.ptr = event; 216 epev.data.ptr = event;
213 return epoll_ctl(h->ep, EPOLL_CTL_ADD, fd, &epev); 217 return epoll_ctl(ev->ep, EPOLL_CTL_ADD, fd, &epev);
214 } 218 }
215 219
216 int ev_remove_poll(EventHandler *h, int fd) { 220 int ev_remove_poll(EventHandler *h, int fd) {
217 return epoll_ctl(h->ep, EPOLL_CTL_DEL, fd, NULL); 221 EventHandlerLinux *ev = (EventHandlerLinux*)h;
222 return epoll_ctl(ev->ep, EPOLL_CTL_DEL, fd, NULL);
218 } 223 }
219 224
220 int event_send(EventHandler *h, Event *event) { 225 int event_send(EventHandler *h, Event *event) {
226 EventHandlerLinux *ev = (EventHandlerLinux*)h;
221 event->object = 0; 227 event->object = 0;
222 event->events = 0; 228 event->events = 0;
223 ssize_t r = write(h->eventout, &event, sizeof(Event*)); 229 ssize_t r = write(ev->eventout, &event, sizeof(Event*));
224 if(r < sizeof(Event*)) { 230 if(r < sizeof(Event*)) {
225 log_ereport(LOG_FAILURE, "failed to send event: %s", strerror(errno)); 231 log_ereport(LOG_FAILURE, "failed to send event: %s", strerror(errno));
226 } 232 }
227 return r > 0 ? 0 : 1; 233 return r > 0 ? 0 : 1;
228 } 234 }
256 } 262 }
257 263
258 int event_removepoll(EventHandler *ev, SYS_NETFD fd) { 264 int event_removepoll(EventHandler *ev, SYS_NETFD fd) {
259 return ((IOStream*)fd)->poll(fd, ev, IO_POLL_NONE, NULL); 265 return ((IOStream*)fd)->poll(fd, ev, IO_POLL_NONE, NULL);
260 } 266 }
261
262
263 void ev_saf_return(EventHandler *h, Session *sn, Request *rq, int ret) {
264 h->fnreturn[h->numret++] = (EVReturn){ sn, rq, ret };
265 }

mercurial