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 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Asynchronous library
|
---|
37 | *
|
---|
38 | * The aim of this library is to provide a facility for writing programs which
|
---|
39 | * utilize the asynchronous nature of HelenOS IPC, yet using a normal way of
|
---|
40 | * programming.
|
---|
41 | *
|
---|
42 | * You should be able to write very simple multithreaded programs. The async
|
---|
43 | * framework will automatically take care of most of the synchronization
|
---|
44 | * problems.
|
---|
45 | *
|
---|
46 | * Example of use (pseudo C):
|
---|
47 | *
|
---|
48 | * 1) Multithreaded client application
|
---|
49 | *
|
---|
50 | * fibril_create(fibril1, ...);
|
---|
51 | * fibril_create(fibril2, ...);
|
---|
52 | * ...
|
---|
53 | *
|
---|
54 | * int fibril1(void *arg)
|
---|
55 | * {
|
---|
56 | * conn = async_connect_me_to(...);
|
---|
57 | *
|
---|
58 | * exch = async_exchange_begin(conn);
|
---|
59 | * c1 = async_send(exch);
|
---|
60 | * async_exchange_end(exch);
|
---|
61 | *
|
---|
62 | * exch = async_exchange_begin(conn);
|
---|
63 | * c2 = async_send(exch);
|
---|
64 | * async_exchange_end(exch);
|
---|
65 | *
|
---|
66 | * async_wait_for(c1);
|
---|
67 | * async_wait_for(c2);
|
---|
68 | * ...
|
---|
69 | * }
|
---|
70 | *
|
---|
71 | *
|
---|
72 | * 2) Multithreaded server application
|
---|
73 | *
|
---|
74 | * main()
|
---|
75 | * {
|
---|
76 | * async_manager();
|
---|
77 | * }
|
---|
78 | *
|
---|
79 | * port_handler(ipc_call_t *icall)
|
---|
80 | * {
|
---|
81 | * if (want_refuse) {
|
---|
82 | * async_answer_0(icall, ELIMIT);
|
---|
83 | * return;
|
---|
84 | * }
|
---|
85 | *
|
---|
86 | * async_answer_0(icall, EOK);
|
---|
87 | *
|
---|
88 | * async_get_call(&call);
|
---|
89 | * somehow_handle_the_call(&call);
|
---|
90 | * async_answer_2(&call, 1, 2, 3);
|
---|
91 | *
|
---|
92 | * async_get_call(&call);
|
---|
93 | * ...
|
---|
94 | * }
|
---|
95 | *
|
---|
96 | */
|
---|
97 |
|
---|
98 | #define _LIBC_ASYNC_C_
|
---|
99 | #include <ipc/ipc.h>
|
---|
100 | #include <async.h>
|
---|
101 | #include "../private/async.h"
|
---|
102 | #undef _LIBC_ASYNC_C_
|
---|
103 |
|
---|
104 | #include <ipc/irq.h>
|
---|
105 | #include <ipc/event.h>
|
---|
106 | #include <fibril.h>
|
---|
107 | #include <adt/hash_table.h>
|
---|
108 | #include <adt/hash.h>
|
---|
109 | #include <adt/list.h>
|
---|
110 | #include <assert.h>
|
---|
111 | #include <errno.h>
|
---|
112 | #include <time.h>
|
---|
113 | #include <stdbool.h>
|
---|
114 | #include <stdlib.h>
|
---|
115 | #include <mem.h>
|
---|
116 | #include <stdlib.h>
|
---|
117 | #include <macros.h>
|
---|
118 | #include <str_error.h>
|
---|
119 | #include <as.h>
|
---|
120 | #include <abi/mm/as.h>
|
---|
121 | #include "../private/libc.h"
|
---|
122 | #include "../private/fibril.h"
|
---|
123 |
|
---|
124 | #define DPRINTF(...) ((void) 0)
|
---|
125 |
|
---|
126 | /* Client connection data */
|
---|
127 | typedef struct {
|
---|
128 | ht_link_t link;
|
---|
129 |
|
---|
130 | task_id_t in_task_id;
|
---|
131 | int refcnt;
|
---|
132 | void *data;
|
---|
133 | } client_t;
|
---|
134 |
|
---|
135 | /* Server connection data */
|
---|
136 | typedef struct {
|
---|
137 | /** Fibril handling the connection. */
|
---|
138 | fid_t fid;
|
---|
139 |
|
---|
140 | /** Hash table link. */
|
---|
141 | ht_link_t link;
|
---|
142 |
|
---|
143 | /** Incoming client task ID. */
|
---|
144 | task_id_t in_task_id;
|
---|
145 |
|
---|
146 | /** Link to the client tracking structure. */
|
---|
147 | client_t *client;
|
---|
148 |
|
---|
149 | /** Channel for messages that should be delivered to this fibril. */
|
---|
150 | mpsc_t *msg_channel;
|
---|
151 |
|
---|
152 | /** Call data of the opening call. */
|
---|
153 | ipc_call_t call;
|
---|
154 |
|
---|
155 | /** Fibril function that will be used to handle the connection. */
|
---|
156 | async_port_handler_t handler;
|
---|
157 |
|
---|
158 | /** Client data */
|
---|
159 | void *data;
|
---|
160 | } connection_t;
|
---|
161 |
|
---|
162 | /* Member of notification_t::msg_list. */
|
---|
163 | typedef struct {
|
---|
164 | link_t link;
|
---|
165 | ipc_call_t calldata;
|
---|
166 | } notification_msg_t;
|
---|
167 |
|
---|
168 | /* Notification data */
|
---|
169 | typedef struct {
|
---|
170 | /** notification_hash_table link */
|
---|
171 | ht_link_t htlink;
|
---|
172 |
|
---|
173 | /** notification_queue link */
|
---|
174 | link_t qlink;
|
---|
175 |
|
---|
176 | /** Notification method */
|
---|
177 | sysarg_t imethod;
|
---|
178 |
|
---|
179 | /** Notification handler */
|
---|
180 | async_notification_handler_t handler;
|
---|
181 |
|
---|
182 | /** Notification handler argument */
|
---|
183 | void *arg;
|
---|
184 |
|
---|
185 | /** List of arrived notifications. */
|
---|
186 | list_t msg_list;
|
---|
187 | } notification_t;
|
---|
188 |
|
---|
189 | /** Identifier of the incoming connection handled by the current fibril. */
|
---|
190 | static fibril_local connection_t *fibril_connection;
|
---|
191 |
|
---|
192 | static void *default_client_data_constructor(void)
|
---|
193 | {
|
---|
194 | return NULL;
|
---|
195 | }
|
---|
196 |
|
---|
197 | static void default_client_data_destructor(void *data)
|
---|
198 | {
|
---|
199 | }
|
---|
200 |
|
---|
201 | static async_client_data_ctor_t async_client_data_create =
|
---|
202 | default_client_data_constructor;
|
---|
203 | static async_client_data_dtor_t async_client_data_destroy =
|
---|
204 | default_client_data_destructor;
|
---|
205 |
|
---|
206 | void async_set_client_data_constructor(async_client_data_ctor_t ctor)
|
---|
207 | {
|
---|
208 | assert(async_client_data_create == default_client_data_constructor);
|
---|
209 | async_client_data_create = ctor;
|
---|
210 | }
|
---|
211 |
|
---|
212 | void async_set_client_data_destructor(async_client_data_dtor_t dtor)
|
---|
213 | {
|
---|
214 | assert(async_client_data_destroy == default_client_data_destructor);
|
---|
215 | async_client_data_destroy = dtor;
|
---|
216 | }
|
---|
217 |
|
---|
218 | static async_port_handler_t implicit_connection = NULL;
|
---|
219 |
|
---|
220 | /** Setter for implicit_connection function pointer.
|
---|
221 | *
|
---|
222 | * @param conn Function that will implement a new connection fibril for
|
---|
223 | * unrouted calls.
|
---|
224 | *
|
---|
225 | */
|
---|
226 | void async_set_implicit_connection(async_port_handler_t conn)
|
---|
227 | {
|
---|
228 | assert(implicit_connection == NULL);
|
---|
229 | implicit_connection = conn;
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | static fibril_rmutex_t client_mutex;
|
---|
234 | static hash_table_t client_hash_table;
|
---|
235 |
|
---|
236 | // TODO: lockfree notification_queue?
|
---|
237 | static fibril_rmutex_t notification_mutex;
|
---|
238 | static hash_table_t notification_hash_table;
|
---|
239 | static LIST_INITIALIZE(notification_queue);
|
---|
240 | static FIBRIL_SEMAPHORE_INITIALIZE(notification_semaphore, 0);
|
---|
241 |
|
---|
242 | static LIST_INITIALIZE(notification_freelist);
|
---|
243 | static long notification_freelist_total = 0;
|
---|
244 | static long notification_freelist_used = 0;
|
---|
245 |
|
---|
246 | static sysarg_t notification_avail = 0;
|
---|
247 |
|
---|
248 | static size_t client_key_hash(const void *key)
|
---|
249 | {
|
---|
250 | const task_id_t *in_task_id = key;
|
---|
251 | return *in_task_id;
|
---|
252 | }
|
---|
253 |
|
---|
254 | static size_t client_hash(const ht_link_t *item)
|
---|
255 | {
|
---|
256 | client_t *client = hash_table_get_inst(item, client_t, link);
|
---|
257 | return client_key_hash(&client->in_task_id);
|
---|
258 | }
|
---|
259 |
|
---|
260 | static bool client_key_equal(const void *key, const ht_link_t *item)
|
---|
261 | {
|
---|
262 | const task_id_t *in_task_id = key;
|
---|
263 | client_t *client = hash_table_get_inst(item, client_t, link);
|
---|
264 | return *in_task_id == client->in_task_id;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /** Operations for the client hash table. */
|
---|
268 | static hash_table_ops_t client_hash_table_ops = {
|
---|
269 | .hash = client_hash,
|
---|
270 | .key_hash = client_key_hash,
|
---|
271 | .key_equal = client_key_equal,
|
---|
272 | .equal = NULL,
|
---|
273 | .remove_callback = NULL
|
---|
274 | };
|
---|
275 |
|
---|
276 | static client_t *async_client_get(task_id_t client_id, bool create)
|
---|
277 | {
|
---|
278 | client_t *client = NULL;
|
---|
279 |
|
---|
280 | fibril_rmutex_lock(&client_mutex);
|
---|
281 | ht_link_t *link = hash_table_find(&client_hash_table, &client_id);
|
---|
282 | if (link) {
|
---|
283 | client = hash_table_get_inst(link, client_t, link);
|
---|
284 | client->refcnt++;
|
---|
285 | } else if (create) {
|
---|
286 | // TODO: move the malloc out of critical section
|
---|
287 | /* malloc() is rmutex safe. */
|
---|
288 | client = malloc(sizeof(client_t));
|
---|
289 | if (client) {
|
---|
290 | client->in_task_id = client_id;
|
---|
291 | client->data = async_client_data_create();
|
---|
292 |
|
---|
293 | client->refcnt = 1;
|
---|
294 | hash_table_insert(&client_hash_table, &client->link);
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | fibril_rmutex_unlock(&client_mutex);
|
---|
299 | return client;
|
---|
300 | }
|
---|
301 |
|
---|
302 | static void async_client_put(client_t *client)
|
---|
303 | {
|
---|
304 | bool destroy;
|
---|
305 |
|
---|
306 | fibril_rmutex_lock(&client_mutex);
|
---|
307 |
|
---|
308 | if (--client->refcnt == 0) {
|
---|
309 | hash_table_remove(&client_hash_table, &client->in_task_id);
|
---|
310 | destroy = true;
|
---|
311 | } else
|
---|
312 | destroy = false;
|
---|
313 |
|
---|
314 | fibril_rmutex_unlock(&client_mutex);
|
---|
315 |
|
---|
316 | if (destroy) {
|
---|
317 | if (client->data)
|
---|
318 | async_client_data_destroy(client->data);
|
---|
319 |
|
---|
320 | free(client);
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | /** Wrapper for client connection fibril.
|
---|
325 | *
|
---|
326 | * When a new connection arrives, a fibril with this implementing
|
---|
327 | * function is created.
|
---|
328 | *
|
---|
329 | * @param arg Connection structure pointer.
|
---|
330 | *
|
---|
331 | * @return Always zero.
|
---|
332 | *
|
---|
333 | */
|
---|
334 | static errno_t connection_fibril(void *arg)
|
---|
335 | {
|
---|
336 | assert(arg);
|
---|
337 |
|
---|
338 | /*
|
---|
339 | * Setup fibril-local connection pointer.
|
---|
340 | */
|
---|
341 | fibril_connection = (connection_t *) arg;
|
---|
342 |
|
---|
343 | mpsc_t *c = fibril_connection->msg_channel;
|
---|
344 |
|
---|
345 | /*
|
---|
346 | * Add our reference for the current connection in the client task
|
---|
347 | * tracking structure. If this is the first reference, create and
|
---|
348 | * hash in a new tracking structure.
|
---|
349 | */
|
---|
350 |
|
---|
351 | client_t *client = async_client_get(fibril_connection->in_task_id, true);
|
---|
352 | if (!client) {
|
---|
353 | ipc_answer_0(fibril_connection->call.cap_handle, ENOMEM);
|
---|
354 | goto out;
|
---|
355 | }
|
---|
356 |
|
---|
357 | fibril_connection->client = client;
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * Call the connection handler function.
|
---|
361 | */
|
---|
362 | fibril_connection->handler(&fibril_connection->call,
|
---|
363 | fibril_connection->data);
|
---|
364 |
|
---|
365 | /*
|
---|
366 | * Remove the reference for this client task connection.
|
---|
367 | */
|
---|
368 | async_client_put(client);
|
---|
369 |
|
---|
370 | /*
|
---|
371 | * Close the channel, if it isn't closed already.
|
---|
372 | */
|
---|
373 | mpsc_close(c);
|
---|
374 |
|
---|
375 | /*
|
---|
376 | * Answer all remaining messages with EHANGUP.
|
---|
377 | */
|
---|
378 | ipc_call_t call;
|
---|
379 | while (mpsc_receive(c, &call, NULL) == EOK)
|
---|
380 | ipc_answer_0(call.cap_handle, EHANGUP);
|
---|
381 |
|
---|
382 | /*
|
---|
383 | * Clean up memory.
|
---|
384 | */
|
---|
385 | out:
|
---|
386 | mpsc_destroy(c);
|
---|
387 | free(fibril_connection);
|
---|
388 | return EOK;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /** Return label usable during replies to IPC_M_CONNECT_ME_TO. */
|
---|
392 | sysarg_t async_get_label(void)
|
---|
393 | {
|
---|
394 | return (sysarg_t) fibril_connection;
|
---|
395 | }
|
---|
396 |
|
---|
397 | /** Create a new fibril for a new connection.
|
---|
398 | *
|
---|
399 | * Create new fibril for connection, fill in connection structures and insert it
|
---|
400 | * into the hash table, so that later we can easily do routing of messages to
|
---|
401 | * particular fibrils.
|
---|
402 | *
|
---|
403 | * @param conn Pointer to the connection structure. Will be used as the
|
---|
404 | * label of the connected phone and request_label of incoming
|
---|
405 | * calls routed through that phone.
|
---|
406 | * @param in_task_id Identification of the incoming connection.
|
---|
407 | * @param call Call data of the opening call. If call is NULL, it's
|
---|
408 | * either a callback connection that was opened by
|
---|
409 | * accepting the IPC_M_CONNECT_TO_ME call.
|
---|
410 | * Alternatively, it is zero when we are opening
|
---|
411 | * implicit connection.
|
---|
412 | * @param handler Connection handler.
|
---|
413 | * @param data Client argument to pass to the connection handler.
|
---|
414 | *
|
---|
415 | * @return New fibril id or NULL on failure.
|
---|
416 | *
|
---|
417 | */
|
---|
418 | static fid_t async_new_connection(connection_t *conn, task_id_t in_task_id,
|
---|
419 | ipc_call_t *call, async_port_handler_t handler, void *data)
|
---|
420 | {
|
---|
421 | conn->in_task_id = in_task_id;
|
---|
422 | conn->msg_channel = mpsc_create(sizeof(ipc_call_t));
|
---|
423 | conn->handler = handler;
|
---|
424 | conn->data = data;
|
---|
425 |
|
---|
426 | if (!conn->msg_channel)
|
---|
427 | goto error;
|
---|
428 |
|
---|
429 | if (call)
|
---|
430 | conn->call = *call;
|
---|
431 | else
|
---|
432 | conn->call.cap_handle = CAP_NIL;
|
---|
433 |
|
---|
434 | /* We will activate the fibril ASAP */
|
---|
435 | conn->fid = fibril_create(connection_fibril, conn);
|
---|
436 |
|
---|
437 | if (conn->fid == 0)
|
---|
438 | goto error;
|
---|
439 |
|
---|
440 | fibril_start(conn->fid);
|
---|
441 |
|
---|
442 | return conn->fid;
|
---|
443 |
|
---|
444 | error:
|
---|
445 | if (conn->msg_channel)
|
---|
446 | mpsc_destroy(conn->msg_channel);
|
---|
447 | free(conn);
|
---|
448 |
|
---|
449 | if (call)
|
---|
450 | ipc_answer_0(call->cap_handle, ENOMEM);
|
---|
451 |
|
---|
452 | return (fid_t) NULL;
|
---|
453 | }
|
---|
454 |
|
---|
455 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
456 | *
|
---|
457 | * Ask through phone for a new connection to some service.
|
---|
458 | *
|
---|
459 | * @param exch Exchange for sending the message.
|
---|
460 | * @param iface Callback interface.
|
---|
461 | * @param arg1 User defined argument.
|
---|
462 | * @param arg2 User defined argument.
|
---|
463 | * @param handler Callback handler.
|
---|
464 | * @param data Handler data.
|
---|
465 | * @param port_id ID of the newly created port.
|
---|
466 | *
|
---|
467 | * @return Zero on success or an error code.
|
---|
468 | *
|
---|
469 | */
|
---|
470 | errno_t async_create_callback_port(async_exch_t *exch, iface_t iface, sysarg_t arg1,
|
---|
471 | sysarg_t arg2, async_port_handler_t handler, void *data, port_id_t *port_id)
|
---|
472 | {
|
---|
473 | if ((iface & IFACE_MOD_CALLBACK) != IFACE_MOD_CALLBACK)
|
---|
474 | return EINVAL;
|
---|
475 |
|
---|
476 | if (exch == NULL)
|
---|
477 | return ENOENT;
|
---|
478 |
|
---|
479 | connection_t *conn = calloc(1, sizeof(*conn));
|
---|
480 | if (!conn)
|
---|
481 | return ENOMEM;
|
---|
482 |
|
---|
483 | ipc_call_t answer;
|
---|
484 | aid_t req = async_send_5(exch, IPC_M_CONNECT_TO_ME, iface, arg1, arg2,
|
---|
485 | 0, (sysarg_t) conn, &answer);
|
---|
486 |
|
---|
487 | errno_t rc;
|
---|
488 | async_wait_for(req, &rc);
|
---|
489 | if (rc != EOK) {
|
---|
490 | free(conn);
|
---|
491 | return rc;
|
---|
492 | }
|
---|
493 |
|
---|
494 | rc = async_create_port_internal(iface, handler, data, port_id);
|
---|
495 | if (rc != EOK) {
|
---|
496 | free(conn);
|
---|
497 | return rc;
|
---|
498 | }
|
---|
499 |
|
---|
500 | fid_t fid = async_new_connection(conn, answer.task_id, NULL, handler,
|
---|
501 | data);
|
---|
502 | if (fid == (fid_t) NULL)
|
---|
503 | return ENOMEM;
|
---|
504 |
|
---|
505 | return EOK;
|
---|
506 | }
|
---|
507 |
|
---|
508 | static size_t notification_key_hash(const void *key)
|
---|
509 | {
|
---|
510 | const sysarg_t *id = key;
|
---|
511 | return *id;
|
---|
512 | }
|
---|
513 |
|
---|
514 | static size_t notification_hash(const ht_link_t *item)
|
---|
515 | {
|
---|
516 | notification_t *notification =
|
---|
517 | hash_table_get_inst(item, notification_t, htlink);
|
---|
518 | return notification_key_hash(¬ification->imethod);
|
---|
519 | }
|
---|
520 |
|
---|
521 | static bool notification_key_equal(const void *key, const ht_link_t *item)
|
---|
522 | {
|
---|
523 | const sysarg_t *id = key;
|
---|
524 | notification_t *notification =
|
---|
525 | hash_table_get_inst(item, notification_t, htlink);
|
---|
526 | return *id == notification->imethod;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** Operations for the notification hash table. */
|
---|
530 | static hash_table_ops_t notification_hash_table_ops = {
|
---|
531 | .hash = notification_hash,
|
---|
532 | .key_hash = notification_key_hash,
|
---|
533 | .key_equal = notification_key_equal,
|
---|
534 | .equal = NULL,
|
---|
535 | .remove_callback = NULL
|
---|
536 | };
|
---|
537 |
|
---|
538 | /** Try to route a call to an appropriate connection fibril.
|
---|
539 | *
|
---|
540 | * If the proper connection fibril is found, a message with the call is added to
|
---|
541 | * its message queue. If the fibril was not active, it is activated and all
|
---|
542 | * timeouts are unregistered.
|
---|
543 | *
|
---|
544 | * @param call Data of the incoming call.
|
---|
545 | *
|
---|
546 | * @return EOK if the call was successfully passed to the respective fibril.
|
---|
547 | * @return ENOENT if the call doesn't match any connection.
|
---|
548 | * @return Other error code if routing failed for other reasons.
|
---|
549 | *
|
---|
550 | */
|
---|
551 | static errno_t route_call(ipc_call_t *call)
|
---|
552 | {
|
---|
553 | assert(call);
|
---|
554 |
|
---|
555 | connection_t *conn = (connection_t *) call->request_label;
|
---|
556 |
|
---|
557 | if (!conn)
|
---|
558 | return ENOENT;
|
---|
559 |
|
---|
560 | assert(conn->msg_channel);
|
---|
561 |
|
---|
562 | errno_t rc = mpsc_send(conn->msg_channel, call);
|
---|
563 |
|
---|
564 | if (ipc_get_imethod(call) == IPC_M_PHONE_HUNGUP) {
|
---|
565 | /* Close the channel, but let the connection fibril answer. */
|
---|
566 | mpsc_close(conn->msg_channel);
|
---|
567 | // FIXME: Ideally, we should be able to discard/answer the
|
---|
568 | // hungup message here and just close the channel without
|
---|
569 | // passing it out. Unfortunatelly, somehow that breaks
|
---|
570 | // handling of CPU exceptions.
|
---|
571 | }
|
---|
572 |
|
---|
573 | return rc;
|
---|
574 | }
|
---|
575 |
|
---|
576 | /** Function implementing the notification handler fibril. Never returns. */
|
---|
577 | static errno_t notification_fibril_func(void *arg)
|
---|
578 | {
|
---|
579 | (void) arg;
|
---|
580 |
|
---|
581 | while (true) {
|
---|
582 | fibril_semaphore_down(¬ification_semaphore);
|
---|
583 |
|
---|
584 | fibril_rmutex_lock(¬ification_mutex);
|
---|
585 |
|
---|
586 | /*
|
---|
587 | * The semaphore ensures that if we get this far,
|
---|
588 | * the queue must be non-empty.
|
---|
589 | */
|
---|
590 | assert(!list_empty(¬ification_queue));
|
---|
591 |
|
---|
592 | notification_t *notification = list_get_instance(
|
---|
593 | list_first(¬ification_queue), notification_t, qlink);
|
---|
594 |
|
---|
595 | async_notification_handler_t handler = notification->handler;
|
---|
596 | void *arg = notification->arg;
|
---|
597 |
|
---|
598 | notification_msg_t *m = list_pop(¬ification->msg_list,
|
---|
599 | notification_msg_t, link);
|
---|
600 | assert(m);
|
---|
601 | ipc_call_t calldata = m->calldata;
|
---|
602 |
|
---|
603 | notification_freelist_used--;
|
---|
604 |
|
---|
605 | if (notification_freelist_total > 64 &&
|
---|
606 | notification_freelist_total > 2 * notification_freelist_used) {
|
---|
607 | /* Going to free the structure if we have too much. */
|
---|
608 | notification_freelist_total--;
|
---|
609 | } else {
|
---|
610 | /* Otherwise add to freelist. */
|
---|
611 | list_append(&m->link, ¬ification_freelist);
|
---|
612 | m = NULL;
|
---|
613 | }
|
---|
614 |
|
---|
615 | if (list_empty(¬ification->msg_list))
|
---|
616 | list_remove(¬ification->qlink);
|
---|
617 |
|
---|
618 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
619 |
|
---|
620 | if (handler)
|
---|
621 | handler(&calldata, arg);
|
---|
622 |
|
---|
623 | free(m);
|
---|
624 | }
|
---|
625 |
|
---|
626 | /* Not reached. */
|
---|
627 | return EOK;
|
---|
628 | }
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * Creates a new dedicated fibril for handling notifications.
|
---|
632 | * By default, there is one such fibril. This function can be used to
|
---|
633 | * create more in order to increase the number of notification that can
|
---|
634 | * be processed concurrently.
|
---|
635 | *
|
---|
636 | * Currently, there is no way to destroy those fibrils after they are created.
|
---|
637 | */
|
---|
638 | errno_t async_spawn_notification_handler(void)
|
---|
639 | {
|
---|
640 | fid_t f = fibril_create(notification_fibril_func, NULL);
|
---|
641 | if (f == 0)
|
---|
642 | return ENOMEM;
|
---|
643 |
|
---|
644 | fibril_add_ready(f);
|
---|
645 | return EOK;
|
---|
646 | }
|
---|
647 |
|
---|
648 | /** Queue notification.
|
---|
649 | *
|
---|
650 | * @param call Data of the incoming call.
|
---|
651 | *
|
---|
652 | */
|
---|
653 | static void queue_notification(ipc_call_t *call)
|
---|
654 | {
|
---|
655 | assert(call);
|
---|
656 |
|
---|
657 | fibril_rmutex_lock(¬ification_mutex);
|
---|
658 |
|
---|
659 | notification_msg_t *m = list_pop(¬ification_freelist,
|
---|
660 | notification_msg_t, link);
|
---|
661 |
|
---|
662 | if (!m) {
|
---|
663 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
664 | m = malloc(sizeof(notification_msg_t));
|
---|
665 | if (!m) {
|
---|
666 | DPRINTF("Out of memory.\n");
|
---|
667 | abort();
|
---|
668 | }
|
---|
669 |
|
---|
670 | fibril_rmutex_lock(¬ification_mutex);
|
---|
671 | notification_freelist_total++;
|
---|
672 | }
|
---|
673 |
|
---|
674 | sysarg_t imethod = ipc_get_imethod(call);
|
---|
675 | ht_link_t *link = hash_table_find(¬ification_hash_table, &imethod);
|
---|
676 | if (!link) {
|
---|
677 | /* Invalid notification. */
|
---|
678 | // TODO: Make sure this can't happen and turn it into assert.
|
---|
679 | notification_freelist_total--;
|
---|
680 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
681 | free(m);
|
---|
682 | return;
|
---|
683 | }
|
---|
684 |
|
---|
685 | notification_t *notification =
|
---|
686 | hash_table_get_inst(link, notification_t, htlink);
|
---|
687 |
|
---|
688 | notification_freelist_used++;
|
---|
689 | m->calldata = *call;
|
---|
690 | list_append(&m->link, ¬ification->msg_list);
|
---|
691 |
|
---|
692 | if (!link_in_use(¬ification->qlink))
|
---|
693 | list_append(¬ification->qlink, ¬ification_queue);
|
---|
694 |
|
---|
695 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
696 |
|
---|
697 | fibril_semaphore_up(¬ification_semaphore);
|
---|
698 | }
|
---|
699 |
|
---|
700 | /**
|
---|
701 | * Creates a new notification structure and inserts it into the hash table.
|
---|
702 | *
|
---|
703 | * @param handler Function to call when notification is received.
|
---|
704 | * @param arg Argument for the handler function.
|
---|
705 | * @return The newly created notification structure.
|
---|
706 | */
|
---|
707 | static notification_t *notification_create(async_notification_handler_t handler, void *arg)
|
---|
708 | {
|
---|
709 | notification_t *notification = calloc(1, sizeof(notification_t));
|
---|
710 | if (!notification)
|
---|
711 | return NULL;
|
---|
712 |
|
---|
713 | notification->handler = handler;
|
---|
714 | notification->arg = arg;
|
---|
715 |
|
---|
716 | list_initialize(¬ification->msg_list);
|
---|
717 |
|
---|
718 | fid_t fib = 0;
|
---|
719 |
|
---|
720 | fibril_rmutex_lock(¬ification_mutex);
|
---|
721 |
|
---|
722 | if (notification_avail == 0) {
|
---|
723 | /* Attempt to create the first handler fibril. */
|
---|
724 | fib = fibril_create(notification_fibril_func, NULL);
|
---|
725 | if (fib == 0) {
|
---|
726 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
727 | free(notification);
|
---|
728 | return NULL;
|
---|
729 | }
|
---|
730 | }
|
---|
731 |
|
---|
732 | sysarg_t imethod = notification_avail;
|
---|
733 | notification_avail++;
|
---|
734 |
|
---|
735 | notification->imethod = imethod;
|
---|
736 | hash_table_insert(¬ification_hash_table, ¬ification->htlink);
|
---|
737 |
|
---|
738 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
739 |
|
---|
740 | if (imethod == 0) {
|
---|
741 | assert(fib);
|
---|
742 | fibril_add_ready(fib);
|
---|
743 | }
|
---|
744 |
|
---|
745 | return notification;
|
---|
746 | }
|
---|
747 |
|
---|
748 | /** Subscribe to IRQ notification.
|
---|
749 | *
|
---|
750 | * @param inr IRQ number.
|
---|
751 | * @param handler Notification handler.
|
---|
752 | * @param data Notification handler client data.
|
---|
753 | * @param ucode Top-half pseudocode handler.
|
---|
754 | *
|
---|
755 | * @param[out] handle IRQ capability handle on success.
|
---|
756 | *
|
---|
757 | * @return An error code.
|
---|
758 | *
|
---|
759 | */
|
---|
760 | errno_t async_irq_subscribe(int inr, async_notification_handler_t handler,
|
---|
761 | void *data, const irq_code_t *ucode, cap_irq_handle_t *handle)
|
---|
762 | {
|
---|
763 | notification_t *notification = notification_create(handler, data);
|
---|
764 | if (!notification)
|
---|
765 | return ENOMEM;
|
---|
766 |
|
---|
767 | cap_irq_handle_t ihandle;
|
---|
768 | errno_t rc = ipc_irq_subscribe(inr, notification->imethod, ucode,
|
---|
769 | &ihandle);
|
---|
770 | if (rc == EOK && handle != NULL) {
|
---|
771 | *handle = ihandle;
|
---|
772 | }
|
---|
773 | return rc;
|
---|
774 | }
|
---|
775 |
|
---|
776 | /** Unsubscribe from IRQ notification.
|
---|
777 | *
|
---|
778 | * @param handle IRQ capability handle.
|
---|
779 | *
|
---|
780 | * @return Zero on success or an error code.
|
---|
781 | *
|
---|
782 | */
|
---|
783 | errno_t async_irq_unsubscribe(cap_irq_handle_t ihandle)
|
---|
784 | {
|
---|
785 | // TODO: Remove entry from hash table
|
---|
786 | // to avoid memory leak
|
---|
787 |
|
---|
788 | return ipc_irq_unsubscribe(ihandle);
|
---|
789 | }
|
---|
790 |
|
---|
791 | /** Subscribe to event notifications.
|
---|
792 | *
|
---|
793 | * @param evno Event type to subscribe.
|
---|
794 | * @param handler Notification handler.
|
---|
795 | * @param data Notification handler client data.
|
---|
796 | *
|
---|
797 | * @return Zero on success or an error code.
|
---|
798 | *
|
---|
799 | */
|
---|
800 | errno_t async_event_subscribe(event_type_t evno,
|
---|
801 | async_notification_handler_t handler, void *data)
|
---|
802 | {
|
---|
803 | notification_t *notification = notification_create(handler, data);
|
---|
804 | if (!notification)
|
---|
805 | return ENOMEM;
|
---|
806 |
|
---|
807 | return ipc_event_subscribe(evno, notification->imethod);
|
---|
808 | }
|
---|
809 |
|
---|
810 | /** Subscribe to task event notifications.
|
---|
811 | *
|
---|
812 | * @param evno Event type to subscribe.
|
---|
813 | * @param handler Notification handler.
|
---|
814 | * @param data Notification handler client data.
|
---|
815 | *
|
---|
816 | * @return Zero on success or an error code.
|
---|
817 | *
|
---|
818 | */
|
---|
819 | errno_t async_event_task_subscribe(event_task_type_t evno,
|
---|
820 | async_notification_handler_t handler, void *data)
|
---|
821 | {
|
---|
822 | notification_t *notification = notification_create(handler, data);
|
---|
823 | if (!notification)
|
---|
824 | return ENOMEM;
|
---|
825 |
|
---|
826 | return ipc_event_task_subscribe(evno, notification->imethod);
|
---|
827 | }
|
---|
828 |
|
---|
829 | /** Unmask event notifications.
|
---|
830 | *
|
---|
831 | * @param evno Event type to unmask.
|
---|
832 | *
|
---|
833 | * @return Value returned by the kernel.
|
---|
834 | *
|
---|
835 | */
|
---|
836 | errno_t async_event_unmask(event_type_t evno)
|
---|
837 | {
|
---|
838 | return ipc_event_unmask(evno);
|
---|
839 | }
|
---|
840 |
|
---|
841 | /** Unmask task event notifications.
|
---|
842 | *
|
---|
843 | * @param evno Event type to unmask.
|
---|
844 | *
|
---|
845 | * @return Value returned by the kernel.
|
---|
846 | *
|
---|
847 | */
|
---|
848 | errno_t async_event_task_unmask(event_task_type_t evno)
|
---|
849 | {
|
---|
850 | return ipc_event_task_unmask(evno);
|
---|
851 | }
|
---|
852 |
|
---|
853 | /** Return new incoming message for the current (fibril-local) connection.
|
---|
854 | *
|
---|
855 | * @param call Storage where the incoming call data will be stored.
|
---|
856 | * @param usecs Timeout in microseconds. Zero denotes no timeout.
|
---|
857 | *
|
---|
858 | * @return If no timeout was specified, then true is returned.
|
---|
859 | * @return If a timeout is specified, then true is returned unless
|
---|
860 | * the timeout expires prior to receiving a message.
|
---|
861 | *
|
---|
862 | */
|
---|
863 | bool async_get_call_timeout(ipc_call_t *call, usec_t usecs)
|
---|
864 | {
|
---|
865 | assert(call);
|
---|
866 | assert(fibril_connection);
|
---|
867 |
|
---|
868 | struct timespec ts;
|
---|
869 | struct timespec *expires = NULL;
|
---|
870 | if (usecs) {
|
---|
871 | getuptime(&ts);
|
---|
872 | ts_add_diff(&ts, USEC2NSEC(usecs));
|
---|
873 | expires = &ts;
|
---|
874 | }
|
---|
875 |
|
---|
876 | errno_t rc = mpsc_receive(fibril_connection->msg_channel,
|
---|
877 | call, expires);
|
---|
878 |
|
---|
879 | if (rc == ETIMEOUT)
|
---|
880 | return false;
|
---|
881 |
|
---|
882 | if (rc != EOK) {
|
---|
883 | /*
|
---|
884 | * The async_get_call_timeout() interface doesn't support
|
---|
885 | * propagating errors. Return a null call instead.
|
---|
886 | */
|
---|
887 |
|
---|
888 | memset(call, 0, sizeof(ipc_call_t));
|
---|
889 | ipc_set_imethod(call, IPC_M_PHONE_HUNGUP);
|
---|
890 | call->cap_handle = CAP_NIL;
|
---|
891 | }
|
---|
892 |
|
---|
893 | return true;
|
---|
894 | }
|
---|
895 |
|
---|
896 | bool async_get_call(ipc_call_t *call)
|
---|
897 | {
|
---|
898 | return async_get_call_timeout(call, 0);
|
---|
899 | }
|
---|
900 |
|
---|
901 | void *async_get_client_data(void)
|
---|
902 | {
|
---|
903 | assert(fibril_connection);
|
---|
904 | return fibril_connection->client->data;
|
---|
905 | }
|
---|
906 |
|
---|
907 | void *async_get_client_data_by_id(task_id_t client_id)
|
---|
908 | {
|
---|
909 | client_t *client = async_client_get(client_id, false);
|
---|
910 | if (!client)
|
---|
911 | return NULL;
|
---|
912 |
|
---|
913 | if (!client->data) {
|
---|
914 | async_client_put(client);
|
---|
915 | return NULL;
|
---|
916 | }
|
---|
917 |
|
---|
918 | return client->data;
|
---|
919 | }
|
---|
920 |
|
---|
921 | void async_put_client_data_by_id(task_id_t client_id)
|
---|
922 | {
|
---|
923 | client_t *client = async_client_get(client_id, false);
|
---|
924 |
|
---|
925 | assert(client);
|
---|
926 | assert(client->data);
|
---|
927 |
|
---|
928 | /* Drop the reference we got in async_get_client_data_by_hash(). */
|
---|
929 | async_client_put(client);
|
---|
930 |
|
---|
931 | /* Drop our own reference we got at the beginning of this function. */
|
---|
932 | async_client_put(client);
|
---|
933 | }
|
---|
934 |
|
---|
935 | /** Handle a call that was received.
|
---|
936 | *
|
---|
937 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
---|
938 | * Otherwise the call is routed to its connection fibril.
|
---|
939 | *
|
---|
940 | * @param call Data of the incoming call.
|
---|
941 | *
|
---|
942 | */
|
---|
943 | static void handle_call(ipc_call_t *call)
|
---|
944 | {
|
---|
945 | assert(call);
|
---|
946 |
|
---|
947 | if (call->flags & IPC_CALL_ANSWERED) {
|
---|
948 | /* Answer to a call made by us. */
|
---|
949 | async_reply_received(call);
|
---|
950 | return;
|
---|
951 | }
|
---|
952 |
|
---|
953 | if (call->cap_handle == CAP_NIL) {
|
---|
954 | if (call->flags & IPC_CALL_NOTIF) {
|
---|
955 | /* Kernel notification */
|
---|
956 | queue_notification(call);
|
---|
957 | }
|
---|
958 | return;
|
---|
959 | }
|
---|
960 |
|
---|
961 | /* New connection */
|
---|
962 | if (ipc_get_imethod(call) == IPC_M_CONNECT_ME_TO) {
|
---|
963 | connection_t *conn = calloc(1, sizeof(*conn));
|
---|
964 | if (!conn) {
|
---|
965 | ipc_answer_0(call->cap_handle, ENOMEM);
|
---|
966 | return;
|
---|
967 | }
|
---|
968 |
|
---|
969 | iface_t iface = (iface_t) ipc_get_arg1(call);
|
---|
970 |
|
---|
971 | // TODO: Currently ignores all ports but the first one.
|
---|
972 | void *data;
|
---|
973 | async_port_handler_t handler =
|
---|
974 | async_get_port_handler(iface, 0, &data);
|
---|
975 |
|
---|
976 | async_new_connection(conn, call->task_id, call, handler, data);
|
---|
977 | return;
|
---|
978 | }
|
---|
979 |
|
---|
980 | /* Route the call according to its request label */
|
---|
981 | errno_t rc = route_call(call);
|
---|
982 | if (rc == EOK) {
|
---|
983 | return;
|
---|
984 | } else if (implicit_connection != NULL) {
|
---|
985 | connection_t *conn = calloc(1, sizeof(connection_t));
|
---|
986 | if (!conn) {
|
---|
987 | ipc_answer_0(call->cap_handle, ENOMEM);
|
---|
988 | return;
|
---|
989 | }
|
---|
990 |
|
---|
991 | async_new_connection(conn, call->task_id, call, implicit_connection, NULL);
|
---|
992 | return;
|
---|
993 | }
|
---|
994 |
|
---|
995 | // TODO: Log the error.
|
---|
996 |
|
---|
997 | if (call->cap_handle != CAP_NIL)
|
---|
998 | /* Unknown call from unknown phone - hang it up */
|
---|
999 | ipc_answer_0(call->cap_handle, EHANGUP);
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | /** Endless loop dispatching incoming calls and answers.
|
---|
1003 | *
|
---|
1004 | * @return Never returns.
|
---|
1005 | *
|
---|
1006 | */
|
---|
1007 | static errno_t async_manager_worker(void)
|
---|
1008 | {
|
---|
1009 | ipc_call_t call;
|
---|
1010 | errno_t rc;
|
---|
1011 |
|
---|
1012 | while (true) {
|
---|
1013 | rc = fibril_ipc_wait(&call, NULL);
|
---|
1014 | if (rc == EOK)
|
---|
1015 | handle_call(&call);
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | return 0;
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | /** Function to start async_manager as a standalone fibril.
|
---|
1022 | *
|
---|
1023 | * When more kernel threads are used, one async manager should exist per thread.
|
---|
1024 | *
|
---|
1025 | * @param arg Unused.
|
---|
1026 | * @return Never returns.
|
---|
1027 | *
|
---|
1028 | */
|
---|
1029 | static errno_t async_manager_fibril(void *arg)
|
---|
1030 | {
|
---|
1031 | async_manager_worker();
|
---|
1032 | return 0;
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | /** Add one manager to manager list. */
|
---|
1036 | static fid_t async_create_manager(void)
|
---|
1037 | {
|
---|
1038 | fid_t fid = fibril_create_generic(async_manager_fibril, NULL, PAGE_SIZE);
|
---|
1039 | fibril_start(fid);
|
---|
1040 | return fid;
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | /** Initialize the async framework.
|
---|
1044 | *
|
---|
1045 | */
|
---|
1046 | void __async_server_init(void)
|
---|
1047 | {
|
---|
1048 | if (fibril_rmutex_initialize(&client_mutex) != EOK)
|
---|
1049 | abort();
|
---|
1050 | if (fibril_rmutex_initialize(¬ification_mutex) != EOK)
|
---|
1051 | abort();
|
---|
1052 |
|
---|
1053 | if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
|
---|
1054 | abort();
|
---|
1055 |
|
---|
1056 | if (!hash_table_create(¬ification_hash_table, 0, 0,
|
---|
1057 | ¬ification_hash_table_ops))
|
---|
1058 | abort();
|
---|
1059 |
|
---|
1060 | async_create_manager();
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | void __async_server_fini(void)
|
---|
1064 | {
|
---|
1065 | fibril_rmutex_destroy(&client_mutex);
|
---|
1066 | fibril_rmutex_destroy(¬ification_mutex);
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | errno_t async_accept_0(ipc_call_t *call)
|
---|
1070 | {
|
---|
1071 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1072 | assert(chandle != CAP_NIL);
|
---|
1073 | call->cap_handle = CAP_NIL;
|
---|
1074 | return ipc_answer_5(chandle, EOK, 0, 0, 0, 0, async_get_label());
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | errno_t async_answer_0(ipc_call_t *call, errno_t retval)
|
---|
1078 | {
|
---|
1079 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1080 | assert(chandle != CAP_NIL);
|
---|
1081 | call->cap_handle = CAP_NIL;
|
---|
1082 | return ipc_answer_0(chandle, retval);
|
---|
1083 | }
|
---|
1084 |
|
---|
1085 | errno_t async_answer_1(ipc_call_t *call, errno_t retval, sysarg_t arg1)
|
---|
1086 | {
|
---|
1087 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1088 | assert(chandle != CAP_NIL);
|
---|
1089 | call->cap_handle = CAP_NIL;
|
---|
1090 | return ipc_answer_1(chandle, retval, arg1);
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | errno_t async_answer_2(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
1094 | sysarg_t arg2)
|
---|
1095 | {
|
---|
1096 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1097 | assert(chandle != CAP_NIL);
|
---|
1098 | call->cap_handle = CAP_NIL;
|
---|
1099 | return ipc_answer_2(chandle, retval, arg1, arg2);
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | errno_t async_answer_3(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
1103 | sysarg_t arg2, sysarg_t arg3)
|
---|
1104 | {
|
---|
1105 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1106 | assert(chandle != CAP_NIL);
|
---|
1107 | call->cap_handle = CAP_NIL;
|
---|
1108 | return ipc_answer_3(chandle, retval, arg1, arg2, arg3);
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 | errno_t async_answer_4(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
1112 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
1113 | {
|
---|
1114 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1115 | assert(chandle != CAP_NIL);
|
---|
1116 | call->cap_handle = CAP_NIL;
|
---|
1117 | return ipc_answer_4(chandle, retval, arg1, arg2, arg3, arg4);
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | errno_t async_answer_5(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
1121 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
1122 | {
|
---|
1123 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1124 | assert(chandle != CAP_NIL);
|
---|
1125 | call->cap_handle = CAP_NIL;
|
---|
1126 | return ipc_answer_5(chandle, retval, arg1, arg2, arg3, arg4, arg5);
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | static errno_t async_forward_fast(ipc_call_t *call, async_exch_t *exch,
|
---|
1130 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
1131 | {
|
---|
1132 | assert(call);
|
---|
1133 |
|
---|
1134 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1135 | assert(chandle != CAP_NIL);
|
---|
1136 | call->cap_handle = CAP_NIL;
|
---|
1137 |
|
---|
1138 | if (exch == NULL)
|
---|
1139 | return ENOENT;
|
---|
1140 |
|
---|
1141 | return ipc_forward_fast(chandle, exch->phone, imethod, arg1, arg2,
|
---|
1142 | mode);
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | static errno_t async_forward_slow(ipc_call_t *call, async_exch_t *exch,
|
---|
1146 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
1147 | sysarg_t arg4, sysarg_t arg5, unsigned int mode)
|
---|
1148 | {
|
---|
1149 | assert(call);
|
---|
1150 |
|
---|
1151 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1152 | assert(chandle != CAP_NIL);
|
---|
1153 | call->cap_handle = CAP_NIL;
|
---|
1154 |
|
---|
1155 | if (exch == NULL)
|
---|
1156 | return ENOENT;
|
---|
1157 |
|
---|
1158 | return ipc_forward_slow(chandle, exch->phone, imethod, arg1, arg2, arg3,
|
---|
1159 | arg4, arg5, mode);
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | errno_t async_forward_0(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
1163 | unsigned int mode)
|
---|
1164 | {
|
---|
1165 | return async_forward_fast(call, exch, imethod, 0, 0, mode);
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | errno_t async_forward_1(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
1169 | sysarg_t arg1, unsigned int mode)
|
---|
1170 | {
|
---|
1171 | return async_forward_fast(call, exch, imethod, arg1, 0, mode);
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 | errno_t async_forward_2(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
1175 | sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
1176 | {
|
---|
1177 | return async_forward_fast(call, exch, imethod, arg1, arg2, mode);
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | errno_t async_forward_3(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
1181 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, unsigned int mode)
|
---|
1182 | {
|
---|
1183 | return async_forward_slow(call, exch, imethod, arg1, arg2, arg3, 0, 0,
|
---|
1184 | mode);
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | errno_t async_forward_4(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
1188 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
1189 | unsigned int mode)
|
---|
1190 | {
|
---|
1191 | return async_forward_slow(call, exch, imethod, arg1, arg2, arg3, arg4,
|
---|
1192 | 0, mode);
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | errno_t async_forward_5(ipc_call_t *call, async_exch_t *exch, sysarg_t imethod,
|
---|
1196 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
1197 | unsigned int mode)
|
---|
1198 | {
|
---|
1199 | return async_forward_slow(call, exch, imethod, arg1, arg2, arg3, arg4,
|
---|
1200 | arg5, mode);
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
1204 | *
|
---|
1205 | * Ask through phone for a new connection to some service.
|
---|
1206 | *
|
---|
1207 | * @param exch Exchange for sending the message.
|
---|
1208 | * @param iface Callback interface.
|
---|
1209 | * @param arg2 User defined argument.
|
---|
1210 | * @param arg3 User defined argument.
|
---|
1211 | *
|
---|
1212 | * @return Zero on success or an error code.
|
---|
1213 | *
|
---|
1214 | */
|
---|
1215 | errno_t async_connect_to_me(async_exch_t *exch, iface_t iface, sysarg_t arg2,
|
---|
1216 | sysarg_t arg3)
|
---|
1217 | {
|
---|
1218 | if (exch == NULL)
|
---|
1219 | return ENOENT;
|
---|
1220 |
|
---|
1221 | sysarg_t label = 0;
|
---|
1222 | errno_t rc = async_req_5_0(exch, IPC_M_CONNECT_TO_ME, iface, arg2, arg3,
|
---|
1223 | 0, label);
|
---|
1224 |
|
---|
1225 | return rc;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
---|
1229 | *
|
---|
1230 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
|
---|
1231 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1232 | * argument.
|
---|
1233 | *
|
---|
1234 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1235 | *
|
---|
1236 | * @param call Storage for the data of the IPC_M_SHARE_IN call.
|
---|
1237 | * @param size Destination address space area size.
|
---|
1238 | *
|
---|
1239 | * @return True on success, false on failure.
|
---|
1240 | *
|
---|
1241 | */
|
---|
1242 | bool async_share_in_receive(ipc_call_t *call, size_t *size)
|
---|
1243 | {
|
---|
1244 | assert(call);
|
---|
1245 | assert(size);
|
---|
1246 |
|
---|
1247 | async_get_call(call);
|
---|
1248 |
|
---|
1249 | if (ipc_get_imethod(call) != IPC_M_SHARE_IN)
|
---|
1250 | return false;
|
---|
1251 |
|
---|
1252 | *size = (size_t) ipc_get_arg1(call);
|
---|
1253 | return true;
|
---|
1254 | }
|
---|
1255 |
|
---|
1256 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
---|
1257 | *
|
---|
1258 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
|
---|
1259 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1260 | * argument.
|
---|
1261 | *
|
---|
1262 | * @param call IPC_M_SHARE_IN call to answer.
|
---|
1263 | * @param src Source address space base.
|
---|
1264 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
1265 | *
|
---|
1266 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1267 | *
|
---|
1268 | */
|
---|
1269 | errno_t async_share_in_finalize(ipc_call_t *call, void *src, unsigned int flags)
|
---|
1270 | {
|
---|
1271 | assert(call);
|
---|
1272 |
|
---|
1273 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1274 | assert(chandle != CAP_NIL);
|
---|
1275 | call->cap_handle = CAP_NIL;
|
---|
1276 |
|
---|
1277 | return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) flags);
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
---|
1281 | *
|
---|
1282 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
|
---|
1283 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1284 | * argument.
|
---|
1285 | *
|
---|
1286 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1287 | *
|
---|
1288 | * @param call Storage for the data of the IPC_M_SHARE_OUT call.
|
---|
1289 | * @param size Storage for the source address space area size.
|
---|
1290 | * @param flags Storage for the sharing flags.
|
---|
1291 | *
|
---|
1292 | * @return True on success, false on failure.
|
---|
1293 | *
|
---|
1294 | */
|
---|
1295 | bool async_share_out_receive(ipc_call_t *call, size_t *size,
|
---|
1296 | unsigned int *flags)
|
---|
1297 | {
|
---|
1298 | assert(call);
|
---|
1299 | assert(size);
|
---|
1300 | assert(flags);
|
---|
1301 |
|
---|
1302 | async_get_call(call);
|
---|
1303 |
|
---|
1304 | if (ipc_get_imethod(call) != IPC_M_SHARE_OUT)
|
---|
1305 | return false;
|
---|
1306 |
|
---|
1307 | *size = (size_t) ipc_get_arg2(call);
|
---|
1308 | *flags = (unsigned int) ipc_get_arg3(call);
|
---|
1309 | return true;
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
---|
1313 | *
|
---|
1314 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
|
---|
1315 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1316 | * argument.
|
---|
1317 | *
|
---|
1318 | * @param call IPC_M_SHARE_OUT call to answer.
|
---|
1319 | * @param dst Address of the storage for the destination address space area
|
---|
1320 | * base address.
|
---|
1321 | *
|
---|
1322 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1323 | *
|
---|
1324 | */
|
---|
1325 | errno_t async_share_out_finalize(ipc_call_t *call, void **dst)
|
---|
1326 | {
|
---|
1327 | assert(call);
|
---|
1328 |
|
---|
1329 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1330 | assert(chandle != CAP_NIL);
|
---|
1331 | call->cap_handle = CAP_NIL;
|
---|
1332 |
|
---|
1333 | return ipc_answer_2(chandle, EOK, (sysarg_t) __progsymbols.end,
|
---|
1334 | (sysarg_t) dst);
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
---|
1338 | *
|
---|
1339 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
|
---|
1340 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1341 | * argument.
|
---|
1342 | *
|
---|
1343 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1344 | *
|
---|
1345 | * @param call Storage for the data of the IPC_M_DATA_READ.
|
---|
1346 | * @param size Storage for the maximum size. Can be NULL.
|
---|
1347 | *
|
---|
1348 | * @return True on success, false on failure.
|
---|
1349 | *
|
---|
1350 | */
|
---|
1351 | bool async_data_read_receive(ipc_call_t *call, size_t *size)
|
---|
1352 | {
|
---|
1353 | assert(call);
|
---|
1354 |
|
---|
1355 | async_get_call(call);
|
---|
1356 |
|
---|
1357 | if (ipc_get_imethod(call) != IPC_M_DATA_READ)
|
---|
1358 | return false;
|
---|
1359 |
|
---|
1360 | if (size)
|
---|
1361 | *size = (size_t) ipc_get_arg2(call);
|
---|
1362 |
|
---|
1363 | return true;
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
---|
1367 | *
|
---|
1368 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
---|
1369 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1370 | * argument.
|
---|
1371 | *
|
---|
1372 | * @param call IPC_M_DATA_READ call to answer.
|
---|
1373 | * @param src Source address for the IPC_M_DATA_READ call.
|
---|
1374 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
---|
1375 | * the maximum size announced by the sender.
|
---|
1376 | *
|
---|
1377 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1378 | *
|
---|
1379 | */
|
---|
1380 | errno_t async_data_read_finalize(ipc_call_t *call, const void *src, size_t size)
|
---|
1381 | {
|
---|
1382 | assert(call);
|
---|
1383 |
|
---|
1384 | cap_call_handle_t chandle = call->cap_handle;
|
---|
1385 | assert(chandle != CAP_NIL);
|
---|
1386 | call->cap_handle = CAP_NIL;
|
---|
1387 |
|
---|
1388 | return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) size);
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | /** Wrapper for forwarding any read request
|
---|
1392 | *
|
---|
1393 | */
|
---|
1394 | static errno_t async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
|
---|
1395 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
1396 | ipc_call_t *dataptr)
|
---|
1397 | {
|
---|
1398 | if (exch == NULL)
|
---|
1399 | return ENOENT;
|
---|
1400 |
|
---|
1401 | ipc_call_t call;
|
---|
1402 | if (!async_data_read_receive(&call, NULL)) {
|
---|
1403 | async_answer_0(&call, EINVAL);
|
---|
1404 | return EINVAL;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | aid_t msg = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
|
---|
1408 | dataptr);
|
---|
1409 | if (msg == 0) {
|
---|
1410 | async_answer_0(&call, EINVAL);
|
---|
1411 | return EINVAL;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | errno_t retval = ipc_forward_fast(call.cap_handle, exch->phone, 0, 0, 0,
|
---|
1415 | IPC_FF_ROUTE_FROM_ME);
|
---|
1416 | if (retval != EOK) {
|
---|
1417 | async_forget(msg);
|
---|
1418 | async_answer_0(&call, retval);
|
---|
1419 | return retval;
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | errno_t rc;
|
---|
1423 | async_wait_for(msg, &rc);
|
---|
1424 |
|
---|
1425 | return (errno_t) rc;
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | errno_t async_data_read_forward_0_0(async_exch_t *exch, sysarg_t imethod)
|
---|
1429 | {
|
---|
1430 | return async_data_read_forward_fast(exch, imethod, 0, 0, 0, 0, NULL);
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | errno_t async_data_read_forward_1_0(async_exch_t *exch, sysarg_t imethod,
|
---|
1434 | sysarg_t arg1)
|
---|
1435 | {
|
---|
1436 | return async_data_read_forward_fast(exch, imethod, arg1, 0, 0, 0, NULL);
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | errno_t async_data_read_forward_2_0(async_exch_t *exch, sysarg_t imethod,
|
---|
1440 | sysarg_t arg1, sysarg_t arg2)
|
---|
1441 | {
|
---|
1442 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, 0,
|
---|
1443 | 0, NULL);
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | errno_t async_data_read_forward_3_0(async_exch_t *exch, sysarg_t imethod,
|
---|
1447 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
---|
1448 | {
|
---|
1449 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
1450 | 0, NULL);
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | errno_t async_data_read_forward_4_0(async_exch_t *exch, sysarg_t imethod,
|
---|
1454 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
1455 | {
|
---|
1456 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
1457 | arg4, NULL);
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 | errno_t async_data_read_forward_0_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1461 | ipc_call_t *dataptr)
|
---|
1462 | {
|
---|
1463 | return async_data_read_forward_fast(exch, imethod, 0, 0, 0,
|
---|
1464 | 0, dataptr);
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 | errno_t async_data_read_forward_1_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1468 | sysarg_t arg1, ipc_call_t *dataptr)
|
---|
1469 | {
|
---|
1470 | return async_data_read_forward_fast(exch, imethod, arg1, 0, 0,
|
---|
1471 | 0, dataptr);
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 | errno_t async_data_read_forward_2_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1475 | sysarg_t arg1, sysarg_t arg2, ipc_call_t *dataptr)
|
---|
1476 | {
|
---|
1477 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, 0,
|
---|
1478 | 0, dataptr);
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | errno_t async_data_read_forward_3_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1482 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, ipc_call_t *dataptr)
|
---|
1483 | {
|
---|
1484 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
1485 | 0, dataptr);
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | errno_t async_data_read_forward_4_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1489 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
1490 | ipc_call_t *dataptr)
|
---|
1491 | {
|
---|
1492 | return async_data_read_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
1493 | arg4, dataptr);
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
---|
1497 | *
|
---|
1498 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
|
---|
1499 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1500 | * argument.
|
---|
1501 | *
|
---|
1502 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1503 | *
|
---|
1504 | * @param call Storage for the data of the IPC_M_DATA_WRITE.
|
---|
1505 | * @param size Storage for the suggested size. May be NULL.
|
---|
1506 | *
|
---|
1507 | * @return True on success, false on failure.
|
---|
1508 | *
|
---|
1509 | */
|
---|
1510 | bool async_data_write_receive(ipc_call_t *call, size_t *size)
|
---|
1511 | {
|
---|
1512 | assert(call);
|
---|
1513 |
|
---|
1514 | async_get_call(call);
|
---|
1515 |
|
---|
1516 | if (ipc_get_imethod(call) != IPC_M_DATA_WRITE)
|
---|
1517 | return false;
|
---|
1518 |
|
---|
1519 | if (size)
|
---|
1520 | *size = (size_t) ipc_get_arg2(call);
|
---|
1521 |
|
---|
1522 | return true;
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
---|
1526 | *
|
---|
1527 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
|
---|
1528 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1529 | * argument.
|
---|
1530 | *
|
---|
1531 | * @param call IPC_M_DATA_WRITE call to answer.
|
---|
1532 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
---|
1533 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
---|
1534 | *
|
---|
1535 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1536 | *
|
---|
1537 | */
|
---|
1538 | errno_t async_data_write_finalize(ipc_call_t *call, void *dst, size_t size)
|
---|
1539 | {
|
---|
1540 | assert(call);
|
---|
1541 |
|
---|
1542 | return async_answer_2(call, EOK, (sysarg_t) dst, (sysarg_t) size);
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | /** Wrapper for receiving binary data or strings
|
---|
1546 | *
|
---|
1547 | * This wrapper only makes it more comfortable to use async_data_write_*
|
---|
1548 | * functions to receive binary data or strings.
|
---|
1549 | *
|
---|
1550 | * @param data Pointer to data pointer (which should be later disposed
|
---|
1551 | * by free()). If the operation fails, the pointer is not
|
---|
1552 | * touched.
|
---|
1553 | * @param nullterm If true then the received data is always zero terminated.
|
---|
1554 | * This also causes to allocate one extra byte beyond the
|
---|
1555 | * raw transmitted data.
|
---|
1556 | * @param min_size Minimum size (in bytes) of the data to receive.
|
---|
1557 | * @param max_size Maximum size (in bytes) of the data to receive. 0 means
|
---|
1558 | * no limit.
|
---|
1559 | * @param granulariy If non-zero then the size of the received data has to
|
---|
1560 | * be divisible by this value.
|
---|
1561 | * @param received If not NULL, the size of the received data is stored here.
|
---|
1562 | *
|
---|
1563 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1564 | *
|
---|
1565 | */
|
---|
1566 | errno_t async_data_write_accept(void **data, const bool nullterm,
|
---|
1567 | const size_t min_size, const size_t max_size, const size_t granularity,
|
---|
1568 | size_t *received)
|
---|
1569 | {
|
---|
1570 | assert(data);
|
---|
1571 |
|
---|
1572 | ipc_call_t call;
|
---|
1573 | size_t size;
|
---|
1574 | if (!async_data_write_receive(&call, &size)) {
|
---|
1575 | async_answer_0(&call, EINVAL);
|
---|
1576 | return EINVAL;
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | if (size < min_size) {
|
---|
1580 | async_answer_0(&call, EINVAL);
|
---|
1581 | return EINVAL;
|
---|
1582 | }
|
---|
1583 |
|
---|
1584 | if ((max_size > 0) && (size > max_size)) {
|
---|
1585 | async_answer_0(&call, EINVAL);
|
---|
1586 | return EINVAL;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | if ((granularity > 0) && ((size % granularity) != 0)) {
|
---|
1590 | async_answer_0(&call, EINVAL);
|
---|
1591 | return EINVAL;
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | void *arg_data;
|
---|
1595 |
|
---|
1596 | if (nullterm)
|
---|
1597 | arg_data = malloc(size + 1);
|
---|
1598 | else
|
---|
1599 | arg_data = malloc(size);
|
---|
1600 |
|
---|
1601 | if (arg_data == NULL) {
|
---|
1602 | async_answer_0(&call, ENOMEM);
|
---|
1603 | return ENOMEM;
|
---|
1604 | }
|
---|
1605 |
|
---|
1606 | errno_t rc = async_data_write_finalize(&call, arg_data, size);
|
---|
1607 | if (rc != EOK) {
|
---|
1608 | free(arg_data);
|
---|
1609 | return rc;
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | if (nullterm)
|
---|
1613 | ((char *) arg_data)[size] = 0;
|
---|
1614 |
|
---|
1615 | *data = arg_data;
|
---|
1616 | if (received != NULL)
|
---|
1617 | *received = size;
|
---|
1618 |
|
---|
1619 | return EOK;
|
---|
1620 | }
|
---|
1621 |
|
---|
1622 | /** Wrapper for voiding any data that is about to be received
|
---|
1623 | *
|
---|
1624 | * This wrapper can be used to void any pending data
|
---|
1625 | *
|
---|
1626 | * @param retval Error value from @ref errno.h to be returned to the caller.
|
---|
1627 | *
|
---|
1628 | */
|
---|
1629 | void async_data_write_void(errno_t retval)
|
---|
1630 | {
|
---|
1631 | ipc_call_t call;
|
---|
1632 | async_data_write_receive(&call, NULL);
|
---|
1633 | async_answer_0(&call, retval);
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | /** Wrapper for forwarding any data that is about to be received
|
---|
1637 | *
|
---|
1638 | */
|
---|
1639 | static errno_t async_data_write_forward_fast(async_exch_t *exch,
|
---|
1640 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
1641 | sysarg_t arg4, ipc_call_t *dataptr)
|
---|
1642 | {
|
---|
1643 | if (exch == NULL)
|
---|
1644 | return ENOENT;
|
---|
1645 |
|
---|
1646 | ipc_call_t call;
|
---|
1647 | if (!async_data_write_receive(&call, NULL)) {
|
---|
1648 | async_answer_0(&call, EINVAL);
|
---|
1649 | return EINVAL;
|
---|
1650 | }
|
---|
1651 |
|
---|
1652 | aid_t msg = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
|
---|
1653 | dataptr);
|
---|
1654 | if (msg == 0) {
|
---|
1655 | async_answer_0(&call, EINVAL);
|
---|
1656 | return EINVAL;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | errno_t retval = ipc_forward_fast(call.cap_handle, exch->phone, 0, 0, 0,
|
---|
1660 | IPC_FF_ROUTE_FROM_ME);
|
---|
1661 | if (retval != EOK) {
|
---|
1662 | async_forget(msg);
|
---|
1663 | async_answer_0(&call, retval);
|
---|
1664 | return retval;
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | errno_t rc;
|
---|
1668 | async_wait_for(msg, &rc);
|
---|
1669 |
|
---|
1670 | return (errno_t) rc;
|
---|
1671 | }
|
---|
1672 |
|
---|
1673 | errno_t async_data_write_forward_0_0(async_exch_t *exch, sysarg_t imethod)
|
---|
1674 | {
|
---|
1675 | return async_data_write_forward_fast(exch, imethod, 0, 0, 0,
|
---|
1676 | 0, NULL);
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | errno_t async_data_write_forward_1_0(async_exch_t *exch, sysarg_t imethod,
|
---|
1680 | sysarg_t arg1)
|
---|
1681 | {
|
---|
1682 | return async_data_write_forward_fast(exch, imethod, arg1, 0, 0,
|
---|
1683 | 0, NULL);
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | errno_t async_data_write_forward_2_0(async_exch_t *exch, sysarg_t imethod,
|
---|
1687 | sysarg_t arg1, sysarg_t arg2)
|
---|
1688 | {
|
---|
1689 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, 0,
|
---|
1690 | 0, NULL);
|
---|
1691 | }
|
---|
1692 |
|
---|
1693 | errno_t async_data_write_forward_3_0(async_exch_t *exch, sysarg_t imethod,
|
---|
1694 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
---|
1695 | {
|
---|
1696 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
1697 | 0, NULL);
|
---|
1698 | }
|
---|
1699 |
|
---|
1700 | errno_t async_data_write_forward_4_0(async_exch_t *exch, sysarg_t imethod,
|
---|
1701 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
1702 | {
|
---|
1703 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
1704 | arg4, NULL);
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | errno_t async_data_write_forward_0_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1708 | ipc_call_t *dataptr)
|
---|
1709 | {
|
---|
1710 | return async_data_write_forward_fast(exch, imethod, 0, 0, 0,
|
---|
1711 | 0, dataptr);
|
---|
1712 | }
|
---|
1713 |
|
---|
1714 | errno_t async_data_write_forward_1_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1715 | sysarg_t arg1, ipc_call_t *dataptr)
|
---|
1716 | {
|
---|
1717 | return async_data_write_forward_fast(exch, imethod, arg1, 0, 0,
|
---|
1718 | 0, dataptr);
|
---|
1719 | }
|
---|
1720 |
|
---|
1721 | errno_t async_data_write_forward_2_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1722 | sysarg_t arg1, sysarg_t arg2, ipc_call_t *dataptr)
|
---|
1723 | {
|
---|
1724 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, 0,
|
---|
1725 | 0, dataptr);
|
---|
1726 | }
|
---|
1727 |
|
---|
1728 | errno_t async_data_write_forward_3_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1729 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, ipc_call_t *dataptr)
|
---|
1730 | {
|
---|
1731 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
1732 | 0, dataptr);
|
---|
1733 | }
|
---|
1734 |
|
---|
1735 | errno_t async_data_write_forward_4_1(async_exch_t *exch, sysarg_t imethod,
|
---|
1736 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
1737 | ipc_call_t *dataptr)
|
---|
1738 | {
|
---|
1739 | return async_data_write_forward_fast(exch, imethod, arg1, arg2, arg3,
|
---|
1740 | arg4, dataptr);
|
---|
1741 | }
|
---|
1742 |
|
---|
1743 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
---|
1744 | *
|
---|
1745 | * If the current call is IPC_M_CONNECT_TO_ME then a new
|
---|
1746 | * async session is created for the accepted phone.
|
---|
1747 | *
|
---|
1748 | * @param mgmt Exchange management style.
|
---|
1749 | *
|
---|
1750 | * @return New async session.
|
---|
1751 | * @return NULL on failure.
|
---|
1752 | *
|
---|
1753 | */
|
---|
1754 | async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
|
---|
1755 | {
|
---|
1756 | /* Accept the phone */
|
---|
1757 | ipc_call_t call;
|
---|
1758 | async_get_call(&call);
|
---|
1759 |
|
---|
1760 | cap_phone_handle_t phandle = (cap_handle_t) ipc_get_arg5(&call);
|
---|
1761 |
|
---|
1762 | if ((ipc_get_imethod(&call) != IPC_M_CONNECT_TO_ME) ||
|
---|
1763 | !cap_handle_valid((phandle))) {
|
---|
1764 | async_answer_0(&call, EINVAL);
|
---|
1765 | return NULL;
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | async_sess_t *sess = create_session(phandle, mgmt, 0, 0, 0);
|
---|
1769 | if (sess == NULL) {
|
---|
1770 | ipc_hangup(phandle);
|
---|
1771 | async_answer_0(&call, errno);
|
---|
1772 | } else {
|
---|
1773 | /* Acknowledge the connected phone */
|
---|
1774 | async_answer_0(&call, EOK);
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 | return sess;
|
---|
1778 | }
|
---|
1779 |
|
---|
1780 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
---|
1781 | *
|
---|
1782 | * If the call is IPC_M_CONNECT_TO_ME then a new
|
---|
1783 | * async session is created. However, the phone is
|
---|
1784 | * not accepted automatically.
|
---|
1785 | *
|
---|
1786 | * @param mgmt Exchange management style.
|
---|
1787 | * @param call Call data.
|
---|
1788 | *
|
---|
1789 | * @return New async session.
|
---|
1790 | * @return NULL on failure.
|
---|
1791 | * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
|
---|
1792 | *
|
---|
1793 | */
|
---|
1794 | async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
|
---|
1795 | {
|
---|
1796 | cap_phone_handle_t phandle = (cap_handle_t) ipc_get_arg5(call);
|
---|
1797 |
|
---|
1798 | if ((ipc_get_imethod(call) != IPC_M_CONNECT_TO_ME) ||
|
---|
1799 | !cap_handle_valid((phandle)))
|
---|
1800 | return NULL;
|
---|
1801 |
|
---|
1802 | return create_session(phandle, mgmt, 0, 0, 0);
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 | bool async_state_change_receive(ipc_call_t *call)
|
---|
1806 | {
|
---|
1807 | assert(call);
|
---|
1808 |
|
---|
1809 | async_get_call(call);
|
---|
1810 |
|
---|
1811 | if (ipc_get_imethod(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
|
---|
1812 | return false;
|
---|
1813 |
|
---|
1814 | return true;
|
---|
1815 | }
|
---|
1816 |
|
---|
1817 | errno_t async_state_change_finalize(ipc_call_t *call, async_exch_t *other_exch)
|
---|
1818 | {
|
---|
1819 | assert(call);
|
---|
1820 |
|
---|
1821 | return async_answer_1(call, EOK, cap_handle_raw(other_exch->phone));
|
---|
1822 | }
|
---|
1823 |
|
---|
1824 | __noreturn void async_manager(void)
|
---|
1825 | {
|
---|
1826 | fibril_event_t ever = FIBRIL_EVENT_INIT;
|
---|
1827 | fibril_wait_for(&ever);
|
---|
1828 | __builtin_unreachable();
|
---|
1829 | }
|
---|
1830 |
|
---|
1831 | /** @}
|
---|
1832 | */
|
---|