Sat, 24 Aug 2024 18:34:13 +0200
move first request poll to the event handler
69 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2013 Olaf Wintermann. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | ||
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
32 | #include "../util/atomic.h" |
433
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
33 | #include "../util/io.h" |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
34 | |
69 | 35 | #include "event_bsd.h" |
36 | ||
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
|
37 | #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
|
38 | |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
39 | EVHandler* evhandler_create(EventHandlerConfig *cfg) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
40 | EVHandler *ev = malloc(sizeof(EVHandler)); |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
41 | ev->current = 0; |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
42 | ev->instances = calloc(cfg->nthreads, sizeof(void*)); |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
43 | ev->numins = cfg->nthreads; |
69 | 44 | |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
45 | 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:
538
diff
changeset
|
46 | EventHandlerKqueue *handler = malloc(sizeof(EventHandlerKqueue)); |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
538
diff
changeset
|
47 | memset(handler, 0, sizeof(EventHandlerKqueue)); |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
48 | ev->instances[i] = handler; |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
49 | |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
50 | handler->kqueue = kqueue(); |
443
ef3c8a0e1fee
improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
433
diff
changeset
|
51 | if(handler->kqueue < 0) { |
ef3c8a0e1fee
improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
433
diff
changeset
|
52 | log_ereport(LOG_FAILURE, "evhandler_create: kqueue: %s", strerror(errno)); |
69 | 53 | return NULL; |
54 | } | |
55 | ||
552
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
56 | handler->thr = systhread_start( |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
57 | 0, |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
58 | 0, |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
59 | (thrstartfunc)ev_handle_events, |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
60 | handler); |
69 | 61 | } |
62 | ||
63 | return ev; | |
64 | } | |
65 | ||
552
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
66 | void ev_instance_wait(EventHandler *h) { |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
67 | EventHandlerLinux *ev = (EventHandlerLinux*)h; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
68 | void *ret; |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
69 | pthread_join(ev->thr, &ret); |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
70 | } |
4ed0e46aa9dc
add test for sending multiple events to an eventhandler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
547
diff
changeset
|
71 | |
547
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
72 | static volatile int ev_close = 0; |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
73 | |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
74 | void ev_instance_close(EventHandler *h) { |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
75 | EventHandlerKqueue *ev = (EventHandlerKqueue*)h; |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
76 | close(ev->kqueue); |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
77 | ev_close = 1; |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
78 | } |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
79 | |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
80 | // unique event addr that indicates shutdown |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
81 | static Event shutdown_event; |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
82 | void ev_instance_shutdown(EventHandler *h) { |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
83 | event_send(h, &shutdown_event); |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
84 | } |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
85 | |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
538
diff
changeset
|
86 | void ev_handle_events(EventHandlerKqueue *ev) { |
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
538
diff
changeset
|
87 | EventHandler *h = (EventHandler*)ev; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
88 | struct timespec timeout; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
89 | timeout.tv_nsec = 0; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
90 | timeout.tv_sec = 600; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
91 | |
529
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
92 | struct kevent events[EV_MAX_EVENTS]; |
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
93 | struct kevent changes[EV_MAX_EVENTS*2]; |
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
94 | Event *finished[EV_MAX_EVENTS]; |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
95 | int numchanges = 0; |
529
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
96 | int numfinished; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
97 | |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
98 | for(;;) { |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
99 | // wait for events |
529
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
100 | int nev = kevent(ev->kqueue, changes, numchanges, events, EV_MAX_EVENTS, &timeout); |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
101 | if(nev == -1) { |
547
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
102 | if(errno != EINTR) { |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
103 | if(!ev_close) { |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
104 | log_ereport(LOG_CATASTROPHE, "kevent failed: %s", strerror(errno)); |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
105 | } |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
106 | break; |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
107 | } |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
108 | continue; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
109 | } |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
110 | |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
111 | numchanges = 0; |
529
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
112 | int numfinished = 0; |
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
|
113 | ev->numret = 0; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
114 | for(int i=0;i<nev;i++) { |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
115 | Event *event = (Event*)events[i].udata; |
263
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
116 | if(!event) { |
266
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
117 | if(events[i].flags == 0) { |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
118 | log_ereport(LOG_WARN, "Unknown kevent (ident=%d)", (int)events[i].ident); |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
119 | } |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
120 | // don't warn in case flags is not 0, because socket EOF events |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
121 | // are triggered even if we apply EV_DELETE in the changelist |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
122 | // the only way to stop this is to apply the changelist without |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
123 | // getting new events, but that comes with a performance penalty |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
124 | |
263
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
125 | continue; |
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
126 | } |
342
ebd1e67c3d5f
fix invalid memory access in event_bsd.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
266
diff
changeset
|
127 | int event_events = event->events; |
263
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
128 | |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
129 | if(event->fn) { |
263
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
130 | int saved_ev = event->events; |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
538
diff
changeset
|
131 | if(!event->fn(h, event)) { |
263
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
132 | // ret 0 => remove event |
265
59f99038a576
fix wrong kqueue usage: split ws events into multiple kevent filters
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
264
diff
changeset
|
133 | |
263
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
134 | if(event->finish) { |
529
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
135 | finished[numfinished++] = event; |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
136 | } |
265
59f99038a576
fix wrong kqueue usage: split ws events into multiple kevent filters
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
264
diff
changeset
|
137 | |
342
ebd1e67c3d5f
fix invalid memory access in event_bsd.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
266
diff
changeset
|
138 | event_events = 0; |
ebd1e67c3d5f
fix invalid memory access in event_bsd.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
266
diff
changeset
|
139 | } else { |
ebd1e67c3d5f
fix invalid memory access in event_bsd.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
266
diff
changeset
|
140 | event_events = event->events; |
263
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
141 | } |
533f9097d712
fix kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
260
diff
changeset
|
142 | |
265
59f99038a576
fix wrong kqueue usage: split ws events into multiple kevent filters
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
264
diff
changeset
|
143 | // if events have changed, we need to add/remove filters |
342
ebd1e67c3d5f
fix invalid memory access in event_bsd.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
266
diff
changeset
|
144 | if(saved_ev != event_events) { |
ebd1e67c3d5f
fix invalid memory access in event_bsd.c
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
266
diff
changeset
|
145 | int e = event_events; |
265
59f99038a576
fix wrong kqueue usage: split ws events into multiple kevent filters
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
264
diff
changeset
|
146 | int e_fd = events[i].ident; |
59f99038a576
fix wrong kqueue usage: split ws events into multiple kevent filters
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
264
diff
changeset
|
147 | if((e & EVENT_POLLIN) != (saved_ev & EVENT_POLLIN)) { |
266
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
148 | if((e & EVENT_POLLIN) == EVENT_POLLIN) { |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
149 | // add |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
150 | EV_SET(&changes[numchanges++], e_fd, EVFILT_READ, EV_ADD, 0, 0, event); |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
151 | } else { |
538
f9a7b5c76208
replace EWOULDBLOCK with EAGAIN
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
537
diff
changeset
|
152 | // deleteh |
266
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
153 | EV_SET(&changes[numchanges++], e_fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
154 | } |
265
59f99038a576
fix wrong kqueue usage: split ws events into multiple kevent filters
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
264
diff
changeset
|
155 | } |
59f99038a576
fix wrong kqueue usage: split ws events into multiple kevent filters
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
264
diff
changeset
|
156 | if((e & EVENT_POLLOUT) != (saved_ev & EVENT_POLLOUT)) { |
266
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
157 | if((e & EVENT_POLLOUT) == EVENT_POLLOUT) { |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
158 | // add |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
159 | EV_SET(&changes[numchanges++], e_fd, EVFILT_WRITE, EV_ADD, 0, 0, event); |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
160 | } else { |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
161 | // delete |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
162 | EV_SET(&changes[numchanges++], e_fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
783467806e2d
fix kqueue event deletion
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
265
diff
changeset
|
163 | } |
265
59f99038a576
fix wrong kqueue usage: split ws events into multiple kevent filters
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
264
diff
changeset
|
164 | } |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
165 | } |
547
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
166 | } else if(event == &shutdown_event) { |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
167 | ev_instance_close(h); |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
168 | } |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
169 | } |
529
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
170 | // call event finish handlers |
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
171 | 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:
503
diff
changeset
|
172 | 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:
531
diff
changeset
|
173 | // 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:
531
diff
changeset
|
174 | if(finished[i]->finish) { |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
538
diff
changeset
|
175 | 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:
531
diff
changeset
|
176 | } |
529
cd606400f0ba
change execution order of event handler and event finish functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
503
diff
changeset
|
177 | } |
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
|
178 | // execute return calls |
541
1e1fca11aaff
refactore EventHandler: add common fields to base struct
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
538
diff
changeset
|
179 | 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:
538
diff
changeset
|
180 | 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
|
181 | 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
|
182 | } |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
183 | } |
547
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
184 | |
280bf87c8689
add functions to stop event handlers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
541
diff
changeset
|
185 | free(ev); |
69 | 186 | } |
187 | ||
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
188 | int ev_pollin(EventHandler *h, int fd, Event *event) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
189 | event->events = EVENT_POLLIN; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
190 | struct kevent kev; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
191 | EV_SET(&kev, fd, EVFILT_READ, EV_ADD, 0, 0, event); |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
192 | return kevent(h->kqueue, &kev, 1, NULL, 0, NULL); |
69 | 193 | } |
194 | ||
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
195 | int ev_pollout(EventHandler *h, int fd, Event *event) { |
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
196 | event->events = EVENT_POLLOUT; |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
197 | struct kevent kev; |
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
198 | EV_SET(&kev, fd, EVFILT_WRITE, EV_ADD, 0, 0, event); |
170
711d00eeed25
refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
133
diff
changeset
|
199 | return kevent(h->kqueue, &kev, 1, NULL, 0, NULL); |
69 | 200 | } |
201 | ||
260
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
202 | int ev_remove_poll(EventHandler *h, int fd) { |
264
81f202caea5b
add ev_remove_poll kqueue implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
263
diff
changeset
|
203 | struct kevent kev; |
81f202caea5b
add ev_remove_poll kqueue implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
263
diff
changeset
|
204 | EV_SET(&kev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); |
81f202caea5b
add ev_remove_poll kqueue implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
263
diff
changeset
|
205 | int r1 = kevent(h->kqueue, &kev, 1, NULL, 0, NULL); |
81f202caea5b
add ev_remove_poll kqueue implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
263
diff
changeset
|
206 | EV_SET(&kev, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
81f202caea5b
add ev_remove_poll kqueue implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
263
diff
changeset
|
207 | int r2 = kevent(h->kqueue, &kev, 1, NULL, 0, NULL); |
503
aeaf7db26fac
fix webdav mkcol error status codes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
443
diff
changeset
|
208 | // in case r1 or r2 was successful, we return 0 (no error) |
264
81f202caea5b
add ev_remove_poll kqueue implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
263
diff
changeset
|
209 | return r1 != -1 || r2 != -1 ? 0 : 1; |
260
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
210 | } |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
211 | |
187
4384bfbb7e26
adds platform independent aio functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
170
diff
changeset
|
212 | int event_send(EventHandler *h, Event *event) { |
109
8a0a7754f123
experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
79
diff
changeset
|
213 | return 0; |
69 | 214 | } |
260
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
215 | |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
216 | // TODO: remove this fake aio |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
217 | int ev_aioread(int fd, aiocb_s *cb) { |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
218 | ssize_t result = pread(fd, cb->buf, cb->nbytes, cb->offset); |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
219 | cb->result = result; |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
220 | if(result < 0) { |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
221 | cb->result_errno = errno; |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
222 | } |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
223 | return event_send(cb->evhandler, cb->event); |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
224 | } |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
225 | |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
226 | int ev_aiowrite(int fd, aiocb_s *cb) { |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
227 | ssize_t result = pwrite(fd, cb->buf, cb->nbytes, cb->offset); |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
228 | cb->result = result; |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
229 | if(result < 0) { |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
230 | cb->result_errno = errno; |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
231 | } |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
232 | return event_send(cb->evhandler, cb->event); |
4779a6fb4fbe
fix freebsd build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
233 | } |
433
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
234 | |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
235 | |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
236 | int event_pollin(EventHandler *ev, SYS_NETFD fd, Event *event) { |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
237 | return ((IOStream*)fd)->poll(fd, ev, IO_POLL_IN, event); |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
238 | } |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
239 | |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
240 | int event_pollout(EventHandler *ev, SYS_NETFD fd, Event *event) { |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
241 | return ((IOStream*)fd)->poll(fd, ev, IO_POLL_OUT, event); |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
242 | } |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
243 | |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
244 | int event_removepoll(EventHandler *ev, SYS_NETFD fd) { |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
245 | return ((IOStream*)fd)->poll(fd, ev, IO_POLL_NONE, NULL); |
39fe86ae4db0
use non-blocking IO for pipes and socket in send_cgi
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
342
diff
changeset
|
246 | } |