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; |
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 } |