source: mainline/uspace/lib/c/generic/async.c@ 8a1fb09

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8a1fb09 was c7bbf029, checked in by Martin Decky <martin@…>, 14 years ago

improve stack traces and assertions
reduce header files pollution

  • Property mode set to 100644
File size: 47.2 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @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 synchronization problems.
44 *
45 * Example of use (pseudo C):
46 *
47 * 1) Multithreaded client application
48 *
49 * fibril_create(fibril1, ...);
50 * fibril_create(fibril2, ...);
51 * ...
52 *
53 * int fibril1(void *arg)
54 * {
55 * conn = async_connect_me_to();
56 * c1 = async_send(conn);
57 * c2 = async_send(conn);
58 * async_wait_for(c1);
59 * async_wait_for(c2);
60 * ...
61 * }
62 *
63 *
64 * 2) Multithreaded server application
65 *
66 * main()
67 * {
68 * async_manager();
69 * }
70 *
71 * my_client_connection(icallid, *icall)
72 * {
73 * if (want_refuse) {
74 * async_answer_0(icallid, ELIMIT);
75 * return;
76 * }
77 * async_answer_0(icallid, EOK);
78 *
79 * callid = async_get_call(&call);
80 * somehow_handle_the_call(callid, call);
81 * async_answer_2(callid, 1, 2, 3);
82 *
83 * callid = async_get_call(&call);
84 * ...
85 * }
86 *
87 */
88
89#define LIBC_ASYNC_C_
90#include <ipc/ipc.h>
91#include <async.h>
92#undef LIBC_ASYNC_C_
93
94#include <futex.h>
95#include <fibril.h>
96#include <stdio.h>
97#include <adt/hash_table.h>
98#include <adt/list.h>
99#include <assert.h>
100#include <errno.h>
101#include <sys/time.h>
102#include <arch/barrier.h>
103#include <bool.h>
104#include <stdlib.h>
105#include <malloc.h>
106#include "private/async.h"
107
108atomic_t async_futex = FUTEX_INITIALIZER;
109
110/** Number of threads waiting for IPC in the kernel. */
111atomic_t threads_in_ipc_wait = { 0 };
112
113typedef struct {
114 awaiter_t wdata;
115
116 /** If reply was received. */
117 bool done;
118
119 /** Pointer to where the answer data is stored. */
120 ipc_call_t *dataptr;
121
122 sysarg_t retval;
123} amsg_t;
124
125/**
126 * Structures of this type are used to group information about
127 * a call and about a message queue link.
128 */
129typedef struct {
130 link_t link;
131 ipc_callid_t callid;
132 ipc_call_t call;
133} msg_t;
134
135typedef struct {
136 sysarg_t in_task_hash;
137 link_t link;
138 int refcnt;
139 void *data;
140} client_t;
141
142typedef struct {
143 awaiter_t wdata;
144
145 /** Hash table link. */
146 link_t link;
147
148 /** Incoming client task hash. */
149 sysarg_t in_task_hash;
150 /** Incoming phone hash. */
151 sysarg_t in_phone_hash;
152
153 /** Link to the client tracking structure. */
154 client_t *client;
155
156 /** Messages that should be delivered to this fibril. */
157 link_t msg_queue;
158
159 /** Identification of the opening call. */
160 ipc_callid_t callid;
161 /** Call data of the opening call. */
162 ipc_call_t call;
163
164 /** Identification of the closing call. */
165 ipc_callid_t close_callid;
166
167 /** Fibril function that will be used to handle the connection. */
168 void (*cfibril)(ipc_callid_t, ipc_call_t *);
169} connection_t;
170
171/** Identifier of the incoming connection handled by the current fibril. */
172static fibril_local connection_t *FIBRIL_connection;
173
174static void *default_client_data_constructor(void)
175{
176 return NULL;
177}
178
179static void default_client_data_destructor(void *data)
180{
181}
182
183static async_client_data_ctor_t async_client_data_create =
184 default_client_data_constructor;
185static async_client_data_dtor_t async_client_data_destroy =
186 default_client_data_destructor;
187
188void async_set_client_data_constructor(async_client_data_ctor_t ctor)
189{
190 async_client_data_create = ctor;
191}
192
193void async_set_client_data_destructor(async_client_data_dtor_t dtor)
194{
195 async_client_data_destroy = dtor;
196}
197
198void *async_client_data_get(void)
199{
200 assert(FIBRIL_connection);
201 return FIBRIL_connection->client->data;
202}
203
204/** Default fibril function that gets called to handle new connection.
205 *
206 * This function is defined as a weak symbol - to be redefined in user code.
207 *
208 * @param callid Hash of the incoming call.
209 * @param call Data of the incoming call.
210 *
211 */
212static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
213{
214 ipc_answer_0(callid, ENOENT);
215}
216
217/**
218 * Pointer to a fibril function that will be used to handle connections.
219 */
220static async_client_conn_t client_connection = default_client_connection;
221
222/** Default fibril function that gets called to handle interrupt notifications.
223 *
224 * This function is defined as a weak symbol - to be redefined in user code.
225 *
226 * @param callid Hash of the incoming call.
227 * @param call Data of the incoming call.
228 *
229 */
230static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
231{
232}
233
234/**
235 * Pointer to a fibril function that will be used to handle interrupt
236 * notifications.
237 */
238static async_client_conn_t interrupt_received = default_interrupt_received;
239
240static hash_table_t client_hash_table;
241static hash_table_t conn_hash_table;
242static LIST_INITIALIZE(timeout_list);
243
244#define CLIENT_HASH_TABLE_BUCKETS 32
245#define CONN_HASH_TABLE_BUCKETS 32
246
247static hash_index_t client_hash(unsigned long key[])
248{
249 assert(key);
250 return (((key[0]) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
251}
252
253static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
254{
255 client_t *client = hash_table_get_instance(item, client_t, link);
256 return (key[0] == client->in_task_hash);
257}
258
259static void client_remove(link_t *item)
260{
261}
262
263/** Operations for the client hash table. */
264static hash_table_operations_t client_hash_table_ops = {
265 .hash = client_hash,
266 .compare = client_compare,
267 .remove_callback = client_remove
268};
269
270/** Compute hash into the connection hash table based on the source phone hash.
271 *
272 * @param key Pointer to source phone hash.
273 *
274 * @return Index into the connection hash table.
275 *
276 */
277static hash_index_t conn_hash(unsigned long key[])
278{
279 assert(key);
280 return (((key[0]) >> 4) % CONN_HASH_TABLE_BUCKETS);
281}
282
283/** Compare hash table item with a key.
284 *
285 * @param key Array containing the source phone hash as the only item.
286 * @param keys Expected 1 but ignored.
287 * @param item Connection hash table item.
288 *
289 * @return True on match, false otherwise.
290 *
291 */
292static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
293{
294 connection_t *conn = hash_table_get_instance(item, connection_t, link);
295 return (key[0] == conn->in_phone_hash);
296}
297
298static void conn_remove(link_t *item)
299{
300}
301
302/** Operations for the connection hash table. */
303static hash_table_operations_t conn_hash_table_ops = {
304 .hash = conn_hash,
305 .compare = conn_compare,
306 .remove_callback = conn_remove
307};
308
309/** Sort in current fibril's timeout request.
310 *
311 * @param wd Wait data of the current fibril.
312 *
313 */
314void async_insert_timeout(awaiter_t *wd)
315{
316 wd->to_event.occurred = false;
317 wd->to_event.inlist = true;
318
319 link_t *tmp = timeout_list.next;
320 while (tmp != &timeout_list) {
321 awaiter_t *cur
322 = list_get_instance(tmp, awaiter_t, to_event.link);
323
324 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
325 break;
326
327 tmp = tmp->next;
328 }
329
330 list_append(&wd->to_event.link, tmp);
331}
332
333/** Try to route a call to an appropriate connection fibril.
334 *
335 * If the proper connection fibril is found, a message with the call is added to
336 * its message queue. If the fibril was not active, it is activated and all
337 * timeouts are unregistered.
338 *
339 * @param callid Hash of the incoming call.
340 * @param call Data of the incoming call.
341 *
342 * @return False if the call doesn't match any connection.
343 * @return True if the call was passed to the respective connection fibril.
344 *
345 */
346static bool route_call(ipc_callid_t callid, ipc_call_t *call)
347{
348 futex_down(&async_futex);
349
350 unsigned long key = call->in_phone_hash;
351 link_t *hlp = hash_table_find(&conn_hash_table, &key);
352
353 if (!hlp) {
354 futex_up(&async_futex);
355 return false;
356 }
357
358 connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
359
360 msg_t *msg = malloc(sizeof(*msg));
361 if (!msg) {
362 futex_up(&async_futex);
363 return false;
364 }
365
366 msg->callid = callid;
367 msg->call = *call;
368 list_append(&msg->link, &conn->msg_queue);
369
370 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
371 conn->close_callid = callid;
372
373 /* If the connection fibril is waiting for an event, activate it */
374 if (!conn->wdata.active) {
375
376 /* If in timeout list, remove it */
377 if (conn->wdata.to_event.inlist) {
378 conn->wdata.to_event.inlist = false;
379 list_remove(&conn->wdata.to_event.link);
380 }
381
382 conn->wdata.active = true;
383 fibril_add_ready(conn->wdata.fid);
384 }
385
386 futex_up(&async_futex);
387 return true;
388}
389
390/** Notification fibril.
391 *
392 * When a notification arrives, a fibril with this implementing function is
393 * created. It calls interrupt_received() and does the final cleanup.
394 *
395 * @param arg Message structure pointer.
396 *
397 * @return Always zero.
398 *
399 */
400static int notification_fibril(void *arg)
401{
402 msg_t *msg = (msg_t *) arg;
403 interrupt_received(msg->callid, &msg->call);
404
405 free(msg);
406 return 0;
407}
408
409/** Process interrupt notification.
410 *
411 * A new fibril is created which would process the notification.
412 *
413 * @param callid Hash of the incoming call.
414 * @param call Data of the incoming call.
415 *
416 * @return False if an error occured.
417 * True if the call was passed to the notification fibril.
418 *
419 */
420static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
421{
422 futex_down(&async_futex);
423
424 msg_t *msg = malloc(sizeof(*msg));
425 if (!msg) {
426 futex_up(&async_futex);
427 return false;
428 }
429
430 msg->callid = callid;
431 msg->call = *call;
432
433 fid_t fid = fibril_create(notification_fibril, msg);
434 if (fid == 0) {
435 free(msg);
436 futex_up(&async_futex);
437 return false;
438 }
439
440 fibril_add_ready(fid);
441
442 futex_up(&async_futex);
443 return true;
444}
445
446/** Return new incoming message for the current (fibril-local) connection.
447 *
448 * @param call Storage where the incoming call data will be stored.
449 * @param usecs Timeout in microseconds. Zero denotes no timeout.
450 *
451 * @return If no timeout was specified, then a hash of the
452 * incoming call is returned. If a timeout is specified,
453 * then a hash of the incoming call is returned unless
454 * the timeout expires prior to receiving a message. In
455 * that case zero is returned.
456 *
457 */
458ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
459{
460 assert(FIBRIL_connection);
461
462 /* Why doing this?
463 * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
464 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
465 * I would never expect to find so many errors in
466 * a compiler.
467 */
468 connection_t *conn = FIBRIL_connection;
469
470 futex_down(&async_futex);
471
472 if (usecs) {
473 gettimeofday(&conn->wdata.to_event.expires, NULL);
474 tv_add(&conn->wdata.to_event.expires, usecs);
475 } else
476 conn->wdata.to_event.inlist = false;
477
478 /* If nothing in queue, wait until something arrives */
479 while (list_empty(&conn->msg_queue)) {
480 if (conn->close_callid) {
481 /*
482 * Handle the case when the connection was already
483 * closed by the client but the server did not notice
484 * the first IPC_M_PHONE_HUNGUP call and continues to
485 * call async_get_call_timeout(). Repeat
486 * IPC_M_PHONE_HUNGUP until the caller notices.
487 */
488 memset(call, 0, sizeof(ipc_call_t));
489 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
490 futex_up(&async_futex);
491 return conn->close_callid;
492 }
493
494 if (usecs)
495 async_insert_timeout(&conn->wdata);
496
497 conn->wdata.active = false;
498
499 /*
500 * Note: the current fibril will be rescheduled either due to a
501 * timeout or due to an arriving message destined to it. In the
502 * former case, handle_expired_timeouts() and, in the latter
503 * case, route_call() will perform the wakeup.
504 */
505 fibril_switch(FIBRIL_TO_MANAGER);
506
507 /*
508 * Futex is up after getting back from async_manager.
509 * Get it again.
510 */
511 futex_down(&async_futex);
512 if ((usecs) && (conn->wdata.to_event.occurred)
513 && (list_empty(&conn->msg_queue))) {
514 /* If we timed out -> exit */
515 futex_up(&async_futex);
516 return 0;
517 }
518 }
519
520 msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
521 list_remove(&msg->link);
522
523 ipc_callid_t callid = msg->callid;
524 *call = msg->call;
525 free(msg);
526
527 futex_up(&async_futex);
528 return callid;
529}
530
531/** Wrapper for client connection fibril.
532 *
533 * When a new connection arrives, a fibril with this implementing function is
534 * created. It calls client_connection() and does the final cleanup.
535 *
536 * @param arg Connection structure pointer.
537 *
538 * @return Always zero.
539 *
540 */
541static int connection_fibril(void *arg)
542{
543 /*
544 * Setup fibril-local connection pointer.
545 */
546 FIBRIL_connection = (connection_t *) arg;
547
548 futex_down(&async_futex);
549
550 /*
551 * Add our reference for the current connection in the client task
552 * tracking structure. If this is the first reference, create and
553 * hash in a new tracking structure.
554 */
555
556 unsigned long key = FIBRIL_connection->in_task_hash;
557 link_t *lnk = hash_table_find(&client_hash_table, &key);
558
559 client_t *client;
560
561 if (lnk) {
562 client = hash_table_get_instance(lnk, client_t, link);
563 client->refcnt++;
564 } else {
565 client = malloc(sizeof(client_t));
566 if (!client) {
567 ipc_answer_0(FIBRIL_connection->callid, ENOMEM);
568 futex_up(&async_futex);
569 return 0;
570 }
571
572 client->in_task_hash = FIBRIL_connection->in_task_hash;
573
574 async_serialize_start();
575 client->data = async_client_data_create();
576 async_serialize_end();
577
578 client->refcnt = 1;
579 hash_table_insert(&client_hash_table, &key, &client->link);
580 }
581
582 futex_up(&async_futex);
583
584 FIBRIL_connection->client = client;
585
586 /*
587 * Call the connection handler function.
588 */
589 FIBRIL_connection->cfibril(FIBRIL_connection->callid,
590 &FIBRIL_connection->call);
591
592 /*
593 * Remove the reference for this client task connection.
594 */
595 bool destroy;
596
597 futex_down(&async_futex);
598
599 if (--client->refcnt == 0) {
600 hash_table_remove(&client_hash_table, &key, 1);
601 destroy = true;
602 } else
603 destroy = false;
604
605 futex_up(&async_futex);
606
607 if (destroy) {
608 if (client->data)
609 async_client_data_destroy(client->data);
610
611 free(client);
612 }
613
614 /*
615 * Remove myself from the connection hash table.
616 */
617 futex_down(&async_futex);
618 key = FIBRIL_connection->in_phone_hash;
619 hash_table_remove(&conn_hash_table, &key, 1);
620 futex_up(&async_futex);
621
622 /*
623 * Answer all remaining messages with EHANGUP.
624 */
625 while (!list_empty(&FIBRIL_connection->msg_queue)) {
626 msg_t *msg =
627 list_get_instance(FIBRIL_connection->msg_queue.next, msg_t,
628 link);
629
630 list_remove(&msg->link);
631 ipc_answer_0(msg->callid, EHANGUP);
632 free(msg);
633 }
634
635 /*
636 * If the connection was hung-up, answer the last call,
637 * i.e. IPC_M_PHONE_HUNGUP.
638 */
639 if (FIBRIL_connection->close_callid)
640 ipc_answer_0(FIBRIL_connection->close_callid, EOK);
641
642 free(FIBRIL_connection);
643 return 0;
644}
645
646/** Create a new fibril for a new connection.
647 *
648 * Create new fibril for connection, fill in connection structures and inserts
649 * it into the hash table, so that later we can easily do routing of messages to
650 * particular fibrils.
651 *
652 * @param in_task_hash Identification of the incoming connection.
653 * @param in_phone_hash Identification of the incoming connection.
654 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
655 * If callid is zero, the connection was opened by
656 * accepting the IPC_M_CONNECT_TO_ME call and this function
657 * is called directly by the server.
658 * @param call Call data of the opening call.
659 * @param cfibril Fibril function that should be called upon opening the
660 * connection.
661 *
662 * @return New fibril id or NULL on failure.
663 *
664 */
665fid_t async_new_connection(sysarg_t in_task_hash, sysarg_t in_phone_hash,
666 ipc_callid_t callid, ipc_call_t *call,
667 void (*cfibril)(ipc_callid_t, ipc_call_t *))
668{
669 connection_t *conn = malloc(sizeof(*conn));
670 if (!conn) {
671 if (callid)
672 ipc_answer_0(callid, ENOMEM);
673
674 return (uintptr_t) NULL;
675 }
676
677 conn->in_task_hash = in_task_hash;
678 conn->in_phone_hash = in_phone_hash;
679 list_initialize(&conn->msg_queue);
680 conn->callid = callid;
681 conn->close_callid = 0;
682
683 if (call)
684 conn->call = *call;
685
686 /* We will activate the fibril ASAP */
687 conn->wdata.active = true;
688 conn->cfibril = cfibril;
689 conn->wdata.fid = fibril_create(connection_fibril, conn);
690
691 if (conn->wdata.fid == 0) {
692 free(conn);
693
694 if (callid)
695 ipc_answer_0(callid, ENOMEM);
696
697 return (uintptr_t) NULL;
698 }
699
700 /* Add connection to the connection hash table */
701 unsigned long key = conn->in_phone_hash;
702
703 futex_down(&async_futex);
704 hash_table_insert(&conn_hash_table, &key, &conn->link);
705 futex_up(&async_futex);
706
707 fibril_add_ready(conn->wdata.fid);
708
709 return conn->wdata.fid;
710}
711
712/** Handle a call that was received.
713 *
714 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
715 * Otherwise the call is routed to its connection fibril.
716 *
717 * @param callid Hash of the incoming call.
718 * @param call Data of the incoming call.
719 *
720 */
721static void handle_call(ipc_callid_t callid, ipc_call_t *call)
722{
723 /* Unrouted call - take some default action */
724 if ((callid & IPC_CALLID_NOTIFICATION)) {
725 process_notification(callid, call);
726 return;
727 }
728
729 switch (IPC_GET_IMETHOD(*call)) {
730 case IPC_M_CONNECT_ME:
731 case IPC_M_CONNECT_ME_TO:
732 /* Open new connection with fibril, etc. */
733 async_new_connection(call->in_task_hash, IPC_GET_ARG5(*call),
734 callid, call, client_connection);
735 return;
736 }
737
738 /* Try to route the call through the connection hash table */
739 if (route_call(callid, call))
740 return;
741
742 /* Unknown call from unknown phone - hang it up */
743 ipc_answer_0(callid, EHANGUP);
744}
745
746/** Fire all timeouts that expired. */
747static void handle_expired_timeouts(void)
748{
749 struct timeval tv;
750 gettimeofday(&tv, NULL);
751
752 futex_down(&async_futex);
753
754 link_t *cur = timeout_list.next;
755 while (cur != &timeout_list) {
756 awaiter_t *waiter =
757 list_get_instance(cur, awaiter_t, to_event.link);
758
759 if (tv_gt(&waiter->to_event.expires, &tv))
760 break;
761
762 cur = cur->next;
763
764 list_remove(&waiter->to_event.link);
765 waiter->to_event.inlist = false;
766 waiter->to_event.occurred = true;
767
768 /*
769 * Redundant condition?
770 * The fibril should not be active when it gets here.
771 */
772 if (!waiter->active) {
773 waiter->active = true;
774 fibril_add_ready(waiter->fid);
775 }
776 }
777
778 futex_up(&async_futex);
779}
780
781/** Endless loop dispatching incoming calls and answers.
782 *
783 * @return Never returns.
784 *
785 */
786static int async_manager_worker(void)
787{
788 while (true) {
789 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
790 futex_up(&async_futex);
791 /*
792 * async_futex is always held when entering a manager
793 * fibril.
794 */
795 continue;
796 }
797
798 futex_down(&async_futex);
799
800 suseconds_t timeout;
801 if (!list_empty(&timeout_list)) {
802 awaiter_t *waiter = list_get_instance(timeout_list.next,
803 awaiter_t, to_event.link);
804
805 struct timeval tv;
806 gettimeofday(&tv, NULL);
807
808 if (tv_gteq(&tv, &waiter->to_event.expires)) {
809 futex_up(&async_futex);
810 handle_expired_timeouts();
811 continue;
812 } else
813 timeout = tv_sub(&waiter->to_event.expires, &tv);
814 } else
815 timeout = SYNCH_NO_TIMEOUT;
816
817 futex_up(&async_futex);
818
819 atomic_inc(&threads_in_ipc_wait);
820
821 ipc_call_t call;
822 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
823 SYNCH_FLAGS_NONE);
824
825 atomic_dec(&threads_in_ipc_wait);
826
827 if (!callid) {
828 handle_expired_timeouts();
829 continue;
830 }
831
832 if (callid & IPC_CALLID_ANSWERED)
833 continue;
834
835 handle_call(callid, &call);
836 }
837
838 return 0;
839}
840
841/** Function to start async_manager as a standalone fibril.
842 *
843 * When more kernel threads are used, one async manager should exist per thread.
844 *
845 * @param arg Unused.
846 * @return Never returns.
847 *
848 */
849static int async_manager_fibril(void *arg)
850{
851 futex_up(&async_futex);
852
853 /*
854 * async_futex is always locked when entering manager
855 */
856 async_manager_worker();
857
858 return 0;
859}
860
861/** Add one manager to manager list. */
862void async_create_manager(void)
863{
864 fid_t fid = fibril_create(async_manager_fibril, NULL);
865 if (fid != 0)
866 fibril_add_manager(fid);
867}
868
869/** Remove one manager from manager list */
870void async_destroy_manager(void)
871{
872 fibril_remove_manager();
873}
874
875/** Initialize the async framework.
876 *
877 */
878void __async_init(void)
879{
880 if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS, 1,
881 &client_hash_table_ops))
882 abort();
883
884 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_BUCKETS, 1,
885 &conn_hash_table_ops))
886 abort();
887}
888
889/** Reply received callback.
890 *
891 * This function is called whenever a reply for an asynchronous message sent out
892 * by the asynchronous framework is received.
893 *
894 * Notify the fibril which is waiting for this message that it has arrived.
895 *
896 * @param arg Pointer to the asynchronous message record.
897 * @param retval Value returned in the answer.
898 * @param data Call data of the answer.
899 *
900 */
901static void reply_received(void *arg, int retval, ipc_call_t *data)
902{
903 futex_down(&async_futex);
904
905 amsg_t *msg = (amsg_t *) arg;
906 msg->retval = retval;
907
908 /* Copy data after futex_down, just in case the call was detached */
909 if ((msg->dataptr) && (data))
910 *msg->dataptr = *data;
911
912 write_barrier();
913
914 /* Remove message from timeout list */
915 if (msg->wdata.to_event.inlist)
916 list_remove(&msg->wdata.to_event.link);
917
918 msg->done = true;
919 if (!msg->wdata.active) {
920 msg->wdata.active = true;
921 fibril_add_ready(msg->wdata.fid);
922 }
923
924 futex_up(&async_futex);
925}
926
927/** Send message and return id of the sent message.
928 *
929 * The return value can be used as input for async_wait() to wait for
930 * completion.
931 *
932 * @param phoneid Handle of the phone that will be used for the send.
933 * @param method Service-defined method.
934 * @param arg1 Service-defined payload argument.
935 * @param arg2 Service-defined payload argument.
936 * @param arg3 Service-defined payload argument.
937 * @param arg4 Service-defined payload argument.
938 * @param dataptr If non-NULL, storage where the reply data will be
939 * stored.
940 *
941 * @return Hash of the sent message or 0 on error.
942 *
943 */
944aid_t async_send_fast(int phoneid, sysarg_t method, sysarg_t arg1,
945 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
946{
947 amsg_t *msg = malloc(sizeof(amsg_t));
948
949 if (!msg)
950 return 0;
951
952 msg->done = false;
953 msg->dataptr = dataptr;
954
955 msg->wdata.to_event.inlist = false;
956
957 /*
958 * We may sleep in the next method,
959 * but it will use its own means
960 */
961 msg->wdata.active = true;
962
963 ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
964 reply_received, true);
965
966 return (aid_t) msg;
967}
968
969/** Send message and return id of the sent message
970 *
971 * The return value can be used as input for async_wait() to wait for
972 * completion.
973 *
974 * @param phoneid Handle of the phone that will be used for the send.
975 * @param method Service-defined method.
976 * @param arg1 Service-defined payload argument.
977 * @param arg2 Service-defined payload argument.
978 * @param arg3 Service-defined payload argument.
979 * @param arg4 Service-defined payload argument.
980 * @param arg5 Service-defined payload argument.
981 * @param dataptr If non-NULL, storage where the reply data will be
982 * stored.
983 *
984 * @return Hash of the sent message or 0 on error.
985 *
986 */
987aid_t async_send_slow(int phoneid, sysarg_t method, sysarg_t arg1,
988 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
989 ipc_call_t *dataptr)
990{
991 amsg_t *msg = malloc(sizeof(amsg_t));
992
993 if (!msg)
994 return 0;
995
996 msg->done = false;
997 msg->dataptr = dataptr;
998
999 msg->wdata.to_event.inlist = false;
1000
1001 /*
1002 * We may sleep in the next method,
1003 * but it will use its own means
1004 */
1005 msg->wdata.active = true;
1006
1007 ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
1008 reply_received, true);
1009
1010 return (aid_t) msg;
1011}
1012
1013/** Wait for a message sent by the async framework.
1014 *
1015 * @param amsgid Hash of the message to wait for.
1016 * @param retval Pointer to storage where the retval of the answer will
1017 * be stored.
1018 *
1019 */
1020void async_wait_for(aid_t amsgid, sysarg_t *retval)
1021{
1022 amsg_t *msg = (amsg_t *) amsgid;
1023
1024 futex_down(&async_futex);
1025 if (msg->done) {
1026 futex_up(&async_futex);
1027 goto done;
1028 }
1029
1030 msg->wdata.fid = fibril_get_id();
1031 msg->wdata.active = false;
1032 msg->wdata.to_event.inlist = false;
1033
1034 /* Leave the async_futex locked when entering this function */
1035 fibril_switch(FIBRIL_TO_MANAGER);
1036
1037 /* Futex is up automatically after fibril_switch */
1038
1039done:
1040 if (retval)
1041 *retval = msg->retval;
1042
1043 free(msg);
1044}
1045
1046/** Wait for a message sent by the async framework, timeout variant.
1047 *
1048 * @param amsgid Hash of the message to wait for.
1049 * @param retval Pointer to storage where the retval of the answer will
1050 * be stored.
1051 * @param timeout Timeout in microseconds.
1052 *
1053 * @return Zero on success, ETIMEOUT if the timeout has expired.
1054 *
1055 */
1056int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
1057{
1058 amsg_t *msg = (amsg_t *) amsgid;
1059
1060 /* TODO: Let it go through the event read at least once */
1061 if (timeout < 0)
1062 return ETIMEOUT;
1063
1064 futex_down(&async_futex);
1065 if (msg->done) {
1066 futex_up(&async_futex);
1067 goto done;
1068 }
1069
1070 gettimeofday(&msg->wdata.to_event.expires, NULL);
1071 tv_add(&msg->wdata.to_event.expires, timeout);
1072
1073 msg->wdata.fid = fibril_get_id();
1074 msg->wdata.active = false;
1075 async_insert_timeout(&msg->wdata);
1076
1077 /* Leave the async_futex locked when entering this function */
1078 fibril_switch(FIBRIL_TO_MANAGER);
1079
1080 /* Futex is up automatically after fibril_switch */
1081
1082 if (!msg->done)
1083 return ETIMEOUT;
1084
1085done:
1086 if (retval)
1087 *retval = msg->retval;
1088
1089 free(msg);
1090
1091 return 0;
1092}
1093
1094/** Wait for specified time.
1095 *
1096 * The current fibril is suspended but the thread continues to execute.
1097 *
1098 * @param timeout Duration of the wait in microseconds.
1099 *
1100 */
1101void async_usleep(suseconds_t timeout)
1102{
1103 amsg_t *msg = malloc(sizeof(amsg_t));
1104
1105 if (!msg)
1106 return;
1107
1108 msg->wdata.fid = fibril_get_id();
1109 msg->wdata.active = false;
1110
1111 gettimeofday(&msg->wdata.to_event.expires, NULL);
1112 tv_add(&msg->wdata.to_event.expires, timeout);
1113
1114 futex_down(&async_futex);
1115
1116 async_insert_timeout(&msg->wdata);
1117
1118 /* Leave the async_futex locked when entering this function */
1119 fibril_switch(FIBRIL_TO_MANAGER);
1120
1121 /* Futex is up automatically after fibril_switch() */
1122
1123 free(msg);
1124}
1125
1126/** Setter for client_connection function pointer.
1127 *
1128 * @param conn Function that will implement a new connection fibril.
1129 *
1130 */
1131void async_set_client_connection(async_client_conn_t conn)
1132{
1133 client_connection = conn;
1134}
1135
1136/** Setter for interrupt_received function pointer.
1137 *
1138 * @param intr Function that will implement a new interrupt
1139 * notification fibril.
1140 */
1141void async_set_interrupt_received(async_client_conn_t intr)
1142{
1143 interrupt_received = intr;
1144}
1145
1146/** Pseudo-synchronous message sending - fast version.
1147 *
1148 * Send message asynchronously and return only after the reply arrives.
1149 *
1150 * This function can only transfer 4 register payload arguments. For
1151 * transferring more arguments, see the slower async_req_slow().
1152 *
1153 * @param phoneid Hash of the phone through which to make the call.
1154 * @param method Method of the call.
1155 * @param arg1 Service-defined payload argument.
1156 * @param arg2 Service-defined payload argument.
1157 * @param arg3 Service-defined payload argument.
1158 * @param arg4 Service-defined payload argument.
1159 * @param r1 If non-NULL, storage for the 1st reply argument.
1160 * @param r2 If non-NULL, storage for the 2nd reply argument.
1161 * @param r3 If non-NULL, storage for the 3rd reply argument.
1162 * @param r4 If non-NULL, storage for the 4th reply argument.
1163 * @param r5 If non-NULL, storage for the 5th reply argument.
1164 *
1165 * @return Return code of the reply or a negative error code.
1166 *
1167 */
1168sysarg_t async_req_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1169 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1170 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1171{
1172 ipc_call_t result;
1173 aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1174 &result);
1175
1176 sysarg_t rc;
1177 async_wait_for(eid, &rc);
1178
1179 if (r1)
1180 *r1 = IPC_GET_ARG1(result);
1181
1182 if (r2)
1183 *r2 = IPC_GET_ARG2(result);
1184
1185 if (r3)
1186 *r3 = IPC_GET_ARG3(result);
1187
1188 if (r4)
1189 *r4 = IPC_GET_ARG4(result);
1190
1191 if (r5)
1192 *r5 = IPC_GET_ARG5(result);
1193
1194 return rc;
1195}
1196
1197/** Pseudo-synchronous message sending - slow version.
1198 *
1199 * Send message asynchronously and return only after the reply arrives.
1200 *
1201 * @param phoneid Hash of the phone through which to make the call.
1202 * @param method Method of the call.
1203 * @param arg1 Service-defined payload argument.
1204 * @param arg2 Service-defined payload argument.
1205 * @param arg3 Service-defined payload argument.
1206 * @param arg4 Service-defined payload argument.
1207 * @param arg5 Service-defined payload argument.
1208 * @param r1 If non-NULL, storage for the 1st reply argument.
1209 * @param r2 If non-NULL, storage for the 2nd reply argument.
1210 * @param r3 If non-NULL, storage for the 3rd reply argument.
1211 * @param r4 If non-NULL, storage for the 4th reply argument.
1212 * @param r5 If non-NULL, storage for the 5th reply argument.
1213 *
1214 * @return Return code of the reply or a negative error code.
1215 *
1216 */
1217sysarg_t async_req_slow(int phoneid, sysarg_t method, sysarg_t arg1,
1218 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1219 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1220{
1221 ipc_call_t result;
1222 aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1223 &result);
1224
1225 sysarg_t rc;
1226 async_wait_for(eid, &rc);
1227
1228 if (r1)
1229 *r1 = IPC_GET_ARG1(result);
1230
1231 if (r2)
1232 *r2 = IPC_GET_ARG2(result);
1233
1234 if (r3)
1235 *r3 = IPC_GET_ARG3(result);
1236
1237 if (r4)
1238 *r4 = IPC_GET_ARG4(result);
1239
1240 if (r5)
1241 *r5 = IPC_GET_ARG5(result);
1242
1243 return rc;
1244}
1245
1246void async_msg_0(int phone, sysarg_t imethod)
1247{
1248 ipc_call_async_0(phone, imethod, NULL, NULL, true);
1249}
1250
1251void async_msg_1(int phone, sysarg_t imethod, sysarg_t arg1)
1252{
1253 ipc_call_async_1(phone, imethod, arg1, NULL, NULL, true);
1254}
1255
1256void async_msg_2(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2)
1257{
1258 ipc_call_async_2(phone, imethod, arg1, arg2, NULL, NULL, true);
1259}
1260
1261void async_msg_3(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
1262 sysarg_t arg3)
1263{
1264 ipc_call_async_3(phone, imethod, arg1, arg2, arg3, NULL, NULL, true);
1265}
1266
1267void async_msg_4(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
1268 sysarg_t arg3, sysarg_t arg4)
1269{
1270 ipc_call_async_4(phone, imethod, arg1, arg2, arg3, arg4, NULL, NULL,
1271 true);
1272}
1273
1274void async_msg_5(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
1275 sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1276{
1277 ipc_call_async_5(phone, imethod, arg1, arg2, arg3, arg4, arg5, NULL,
1278 NULL, true);
1279}
1280
1281sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
1282{
1283 return ipc_answer_0(callid, retval);
1284}
1285
1286sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
1287{
1288 return ipc_answer_1(callid, retval, arg1);
1289}
1290
1291sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1292 sysarg_t arg2)
1293{
1294 return ipc_answer_2(callid, retval, arg1, arg2);
1295}
1296
1297sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1298 sysarg_t arg2, sysarg_t arg3)
1299{
1300 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
1301}
1302
1303sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1304 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1305{
1306 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
1307}
1308
1309sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1310 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1311{
1312 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
1313}
1314
1315int async_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
1316 sysarg_t arg1, sysarg_t arg2, unsigned int mode)
1317{
1318 return ipc_forward_fast(callid, phoneid, imethod, arg1, arg2, mode);
1319}
1320
1321int async_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
1322 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
1323 unsigned int mode)
1324{
1325 return ipc_forward_slow(callid, phoneid, imethod, arg1, arg2, arg3, arg4,
1326 arg5, mode);
1327}
1328
1329/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1330 *
1331 * Ask through phone for a new connection to some service.
1332 *
1333 * @param phone Phone handle used for contacting the other side.
1334 * @param arg1 User defined argument.
1335 * @param arg2 User defined argument.
1336 * @param arg3 User defined argument.
1337 * @param client_receiver Connection handing routine.
1338 *
1339 * @return New phone handle on success or a negative error code.
1340 *
1341 */
1342int async_connect_to_me(int phone, sysarg_t arg1, sysarg_t arg2,
1343 sysarg_t arg3, async_client_conn_t client_receiver)
1344{
1345 sysarg_t task_hash;
1346 sysarg_t phone_hash;
1347 int rc = async_req_3_5(phone, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1348 NULL, NULL, NULL, &task_hash, &phone_hash);
1349 if (rc != EOK)
1350 return rc;
1351
1352 if (client_receiver != NULL)
1353 async_new_connection(task_hash, phone_hash, 0, NULL,
1354 client_receiver);
1355
1356 return EOK;
1357}
1358
1359/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1360 *
1361 * Ask through phone for a new connection to some service.
1362 *
1363 * @param phone Phone handle used for contacting the other side.
1364 * @param arg1 User defined argument.
1365 * @param arg2 User defined argument.
1366 * @param arg3 User defined argument.
1367 *
1368 * @return New phone handle on success or a negative error code.
1369 *
1370 */
1371int async_connect_me_to(int phone, sysarg_t arg1, sysarg_t arg2,
1372 sysarg_t arg3)
1373{
1374 sysarg_t newphid;
1375 int rc = async_req_3_5(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
1376 NULL, NULL, NULL, NULL, &newphid);
1377
1378 if (rc != EOK)
1379 return rc;
1380
1381 return newphid;
1382}
1383
1384/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1385 *
1386 * Ask through phone for a new connection to some service and block until
1387 * success.
1388 *
1389 * @param phoneid Phone handle used for contacting the other side.
1390 * @param arg1 User defined argument.
1391 * @param arg2 User defined argument.
1392 * @param arg3 User defined argument.
1393 *
1394 * @return New phone handle on success or a negative error code.
1395 *
1396 */
1397int async_connect_me_to_blocking(int phoneid, sysarg_t arg1, sysarg_t arg2,
1398 sysarg_t arg3)
1399{
1400 sysarg_t newphid;
1401 int rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
1402 IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
1403
1404 if (rc != EOK)
1405 return rc;
1406
1407 return newphid;
1408}
1409
1410/** Connect to a task specified by id.
1411 *
1412 */
1413int async_connect_kbox(task_id_t id)
1414{
1415 return ipc_connect_kbox(id);
1416}
1417
1418/** Wrapper for ipc_hangup.
1419 *
1420 * @param phone Phone handle to hung up.
1421 *
1422 * @return Zero on success or a negative error code.
1423 *
1424 */
1425int async_hangup(int phone)
1426{
1427 return ipc_hangup(phone);
1428}
1429
1430/** Interrupt one thread of this task from waiting for IPC. */
1431void async_poke(void)
1432{
1433 ipc_poke();
1434}
1435
1436/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
1437 *
1438 * @param phoneid Phone that will be used to contact the receiving side.
1439 * @param dst Destination address space area base.
1440 * @param size Size of the destination address space area.
1441 * @param arg User defined argument.
1442 * @param flags Storage for the received flags. Can be NULL.
1443 *
1444 * @return Zero on success or a negative error code from errno.h.
1445 *
1446 */
1447int async_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
1448 unsigned int *flags)
1449{
1450 sysarg_t tmp_flags;
1451 int res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
1452 (sysarg_t) size, arg, NULL, &tmp_flags);
1453
1454 if (flags)
1455 *flags = (unsigned int) tmp_flags;
1456
1457 return res;
1458}
1459
1460/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1461 *
1462 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
1463 * calls so that the user doesn't have to remember the meaning of each IPC
1464 * argument.
1465 *
1466 * So far, this wrapper is to be used from within a connection fibril.
1467 *
1468 * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
1469 * @param size Destination address space area size.
1470 *
1471 * @return True on success, false on failure.
1472 *
1473 */
1474bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
1475{
1476 assert(callid);
1477 assert(size);
1478
1479 ipc_call_t data;
1480 *callid = async_get_call(&data);
1481
1482 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
1483 return false;
1484
1485 *size = (size_t) IPC_GET_ARG2(data);
1486 return true;
1487}
1488
1489/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1490 *
1491 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
1492 * calls so that the user doesn't have to remember the meaning of each IPC
1493 * argument.
1494 *
1495 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1496 * @param src Source address space base.
1497 * @param flags Flags to be used for sharing. Bits can be only cleared.
1498 *
1499 * @return Zero on success or a value from @ref errno.h on failure.
1500 *
1501 */
1502int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
1503{
1504 return ipc_share_in_finalize(callid, src, flags);
1505}
1506
1507/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
1508 *
1509 * @param phoneid Phone that will be used to contact the receiving side.
1510 * @param src Source address space area base address.
1511 * @param flags Flags to be used for sharing. Bits can be only cleared.
1512 *
1513 * @return Zero on success or a negative error code from errno.h.
1514 *
1515 */
1516int async_share_out_start(int phoneid, void *src, unsigned int flags)
1517{
1518 return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
1519 (sysarg_t) flags);
1520}
1521
1522/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1523 *
1524 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
1525 * calls so that the user doesn't have to remember the meaning of each IPC
1526 * argument.
1527 *
1528 * So far, this wrapper is to be used from within a connection fibril.
1529 *
1530 * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
1531 * @param size Storage for the source address space area size.
1532 * @param flags Storage for the sharing flags.
1533 *
1534 * @return True on success, false on failure.
1535 *
1536 */
1537bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
1538{
1539 assert(callid);
1540 assert(size);
1541 assert(flags);
1542
1543 ipc_call_t data;
1544 *callid = async_get_call(&data);
1545
1546 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
1547 return false;
1548
1549 *size = (size_t) IPC_GET_ARG2(data);
1550 *flags = (unsigned int) IPC_GET_ARG3(data);
1551 return true;
1552}
1553
1554/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
1555 *
1556 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
1557 * calls so that the user doesn't have to remember the meaning of each IPC
1558 * argument.
1559 *
1560 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1561 * @param dst Destination address space area base address.
1562 *
1563 * @return Zero on success or a value from @ref errno.h on failure.
1564 *
1565 */
1566int async_share_out_finalize(ipc_callid_t callid, void *dst)
1567{
1568 return ipc_share_out_finalize(callid, dst);
1569}
1570
1571/** Wrapper for IPC_M_DATA_READ calls using the async framework.
1572 *
1573 * @param phoneid Phone that will be used to contact the receiving side.
1574 * @param dst Address of the beginning of the destination buffer.
1575 * @param size Size of the destination buffer.
1576 * @param flags Flags to control the data transfer.
1577 *
1578 * @return Zero on success or a negative error code from errno.h.
1579 *
1580 */
1581int
1582async_data_read_start_generic(int phoneid, void *dst, size_t size, int flags)
1583{
1584 return async_req_3_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
1585 (sysarg_t) size, (sysarg_t) flags);
1586}
1587
1588/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
1589 *
1590 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
1591 * calls so that the user doesn't have to remember the meaning of each IPC
1592 * argument.
1593 *
1594 * So far, this wrapper is to be used from within a connection fibril.
1595 *
1596 * @param callid Storage for the hash of the IPC_M_DATA_READ.
1597 * @param size Storage for the maximum size. Can be NULL.
1598 *
1599 * @return True on success, false on failure.
1600 *
1601 */
1602bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
1603{
1604 assert(callid);
1605
1606 ipc_call_t data;
1607 *callid = async_get_call(&data);
1608
1609 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
1610 return false;
1611
1612 if (size)
1613 *size = (size_t) IPC_GET_ARG2(data);
1614
1615 return true;
1616}
1617
1618/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
1619 *
1620 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
1621 * calls so that the user doesn't have to remember the meaning of each IPC
1622 * argument.
1623 *
1624 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1625 * @param src Source address for the IPC_M_DATA_READ call.
1626 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
1627 * the maximum size announced by the sender.
1628 *
1629 * @return Zero on success or a value from @ref errno.h on failure.
1630 *
1631 */
1632int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
1633{
1634 return ipc_data_read_finalize(callid, src, size);
1635}
1636
1637/** Wrapper for forwarding any read request
1638 *
1639 */
1640int async_data_read_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1641 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1642{
1643 ipc_callid_t callid;
1644 if (!async_data_read_receive(&callid, NULL)) {
1645 ipc_answer_0(callid, EINVAL);
1646 return EINVAL;
1647 }
1648
1649 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1650 dataptr);
1651 if (msg == 0) {
1652 ipc_answer_0(callid, EINVAL);
1653 return EINVAL;
1654 }
1655
1656 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1657 IPC_FF_ROUTE_FROM_ME);
1658 if (retval != EOK) {
1659 async_wait_for(msg, NULL);
1660 ipc_answer_0(callid, retval);
1661 return retval;
1662 }
1663
1664 sysarg_t rc;
1665 async_wait_for(msg, &rc);
1666
1667 return (int) rc;
1668}
1669
1670/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
1671 *
1672 * @param phoneid Phone that will be used to contact the receiving side.
1673 * @param src Address of the beginning of the source buffer.
1674 * @param size Size of the source buffer.
1675 * @param flags Flags to control the data transfer.
1676 *
1677 * @return Zero on success or a negative error code from errno.h.
1678 *
1679 */
1680int
1681async_data_write_start_generic(int phoneid, const void *src, size_t size,
1682 int flags)
1683{
1684 return async_req_3_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
1685 (sysarg_t) size, (sysarg_t) flags);
1686}
1687
1688/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
1689 *
1690 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
1691 * calls so that the user doesn't have to remember the meaning of each IPC
1692 * argument.
1693 *
1694 * So far, this wrapper is to be used from within a connection fibril.
1695 *
1696 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
1697 * @param size Storage for the suggested size. May be NULL.
1698 *
1699 * @return True on success, false on failure.
1700 *
1701 */
1702bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
1703{
1704 assert(callid);
1705
1706 ipc_call_t data;
1707 *callid = async_get_call(&data);
1708
1709 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
1710 return false;
1711
1712 if (size)
1713 *size = (size_t) IPC_GET_ARG2(data);
1714
1715 return true;
1716}
1717
1718/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
1719 *
1720 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
1721 * calls so that the user doesn't have to remember the meaning of each IPC
1722 * argument.
1723 *
1724 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1725 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
1726 * @param size Final size for the IPC_M_DATA_WRITE call.
1727 *
1728 * @return Zero on success or a value from @ref errno.h on failure.
1729 *
1730 */
1731int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
1732{
1733 return ipc_data_write_finalize(callid, dst, size);
1734}
1735
1736/** Wrapper for receiving binary data or strings
1737 *
1738 * This wrapper only makes it more comfortable to use async_data_write_*
1739 * functions to receive binary data or strings.
1740 *
1741 * @param data Pointer to data pointer (which should be later disposed
1742 * by free()). If the operation fails, the pointer is not
1743 * touched.
1744 * @param nullterm If true then the received data is always zero terminated.
1745 * This also causes to allocate one extra byte beyond the
1746 * raw transmitted data.
1747 * @param min_size Minimum size (in bytes) of the data to receive.
1748 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
1749 * no limit.
1750 * @param granulariy If non-zero then the size of the received data has to
1751 * be divisible by this value.
1752 * @param received If not NULL, the size of the received data is stored here.
1753 *
1754 * @return Zero on success or a value from @ref errno.h on failure.
1755 *
1756 */
1757int async_data_write_accept(void **data, const bool nullterm,
1758 const size_t min_size, const size_t max_size, const size_t granularity,
1759 size_t *received)
1760{
1761 ipc_callid_t callid;
1762 size_t size;
1763 if (!async_data_write_receive(&callid, &size)) {
1764 ipc_answer_0(callid, EINVAL);
1765 return EINVAL;
1766 }
1767
1768 if (size < min_size) {
1769 ipc_answer_0(callid, EINVAL);
1770 return EINVAL;
1771 }
1772
1773 if ((max_size > 0) && (size > max_size)) {
1774 ipc_answer_0(callid, EINVAL);
1775 return EINVAL;
1776 }
1777
1778 if ((granularity > 0) && ((size % granularity) != 0)) {
1779 ipc_answer_0(callid, EINVAL);
1780 return EINVAL;
1781 }
1782
1783 void *_data;
1784
1785 if (nullterm)
1786 _data = malloc(size + 1);
1787 else
1788 _data = malloc(size);
1789
1790 if (_data == NULL) {
1791 ipc_answer_0(callid, ENOMEM);
1792 return ENOMEM;
1793 }
1794
1795 int rc = async_data_write_finalize(callid, _data, size);
1796 if (rc != EOK) {
1797 free(_data);
1798 return rc;
1799 }
1800
1801 if (nullterm)
1802 ((char *) _data)[size] = 0;
1803
1804 *data = _data;
1805 if (received != NULL)
1806 *received = size;
1807
1808 return EOK;
1809}
1810
1811/** Wrapper for voiding any data that is about to be received
1812 *
1813 * This wrapper can be used to void any pending data
1814 *
1815 * @param retval Error value from @ref errno.h to be returned to the caller.
1816 *
1817 */
1818void async_data_write_void(sysarg_t retval)
1819{
1820 ipc_callid_t callid;
1821 async_data_write_receive(&callid, NULL);
1822 ipc_answer_0(callid, retval);
1823}
1824
1825/** Wrapper for forwarding any data that is about to be received
1826 *
1827 */
1828int async_data_write_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1829 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1830{
1831 ipc_callid_t callid;
1832 if (!async_data_write_receive(&callid, NULL)) {
1833 ipc_answer_0(callid, EINVAL);
1834 return EINVAL;
1835 }
1836
1837 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1838 dataptr);
1839 if (msg == 0) {
1840 ipc_answer_0(callid, EINVAL);
1841 return EINVAL;
1842 }
1843
1844 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1845 IPC_FF_ROUTE_FROM_ME);
1846 if (retval != EOK) {
1847 async_wait_for(msg, NULL);
1848 ipc_answer_0(callid, retval);
1849 return retval;
1850 }
1851
1852 sysarg_t rc;
1853 async_wait_for(msg, &rc);
1854
1855 return (int) rc;
1856}
1857
1858/** @}
1859 */
Note: See TracBrowser for help on using the repository browser.