source: mainline/uspace/lib/c/generic/async/ports.c@ d73d992

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d73d992 was 5c76cc61, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

async: Lock interface hash table with a dedicated interface_futex, and remove redundant per-interface futexes.

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#define LIBC_ASYNC_C_
30#include <ipc/ipc.h>
31#include <async.h>
32#include "../private/async.h"
33#undef LIBC_ASYNC_C_
34
35#include <ipc/irq.h>
36#include <ipc/event.h>
37#include <futex.h>
38#include <fibril.h>
39#include <adt/hash_table.h>
40#include <adt/hash.h>
41#include <adt/list.h>
42#include <assert.h>
43#include <errno.h>
44#include <sys/time.h>
45#include <libarch/barrier.h>
46#include <stdbool.h>
47#include <stdlib.h>
48#include <mem.h>
49#include <stdlib.h>
50#include <macros.h>
51#include <as.h>
52#include <abi/mm/as.h>
53#include "../private/libc.h"
54
55/** Interface data */
56typedef struct {
57 ht_link_t link;
58
59 /** Interface ID */
60 iface_t iface;
61
62 /** Interface ports */
63 hash_table_t port_hash_table;
64
65 /** Next available port ID */
66 port_id_t port_id_avail;
67} interface_t;
68
69/* Port data */
70typedef struct {
71 ht_link_t link;
72
73 /** Port ID */
74 port_id_t id;
75
76 /** Port connection handler */
77 async_port_handler_t handler;
78
79 /** Client data */
80 void *data;
81} port_t;
82
83/** Default fallback fibril function.
84 *
85 * This fallback fibril function gets called on incomming connections that do
86 * not have a specific handler defined.
87 *
88 * @param chandle Handle of the incoming call.
89 * @param call Data of the incoming call.
90 * @param arg Local argument
91 *
92 */
93static void default_fallback_port_handler(cap_call_handle_t chandle,
94 ipc_call_t *call, void *arg)
95{
96 ipc_answer_0(chandle, ENOENT);
97}
98
99static async_port_handler_t fallback_port_handler =
100 default_fallback_port_handler;
101static void *fallback_port_data = NULL;
102
103/** Futex guarding the interface hash table. */
104static futex_t interface_futex = FUTEX_INITIALIZER;
105static hash_table_t interface_hash_table;
106
107static size_t interface_key_hash(void *key)
108{
109 iface_t iface = *(iface_t *) key;
110 return iface;
111}
112
113static size_t interface_hash(const ht_link_t *item)
114{
115 interface_t *interface = hash_table_get_inst(item, interface_t, link);
116 return interface_key_hash(&interface->iface);
117}
118
119static bool interface_key_equal(void *key, const ht_link_t *item)
120{
121 iface_t iface = *(iface_t *) key;
122 interface_t *interface = hash_table_get_inst(item, interface_t, link);
123 return iface == interface->iface;
124}
125
126/** Operations for the port hash table. */
127static hash_table_ops_t interface_hash_table_ops = {
128 .hash = interface_hash,
129 .key_hash = interface_key_hash,
130 .key_equal = interface_key_equal,
131 .equal = NULL,
132 .remove_callback = NULL
133};
134
135static size_t port_key_hash(void *key)
136{
137 port_id_t port_id = *(port_id_t *) key;
138 return port_id;
139}
140
141static size_t port_hash(const ht_link_t *item)
142{
143 port_t *port = hash_table_get_inst(item, port_t, link);
144 return port_key_hash(&port->id);
145}
146
147static bool port_key_equal(void *key, const ht_link_t *item)
148{
149 port_id_t port_id = *(port_id_t *) key;
150 port_t *port = hash_table_get_inst(item, port_t, link);
151 return port_id == port->id;
152}
153
154/** Operations for the port hash table. */
155static hash_table_ops_t port_hash_table_ops = {
156 .hash = port_hash,
157 .key_hash = port_key_hash,
158 .key_equal = port_key_equal,
159 .equal = NULL,
160 .remove_callback = NULL
161};
162
163static interface_t *async_new_interface(iface_t iface)
164{
165 interface_t *interface =
166 (interface_t *) malloc(sizeof(interface_t));
167 if (!interface)
168 return NULL;
169
170 bool ret = hash_table_create(&interface->port_hash_table, 0, 0,
171 &port_hash_table_ops);
172 if (!ret) {
173 free(interface);
174 return NULL;
175 }
176
177 interface->iface = iface;
178 interface->port_id_avail = 0;
179
180 hash_table_insert(&interface_hash_table, &interface->link);
181
182 return interface;
183}
184
185static port_t *async_new_port(interface_t *interface,
186 async_port_handler_t handler, void *data)
187{
188 // TODO: Move the malloc out of critical section.
189 port_t *port = (port_t *) malloc(sizeof(port_t));
190 if (!port)
191 return NULL;
192
193 port_id_t id = interface->port_id_avail;
194 interface->port_id_avail++;
195
196 port->id = id;
197 port->handler = handler;
198 port->data = data;
199
200 hash_table_insert(&interface->port_hash_table, &port->link);
201
202 return port;
203}
204
205errno_t async_create_port_internal(iface_t iface, async_port_handler_t handler,
206 void *data, port_id_t *port_id)
207{
208 interface_t *interface;
209
210 futex_lock(&interface_futex);
211
212 ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
213 if (link)
214 interface = hash_table_get_inst(link, interface_t, link);
215 else
216 interface = async_new_interface(iface);
217
218 if (!interface) {
219 futex_unlock(&interface_futex);
220 return ENOMEM;
221 }
222
223 port_t *port = async_new_port(interface, handler, data);
224 if (!port) {
225 futex_unlock(&interface_futex);
226 return ENOMEM;
227 }
228
229 *port_id = port->id;
230
231 futex_unlock(&interface_futex);
232
233 return EOK;
234}
235
236errno_t async_create_port(iface_t iface, async_port_handler_t handler,
237 void *data, port_id_t *port_id)
238{
239 if ((iface & IFACE_MOD_MASK) == IFACE_MOD_CALLBACK)
240 return EINVAL;
241
242 return async_create_port_internal(iface, handler, data, port_id);
243}
244
245void async_set_fallback_port_handler(async_port_handler_t handler, void *data)
246{
247 assert(handler != NULL);
248
249 fallback_port_handler = handler;
250 fallback_port_data = data;
251}
252
253static port_t *async_find_port(iface_t iface, port_id_t port_id)
254{
255 port_t *port = NULL;
256
257 futex_lock(&interface_futex);
258
259 ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
260 if (link) {
261 interface_t *interface =
262 hash_table_get_inst(link, interface_t, link);
263
264 link = hash_table_find(&interface->port_hash_table, &port_id);
265 if (link)
266 port = hash_table_get_inst(link, port_t, link);
267 }
268
269 futex_unlock(&interface_futex);
270
271 return port;
272}
273
274async_port_handler_t async_get_port_handler(iface_t iface, port_id_t port_id,
275 void **data)
276{
277 assert(data);
278
279 async_port_handler_t handler = fallback_port_handler;
280 *data = fallback_port_data;
281
282 port_t *port = async_find_port(iface, port_id);
283 if (port) {
284 handler = port->handler;
285 *data = port->data;
286 }
287
288 return handler;
289}
290
291/** Initialize the async framework.
292 *
293 */
294void __async_ports_init(void)
295{
296 if (!hash_table_create(&interface_hash_table, 0, 0,
297 &interface_hash_table_ops))
298 abort();
299}
Note: See TracBrowser for help on using the repository browser.