src/server/daemon/event_linux.c

changeset 41
bb7a1f5a8b48
child 43
8ac56edb4e94
equal deleted inserted replaced
40:56cda23f48d4 41:bb7a1f5a8b48
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2011 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <sys/epoll.h>
33
34 #include "../util/systhr.h"
35 #include "../util/atomic.h"
36
37
38 #include "event.h"
39 #include "event_linux.h"
40
41
42 event_handler_t* evhandler_create(int numthreads) {
43 event_handler_t *ev = malloc(sizeof(event_handler_t));
44 if(ev == NULL) {
45 return NULL;
46 }
47
48 ev->ep = calloc(numthreads, sizeof(int));
49 if(ev->ep == NULL) {
50 free(ev);
51 return NULL;
52 }
53 ev->nep = numthreads;
54 ev->lep = 0;
55
56 /* create ports event threads */
57 for(int i=0;i<numthreads;i++) {
58 /* create port */
59 ev->ep[i] = epoll_create(64);
60 if(ev->ep[i] == 0) {
61 free(ev->ep);
62 free(ev);
63 return NULL;
64 }
65
66 /*
67 * start a new handler thread
68 * the thread needs the event port and a pointer to the event handler
69 */
70 ev_thr_conf_t *conf = malloc(sizeof(ev_thr_conf_t));
71 if(conf == NULL) {
72 free(ev->ep);
73 free(ev);
74 return NULL;
75 }
76 conf->handler = ev;
77 conf->ep = ev->ep[i];
78
79 ev_thr_conf_t *thrconf = malloc(sizeof(ev_thr_conf_t));
80 thrconf->handler = ev;
81 thrconf->ep= ev->ep[i];
82 systhread_start(0, 0, (thrstartfunc)ev_handle_events, thrconf);
83 /* TODO: error handling */
84 }
85
86 return ev;
87 }
88
89
90 void ev_handle_events(ev_thr_conf_t *conf) {
91 event_handler_t *ev = conf->handler;
92 int ep = conf->ep;
93
94 free(conf);
95
96 //port_event_t events[16];
97 struct epoll_event events[16];
98 struct timespec timeout;
99 timeout.tv_nsec = 0;
100 timeout.tv_sec = 600;
101
102 for(;;) {
103 /* wait for events */
104 int ret = epoll_wait(ep, events, 16, 0);
105 if(ret == -1) {
106 /* TODO: check for error */
107 perror("epoll_wait");
108 continue;
109 }
110
111 for(int i=0;i<ret;i++) {
112 event_t *event = events[i].data.ptr;
113 if(event->fn) {
114 if(!event->fn(ev, event)) {
115 // event fn returned 0 -> remove event from epoll
116 if(epoll_ctl(ep, EPOLL_CTL_DEL, event->object, NULL) != 0){
117 perror("epoll_ctl");
118 }
119 }
120 }
121 }
122 }
123 }
124
125 /* returns a event handler port */
126 int ev_get_port(event_handler_t *h) {
127 int nps = h->nep;
128 if(nps == 1) {
129 return h->ep[0];
130 }
131
132 int cp = h->lep % nps;
133 ws_atomic_inc32(&h->lep);
134
135 return h->ep[cp];
136 }
137
138 int ev_pollin(event_handler_t *h, int fd, event_t *event) {
139 event->object = (intptr_t)fd;
140 struct epoll_event epev;
141 epev.events = EPOLLIN | EPOLLET; /* input event, edge triggered */
142 epev.data.ptr = event;
143 return epoll_ctl(ev_get_port(h), EPOLL_CTL_ADD, fd, &epev);
144 }
145
146 int ev_pollout(event_handler_t *h, int fd, event_t *event) {
147 event->object = (intptr_t)fd;
148 struct epoll_event epev;
149 epev.events = EPOLLOUT | EPOLLET; /* input event, edge triggered */
150 epev.data.ptr = event;
151 return epoll_ctl(ev_get_port(h), EPOLL_CTL_ADD, fd, &epev);
152 }
153
154 int evt_send(event_handler_t *h, event_t *event) {
155 event->object = 0;
156 // TODO: implement using threadpool
157 fprintf(stderr, "Warning: evt_send not implemented\n");
158 return 0;
159 }

mercurial