src/server/daemon/event_linux.c

Tue, 13 Aug 2024 20:08:13 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 13 Aug 2024 20:08:13 +0200
changeset 546
5494c28db896
parent 545
720893ec7d48
child 547
280bf87c8689
permissions
-rw-r--r--

increase keep-alive timeout

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
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
106 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
107 EventHandler *h = (EventHandler*)ev;
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
108 int ep = ev->ep;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
110 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
111 Event* finished[EV_MAX_EVENTS];
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
113 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
114
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
115 int loop_ctn = 0;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 for(;;) {
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
117 // 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
118 // 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
119 int ret = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
120 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
121 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
122
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
123 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
124 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
125
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
126 // 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
127 // 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
128 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
129 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
130 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
131 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
132
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
133 queue->numevents = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
134 if(queue->next) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
135 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
136
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
137 // 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
138 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
139 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
140 queue->next = NULL;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
141 } else {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
142 free(queue);
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
143 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
144 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
145
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
146 ret = queue_len;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
147 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
148
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
149 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
150 } else {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
151 // wait for events
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
152 ret = epoll_wait(ep, events, EV_MAX_EVENTS, EV_IDLE_TIMEOUT * 1000);
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
153 if(ret == -1 && errno != EINTR) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
154 log_ereport(LOG_FAILURE, "epoll_wait failed: %s", strerror(errno));
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
155 continue;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
156 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 }
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
158
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
159 int numfinished = 0;
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
160 ev->base.numret = 0;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 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
162 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
163 if(!event) {
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
164 // 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
165
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
166 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
167 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
168 if(r > 0) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
169 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
170 } else {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
171 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
172 }
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
173
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
174 continue;
533
2d6099ba2a7c simplify ev_handle_events on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 531
diff changeset
175 }
2d6099ba2a7c simplify ev_handle_events on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 531
diff changeset
176
2d6099ba2a7c simplify ev_handle_events on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 531
diff changeset
177 if(event->fn) {
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
178 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
179 if(!event->fn(h, event)) {
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 // event fn returned 0 -> remove event from epoll
542
1327febf99c4 refactore keep alive handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 541
diff changeset
181 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
182 event->error = 1;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
183 log_ereport(
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
184 LOG_FAILURE,
152
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
185 "epoll_ctl failed: fd: %d error: %s",
8b85c5face66 improves sessionhandler logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
186 event->object,
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
187 strerror(errno));
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 }
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
189
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
190 // 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
191 // execute event->finish later
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
192 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
193 finished[numfinished++] = event;
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
194 //event->finish(ev, event);
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
195 }
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
196 } else {
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
197 if(saved_ev != event->events) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
198 // event type changed
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
199 struct epoll_event epev;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
200 epev.events = EPOLLET;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
201 epev.data.ptr = event;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
202
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
203 // adjust epoll events
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
204 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
205
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
206 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
207 log_ereport(
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
208 LOG_FAILURE,
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
209 "epoll_wait failed: %s",
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
210 strerror(errno));
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
211 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
212 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 }
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
216 // 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
217 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
218 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
219 // 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
220 if(finished[i]->finish) {
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
221 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
222 }
529
cd606400f0ba change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
223 }
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
224 // execute return calls
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
225 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
226 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
227 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
228 }
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
229
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
230 if(ret == 0 || ++loop_ctn >= EV_IDLE_LOOP_CTN) {
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
231 watchlist_check(&ev->base, 0);
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
232 loop_ctn = 0;
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
233 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
237 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
238 int e = EPOLLET;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
239 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
240 e |= EPOLLIN;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 }
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
242 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
243 e |= EPOLLOUT;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
244 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
245 return e;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
248 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
249 EventHandlerLinux *ev = (EventHandlerLinux*)h;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 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
251 event->events = EVENT_POLLIN;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 struct epoll_event epev;
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
253 epev.events = EPOLLIN | EPOLLRDHUP | EPOLLET; // input event, edge triggered
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 epev.data.ptr = event;
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
255 return epoll_ctl(ev->ep, EPOLL_CTL_ADD, fd, &epev);
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
258 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
259 EventHandlerLinux *ev = (EventHandlerLinux*)h;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 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
261 event->events = EVENT_POLLOUT;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 struct epoll_event epev;
543
3335f431a91b implement keep-alive timeout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
263 epev.events = EPOLLOUT | EPOLLRDHUP | EPOLLET; // input event, edge triggered
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 epev.data.ptr = event;
541
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
265 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
266 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
267
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
268 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
269 EventHandlerLinux *ev = (EventHandlerLinux*)h;
1e1fca11aaff refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 537
diff changeset
270 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
271 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
272
187
4384bfbb7e26 adds platform independent aio functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
273 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
274 EventHandlerLinux *ev = (EventHandlerLinux*)h;
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
275 event->object = 0;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
276 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
277
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
278 int err = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
279 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
280
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
281 // 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
282 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
283 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
284 // 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
285 // 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
286 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
287 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
288 ev->num_reserve--;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
289 } else {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
290 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
291 if(!block) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
292 block = NULL;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
293 err = 1;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
294 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
295 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
296
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
297 if(block) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
298 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
299 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
300
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
301 block->numevents = 0;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
302 block->next = NULL;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
303 }
192
6a145e13d933 replaces eventfd with pipe and closes aio branch
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 187
diff changeset
304 }
545
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
305
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
306 if(block) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
307 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
308 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
309
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
310
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
311 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
312
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
313 if(!err) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
314 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
315 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
316 if(r == 0) {
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
317 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
318 err = 1;
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
319 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
320 }
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
321
720893ec7d48 new linux event_send implementation, replace event pipes with eventfd
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
322 return err;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
325 // TODO: remove this fake aio
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
326 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
327 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
328 cb->result = result;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
329 if(result < 0) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
330 cb->result_errno = errno;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
331 }
187
4384bfbb7e26 adds platform independent aio functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
332 return event_send(cb->evhandler, cb->event);
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 }
185
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
334
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
335 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
336 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
337 cb->result = result;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
338 if(result < 0) {
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
339 cb->result_errno = errno;
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
340 }
187
4384bfbb7e26 adds platform independent aio functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
341 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
342 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
343
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
344
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
345 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
346 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
347 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
348
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
349 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
350 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
351 }
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
352
b4d7ccf4e06d implements new event handler and aio on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 152
diff changeset
353 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
354 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
355 }

mercurial