src/server/daemon/event_linux.c

Thu, 15 Aug 2024 22:38:03 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 15 Aug 2024 22:38:03 +0200
changeset 549
b26bec196a2e
parent 547
280bf87c8689
child 552
4ed0e46aa9dc
permissions
-rw-r--r--

fix http_stream_parse_chunk_header: check if the chunk starts with a digit

41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <errno.h>
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <sys/epoll.h>
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
33 #include <sys/eventfd.h>
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
35 #include <cx/array_list.h>
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
36 #include <cx/linked_list.h>
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
37
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "../util/systhr.h"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include "../util/atomic.h"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
41 #include "../util/io.h"
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include "event.h"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include "event_linux.h"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
531
9b15b1f72bef change nsapi_function_return behavior: move saf return to the end of the event cycle
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 529
diff changeset
46 #include "httprequest.h"
9b15b1f72bef change nsapi_function_return behavior: move saf return to the end of the event cycle
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 529
diff changeset
47
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
49 EVHandler* evhandler_create(EventHandlerConfig *cfg) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
50 EVHandler *ev = malloc(sizeof(EVHandler));
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
51 ev->current = 0;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
52 ev->instances = calloc(cfg->nthreads, sizeof(void*));
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
53 ev->numins = cfg->nthreads;
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
54
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
55 for(int i=0;i<cfg->nthreads;i++) {
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
56 EventHandlerLinux *handler = malloc(sizeof(EventHandlerLinux));
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
57 memset(handler, 0, sizeof(EventHandlerLinux));
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
58 ev->instances[i] = (EventHandler*)handler;
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
59
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
60 handler->ep = epoll_create(64);
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
61 if(handler->ep < 0) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
62 log_ereport(LOG_FAILURE, "evhandler_create: epoll_create: %s", strerror(errno));
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
63 return NULL;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
64 }
192
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
65
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
66 handler->event_fd = eventfd(0, EFD_NONBLOCK);
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
67 if(handler->event_fd < 0) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
68 log_ereport(LOG_FAILURE, "evhandler_create: eventfd: %s", strerror(errno));
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
69 return NULL;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
70 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
71
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
72 EventQueue *queue = malloc(sizeof(EventQueue));
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
73 if(!queue) {
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
74 return NULL;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
75 }
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
76 queue->numevents = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
77 queue->next = NULL;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
78 handler->queue_begin = queue;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
79 handler->queue_end = queue;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
80 handler->num_reserve = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
81
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
82 if(pthread_mutex_init(&handler->queue_lock, NULL)) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
83 log_ereport(LOG_FAILURE, "evhandler_create: cannot initialize mutex");
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
84 return NULL;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
85 }
192
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
86
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
87 struct epoll_event epev;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
88 epev.events = EPOLLIN | EPOLLET; // input event, edge triggered
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
89 epev.data.ptr = NULL;
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
90 if(epoll_ctl(handler->ep, EPOLL_CTL_ADD, handler->event_fd, &epev)) {
474
eb7640c59e9b fix broken log_ereport call
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 443
diff changeset
91 log_ereport(LOG_FAILURE, "evhandler_create: epoll_ctl: %s", strerror(errno));
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 return NULL;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
95 SYS_THREAD t = systhread_start(
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
96 0,
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
97 0,
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
98 (thrstartfunc)ev_handle_events,
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
99 handler);
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
100 systhread_detach(t);
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 193
diff changeset
101 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 return ev;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
547
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
106 static volatile int ev_close = 0;
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
107
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
108 void ev_instance_close(EventHandler *h) {
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
109 EventHandlerLinux *ev = (EventHandlerLinux*)h;
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
110 ev_close = 1;
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
111 close(ev->ep);
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
112 }
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
113
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
114 // unique event addr that indicates shutdown
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
115 static Event shutdown_event;
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
116 void ev_instance_shutdown(EventHandler *h) {
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
117 event_send(h, &shutdown_event);
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
118 }
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
119
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
120 void ev_handle_events(EventHandlerLinux *ev) {
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
121 EventHandler *h = (EventHandler*)ev;
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
122 int ep = ev->ep;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
124 struct epoll_event events[EV_MAX_EVENTS];
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
125 Event* finished[EV_MAX_EVENTS];
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
127 size_t queue_len = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
128
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
129 int loop_ctn = 0;
547
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
130 int ev_shutdown = 0;
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
131 while(!ev_shutdown) {
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
132 // if ev->event_queue contains events, we process them first
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
133 // otherwise we get events from epoll
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
134 int ret = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
135 if(queue_len > 0) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
136 pthread_mutex_lock(&ev->queue_lock);
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
137
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
138 EventQueue *queue = ev->queue_begin;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
139 queue_len = queue->numevents;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
140
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
141 // queue_len cannot be bigger than EV_MAX_EVENTS
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
142 // get events from the queue
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
143 for(int i=0;i<queue_len;i++) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
144 events[i].events = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
145 events[i].data.ptr = queue->events[i];
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
146 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
147
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
148 queue->numevents = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
149 if(queue->next) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
150 ev->queue_begin = ev->queue_begin->next;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
151
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
152 // more than 1 queue block available, remove first block
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
153 if(ev->num_reserve < EV_QUEUE_RESERVE) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
154 ev->reserve_block[ev->num_reserve++] = queue;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
155 queue->next = NULL;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
156 } else {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
157 free(queue);
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
158 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
159 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
160
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
161 ret = queue_len;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
162 queue_len = ev->queue_begin->numevents;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
163
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
164 pthread_mutex_unlock(&ev->queue_lock);
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
165 } else {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
166 // wait for events
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
167 ret = epoll_wait(ep, events, EV_MAX_EVENTS, EV_IDLE_TIMEOUT * 1000);
547
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
168 if(ret == -1) {
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
169 if(errno != EINTR) {
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
170 if(!ev_close) {
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
171 log_ereport(LOG_CATASTROPHE, "epoll_wait failed: %s", strerror(errno));
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
172 }
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
173 break;
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
174 }
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
175 continue;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
176 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 }
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
178
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
179 int numfinished = 0;
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
180 ev->base.numret = 0;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 for(int i=0;i<ret;i++) {
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
182 Event *event = events[i].data.ptr;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
183 if(!event) {
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
184 // the only epoll_event without Event ptr is from eventfd
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
185
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
186 uint64_t eventfd_r = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
187 ssize_t r = read(ev->event_fd, &eventfd_r, sizeof(uint64_t));
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
188 if(r > 0) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
189 queue_len = eventfd_r;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
190 } else {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
191 log_ereport(LOG_FAILURE, "eventhandler: eventfd read failed: %s", strerror(errno));
192
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
192 }
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
193
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
194 continue;
533
2d6099ba2a7c simplify ev_handle_events on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 531
diff changeset
195 }
2d6099ba2a7c simplify ev_handle_events on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 531
diff changeset
196
2d6099ba2a7c simplify ev_handle_events on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 531
diff changeset
197 if(event->fn) {
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
198 int saved_ev = event->events;
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
199 if(!event->fn(h, event)) {
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 // event fn returned 0 -> remove event from epoll
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 541
diff changeset
201 if(saved_ev && epoll_ctl(ep, EPOLL_CTL_DEL, event->object, NULL)) {
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
202 event->error = 1;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
203 log_ereport(
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
204 LOG_FAILURE,
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
205 "epoll_ctl failed: fd: %d error: %s",
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
206 event->object,
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
207 strerror(errno));
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 }
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
209
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
210 // if set, remember this event and
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
211 // execute event->finish later
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
212 if(event->finish) {
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
213 finished[numfinished++] = event;
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
214 //event->finish(ev, event);
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
215 }
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
216 } else {
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
217 if(saved_ev != event->events) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
218 // event type changed
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
219 struct epoll_event epev;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
220 epev.events = EPOLLET;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
221 epev.data.ptr = event;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
222
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
223 // adjust epoll events
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
224 epev.events = ev_convert2sys_events(event->events);
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
225
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
226 if(epoll_ctl(ep, EPOLL_CTL_MOD, event->object, &epev)) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
227 log_ereport(
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
228 LOG_FAILURE,
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
229 "epoll_wait failed: %s",
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
230 strerror(errno));
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
231 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
232 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 }
547
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
234 } else if(event == &shutdown_event) {
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
235 ev_instance_close(h);
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 }
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
238 // call event finish handlers
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
239 for(int i=0;i<numfinished;i++) {
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
240 Event *event = finished[i];
537
ad44e72fbf50 add extra nullptr check in the event loop to handle the case when the finish ptr is set to NULL after it was already scheduled
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 533
diff changeset
241 // check again if the finish callback is set
ad44e72fbf50 add extra nullptr check in the event loop to handle the case when the finish ptr is set to NULL after it was already scheduled
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 533
diff changeset
242 if(finished[i]->finish) {
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
243 finished[i]->finish(h, event);
537
ad44e72fbf50 add extra nullptr check in the event loop to handle the case when the finish ptr is set to NULL after it was already scheduled
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 533
diff changeset
244 }
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
245 }
531
9b15b1f72bef change nsapi_function_return behavior: move saf return to the end of the event cycle
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 529
diff changeset
246 // execute return calls
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
247 for(int i=0;i<ev->base.numret;i++) {
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
248 EVReturn ret = ev->base.fnreturn[i];
531
9b15b1f72bef change nsapi_function_return behavior: move saf return to the end of the event cycle
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 529
diff changeset
249 nsapi_saf_return(ret.sn, ret.rq, ret.ret);
9b15b1f72bef change nsapi_function_return behavior: move saf return to the end of the event cycle
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 529
diff changeset
250 }
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
251
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
252 if(ret == 0 || ++loop_ctn >= EV_IDLE_LOOP_CTN) {
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
253 watchlist_check(&ev->base, 0);
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
254 loop_ctn = 0;
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
255 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 }
547
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
257
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
258 // epoll fd is already closed
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
259
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
260 ev_queue_free(ev->queue_begin);
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
261 pthread_mutex_destroy(&ev->queue_lock);
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
262 close(ev->event_fd);
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
263 free(ev);
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
264 }
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
265
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
266 void ev_queue_free(EventQueue *queue) {
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
267 while(queue) {
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
268 EventQueue *next = queue->next;
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
269 free(queue);
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
270 queue = next;
280bf87c8689 add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 545
diff changeset
271 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
274 int ev_convert2sys_events(int events) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
275 int e = EPOLLET;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
276 if((events & EVENT_POLLIN) == EVENT_POLLIN) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
277 e |= EPOLLIN;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 }
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
279 if((events & EVENT_POLLOUT) == EVENT_POLLOUT) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
280 e |= EPOLLOUT;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
281 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
282 return e;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
285 int ev_pollin(EventHandler *h, int fd, Event *event) {
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
286 EventHandlerLinux *ev = (EventHandlerLinux*)h;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 event->object = (intptr_t)fd;
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
288 event->events = EVENT_POLLIN;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 struct epoll_event epev;
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
290 epev.events = EPOLLIN | EPOLLRDHUP | EPOLLET; // input event, edge triggered
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 epev.data.ptr = event;
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
292 return epoll_ctl(ev->ep, EPOLL_CTL_ADD, fd, &epev);
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
295 int ev_pollout(EventHandler *h, int fd, Event *event) {
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
296 EventHandlerLinux *ev = (EventHandlerLinux*)h;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 event->object = (intptr_t)fd;
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
298 event->events = EVENT_POLLOUT;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 struct epoll_event epev;
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
300 epev.events = EPOLLOUT | EPOLLRDHUP | EPOLLET; // input event, edge triggered
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 epev.data.ptr = event;
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
302 return epoll_ctl(ev->ep, EPOLL_CTL_ADD, fd, &epev);
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
303 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
304
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
305 int ev_remove_poll(EventHandler *h, int fd) {
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
306 EventHandlerLinux *ev = (EventHandlerLinux*)h;
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
307 return epoll_ctl(ev->ep, EPOLL_CTL_DEL, fd, NULL);
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
308 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
309
187
4384bfbb7e26 adds platform independent aio functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
310 int event_send(EventHandler *h, Event *event) {
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
311 EventHandlerLinux *ev = (EventHandlerLinux*)h;
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
312 event->object = 0;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
313 event->events = 0;
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
314
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
315 int err = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
316 pthread_mutex_lock(&ev->queue_lock);
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
317
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
318 // add event to the last block
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
319 EventQueue *block = ev->queue_end;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
320 if(block->numevents >= EV_MAX_EVENTS) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
321 // last block is full
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
322 // create a new block or just use a reserved block
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
323 if(ev->num_reserve > 0) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
324 block = ev->reserve_block[ev->num_reserve-1];
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
325 ev->num_reserve--;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
326 } else {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
327 block = malloc(sizeof(EventQueue));
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
328 if(!block) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
329 block = NULL;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
330 err = 1;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
331 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
332 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
333
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
334 if(block) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
335 ev->queue_end->next = block;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
336 ev->queue_end = block;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
337
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
338 block->numevents = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
339 block->next = NULL;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
340 }
192
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
341 }
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
342
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
343 if(block) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
344 block->events[block->numevents++] = event;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
345 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
346
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
347
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
348 pthread_mutex_unlock(&ev->queue_lock);
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
349
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
350 if(!err) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
351 uint64_t data = 1;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
352 ssize_t r = write(ev->event_fd, &data, sizeof(uint64_t));
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
353 if(r == 0) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
354 log_ereport(LOG_FAILURE, "eventhandler: failed to send event: %s", strerror(errno));
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
355 err = 1;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
356 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
357 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
358
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
359 return err;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
362 // TODO: remove this fake aio
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
363 int ev_aioread(int fd, aiocb_s *cb) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
364 ssize_t result = pread(fd, cb->buf, cb->nbytes, cb->offset);
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
365 cb->result = result;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
366 if(result < 0) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
367 cb->result_errno = errno;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
368 }
187
4384bfbb7e26 adds platform independent aio functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
369 return event_send(cb->evhandler, cb->event);
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 }
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
371
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
372 int ev_aiowrite(int fd, aiocb_s *cb) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
373 ssize_t result = pwrite(fd, cb->buf, cb->nbytes, cb->offset);
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
374 cb->result = result;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
375 if(result < 0) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
376 cb->result_errno = errno;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
377 }
187
4384bfbb7e26 adds platform independent aio functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
378 return event_send(cb->evhandler, cb->event);
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
379 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
380
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
381
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
382 int event_pollin(EventHandler *ev, SYS_NETFD fd, Event *event) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
383 return ((IOStream*)fd)->poll(fd, ev, IO_POLL_IN, event);
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
384 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
385
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
386 int event_pollout(EventHandler *ev, SYS_NETFD fd, Event *event) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
387 return ((IOStream*)fd)->poll(fd, ev, IO_POLL_OUT, event);
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
388 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
389
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
390 int event_removepoll(EventHandler *ev, SYS_NETFD fd) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
391 return ((IOStream*)fd)->poll(fd, ev, IO_POLL_NONE, NULL);
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
392 }

mercurial