ah: reuse at end of transaction has no timeout
[platform/upstream/libwebsockets.git] / lib / libev.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2014 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 void lws_feature_status_libev(struct lws_context_creation_info *info)
25 {
26         if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBEV))
27                 lwsl_notice("libev support compiled in and enabled\n");
28         else
29                 lwsl_notice("libev support compiled in but disabled\n");
30 }
31
32 static void
33 lws_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
34 {
35         struct lws_io_watcher *lws_io = lws_container_of(watcher,
36                                         struct lws_io_watcher, ev_watcher);
37         struct lws_context *context = lws_io->context;
38         struct lws_pollfd eventfd;
39
40         if (revents & EV_ERROR)
41                 return;
42
43         eventfd.fd = watcher->fd;
44         eventfd.events = 0;
45         eventfd.revents = EV_NONE;
46         if (revents & EV_READ) {
47                 eventfd.events |= LWS_POLLIN;
48                 eventfd.revents |= LWS_POLLIN;
49         }
50         if (revents & EV_WRITE) {
51                 eventfd.events |= LWS_POLLOUT;
52                 eventfd.revents |= LWS_POLLOUT;
53         }
54         lws_service_fd(context, &eventfd);
55 }
56
57 LWS_VISIBLE void
58 lws_ev_sigint_cb(struct ev_loop *loop, struct ev_signal *watcher, int revents)
59 {
60         ev_break(loop, EVBREAK_ALL);
61 }
62
63 LWS_VISIBLE int
64 lws_ev_sigint_cfg(struct lws_context *context, int use_ev_sigint,
65                   lws_ev_signal_cb_t *cb)
66 {
67         context->use_ev_sigint = use_ev_sigint;
68         if (cb)
69                 context->lws_ev_sigint_cb = cb;
70         else
71                 context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
72
73         return 0;
74 }
75
76 LWS_VISIBLE int
77 lws_ev_initloop(struct lws_context *context, struct ev_loop *loop, int tsi)
78 {
79         struct ev_signal *w_sigint = &context->pt[tsi].w_sigint.ev_watcher;
80         struct ev_io *w_accept = &context->pt[tsi].w_accept.ev_watcher;
81         struct lws_vhost *vh = context->vhost_list;
82         const char * backend_name;
83         int status = 0;
84         int backend;
85
86         if (!loop)
87                 loop = ev_loop_new(0);
88         else
89                 context->pt[tsi].ev_loop_foreign = 1;
90
91         context->pt[tsi].io_loop_ev = loop;
92
93         /*
94          * Initialize the accept w_accept with all the listening sockets
95          * and register a callback for read operations
96          */
97         while (vh) {
98                 if (vh->lserv_wsi) {
99                         vh->lserv_wsi->w_read.context = context;
100                         ev_io_init(w_accept, lws_accept_cb, vh->lserv_wsi->desc.sockfd,
101                                   EV_READ);
102                 }
103                 vh = vh->vhost_next;
104         }
105         ev_io_start(context->pt[tsi].io_loop_ev, w_accept);
106
107         /* Register the signal watcher unless the user says not to */
108         if (context->use_ev_sigint) {
109                 ev_signal_init(w_sigint, context->lws_ev_sigint_cb, SIGINT);
110                 ev_signal_start(context->pt[tsi].io_loop_ev, w_sigint);
111         }
112         backend = ev_backend(loop);
113
114         switch (backend) {
115         case EVBACKEND_SELECT:
116                 backend_name = "select";
117                 break;
118         case EVBACKEND_POLL:
119                 backend_name = "poll";
120                 break;
121         case EVBACKEND_EPOLL:
122                 backend_name = "epoll";
123                 break;
124         case EVBACKEND_KQUEUE:
125                 backend_name = "kqueue";
126                 break;
127         case EVBACKEND_DEVPOLL:
128                 backend_name = "/dev/poll";
129                 break;
130         case EVBACKEND_PORT:
131                 backend_name = "Solaris 10 \"port\"";
132                 break;
133         default:
134                 backend_name = "Unknown libev backend";
135                 break;
136         }
137
138         lwsl_notice(" libev backend: %s\n", backend_name);
139
140         return status;
141 }
142
143 void
144 lws_libev_destroyloop(struct lws_context *context, int tsi)
145 {
146         struct lws_context_per_thread *pt = &context->pt[tsi];
147
148         if (!lws_check_opt(context->options, LWS_SERVER_OPTION_LIBEV))
149                 return;
150
151         if (!pt->io_loop_ev)
152                 return;
153
154         ev_io_stop(pt->io_loop_ev, &pt->w_accept.ev_watcher);
155         if (context->use_ev_sigint)
156                 ev_signal_stop(pt->io_loop_ev,
157                        &pt->w_sigint.ev_watcher);
158         if (!pt->ev_loop_foreign)
159                 ev_loop_destroy(pt->io_loop_ev);
160 }
161
162 LWS_VISIBLE void
163 lws_libev_accept(struct lws *new_wsi, lws_sock_file_fd_type desc)
164 {
165         struct lws_context *context = lws_get_context(new_wsi);
166         struct ev_io *r = &new_wsi->w_read.ev_watcher;
167         struct ev_io *w = &new_wsi->w_write.ev_watcher;
168         int fd;
169
170         if (!LWS_LIBEV_ENABLED(context))
171                 return;
172
173         if (new_wsi->mode == LWSCM_RAW_FILEDESC)
174                 fd = desc.filefd;
175         else
176                 fd = desc.sockfd;
177
178         new_wsi->w_read.context = context;
179         new_wsi->w_write.context = context;
180         ev_io_init(r, lws_accept_cb, fd, EV_READ);
181         ev_io_init(w, lws_accept_cb, fd, EV_WRITE);
182 }
183
184 LWS_VISIBLE void
185 lws_libev_io(struct lws *wsi, int flags)
186 {
187         struct lws_context *context = lws_get_context(wsi);
188         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
189
190         if (!LWS_LIBEV_ENABLED(context))
191                 return;
192
193         if (!pt->io_loop_ev)
194                 return;
195
196         assert((flags & (LWS_EV_START | LWS_EV_STOP)) &&
197                (flags & (LWS_EV_READ | LWS_EV_WRITE)));
198
199         if (flags & LWS_EV_START) {
200                 if (flags & LWS_EV_WRITE)
201                         ev_io_start(pt->io_loop_ev, &wsi->w_write.ev_watcher);
202                 if (flags & LWS_EV_READ)
203                         ev_io_start(pt->io_loop_ev, &wsi->w_read.ev_watcher);
204         } else {
205                 if (flags & LWS_EV_WRITE)
206                         ev_io_stop(pt->io_loop_ev, &wsi->w_write.ev_watcher);
207                 if (flags & LWS_EV_READ)
208                         ev_io_stop(pt->io_loop_ev, &wsi->w_read.ev_watcher);
209         }
210 }
211
212 LWS_VISIBLE int
213 lws_libev_init_fd_table(struct lws_context *context)
214 {
215         int n;
216
217         if (!LWS_LIBEV_ENABLED(context))
218                 return 0;
219
220         for (n = 0; n < context->count_threads; n++) {
221                 context->pt[n].w_accept.context = context;
222                 context->pt[n].w_sigint.context = context;
223         }
224
225         return 1;
226 }
227
228 LWS_VISIBLE void
229 lws_libev_run(const struct lws_context *context, int tsi)
230 {
231         if (context->pt[tsi].io_loop_ev && LWS_LIBEV_ENABLED(context))
232                 ev_run(context->pt[tsi].io_loop_ev, 0);
233 }