source: mainline/uspace/lib/c/generic/async/server.c@ 5c55eb7

Last change on this file since 5c55eb7 was 0db0df2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Hash table improvements

Implement hash_table_foreach macro, analogous to list_foreach.

Remove superfluous argument to hash_table_find_next().
(If the user needs to recheck the part of the list already
checked by hash_table_find(), they can just rerun that function.)

Add hash argument to hash_table_ops_t::key_equal.
The big change here is that users with big keys can store the hash
value alongside key in their entries, and for the low low cost of
sizeof(size_t) bytes eliminate a bunch of expensive key comparisons.

Also added a hash function for strings and arbitrary data.
Found this one by asking ChatGPT, because the latency of accesses
to my book collection is currently a couple of hours.

+ Some drive-by unused #include removal.

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