|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2011 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 #include <atomic.h> |
|
32 |
|
33 #include "event_solaris.h" |
|
34 |
|
35 event_handler_t* evhandler_create(int numthreads) { |
|
36 event_handler_t *ev = malloc(sizeof(event_handler_t)); |
|
37 if(ev == NULL) { |
|
38 return NULL; |
|
39 } |
|
40 |
|
41 ev->ports = calloc(numthreads, sizeof(int)); |
|
42 if(ev->ports == NULL) { |
|
43 free(ev); |
|
44 return NULL; |
|
45 } |
|
46 ev->nports = numthreads; |
|
47 ev->lp = 0; |
|
48 |
|
49 /* create ports event threads */ |
|
50 for(int i=0;i<numthreads;i++) { |
|
51 /* create port */ |
|
52 ev->ports[i] = port_create(); |
|
53 if(ev->ports[i] == 0) { |
|
54 free(ev->ports); |
|
55 free(ev); |
|
56 return NULL; |
|
57 } |
|
58 |
|
59 /* |
|
60 * start a new handler thread |
|
61 * the thread needs the event port and a pointer to the event handler |
|
62 */ |
|
63 ev_thr_conf_t *conf = malloc(sizeof(ev_thr_conf_t)); |
|
64 if(conf == NULL) { |
|
65 free(ev->ports); |
|
66 free(ev); |
|
67 return NULL; |
|
68 } |
|
69 conf->handler = ev; |
|
70 conf->port = ev->ports[i]; |
|
71 |
|
72 systhread_start(0, 0, (thrstartfunc)ev_handle_events, ev); |
|
73 /* TODO: error handling */ |
|
74 } |
|
75 |
|
76 return ev; |
|
77 } |
|
78 |
|
79 void ev_handle_events(ev_thr_conf_t *conf) { |
|
80 event_handler_t *ev = conf->handler; |
|
81 int port = conf->port; |
|
82 |
|
83 free(conf); |
|
84 |
|
85 port_event_t events[16]; |
|
86 struct timespec timeout; |
|
87 timeout.tv_nsec = 0; |
|
88 timeout.tv_sec = 600; |
|
89 |
|
90 for(;;) { |
|
91 /* wait for events */ |
|
92 uint_t nev = 1; |
|
93 int ret = port_getn(port, events, 16, &nev, &timeout); |
|
94 if(ret == -1) { |
|
95 /* TODO: check for error */ |
|
96 continue; |
|
97 } |
|
98 |
|
99 for(int i=0;i<nev;i++) { |
|
100 event_t *event = events[i]->portev_user; |
|
101 if(event->fn) { |
|
102 event->fn(ev, event); |
|
103 } |
|
104 } |
|
105 } |
|
106 } |
|
107 |
|
108 /* returns a event handler port */ |
|
109 int ev_get_port(event_handler_t *h) { |
|
110 int cp = h->lp % h->nports; |
|
111 atomic_inc_32(&h->lp); |
|
112 return cp; |
|
113 } |
|
114 |
|
115 int ev_pollin(event_handler_t *h, int fd, event_t *event) { |
|
116 event->object = (intptr_t)fd; |
|
117 return port_associate( |
|
118 ev_get_port(h), |
|
119 PORT_SOURCE_FD, |
|
120 (uintptr_t)fd, |
|
121 POLLIN, |
|
122 event); |
|
123 } |
|
124 |
|
125 int ev_pollout(event_handler_t *h, int fd, event_t *event) { |
|
126 event->object = (intptr_t)fd; |
|
127 return port_associate( |
|
128 ev_get_port(h), |
|
129 PORT_SOURCE_FD, |
|
130 (uintptr_t)fd, |
|
131 POLLOUT, |
|
132 event); |
|
133 } |
|
134 |
|
135 int evt_send(event_handler_t *h, event_t *event) { |
|
136 event->object = 0; |
|
137 return port_send(ev_get_port(h), 0, event); |
|
138 } |