src/server/daemon/event_solaris.c

Sat, 06 Oct 2012 13:00:07 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 06 Oct 2012 13:00:07 +0200
changeset 36
450d2d5f4735
parent 35
4417619a9bbd
child 43
8ac56edb4e94
permissions
-rw-r--r--

server can reload configuration

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 *
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2011 Olaf Wintermann. All rights reserved.
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
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 event_handler_t* evhandler_create(int numthreads) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 event_handler_t *ev = malloc(sizeof(event_handler_t));
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 if(ev == NULL) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 return NULL;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 ev->ports = calloc(numthreads, sizeof(int));
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 if(ev->ports == NULL) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 free(ev);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 return NULL;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 ev->nports = numthreads;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 ev->lp = 0;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 /* create ports event threads */
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 for(int i=0;i<numthreads;i++) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 /* create port */
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 ev->ports[i] = port_create();
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 if(ev->ports[i] == 0) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 free(ev->ports);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 free(ev);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 return NULL;
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 /*
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 * start a new handler thread
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 * the thread needs the event port and a pointer to the event handler
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 */
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 ev_thr_conf_t *conf = malloc(sizeof(ev_thr_conf_t));
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 if(conf == NULL) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 free(ev->ports);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 free(ev);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 return NULL;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 conf->handler = ev;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 conf->port = ev->ports[i];
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
72 ev_thr_conf_t *thrconf = malloc(sizeof(ev_thr_conf_t));
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
73 thrconf->handler = ev;
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
74 thrconf->port = ev->ports[i];
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
75 systhread_start(0, 0, (thrstartfunc)ev_handle_events, thrconf);
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 /* TODO: error handling */
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
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 return ev;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 void ev_handle_events(ev_thr_conf_t *conf) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 event_handler_t *ev = conf->handler;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 int port = conf->port;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 free(conf);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 port_event_t events[16];
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 struct timespec timeout;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 timeout.tv_nsec = 0;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 timeout.tv_sec = 600;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 for(;;) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 /* wait for events */
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 uint_t nev = 1;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 int ret = port_getn(port, events, 16, &nev, &timeout);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 if(ret == -1) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 /* TODO: check for error */
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
99 perror("port_getn");
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 continue;
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 for(int i=0;i<nev;i++) {
30
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
104 event_t *event = events[i].portev_user;
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 if(event->fn) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 event->fn(ev, event);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 /* returns a event handler port */
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 int ev_get_port(event_handler_t *h) {
30
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
114 int nps = h->nports;
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
115 if(nps == 1) {
35
4417619a9bbd using non blocking IO for request input
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 30
diff changeset
116 return h->ports[0];
30
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
117 }
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
118
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
119 int cp = h->lp % nps;
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 atomic_inc_32(&h->lp);
30
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
121
27c7511c0e34 new proppatch, mkcol and delete method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 29
diff changeset
122 return h->ports[cp];
29
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 int ev_pollin(event_handler_t *h, int fd, event_t *event) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 event->object = (intptr_t)fd;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 return port_associate(
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 ev_get_port(h),
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 PORT_SOURCE_FD,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 (uintptr_t)fd,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 POLLIN,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 event);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 int ev_pollout(event_handler_t *h, int fd, event_t *event) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 event->object = (intptr_t)fd;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 return port_associate(
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 ev_get_port(h),
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 PORT_SOURCE_FD,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 (uintptr_t)fd,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 POLLOUT,
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 event);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 }
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 int evt_send(event_handler_t *h, event_t *event) {
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 event->object = 0;
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 return port_send(ev_get_port(h), 0, event);
e8619defde14 added event handler
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 }

mercurial