source: mainline/uspace/lib/c/generic/async.c@ 11bb813

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 11bb813 was c1c0184, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Make session management explicit.

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