src/server/daemon/event_bsd.c

Sun, 19 Jan 2020 09:31:45 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 19 Jan 2020 09:31:45 +0100
branch
webdav
changeset 236
e81d3e517b57
parent 187
4384bfbb7e26
child 260
4779a6fb4fbe
permissions
-rw-r--r--

fix response href

69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4a10bc0ee80d compiles on os x
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
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
32 #include "../util/atomic.h"
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
33
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "event_bsd.h"
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
36 EVHandler* evhandler_create(EventHandlerConfig *cfg) {
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
37 EVHandler *ev = malloc(sizeof(EVHandler));
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
38 ev->current = 0;
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
39 ev->instances = calloc(cfg->nthreads, sizeof(void*));
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
40 ev->numins = cfg->nthreads;
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
42 for(int i=0;i<cfg->nthreads;i++) {
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
43 EventHandler *handler = malloc(sizeof(EventHandler));
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
44 ev->instances[i] = handler;
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
45
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
46 handler->kqueue = kqueue();
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
47 if(handler->kqueue == 0) {
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
48 // TODO: error
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 return NULL;
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 }
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
52 SYS_THREAD t = systhread_start(
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
53 0,
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
54 0,
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
55 (thrstartfunc)ev_handle_events,
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
56 handler);
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
57 systhread_detach(t);
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 }
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 return ev;
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 }
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
63
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
64 void ev_handle_events(EventHandler *ev) {
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
65 struct timespec timeout;
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
66 timeout.tv_nsec = 0;
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
67 timeout.tv_sec = 600;
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
68
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
69 struct kevent events[64];
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
70 struct kevent changes[64];
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
71 int numchanges = 0;
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
72
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
73 for(;;) {
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
74 // wait for events
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
75 int nev = kevent(ev->kqueue, changes, numchanges, events, 64, &timeout);
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
76 if(nev == -1) {
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
77 // TODO: check for error
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
78 perror("kevent");
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
79 continue;
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
80 }
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
81
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
82 numchanges = 0;
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
83 for(int i=0;i<nev;i++) {
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
84 Event *event = (Event*)events[i].udata;
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
85 if(event->fn) {
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
86 int ep = event->events;
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
87 if(event->fn(ev, event)) {
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
88 if(event->events != ep) {
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
89 changes[numchanges++].filter = ev_convert2sys_events(ep);
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
90 }
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
91 } else if(event->finish) {
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
92 changes[numchanges++].filter = ev_convert2sys_events(ep);
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
93 event->finish(ev, event);
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
94 }
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
95 }
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
96 }
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
97 }
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 }
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
100 int ev_convert2sys_events(int events) {
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
101 int e = 0;
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
102 if((events & EVENT_POLLIN) == EVENT_POLLIN) {
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
103 e |= EVFILT_READ;
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
104 }
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
105 if((events & EVENT_POLLOUT) == EVENT_POLLOUT) {
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
106 e |= EVFILT_WRITE;
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
107 }
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
108 return e;
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
111 int ev_pollin(EventHandler *h, int fd, Event *event) {
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
112 event->events = EVENT_POLLIN;
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
113 struct kevent kev;
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
114 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
115 return kevent(h->kqueue, &kev, 1, NULL, 0, NULL);
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 }
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117
170
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
118 int ev_pollout(EventHandler *h, int fd, Event *event) {
711d00eeed25 refactors kqueue event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 133
diff changeset
119 event->events = EVENT_POLLOUT;
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
120 struct kevent kev;
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
121 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
122 return kevent(h->kqueue, &kev, 1, NULL, 0, NULL);
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124
187
4384bfbb7e26 adds platform independent aio functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 170
diff changeset
125 int event_send(EventHandler *h, Event *event) {
109
8a0a7754f123 experimental BSD support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
126 return 0;
69
4a10bc0ee80d compiles on os x
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 }

mercurial