source: mainline/uspace/lib/c/generic/async.c@ a39cfb8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a39cfb8 was b77ce84, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Merge mainline changes

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