src/server/daemon/event_solaris.c

Sat, 04 Feb 2017 16:42:11 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 04 Feb 2017 16:42:11 +0100
branch
aio
changeset 170
711d00eeed25
parent 159
9ba9f8befa80
child 172
5580517faafc
permissions
-rw-r--r--

refactors kqueue event handler

29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
e8619defde14 added event handler
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.
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
e8619defde14 added event handler
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
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <atomic.h>
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "event_solaris.h"
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
35 EVHandler* evhandler_create(EventHandlerConfig *cfg) {
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
36 EVHandler *ev = malloc(sizeof(EVHandler));
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
37 ev->current = 0;
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
38 ev->instances = calloc(cfg->nthreads, sizeof(void*));
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
39 ev->numins = cfg->nthreads;
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
41 for(int i=0;i<cfg->nthreads;i++) {
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
42 EventHandler *handler = malloc(sizeof(EventHandler));
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
43 ev->instances[i] = handler;
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
44
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
45 handler->port = port_create();
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
46 if(handler->port == 0) {
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
47 // TODO: error
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 return NULL;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
51 SYS_THREAD t = systhread_start(
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
52 0,
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
53 0,
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
54 (thrstartfunc)ev_handle_events,
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
55 handler);
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
56 systhread_detach(t);
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 return ev;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
62 void ev_handle_events(EventHandler *ev) {
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
63 port_event_t events[64];
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 struct timespec timeout;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 timeout.tv_nsec = 0;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 timeout.tv_sec = 600;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 for(;;) {
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
69 // wait for events
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 uint_t nev = 1;
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
71 int ret = port_getn(ev->port, events, 64, &nev, &timeout);
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 if(ret == -1) {
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
73 // TODO: check for error
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
74 perror("port_getn");
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 continue;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 for(int i=0;i<nev;i++) {
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
79 Event *event = events[i].portev_user;
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 if(event->fn) {
43
8ac56edb4e94 fixed solaris build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
81 if(event->fn(ev, event)) {
8ac56edb4e94 fixed solaris build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
82 /*
8ac56edb4e94 fixed solaris build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
83 * on solaris we have to reassociate the fd after
8ac56edb4e94 fixed solaris build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
84 * each event
8ac56edb4e94 fixed solaris build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
85 * we do this if the event function returns 1
8ac56edb4e94 fixed solaris build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
86 */
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
87 if(port_associate(
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
88 ev->port,
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
89 PORT_SOURCE_FD,
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
90 (uintptr_t)event->object,
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
91 ev_convert2sys_events(event->events),
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
92 event))
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
93 {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
94 perror("port_associate");
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
95 }
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
96 } else if(event->finish) {
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
97 event->finish(ev, event);
43
8ac56edb4e94 fixed solaris build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
98 }
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
104 int ev_convert2sys_events(int events) {
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
105 int e = 0;
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
106 if((events & EVENT_POLLIN) == EVENT_POLLIN) {
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
107 e |= POLLIN;
30
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
108 }
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
109 if((events & EVENT_POLLOUT) == EVENT_POLLOUT) {
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
110 e |= POLLOUT;
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
111 }
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
112 return e;
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
115
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
116 int ev_pollin(EventHandler *h, int fd, Event *event) {
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 event->object = (intptr_t)fd;
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
118 event->events = EVENT_POLLIN;
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 return port_associate(
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
120 h->port,
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 PORT_SOURCE_FD,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 (uintptr_t)fd,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 POLLIN,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 event);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
127 int ev_pollout(EventHandler *h, int fd, Event *event) {
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 event->object = (intptr_t)fd;
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
129 event->events = EVENT_POLLOUT;
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 return port_associate(
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
131 h->port,
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 PORT_SOURCE_FD,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 (uintptr_t)fd,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 POLLOUT,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 event);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137
159
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
138 int evt_send(EventHandler *h, Event *event) {
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
139 event->object = 0;
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
140 event->events = 0;
9ba9f8befa80 makes EventHandler public
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 153
diff changeset
141 return port_send(h->port, 0, event);
43
8ac56edb4e94 fixed solaris build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 35
diff changeset
142 }

mercurial