source: mainline/uspace/lib/c/generic/async.c@ 007e6efa

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