source: mainline/uspace/lib/c/generic/async.c@ 36b16bc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36b16bc was 36b16bc, checked in by Jakub Jermar <jakub@…>, 14 years ago

Fix leftover use of task hash in async_connect_to_me().

  • Property mode set to 100644
File size: 60.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 *
[79ae36dd]42 * You should be able to write very simple multithreaded programs. The async
43 * framework will automatically take care of most of the synchronization
44 * problems.
[80649a91]45 *
[9591265]46 * Example of use (pseudo C):
[c07544d3]47 *
[80649a91]48 * 1) Multithreaded client application
[9591265]49 *
[c07544d3]50 * fibril_create(fibril1, ...);
51 * fibril_create(fibril2, ...);
52 * ...
53 *
54 * int fibril1(void *arg)
55 * {
[79ae36dd]56 * conn = async_connect_me_to(...);
57 *
58 * exch = async_exchange_begin(conn);
59 * c1 = async_send(exch);
60 * async_exchange_end(exch);
61 *
62 * exch = async_exchange_begin(conn);
63 * c2 = async_send(exch);
64 * async_exchange_end(exch);
65 *
[c07544d3]66 * async_wait_for(c1);
67 * async_wait_for(c2);
68 * ...
69 * }
[80649a91]70 *
71 *
72 * 2) Multithreaded server application
73 *
[c07544d3]74 * main()
75 * {
76 * async_manager();
77 * }
78 *
79 * my_client_connection(icallid, *icall)
80 * {
81 * if (want_refuse) {
[64d2b10]82 * async_answer_0(icallid, ELIMIT);
[c07544d3]83 * return;
84 * }
[64d2b10]85 * async_answer_0(icallid, EOK);
[80649a91]86 *
[c07544d3]87 * callid = async_get_call(&call);
[0772aff]88 * somehow_handle_the_call(callid, call);
[64d2b10]89 * async_answer_2(callid, 1, 2, 3);
[53ca318]90 *
[c07544d3]91 * callid = async_get_call(&call);
92 * ...
93 * }
[a2cd194]94 *
[80649a91]95 */
[9591265]96
[64d2b10]97#define LIBC_ASYNC_C_
98#include <ipc/ipc.h>
[80649a91]99#include <async.h>
[64d2b10]100#undef LIBC_ASYNC_C_
101
102#include <futex.h>
[bc1f1c2]103#include <fibril.h>
[d9c8c81]104#include <adt/hash_table.h>
105#include <adt/list.h>
[80649a91]106#include <assert.h>
107#include <errno.h>
[daa90e8]108#include <sys/time.h>
[c0699467]109#include <libarch/barrier.h>
[0cc4313]110#include <bool.h>
[c7bbf029]111#include <malloc.h>
[79ae36dd]112#include <mem.h>
113#include <stdlib.h>
[e2ab36f1]114#include <macros.h>
[e26a4633]115#include "private/async.h"
[80649a91]116
[79ae36dd]117#define CLIENT_HASH_TABLE_BUCKETS 32
118#define CONN_HASH_TABLE_BUCKETS 32
119
120/** Async framework global futex */
[fc42b28]121atomic_t async_futex = FUTEX_INITIALIZER;
[80649a91]122
[8619f25]123/** Number of threads waiting for IPC in the kernel. */
124atomic_t threads_in_ipc_wait = { 0 };
125
[79ae36dd]126/** Naming service session */
127async_sess_t *session_ns;
[01ff41c]128
[79ae36dd]129/** Call data */
[80649a91]130typedef struct {
131 link_t link;
[79ae36dd]132
[80649a91]133 ipc_callid_t callid;
134 ipc_call_t call;
135} msg_t;
136
[79ae36dd]137/* Client connection data */
[c80fdd0]138typedef struct {
139 link_t link;
[79ae36dd]140
[e2ab36f1]141 sysarg_t in_task_id;
[79ae36dd]142 atomic_t refcnt;
[c80fdd0]143 void *data;
144} client_t;
145
[79ae36dd]146/* Server connection data */
[80649a91]147typedef struct {
[49d072e]148 awaiter_t wdata;
[c07544d3]149
[e70bfa5]150 /** Hash table link. */
151 link_t link;
[c07544d3]152
[e2ab36f1]153 /** Incoming client task ID. */
154 task_id_t in_task_id;
[79ae36dd]155
[e70bfa5]156 /** Incoming phone hash. */
[96b02eb9]157 sysarg_t in_phone_hash;
[c07544d3]158
[23882034]159 /** Link to the client tracking structure. */
160 client_t *client;
[47b7006]161
[e70bfa5]162 /** Messages that should be delivered to this fibril. */
[b72efe8]163 list_t msg_queue;
[c07544d3]164
[e70bfa5]165 /** Identification of the opening call. */
[80649a91]166 ipc_callid_t callid;
[e70bfa5]167 /** Call data of the opening call. */
[80649a91]168 ipc_call_t call;
[9934f7d]169 /** Local argument or NULL if none. */
170 void *carg;
[c07544d3]171
[e70bfa5]172 /** Identification of the closing call. */
173 ipc_callid_t close_callid;
[c07544d3]174
[e70bfa5]175 /** Fibril function that will be used to handle the connection. */
[9934f7d]176 async_client_conn_t cfibril;
[80649a91]177} connection_t;
178
[bc1f1c2]179/** Identifier of the incoming connection handled by the current fibril. */
[79ae36dd]180static fibril_local connection_t *fibril_connection;
[e70bfa5]181
[46eec3b]182static void *default_client_data_constructor(void)
183{
184 return NULL;
185}
186
187static void default_client_data_destructor(void *data)
188{
189}
190
191static async_client_data_ctor_t async_client_data_create =
192 default_client_data_constructor;
193static async_client_data_dtor_t async_client_data_destroy =
194 default_client_data_destructor;
195
196void async_set_client_data_constructor(async_client_data_ctor_t ctor)
197{
198 async_client_data_create = ctor;
199}
200
201void async_set_client_data_destructor(async_client_data_dtor_t dtor)
202{
203 async_client_data_destroy = dtor;
204}
205
[47b7006]206/** Default fibril function that gets called to handle new connection.
207 *
208 * This function is defined as a weak symbol - to be redefined in user code.
209 *
[3815efb]210 * @param callid Hash of the incoming call.
211 * @param call Data of the incoming call.
212 * @param arg Local argument
[47b7006]213 *
214 */
[9934f7d]215static void default_client_connection(ipc_callid_t callid, ipc_call_t *call,
216 void *arg)
[47b7006]217{
218 ipc_answer_0(callid, ENOENT);
219}
[36c9234]220
[47b7006]221/** Default fibril function that gets called to handle interrupt notifications.
222 *
223 * This function is defined as a weak symbol - to be redefined in user code.
224 *
[3815efb]225 * @param callid Hash of the incoming call.
226 * @param call Data of the incoming call.
227 * @param arg Local argument.
[47b7006]228 *
229 */
230static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
231{
232}
233
[79ae36dd]234static async_client_conn_t client_connection = default_client_connection;
[9934f7d]235static async_interrupt_handler_t interrupt_received = default_interrupt_received;
[da0c91e7]236
[79ae36dd]237/** Setter for client_connection function pointer.
238 *
239 * @param conn Function that will implement a new connection fibril.
240 *
241 */
242void async_set_client_connection(async_client_conn_t conn)
243{
244 client_connection = conn;
245}
246
247/** Setter for interrupt_received function pointer.
248 *
249 * @param intr Function that will implement a new interrupt
250 * notification fibril.
251 */
[9934f7d]252void async_set_interrupt_received(async_interrupt_handler_t intr)
[79ae36dd]253{
254 interrupt_received = intr;
255}
256
257/** Mutex protecting inactive_exch_list and avail_phone_cv.
258 *
259 */
260static FIBRIL_MUTEX_INITIALIZE(async_sess_mutex);
261
262/** List of all currently inactive exchanges.
263 *
264 */
265static LIST_INITIALIZE(inactive_exch_list);
266
267/** Condition variable to wait for a phone to become available.
268 *
269 */
270static FIBRIL_CONDVAR_INITIALIZE(avail_phone_cv);
271
[c80fdd0]272static hash_table_t client_hash_table;
[c07544d3]273static hash_table_t conn_hash_table;
274static LIST_INITIALIZE(timeout_list);
275
[47b7006]276static hash_index_t client_hash(unsigned long key[])
[c80fdd0]277{
278 assert(key);
[79ae36dd]279
[47b7006]280 return (((key[0]) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
[c80fdd0]281}
282
283static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
284{
[79ae36dd]285 assert(key);
[e2ab36f1]286 assert(keys == 2);
[79ae36dd]287 assert(item);
288
[47b7006]289 client_t *client = hash_table_get_instance(item, client_t, link);
[e2ab36f1]290 return (key[0] == LOWER32(client->in_task_id) &&
291 (key[1] == UPPER32(client->in_task_id)));
[c80fdd0]292}
293
294static void client_remove(link_t *item)
295{
296}
297
298/** Operations for the client hash table. */
299static hash_table_operations_t client_hash_table_ops = {
300 .hash = client_hash,
301 .compare = client_compare,
302 .remove_callback = client_remove
303};
[80649a91]304
[e70bfa5]305/** Compute hash into the connection hash table based on the source phone hash.
306 *
[c07544d3]307 * @param key Pointer to source phone hash.
308 *
309 * @return Index into the connection hash table.
[e70bfa5]310 *
311 */
[47b7006]312static hash_index_t conn_hash(unsigned long key[])
[450cd3a]313{
[80649a91]314 assert(key);
[79ae36dd]315
[47b7006]316 return (((key[0]) >> 4) % CONN_HASH_TABLE_BUCKETS);
[450cd3a]317}
[06502f7d]318
[e70bfa5]319/** Compare hash table item with a key.
320 *
[c07544d3]321 * @param key Array containing the source phone hash as the only item.
322 * @param keys Expected 1 but ignored.
323 * @param item Connection hash table item.
324 *
325 * @return True on match, false otherwise.
[e70bfa5]326 *
327 */
[80649a91]328static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
[450cd3a]329{
[79ae36dd]330 assert(key);
331 assert(item);
332
[47b7006]333 connection_t *conn = hash_table_get_instance(item, connection_t, link);
334 return (key[0] == conn->in_phone_hash);
[450cd3a]335}
[06502f7d]336
[80649a91]337static void conn_remove(link_t *item)
[450cd3a]338{
339}
340
[e70bfa5]341/** Operations for the connection hash table. */
[80649a91]342static hash_table_operations_t conn_hash_table_ops = {
343 .hash = conn_hash,
344 .compare = conn_compare,
345 .remove_callback = conn_remove
346};
347
[e70bfa5]348/** Sort in current fibril's timeout request.
[49d072e]349 *
[c07544d3]350 * @param wd Wait data of the current fibril.
351 *
[49d072e]352 */
[b6ee5b1]353void async_insert_timeout(awaiter_t *wd)
[49d072e]354{
[79ae36dd]355 assert(wd);
356
[f53cc81]357 wd->to_event.occurred = false;
358 wd->to_event.inlist = true;
[c07544d3]359
[b72efe8]360 link_t *tmp = timeout_list.head.next;
361 while (tmp != &timeout_list.head) {
[47b7006]362 awaiter_t *cur
363 = list_get_instance(tmp, awaiter_t, to_event.link);
[c07544d3]364
[f53cc81]365 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
[49d072e]366 break;
[47b7006]367
[49d072e]368 tmp = tmp->next;
369 }
[c07544d3]370
[b72efe8]371 list_insert_before(&wd->to_event.link, tmp);
[49d072e]372}
373
[e70bfa5]374/** Try to route a call to an appropriate connection fibril.
[80649a91]375 *
[36c9234]376 * If the proper connection fibril is found, a message with the call is added to
377 * its message queue. If the fibril was not active, it is activated and all
378 * timeouts are unregistered.
379 *
[c07544d3]380 * @param callid Hash of the incoming call.
381 * @param call Data of the incoming call.
382 *
383 * @return False if the call doesn't match any connection.
[47b7006]384 * @return True if the call was passed to the respective connection fibril.
[36c9234]385 *
[80649a91]386 */
[c07544d3]387static bool route_call(ipc_callid_t callid, ipc_call_t *call)
[450cd3a]388{
[79ae36dd]389 assert(call);
390
[01ff41c]391 futex_down(&async_futex);
[c07544d3]392
393 unsigned long key = call->in_phone_hash;
394 link_t *hlp = hash_table_find(&conn_hash_table, &key);
395
[80649a91]396 if (!hlp) {
[01ff41c]397 futex_up(&async_futex);
[c07544d3]398 return false;
[450cd3a]399 }
[c07544d3]400
401 connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
402
403 msg_t *msg = malloc(sizeof(*msg));
404 if (!msg) {
405 futex_up(&async_futex);
406 return false;
407 }
408
[80649a91]409 msg->callid = callid;
410 msg->call = *call;
411 list_append(&msg->link, &conn->msg_queue);
[c07544d3]412
[228e490]413 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
[41269bd]414 conn->close_callid = callid;
[80649a91]415
[36c9234]416 /* If the connection fibril is waiting for an event, activate it */
[49d072e]417 if (!conn->wdata.active) {
[c07544d3]418
[49d072e]419 /* If in timeout list, remove it */
[f53cc81]420 if (conn->wdata.to_event.inlist) {
421 conn->wdata.to_event.inlist = false;
422 list_remove(&conn->wdata.to_event.link);
[49d072e]423 }
[c07544d3]424
425 conn->wdata.active = true;
[bc1f1c2]426 fibril_add_ready(conn->wdata.fid);
[80649a91]427 }
[c07544d3]428
[01ff41c]429 futex_up(&async_futex);
[c07544d3]430 return true;
431}
[80649a91]432
[c07544d3]433/** Notification fibril.
434 *
435 * When a notification arrives, a fibril with this implementing function is
436 * created. It calls interrupt_received() and does the final cleanup.
437 *
438 * @param arg Message structure pointer.
439 *
440 * @return Always zero.
441 *
442 */
443static int notification_fibril(void *arg)
444{
[79ae36dd]445 assert(arg);
446
[c07544d3]447 msg_t *msg = (msg_t *) arg;
448 interrupt_received(msg->callid, &msg->call);
449
450 free(msg);
451 return 0;
452}
453
454/** Process interrupt notification.
455 *
456 * A new fibril is created which would process the notification.
457 *
458 * @param callid Hash of the incoming call.
459 * @param call Data of the incoming call.
460 *
461 * @return False if an error occured.
462 * True if the call was passed to the notification fibril.
463 *
464 */
465static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
466{
[79ae36dd]467 assert(call);
468
[c07544d3]469 futex_down(&async_futex);
470
471 msg_t *msg = malloc(sizeof(*msg));
472 if (!msg) {
473 futex_up(&async_futex);
474 return false;
475 }
476
477 msg->callid = callid;
478 msg->call = *call;
479
480 fid_t fid = fibril_create(notification_fibril, msg);
[86d7bfa]481 if (fid == 0) {
482 free(msg);
483 futex_up(&async_futex);
484 return false;
485 }
486
[c07544d3]487 fibril_add_ready(fid);
488
489 futex_up(&async_futex);
490 return true;
[80649a91]491}
492
[e70bfa5]493/** Return new incoming message for the current (fibril-local) connection.
494 *
[c07544d3]495 * @param call Storage where the incoming call data will be stored.
496 * @param usecs Timeout in microseconds. Zero denotes no timeout.
497 *
498 * @return If no timeout was specified, then a hash of the
499 * incoming call is returned. If a timeout is specified,
500 * then a hash of the incoming call is returned unless
501 * the timeout expires prior to receiving a message. In
502 * that case zero is returned.
[e70bfa5]503 *
504 */
[49d072e]505ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
[80649a91]506{
[79ae36dd]507 assert(call);
508 assert(fibril_connection);
[c07544d3]509
510 /* Why doing this?
[79ae36dd]511 * GCC 4.1.0 coughs on fibril_connection-> dereference.
[6c46350]512 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
[c07544d3]513 * I would never expect to find so many errors in
514 * a compiler.
[6c46350]515 */
[79ae36dd]516 connection_t *conn = fibril_connection;
[c07544d3]517
[01ff41c]518 futex_down(&async_futex);
[c07544d3]519
[49d072e]520 if (usecs) {
[f53cc81]521 gettimeofday(&conn->wdata.to_event.expires, NULL);
522 tv_add(&conn->wdata.to_event.expires, usecs);
[c07544d3]523 } else
[f53cc81]524 conn->wdata.to_event.inlist = false;
[c07544d3]525
[e70bfa5]526 /* If nothing in queue, wait until something arrives */
[6c46350]527 while (list_empty(&conn->msg_queue)) {
[8c8f8d6]528 if (conn->close_callid) {
529 /*
530 * Handle the case when the connection was already
531 * closed by the client but the server did not notice
532 * the first IPC_M_PHONE_HUNGUP call and continues to
533 * call async_get_call_timeout(). Repeat
[47b7006]534 * IPC_M_PHONE_HUNGUP until the caller notices.
[8c8f8d6]535 */
536 memset(call, 0, sizeof(ipc_call_t));
[228e490]537 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
[8c8f8d6]538 futex_up(&async_futex);
539 return conn->close_callid;
540 }
[47b7006]541
[085bd54]542 if (usecs)
[b6ee5b1]543 async_insert_timeout(&conn->wdata);
[c07544d3]544
545 conn->wdata.active = false;
546
[c7509e5]547 /*
548 * Note: the current fibril will be rescheduled either due to a
549 * timeout or due to an arriving message destined to it. In the
550 * former case, handle_expired_timeouts() and, in the latter
551 * case, route_call() will perform the wakeup.
552 */
[116d3f6f]553 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]554
[e70bfa5]555 /*
[c07544d3]556 * Futex is up after getting back from async_manager.
557 * Get it again.
[c7509e5]558 */
[49d072e]559 futex_down(&async_futex);
[f53cc81]560 if ((usecs) && (conn->wdata.to_event.occurred)
[c07544d3]561 && (list_empty(&conn->msg_queue))) {
[e70bfa5]562 /* If we timed out -> exit */
[49d072e]563 futex_up(&async_futex);
564 return 0;
565 }
[450cd3a]566 }
567
[b72efe8]568 msg_t *msg = list_get_instance(list_first(&conn->msg_queue), msg_t, link);
[80649a91]569 list_remove(&msg->link);
[c07544d3]570
571 ipc_callid_t callid = msg->callid;
[80649a91]572 *call = msg->call;
573 free(msg);
574
[01ff41c]575 futex_up(&async_futex);
[80649a91]576 return callid;
577}
578
[e2ab36f1]579static client_t *async_client_get(task_id_t client_id, bool create)
[26fbb7bb]580{
[e2ab36f1]581 unsigned long key[2] = {
582 LOWER32(client_id),
583 UPPER32(client_id),
584 };
[26fbb7bb]585 client_t *client = NULL;
586
587 futex_down(&async_futex);
[e2ab36f1]588 link_t *lnk = hash_table_find(&client_hash_table, key);
[26fbb7bb]589 if (lnk) {
590 client = hash_table_get_instance(lnk, client_t, link);
591 atomic_inc(&client->refcnt);
592 } else if (create) {
593 client = malloc(sizeof(client_t));
594 if (client) {
[e2ab36f1]595 client->in_task_id = client_id;
[26fbb7bb]596 client->data = async_client_data_create();
597
598 atomic_set(&client->refcnt, 1);
[e2ab36f1]599 hash_table_insert(&client_hash_table, key, &client->link);
[26fbb7bb]600 }
601 }
602
603 futex_up(&async_futex);
604 return client;
605}
606
607static void async_client_put(client_t *client)
608{
609 bool destroy;
[e2ab36f1]610 unsigned long key[2] = {
611 LOWER32(client->in_task_id),
612 UPPER32(client->in_task_id)
613 };
[26fbb7bb]614
615 futex_down(&async_futex);
616
617 if (atomic_predec(&client->refcnt) == 0) {
[e2ab36f1]618 hash_table_remove(&client_hash_table, key, 2);
[26fbb7bb]619 destroy = true;
620 } else
621 destroy = false;
622
623 futex_up(&async_futex);
624
625 if (destroy) {
626 if (client->data)
627 async_client_data_destroy(client->data);
628
629 free(client);
630 }
631}
632
[455f190]633void *async_get_client_data(void)
634{
635 assert(fibril_connection);
636 return fibril_connection->client->data;
637}
638
[e2ab36f1]639void *async_get_client_data_by_id(task_id_t client_id)
[455f190]640{
[e2ab36f1]641 client_t *client = async_client_get(client_id, false);
[455f190]642 if (!client)
643 return NULL;
644 if (!client->data) {
645 async_client_put(client);
646 return NULL;
647 }
648
649 return client->data;
650}
651
[e2ab36f1]652void async_put_client_data_by_id(task_id_t client_id)
[455f190]653{
[e2ab36f1]654 client_t *client = async_client_get(client_id, false);
[455f190]655
656 assert(client);
657 assert(client->data);
658
[cdc8ee2d]659 /* Drop the reference we got in async_get_client_data_by_hash(). */
660 async_client_put(client);
661
662 /* Drop our own reference we got at the beginning of this function. */
[455f190]663 async_client_put(client);
664}
665
[f2f0392]666/** Wrapper for client connection fibril.
667 *
[36c9234]668 * When a new connection arrives, a fibril with this implementing function is
[f2f0392]669 * created. It calls client_connection() and does the final cleanup.
[a2cd194]670 *
[c07544d3]671 * @param arg Connection structure pointer.
672 *
673 * @return Always zero.
[a2cd194]674 *
675 */
[c07544d3]676static int connection_fibril(void *arg)
[80649a91]677{
[79ae36dd]678 assert(arg);
679
[c07544d3]680 /*
[c80fdd0]681 * Setup fibril-local connection pointer.
[c07544d3]682 */
[79ae36dd]683 fibril_connection = (connection_t *) arg;
[47b7006]684
[c80fdd0]685 /*
686 * Add our reference for the current connection in the client task
687 * tracking structure. If this is the first reference, create and
688 * hash in a new tracking structure.
689 */
[26fbb7bb]690
[e2ab36f1]691 client_t *client = async_client_get(fibril_connection->in_task_id, true);
[26fbb7bb]692 if (!client) {
693 ipc_answer_0(fibril_connection->callid, ENOMEM);
694 return 0;
[c80fdd0]695 }
[26fbb7bb]696
[79ae36dd]697 fibril_connection->client = client;
[47b7006]698
[c80fdd0]699 /*
700 * Call the connection handler function.
701 */
[79ae36dd]702 fibril_connection->cfibril(fibril_connection->callid,
[9934f7d]703 &fibril_connection->call, fibril_connection->carg);
[a46da63]704
[c80fdd0]705 /*
706 * Remove the reference for this client task connection.
707 */
[26fbb7bb]708 async_client_put(client);
[47b7006]709
[c80fdd0]710 /*
711 * Remove myself from the connection hash table.
712 */
713 futex_down(&async_futex);
[26fbb7bb]714 unsigned long key = fibril_connection->in_phone_hash;
[a2cd194]715 hash_table_remove(&conn_hash_table, &key, 1);
[01ff41c]716 futex_up(&async_futex);
[a46da63]717
[c80fdd0]718 /*
719 * Answer all remaining messages with EHANGUP.
720 */
[79ae36dd]721 while (!list_empty(&fibril_connection->msg_queue)) {
[47b7006]722 msg_t *msg =
[b72efe8]723 list_get_instance(list_first(&fibril_connection->msg_queue),
724 msg_t, link);
[c07544d3]725
[a2cd194]726 list_remove(&msg->link);
[b74959bd]727 ipc_answer_0(msg->callid, EHANGUP);
[a2cd194]728 free(msg);
729 }
[c07544d3]730
[c80fdd0]731 /*
732 * If the connection was hung-up, answer the last call,
733 * i.e. IPC_M_PHONE_HUNGUP.
734 */
[79ae36dd]735 if (fibril_connection->close_callid)
736 ipc_answer_0(fibril_connection->close_callid, EOK);
[a46da63]737
[79ae36dd]738 free(fibril_connection);
[a46da63]739 return 0;
[80649a91]740}
741
[f2f0392]742/** Create a new fibril for a new connection.
[80649a91]743 *
[79ae36dd]744 * Create new fibril for connection, fill in connection structures and insert
[f2f0392]745 * it into the hash table, so that later we can easily do routing of messages to
746 * particular fibrils.
[53ca318]747 *
[e2ab36f1]748 * @param in_task_id Identification of the incoming connection.
[c07544d3]749 * @param in_phone_hash Identification of the incoming connection.
750 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
751 * If callid is zero, the connection was opened by
752 * accepting the IPC_M_CONNECT_TO_ME call and this function
753 * is called directly by the server.
754 * @param call Call data of the opening call.
755 * @param cfibril Fibril function that should be called upon opening the
756 * connection.
[3815efb]757 * @param carg Extra argument to pass to the connection fibril
[c07544d3]758 *
759 * @return New fibril id or NULL on failure.
[36c9234]760 *
[80649a91]761 */
[e2ab36f1]762fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
[3c22f70]763 ipc_callid_t callid, ipc_call_t *call,
[9934f7d]764 async_client_conn_t cfibril, void *carg)
[80649a91]765{
[c07544d3]766 connection_t *conn = malloc(sizeof(*conn));
[80649a91]767 if (!conn) {
[6675c70]768 if (callid)
[b74959bd]769 ipc_answer_0(callid, ENOMEM);
[47b7006]770
[0b4a67a]771 return (uintptr_t) NULL;
[80649a91]772 }
[c07544d3]773
[e2ab36f1]774 conn->in_task_id = in_task_id;
[44c6d88d]775 conn->in_phone_hash = in_phone_hash;
[80649a91]776 list_initialize(&conn->msg_queue);
777 conn->callid = callid;
[c4702804]778 conn->close_callid = 0;
[9934f7d]779 conn->carg = carg;
[c07544d3]780
[eaf34f7]781 if (call)
782 conn->call = *call;
[6b21292]783
[c07544d3]784 /* We will activate the fibril ASAP */
785 conn->wdata.active = true;
786 conn->cfibril = cfibril;
[bc1f1c2]787 conn->wdata.fid = fibril_create(connection_fibril, conn);
[c07544d3]788
[86d7bfa]789 if (conn->wdata.fid == 0) {
[80649a91]790 free(conn);
[86d7bfa]791
[6675c70]792 if (callid)
[b74959bd]793 ipc_answer_0(callid, ENOMEM);
[86d7bfa]794
[0b4a67a]795 return (uintptr_t) NULL;
[80649a91]796 }
[6b21292]797
[36c9234]798 /* Add connection to the connection hash table */
[9db9b10]799 unsigned long key = conn->in_phone_hash;
[c07544d3]800
[01ff41c]801 futex_down(&async_futex);
[80649a91]802 hash_table_insert(&conn_hash_table, &key, &conn->link);
[01ff41c]803 futex_up(&async_futex);
[6b21292]804
[bc1f1c2]805 fibril_add_ready(conn->wdata.fid);
[6b21292]806
[bc1f1c2]807 return conn->wdata.fid;
[80649a91]808}
809
[36c9234]810/** Handle a call that was received.
811 *
812 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
813 * Otherwise the call is routed to its connection fibril.
814 *
[c07544d3]815 * @param callid Hash of the incoming call.
816 * @param call Data of the incoming call.
[6b21292]817 *
[36c9234]818 */
[80649a91]819static void handle_call(ipc_callid_t callid, ipc_call_t *call)
820{
[79ae36dd]821 assert(call);
822
[47b7006]823 /* Unrouted call - take some default action */
[15039b67]824 if ((callid & IPC_CALLID_NOTIFICATION)) {
[c07544d3]825 process_notification(callid, call);
[47b7006]826 return;
[6b21292]827 }
828
[228e490]829 switch (IPC_GET_IMETHOD(*call)) {
[2c0e5d2]830 case IPC_M_CONNECT_ME:
[80649a91]831 case IPC_M_CONNECT_ME_TO:
[47b7006]832 /* Open new connection with fibril, etc. */
[e2ab36f1]833 async_new_connection(call->in_task_id, IPC_GET_ARG5(*call),
[9934f7d]834 callid, call, client_connection, NULL);
[47b7006]835 return;
[80649a91]836 }
[6b21292]837
[36c9234]838 /* Try to route the call through the connection hash table */
[44c6d88d]839 if (route_call(callid, call))
[47b7006]840 return;
[6b21292]841
[44c6d88d]842 /* Unknown call from unknown phone - hang it up */
[b74959bd]843 ipc_answer_0(callid, EHANGUP);
[450cd3a]844}
845
[f2f0392]846/** Fire all timeouts that expired. */
[c042bdd]847static void handle_expired_timeouts(void)
848{
849 struct timeval tv;
[36c9234]850 gettimeofday(&tv, NULL);
[c07544d3]851
[c042bdd]852 futex_down(&async_futex);
[c07544d3]853
[b72efe8]854 link_t *cur = list_first(&timeout_list);
855 while (cur != NULL) {
[47b7006]856 awaiter_t *waiter =
857 list_get_instance(cur, awaiter_t, to_event.link);
[c07544d3]858
[f53cc81]859 if (tv_gt(&waiter->to_event.expires, &tv))
[c042bdd]860 break;
[47b7006]861
[f53cc81]862 list_remove(&waiter->to_event.link);
863 waiter->to_event.inlist = false;
864 waiter->to_event.occurred = true;
[c07544d3]865
[36c9234]866 /*
[c07544d3]867 * Redundant condition?
868 * The fibril should not be active when it gets here.
[c042bdd]869 */
[49d072e]870 if (!waiter->active) {
[c07544d3]871 waiter->active = true;
[bc1f1c2]872 fibril_add_ready(waiter->fid);
[c042bdd]873 }
[b72efe8]874
875 cur = list_first(&timeout_list);
[c042bdd]876 }
[c07544d3]877
[c042bdd]878 futex_up(&async_futex);
879}
880
[36c9234]881/** Endless loop dispatching incoming calls and answers.
882 *
[c07544d3]883 * @return Never returns.
884 *
[36c9234]885 */
[085bd54]886static int async_manager_worker(void)
[80649a91]887{
[c07544d3]888 while (true) {
[116d3f6f]889 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
[47b7006]890 futex_up(&async_futex);
[36c9234]891 /*
892 * async_futex is always held when entering a manager
893 * fibril.
[a46da63]894 */
[80649a91]895 continue;
896 }
[c07544d3]897
[c042bdd]898 futex_down(&async_futex);
[c07544d3]899
900 suseconds_t timeout;
[c042bdd]901 if (!list_empty(&timeout_list)) {
[b72efe8]902 awaiter_t *waiter = list_get_instance(
903 list_first(&timeout_list), awaiter_t, to_event.link);
[c07544d3]904
905 struct timeval tv;
[bc1f1c2]906 gettimeofday(&tv, NULL);
[c07544d3]907
[f53cc81]908 if (tv_gteq(&tv, &waiter->to_event.expires)) {
[6c46350]909 futex_up(&async_futex);
[c042bdd]910 handle_expired_timeouts();
911 continue;
912 } else
[47b7006]913 timeout = tv_sub(&waiter->to_event.expires, &tv);
[c042bdd]914 } else
[0b99e40]915 timeout = SYNCH_NO_TIMEOUT;
[c07544d3]916
[c042bdd]917 futex_up(&async_futex);
[47b7006]918
[8619f25]919 atomic_inc(&threads_in_ipc_wait);
[c07544d3]920
921 ipc_call_t call;
[cc27c8c5]922 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
923 SYNCH_FLAGS_NONE);
[c07544d3]924
[8619f25]925 atomic_dec(&threads_in_ipc_wait);
[47b7006]926
[0b99e40]927 if (!callid) {
[c042bdd]928 handle_expired_timeouts();
[0b99e40]929 continue;
930 }
[c07544d3]931
932 if (callid & IPC_CALLID_ANSWERED)
[80649a91]933 continue;
[c07544d3]934
[80649a91]935 handle_call(callid, &call);
936 }
[a46da63]937
938 return 0;
[80649a91]939}
940
[36c9234]941/** Function to start async_manager as a standalone fibril.
[c07544d3]942 *
[36c9234]943 * When more kernel threads are used, one async manager should exist per thread.
944 *
[c07544d3]945 * @param arg Unused.
946 * @return Never returns.
[36c9234]947 *
[a2cd194]948 */
[9591265]949static int async_manager_fibril(void *arg)
[80649a91]950{
[a46da63]951 futex_up(&async_futex);
[c07544d3]952
[36c9234]953 /*
954 * async_futex is always locked when entering manager
955 */
[085bd54]956 async_manager_worker();
[a46da63]957
958 return 0;
[80649a91]959}
[450cd3a]960
[36c9234]961/** Add one manager to manager list. */
[80649a91]962void async_create_manager(void)
[450cd3a]963{
[c07544d3]964 fid_t fid = fibril_create(async_manager_fibril, NULL);
[86d7bfa]965 if (fid != 0)
966 fibril_add_manager(fid);
[80649a91]967}
968
969/** Remove one manager from manager list */
970void async_destroy_manager(void)
971{
[bc1f1c2]972 fibril_remove_manager();
[80649a91]973}
974
[36c9234]975/** Initialize the async framework.
976 *
977 */
[47b7006]978void __async_init(void)
[80649a91]979{
[79ae36dd]980 if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS,
[e2ab36f1]981 2, &client_hash_table_ops))
[47b7006]982 abort();
[80649a91]983
[79ae36dd]984 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_BUCKETS,
985 1, &conn_hash_table_ops))
[47b7006]986 abort();
[79ae36dd]987
988 session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
989 if (session_ns == NULL)
990 abort();
991
992 session_ns->mgmt = EXCHANGE_ATOMIC;
993 session_ns->phone = PHONE_NS;
994 session_ns->arg1 = 0;
995 session_ns->arg2 = 0;
996 session_ns->arg3 = 0;
997
998 list_initialize(&session_ns->exch_list);
999 fibril_mutex_initialize(&session_ns->mutex);
1000 atomic_set(&session_ns->refcnt, 0);
[450cd3a]1001}
[01ff41c]1002
[36c9234]1003/** Reply received callback.
[01ff41c]1004 *
[36c9234]1005 * This function is called whenever a reply for an asynchronous message sent out
1006 * by the asynchronous framework is received.
1007 *
1008 * Notify the fibril which is waiting for this message that it has arrived.
1009 *
[c07544d3]1010 * @param arg Pointer to the asynchronous message record.
1011 * @param retval Value returned in the answer.
1012 * @param data Call data of the answer.
[47b7006]1013 *
[01ff41c]1014 */
[79ae36dd]1015void reply_received(void *arg, int retval, ipc_call_t *data)
[01ff41c]1016{
[79ae36dd]1017 assert(arg);
1018
[9db9b10]1019 futex_down(&async_futex);
1020
[c07544d3]1021 amsg_t *msg = (amsg_t *) arg;
[01ff41c]1022 msg->retval = retval;
[c07544d3]1023
[36c9234]1024 /* Copy data after futex_down, just in case the call was detached */
[9db9b10]1025 if ((msg->dataptr) && (data))
[c07544d3]1026 *msg->dataptr = *data;
1027
[c042bdd]1028 write_barrier();
[c07544d3]1029
[c042bdd]1030 /* Remove message from timeout list */
[f53cc81]1031 if (msg->wdata.to_event.inlist)
1032 list_remove(&msg->wdata.to_event.link);
[c07544d3]1033
1034 msg->done = true;
[36c9234]1035 if (!msg->wdata.active) {
[c07544d3]1036 msg->wdata.active = true;
[bc1f1c2]1037 fibril_add_ready(msg->wdata.fid);
[01ff41c]1038 }
[c07544d3]1039
[01ff41c]1040 futex_up(&async_futex);
1041}
1042
[36c9234]1043/** Send message and return id of the sent message.
1044 *
1045 * The return value can be used as input for async_wait() to wait for
1046 * completion.
[01ff41c]1047 *
[79ae36dd]1048 * @param exch Exchange for sending the message.
1049 * @param imethod Service-defined interface and method.
[c07544d3]1050 * @param arg1 Service-defined payload argument.
1051 * @param arg2 Service-defined payload argument.
1052 * @param arg3 Service-defined payload argument.
1053 * @param arg4 Service-defined payload argument.
1054 * @param dataptr If non-NULL, storage where the reply data will be
1055 * stored.
1056 *
1057 * @return Hash of the sent message or 0 on error.
[36c9234]1058 *
[01ff41c]1059 */
[79ae36dd]1060aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1061 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[01ff41c]1062{
[79ae36dd]1063 if (exch == NULL)
1064 return 0;
[c07544d3]1065
[79ae36dd]1066 amsg_t *msg = malloc(sizeof(amsg_t));
1067 if (msg == NULL)
[c07544d3]1068 return 0;
[6b21292]1069
[c07544d3]1070 msg->done = false;
[01ff41c]1071 msg->dataptr = dataptr;
[6b21292]1072
[f53cc81]1073 msg->wdata.to_event.inlist = false;
[47b7006]1074
1075 /*
1076 * We may sleep in the next method,
1077 * but it will use its own means
1078 */
[c07544d3]1079 msg->wdata.active = true;
1080
[79ae36dd]1081 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
[c07544d3]1082 reply_received, true);
[6b21292]1083
[01ff41c]1084 return (aid_t) msg;
1085}
1086
[90f5d64]1087/** Send message and return id of the sent message
1088 *
[36c9234]1089 * The return value can be used as input for async_wait() to wait for
1090 * completion.
1091 *
[79ae36dd]1092 * @param exch Exchange for sending the message.
1093 * @param imethod Service-defined interface and method.
[c07544d3]1094 * @param arg1 Service-defined payload argument.
1095 * @param arg2 Service-defined payload argument.
1096 * @param arg3 Service-defined payload argument.
1097 * @param arg4 Service-defined payload argument.
1098 * @param arg5 Service-defined payload argument.
1099 * @param dataptr If non-NULL, storage where the reply data will be
1100 * stored.
1101 *
1102 * @return Hash of the sent message or 0 on error.
[36c9234]1103 *
[90f5d64]1104 */
[79ae36dd]1105aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1106 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[0cc4313]1107 ipc_call_t *dataptr)
[90f5d64]1108{
[79ae36dd]1109 if (exch == NULL)
1110 return 0;
1111
[47b7006]1112 amsg_t *msg = malloc(sizeof(amsg_t));
[6b21292]1113
[79ae36dd]1114 if (msg == NULL)
[c07544d3]1115 return 0;
1116
1117 msg->done = false;
[90f5d64]1118 msg->dataptr = dataptr;
[6b21292]1119
[f53cc81]1120 msg->wdata.to_event.inlist = false;
[47b7006]1121
1122 /*
1123 * We may sleep in the next method,
1124 * but it will use its own means
1125 */
[c07544d3]1126 msg->wdata.active = true;
[6b21292]1127
[79ae36dd]1128 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
1129 msg, reply_received, true);
[6b21292]1130
[90f5d64]1131 return (aid_t) msg;
1132}
1133
[36c9234]1134/** Wait for a message sent by the async framework.
[01ff41c]1135 *
[c07544d3]1136 * @param amsgid Hash of the message to wait for.
1137 * @param retval Pointer to storage where the retval of the answer will
1138 * be stored.
1139 *
[01ff41c]1140 */
[96b02eb9]1141void async_wait_for(aid_t amsgid, sysarg_t *retval)
[01ff41c]1142{
[79ae36dd]1143 assert(amsgid);
1144
[01ff41c]1145 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]1146
[01ff41c]1147 futex_down(&async_futex);
1148 if (msg->done) {
1149 futex_up(&async_futex);
1150 goto done;
1151 }
[c07544d3]1152
[bc1f1c2]1153 msg->wdata.fid = fibril_get_id();
[c07544d3]1154 msg->wdata.active = false;
[f53cc81]1155 msg->wdata.to_event.inlist = false;
[c07544d3]1156
[36c9234]1157 /* Leave the async_futex locked when entering this function */
[116d3f6f]1158 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1159
1160 /* Futex is up automatically after fibril_switch */
1161
[01ff41c]1162done:
1163 if (retval)
1164 *retval = msg->retval;
[c07544d3]1165
[01ff41c]1166 free(msg);
1167}
[0b99e40]1168
[36c9234]1169/** Wait for a message sent by the async framework, timeout variant.
[c042bdd]1170 *
[c07544d3]1171 * @param amsgid Hash of the message to wait for.
1172 * @param retval Pointer to storage where the retval of the answer will
1173 * be stored.
1174 * @param timeout Timeout in microseconds.
1175 *
1176 * @return Zero on success, ETIMEOUT if the timeout has expired.
[c042bdd]1177 *
1178 */
[96b02eb9]1179int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
[c042bdd]1180{
[79ae36dd]1181 assert(amsgid);
1182
[c042bdd]1183 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]1184
[86029498]1185 /* TODO: Let it go through the event read at least once */
1186 if (timeout < 0)
1187 return ETIMEOUT;
[c07544d3]1188
[c042bdd]1189 futex_down(&async_futex);
1190 if (msg->done) {
1191 futex_up(&async_futex);
1192 goto done;
1193 }
[c07544d3]1194
[f53cc81]1195 gettimeofday(&msg->wdata.to_event.expires, NULL);
1196 tv_add(&msg->wdata.to_event.expires, timeout);
[c07544d3]1197
[bc1f1c2]1198 msg->wdata.fid = fibril_get_id();
[c07544d3]1199 msg->wdata.active = false;
[b6ee5b1]1200 async_insert_timeout(&msg->wdata);
[c07544d3]1201
[36c9234]1202 /* Leave the async_futex locked when entering this function */
[116d3f6f]1203 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1204
1205 /* Futex is up automatically after fibril_switch */
1206
[c042bdd]1207 if (!msg->done)
1208 return ETIMEOUT;
[c07544d3]1209
[c042bdd]1210done:
1211 if (retval)
1212 *retval = msg->retval;
[c07544d3]1213
[c042bdd]1214 free(msg);
[c07544d3]1215
[c042bdd]1216 return 0;
1217}
[0b99e40]1218
[36c9234]1219/** Wait for specified time.
[44c6d88d]1220 *
[36c9234]1221 * The current fibril is suspended but the thread continues to execute.
1222 *
[c07544d3]1223 * @param timeout Duration of the wait in microseconds.
1224 *
[44c6d88d]1225 */
1226void async_usleep(suseconds_t timeout)
1227{
[47b7006]1228 amsg_t *msg = malloc(sizeof(amsg_t));
[44c6d88d]1229
1230 if (!msg)
1231 return;
[6b21292]1232
[bc1f1c2]1233 msg->wdata.fid = fibril_get_id();
[c07544d3]1234 msg->wdata.active = false;
[6b21292]1235
[f53cc81]1236 gettimeofday(&msg->wdata.to_event.expires, NULL);
1237 tv_add(&msg->wdata.to_event.expires, timeout);
[6b21292]1238
[44c6d88d]1239 futex_down(&async_futex);
[c07544d3]1240
[b6ee5b1]1241 async_insert_timeout(&msg->wdata);
[c07544d3]1242
[36c9234]1243 /* Leave the async_futex locked when entering this function */
[116d3f6f]1244 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1245
1246 /* Futex is up automatically after fibril_switch() */
1247
[44c6d88d]1248 free(msg);
1249}
[da0c91e7]1250
[0cc4313]1251/** Pseudo-synchronous message sending - fast version.
1252 *
1253 * Send message asynchronously and return only after the reply arrives.
1254 *
1255 * This function can only transfer 4 register payload arguments. For
1256 * transferring more arguments, see the slower async_req_slow().
1257 *
[79ae36dd]1258 * @param exch Exchange for sending the message.
1259 * @param imethod Interface and method of the call.
[c07544d3]1260 * @param arg1 Service-defined payload argument.
1261 * @param arg2 Service-defined payload argument.
1262 * @param arg3 Service-defined payload argument.
1263 * @param arg4 Service-defined payload argument.
1264 * @param r1 If non-NULL, storage for the 1st reply argument.
1265 * @param r2 If non-NULL, storage for the 2nd reply argument.
1266 * @param r3 If non-NULL, storage for the 3rd reply argument.
1267 * @param r4 If non-NULL, storage for the 4th reply argument.
1268 * @param r5 If non-NULL, storage for the 5th reply argument.
1269 *
1270 * @return Return code of the reply or a negative error code.
1271 *
[0cc4313]1272 */
[79ae36dd]1273sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1274 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1275 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1276{
[79ae36dd]1277 if (exch == NULL)
1278 return ENOENT;
1279
[0cc4313]1280 ipc_call_t result;
[79ae36dd]1281 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
[0cc4313]1282 &result);
[c07544d3]1283
[96b02eb9]1284 sysarg_t rc;
[79ae36dd]1285 async_wait_for(aid, &rc);
[c07544d3]1286
1287 if (r1)
[0cc4313]1288 *r1 = IPC_GET_ARG1(result);
[c07544d3]1289
[0cc4313]1290 if (r2)
1291 *r2 = IPC_GET_ARG2(result);
[c07544d3]1292
[0cc4313]1293 if (r3)
1294 *r3 = IPC_GET_ARG3(result);
[c07544d3]1295
[0cc4313]1296 if (r4)
1297 *r4 = IPC_GET_ARG4(result);
[c07544d3]1298
[0cc4313]1299 if (r5)
1300 *r5 = IPC_GET_ARG5(result);
[c07544d3]1301
[0cc4313]1302 return rc;
[085bd54]1303}
1304
[0cc4313]1305/** Pseudo-synchronous message sending - slow version.
1306 *
1307 * Send message asynchronously and return only after the reply arrives.
1308 *
[79ae36dd]1309 * @param exch Exchange for sending the message.
1310 * @param imethod Interface and method of the call.
[c07544d3]1311 * @param arg1 Service-defined payload argument.
1312 * @param arg2 Service-defined payload argument.
1313 * @param arg3 Service-defined payload argument.
1314 * @param arg4 Service-defined payload argument.
1315 * @param arg5 Service-defined payload argument.
1316 * @param r1 If non-NULL, storage for the 1st reply argument.
1317 * @param r2 If non-NULL, storage for the 2nd reply argument.
1318 * @param r3 If non-NULL, storage for the 3rd reply argument.
1319 * @param r4 If non-NULL, storage for the 4th reply argument.
1320 * @param r5 If non-NULL, storage for the 5th reply argument.
1321 *
1322 * @return Return code of the reply or a negative error code.
1323 *
[0cc4313]1324 */
[79ae36dd]1325sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1326 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1327 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1328{
[79ae36dd]1329 if (exch == NULL)
1330 return ENOENT;
1331
[0cc4313]1332 ipc_call_t result;
[79ae36dd]1333 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
[0cc4313]1334 &result);
[c07544d3]1335
[96b02eb9]1336 sysarg_t rc;
[79ae36dd]1337 async_wait_for(aid, &rc);
[c07544d3]1338
1339 if (r1)
[0cc4313]1340 *r1 = IPC_GET_ARG1(result);
[c07544d3]1341
[0cc4313]1342 if (r2)
1343 *r2 = IPC_GET_ARG2(result);
[c07544d3]1344
[0cc4313]1345 if (r3)
1346 *r3 = IPC_GET_ARG3(result);
[c07544d3]1347
[0cc4313]1348 if (r4)
1349 *r4 = IPC_GET_ARG4(result);
[c07544d3]1350
[0cc4313]1351 if (r5)
1352 *r5 = IPC_GET_ARG5(result);
[c07544d3]1353
[0cc4313]1354 return rc;
[085bd54]1355}
[b2951e2]1356
[79ae36dd]1357void async_msg_0(async_exch_t *exch, sysarg_t imethod)
[64d2b10]1358{
[79ae36dd]1359 if (exch != NULL)
1360 ipc_call_async_0(exch->phone, imethod, NULL, NULL, true);
[64d2b10]1361}
1362
[79ae36dd]1363void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
[64d2b10]1364{
[79ae36dd]1365 if (exch != NULL)
1366 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL, true);
[64d2b10]1367}
1368
[79ae36dd]1369void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1370 sysarg_t arg2)
[64d2b10]1371{
[79ae36dd]1372 if (exch != NULL)
1373 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL,
1374 true);
[64d2b10]1375}
1376
[79ae36dd]1377void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1378 sysarg_t arg2, sysarg_t arg3)
[64d2b10]1379{
[79ae36dd]1380 if (exch != NULL)
1381 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
1382 NULL, true);
[64d2b10]1383}
1384
[79ae36dd]1385void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1386 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[64d2b10]1387{
[79ae36dd]1388 if (exch != NULL)
1389 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
1390 NULL, NULL, true);
[64d2b10]1391}
1392
[79ae36dd]1393void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1394 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
[64d2b10]1395{
[79ae36dd]1396 if (exch != NULL)
1397 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
1398 arg5, NULL, NULL, true);
[64d2b10]1399}
1400
1401sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
1402{
1403 return ipc_answer_0(callid, retval);
1404}
1405
1406sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
1407{
1408 return ipc_answer_1(callid, retval, arg1);
1409}
1410
1411sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1412 sysarg_t arg2)
1413{
1414 return ipc_answer_2(callid, retval, arg1, arg2);
1415}
1416
1417sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1418 sysarg_t arg2, sysarg_t arg3)
1419{
1420 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
1421}
1422
1423sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1424 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1425{
1426 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
1427}
1428
1429sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1430 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1431{
1432 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
1433}
1434
[79ae36dd]1435int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
1436 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[64d2b10]1437{
[79ae36dd]1438 if (exch == NULL)
1439 return ENOENT;
1440
1441 return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
[64d2b10]1442}
1443
[79ae36dd]1444int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
1445 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
1446 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
[64d2b10]1447{
[79ae36dd]1448 if (exch == NULL)
1449 return ENOENT;
1450
1451 return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
1452 arg4, arg5, mode);
[64d2b10]1453}
1454
[007e6efa]1455/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1456 *
1457 * Ask through phone for a new connection to some service.
1458 *
[79ae36dd]1459 * @param exch Exchange for sending the message.
[007e6efa]1460 * @param arg1 User defined argument.
1461 * @param arg2 User defined argument.
1462 * @param arg3 User defined argument.
1463 * @param client_receiver Connection handing routine.
1464 *
[79ae36dd]1465 * @return Zero on success or a negative error code.
[007e6efa]1466 *
1467 */
[79ae36dd]1468int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
[9934f7d]1469 sysarg_t arg3, async_client_conn_t client_receiver, void *carg)
[007e6efa]1470{
[79ae36dd]1471 if (exch == NULL)
1472 return ENOENT;
1473
[36b16bc]1474 task_id_t task_id;
1475 sysarg_t task_id_lo;
1476 sysarg_t task_id_hi;
[007e6efa]1477 sysarg_t phone_hash;
[79ae36dd]1478 int rc = async_req_3_5(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
[36b16bc]1479 NULL, NULL, &task_id_lo, &task_id_hi, &phone_hash);
[007e6efa]1480 if (rc != EOK)
1481 return rc;
[36b16bc]1482
1483 task_id = (task_id_t) MERGE_LOUP32(task_id_lo, task_id_hi);
[007e6efa]1484
1485 if (client_receiver != NULL)
[36b16bc]1486 async_new_connection(task_id, phone_hash, 0, NULL,
[9934f7d]1487 client_receiver, carg);
[007e6efa]1488
1489 return EOK;
1490}
1491
[79ae36dd]1492/** Wrapper for making IPC_M_CONNECT_ME calls using the async framework.
[007e6efa]1493 *
[79ae36dd]1494 * Ask through for a cloned connection to some service.
[f74392f]1495 *
[79ae36dd]1496 * @param mgmt Exchange management style.
1497 * @param exch Exchange for sending the message.
[007e6efa]1498 *
[79ae36dd]1499 * @return New session on success or NULL on error.
[f74392f]1500 *
1501 */
[79ae36dd]1502async_sess_t *async_connect_me(exch_mgmt_t mgmt, async_exch_t *exch)
1503{
1504 if (exch == NULL) {
1505 errno = ENOENT;
1506 return NULL;
1507 }
1508
1509 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1510 if (sess == NULL) {
1511 errno = ENOMEM;
1512 return NULL;
1513 }
1514
1515 ipc_call_t result;
1516
1517 amsg_t *msg = malloc(sizeof(amsg_t));
1518 if (msg == NULL) {
1519 free(sess);
1520 errno = ENOMEM;
1521 return NULL;
1522 }
1523
1524 msg->done = false;
1525 msg->dataptr = &result;
1526
1527 msg->wdata.to_event.inlist = false;
1528
1529 /*
1530 * We may sleep in the next method,
1531 * but it will use its own means
1532 */
1533 msg->wdata.active = true;
1534
1535 ipc_call_async_0(exch->phone, IPC_M_CONNECT_ME, msg,
1536 reply_received, true);
1537
1538 sysarg_t rc;
1539 async_wait_for((aid_t) msg, &rc);
1540
1541 if (rc != EOK) {
1542 errno = rc;
1543 free(sess);
1544 return NULL;
1545 }
1546
1547 int phone = (int) IPC_GET_ARG5(result);
1548
1549 if (phone < 0) {
1550 errno = phone;
1551 free(sess);
1552 return NULL;
1553 }
1554
1555 sess->mgmt = mgmt;
1556 sess->phone = phone;
1557 sess->arg1 = 0;
1558 sess->arg2 = 0;
1559 sess->arg3 = 0;
1560
1561 list_initialize(&sess->exch_list);
1562 fibril_mutex_initialize(&sess->mutex);
1563 atomic_set(&sess->refcnt, 0);
1564
1565 return sess;
1566}
1567
1568static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
1569 sysarg_t arg3, sysarg_t arg4)
[f74392f]1570{
[79ae36dd]1571 ipc_call_t result;
1572
1573 amsg_t *msg = malloc(sizeof(amsg_t));
1574 if (msg == NULL)
1575 return ENOENT;
1576
1577 msg->done = false;
1578 msg->dataptr = &result;
1579
1580 msg->wdata.to_event.inlist = false;
1581
1582 /*
1583 * We may sleep in the next method,
1584 * but it will use its own means
1585 */
1586 msg->wdata.active = true;
1587
1588 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
1589 msg, reply_received, true);
1590
1591 sysarg_t rc;
1592 async_wait_for((aid_t) msg, &rc);
[f74392f]1593
[007e6efa]1594 if (rc != EOK)
[f74392f]1595 return rc;
[007e6efa]1596
[79ae36dd]1597 return (int) IPC_GET_ARG5(result);
1598}
1599
1600/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1601 *
1602 * Ask through for a new connection to some service.
1603 *
1604 * @param mgmt Exchange management style.
1605 * @param exch Exchange for sending the message.
1606 * @param arg1 User defined argument.
1607 * @param arg2 User defined argument.
1608 * @param arg3 User defined argument.
1609 *
1610 * @return New session on success or NULL on error.
1611 *
1612 */
1613async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
1614 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1615{
1616 if (exch == NULL) {
1617 errno = ENOENT;
1618 return NULL;
1619 }
1620
1621 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1622 if (sess == NULL) {
1623 errno = ENOMEM;
1624 return NULL;
1625 }
1626
1627 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1628 0);
1629
1630 if (phone < 0) {
1631 errno = phone;
1632 free(sess);
1633 return NULL;
1634 }
1635
1636 sess->mgmt = mgmt;
1637 sess->phone = phone;
1638 sess->arg1 = arg1;
1639 sess->arg2 = arg2;
1640 sess->arg3 = arg3;
1641
1642 list_initialize(&sess->exch_list);
1643 fibril_mutex_initialize(&sess->mutex);
1644 atomic_set(&sess->refcnt, 0);
1645
1646 return sess;
[f74392f]1647}
1648
1649/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
[007e6efa]1650 *
[f74392f]1651 * Ask through phone for a new connection to some service and block until
1652 * success.
1653 *
[79ae36dd]1654 * @param mgmt Exchange management style.
1655 * @param exch Exchange for sending the message.
1656 * @param arg1 User defined argument.
1657 * @param arg2 User defined argument.
1658 * @param arg3 User defined argument.
[007e6efa]1659 *
[79ae36dd]1660 * @return New session on success or NULL on error.
[f74392f]1661 *
1662 */
[79ae36dd]1663async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
1664 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
[f74392f]1665{
[79ae36dd]1666 if (exch == NULL) {
1667 errno = ENOENT;
1668 return NULL;
1669 }
[f74392f]1670
[79ae36dd]1671 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1672 if (sess == NULL) {
1673 errno = ENOMEM;
1674 return NULL;
1675 }
[007e6efa]1676
[79ae36dd]1677 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1678 IPC_FLAG_BLOCKING);
1679
1680 if (phone < 0) {
1681 errno = phone;
1682 free(sess);
1683 return NULL;
1684 }
1685
1686 sess->mgmt = mgmt;
1687 sess->phone = phone;
1688 sess->arg1 = arg1;
1689 sess->arg2 = arg2;
1690 sess->arg3 = arg3;
1691
1692 list_initialize(&sess->exch_list);
1693 fibril_mutex_initialize(&sess->mutex);
1694 atomic_set(&sess->refcnt, 0);
1695
1696 return sess;
[f74392f]1697}
1698
[64d2b10]1699/** Connect to a task specified by id.
1700 *
1701 */
[79ae36dd]1702async_sess_t *async_connect_kbox(task_id_t id)
[64d2b10]1703{
[79ae36dd]1704 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1705 if (sess == NULL) {
1706 errno = ENOMEM;
1707 return NULL;
1708 }
1709
1710 int phone = ipc_connect_kbox(id);
1711 if (phone < 0) {
1712 errno = phone;
1713 free(sess);
1714 return NULL;
1715 }
1716
1717 sess->mgmt = EXCHANGE_ATOMIC;
1718 sess->phone = phone;
1719 sess->arg1 = 0;
1720 sess->arg2 = 0;
1721 sess->arg3 = 0;
1722
1723 list_initialize(&sess->exch_list);
1724 fibril_mutex_initialize(&sess->mutex);
1725 atomic_set(&sess->refcnt, 0);
1726
1727 return sess;
1728}
1729
1730static int async_hangup_internal(int phone)
1731{
1732 return ipc_hangup(phone);
[64d2b10]1733}
1734
1735/** Wrapper for ipc_hangup.
1736 *
[79ae36dd]1737 * @param sess Session to hung up.
[64d2b10]1738 *
1739 * @return Zero on success or a negative error code.
1740 *
1741 */
[79ae36dd]1742int async_hangup(async_sess_t *sess)
[64d2b10]1743{
[79ae36dd]1744 assert(sess);
1745
1746 if (atomic_get(&sess->refcnt) > 0)
1747 return EBUSY;
1748
1749 int rc = async_hangup_internal(sess->phone);
1750 if (rc == EOK)
1751 free(sess);
1752
1753 return rc;
[64d2b10]1754}
1755
1756/** Interrupt one thread of this task from waiting for IPC. */
1757void async_poke(void)
1758{
1759 ipc_poke();
1760}
1761
[79ae36dd]1762/** Start new exchange in a session.
1763 *
1764 * @param session Session.
1765 *
1766 * @return New exchange or NULL on error.
1767 *
1768 */
1769async_exch_t *async_exchange_begin(async_sess_t *sess)
1770{
1771 if (sess == NULL)
1772 return NULL;
1773
1774 async_exch_t *exch;
1775
1776 fibril_mutex_lock(&async_sess_mutex);
1777
1778 if (!list_empty(&sess->exch_list)) {
1779 /*
1780 * There are inactive exchanges in the session.
1781 */
1782 exch = (async_exch_t *)
[b72efe8]1783 list_get_instance(list_first(&sess->exch_list),
1784 async_exch_t, sess_link);
1785
[79ae36dd]1786 list_remove(&exch->sess_link);
1787 list_remove(&exch->global_link);
1788 } else {
1789 /*
1790 * There are no available exchanges in the session.
1791 */
1792
1793 if ((sess->mgmt == EXCHANGE_ATOMIC) ||
1794 (sess->mgmt == EXCHANGE_SERIALIZE)) {
1795 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
1796 if (exch != NULL) {
[b72efe8]1797 link_initialize(&exch->sess_link);
1798 link_initialize(&exch->global_link);
[79ae36dd]1799 exch->sess = sess;
1800 exch->phone = sess->phone;
1801 }
1802 } else { /* EXCHANGE_PARALLEL */
1803 /*
1804 * Make a one-time attempt to connect a new data phone.
1805 */
1806
1807 int phone;
1808
1809retry:
1810 phone = async_connect_me_to_internal(sess->phone, sess->arg1,
1811 sess->arg2, sess->arg3, 0);
1812 if (phone >= 0) {
1813 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
1814 if (exch != NULL) {
[b72efe8]1815 link_initialize(&exch->sess_link);
1816 link_initialize(&exch->global_link);
[79ae36dd]1817 exch->sess = sess;
1818 exch->phone = phone;
1819 } else
1820 async_hangup_internal(phone);
1821 } else if (!list_empty(&inactive_exch_list)) {
1822 /*
1823 * We did not manage to connect a new phone. But we
1824 * can try to close some of the currently inactive
1825 * connections in other sessions and try again.
1826 */
1827 exch = (async_exch_t *)
[b72efe8]1828 list_get_instance(list_first(&inactive_exch_list),
1829 async_exch_t, global_link);
1830
[79ae36dd]1831 list_remove(&exch->sess_link);
1832 list_remove(&exch->global_link);
1833 async_hangup_internal(exch->phone);
1834 free(exch);
1835 goto retry;
1836 } else {
1837 /*
1838 * Wait for a phone to become available.
1839 */
1840 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
1841 goto retry;
1842 }
1843 }
1844 }
1845
1846 fibril_mutex_unlock(&async_sess_mutex);
1847
1848 if (exch != NULL) {
1849 atomic_inc(&sess->refcnt);
1850
1851 if (sess->mgmt == EXCHANGE_SERIALIZE)
1852 fibril_mutex_lock(&sess->mutex);
1853 }
1854
1855 return exch;
1856}
1857
1858/** Finish an exchange.
1859 *
1860 * @param exch Exchange to finish.
1861 *
1862 */
1863void async_exchange_end(async_exch_t *exch)
1864{
1865 if (exch == NULL)
1866 return;
1867
1868 async_sess_t *sess = exch->sess;
1869
[1c6436a]1870 atomic_dec(&sess->refcnt);
1871
[79ae36dd]1872 if (sess->mgmt == EXCHANGE_SERIALIZE)
1873 fibril_mutex_unlock(&sess->mutex);
1874
1875 fibril_mutex_lock(&async_sess_mutex);
1876
1877 list_append(&exch->sess_link, &sess->exch_list);
1878 list_append(&exch->global_link, &inactive_exch_list);
1879 fibril_condvar_signal(&avail_phone_cv);
1880
1881 fibril_mutex_unlock(&async_sess_mutex);
1882}
1883
[47b7006]1884/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
1885 *
[79ae36dd]1886 * @param exch Exchange for sending the message.
1887 * @param dst Destination address space area base.
1888 * @param size Size of the destination address space area.
1889 * @param arg User defined argument.
1890 * @param flags Storage for the received flags. Can be NULL.
[0da4e41]1891 *
[47b7006]1892 * @return Zero on success or a negative error code from errno.h.
[0da4e41]1893 *
1894 */
[79ae36dd]1895int async_share_in_start(async_exch_t *exch, void *dst, size_t size,
1896 sysarg_t arg, unsigned int *flags)
[0da4e41]1897{
[79ae36dd]1898 if (exch == NULL)
1899 return ENOENT;
1900
[0da4e41]1901 sysarg_t tmp_flags;
[79ae36dd]1902 int res = async_req_3_2(exch, IPC_M_SHARE_IN, (sysarg_t) dst,
[96b02eb9]1903 (sysarg_t) size, arg, NULL, &tmp_flags);
[47b7006]1904
[0da4e41]1905 if (flags)
[47b7006]1906 *flags = (unsigned int) tmp_flags;
1907
[0da4e41]1908 return res;
1909}
1910
1911/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1912 *
[47b7006]1913 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
1914 * calls so that the user doesn't have to remember the meaning of each IPC
1915 * argument.
[0da4e41]1916 *
1917 * So far, this wrapper is to be used from within a connection fibril.
1918 *
[47b7006]1919 * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
1920 * @param size Destination address space area size.
1921 *
1922 * @return True on success, false on failure.
[0da4e41]1923 *
1924 */
[47b7006]1925bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
[0da4e41]1926{
1927 assert(callid);
1928 assert(size);
[47b7006]1929
1930 ipc_call_t data;
[0da4e41]1931 *callid = async_get_call(&data);
[47b7006]1932
[228e490]1933 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
[47b7006]1934 return false;
1935
[0da4e41]1936 *size = (size_t) IPC_GET_ARG2(data);
[47b7006]1937 return true;
[0da4e41]1938}
1939
1940/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1941 *
[47b7006]1942 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
1943 * calls so that the user doesn't have to remember the meaning of each IPC
1944 * argument.
[0da4e41]1945 *
[47b7006]1946 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1947 * @param src Source address space base.
1948 * @param flags Flags to be used for sharing. Bits can be only cleared.
1949 *
1950 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]1951 *
1952 */
[47b7006]1953int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
[0da4e41]1954{
1955 return ipc_share_in_finalize(callid, src, flags);
1956}
1957
[47b7006]1958/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
[0da4e41]1959 *
[79ae36dd]1960 * @param exch Exchange for sending the message.
1961 * @param src Source address space area base address.
1962 * @param flags Flags to be used for sharing. Bits can be only cleared.
[47b7006]1963 *
1964 * @return Zero on success or a negative error code from errno.h.
[0da4e41]1965 *
1966 */
[79ae36dd]1967int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
[0da4e41]1968{
[79ae36dd]1969 if (exch == NULL)
1970 return ENOENT;
1971
1972 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
[96b02eb9]1973 (sysarg_t) flags);
[0da4e41]1974}
1975
1976/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1977 *
[47b7006]1978 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
1979 * calls so that the user doesn't have to remember the meaning of each IPC
1980 * argument.
[0da4e41]1981 *
1982 * So far, this wrapper is to be used from within a connection fibril.
1983 *
[47b7006]1984 * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
1985 * @param size Storage for the source address space area size.
1986 * @param flags Storage for the sharing flags.
1987 *
1988 * @return True on success, false on failure.
[0da4e41]1989 *
1990 */
[47b7006]1991bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
[0da4e41]1992{
1993 assert(callid);
1994 assert(size);
1995 assert(flags);
[47b7006]1996
1997 ipc_call_t data;
[0da4e41]1998 *callid = async_get_call(&data);
[47b7006]1999
[228e490]2000 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
[47b7006]2001 return false;
2002
[0da4e41]2003 *size = (size_t) IPC_GET_ARG2(data);
[47b7006]2004 *flags = (unsigned int) IPC_GET_ARG3(data);
2005 return true;
[0da4e41]2006}
2007
2008/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2009 *
[47b7006]2010 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2011 * calls so that the user doesn't have to remember the meaning of each IPC
2012 * argument.
[0da4e41]2013 *
[47b7006]2014 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2015 * @param dst Destination address space area base address.
2016 *
2017 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2018 *
2019 */
2020int async_share_out_finalize(ipc_callid_t callid, void *dst)
2021{
2022 return ipc_share_out_finalize(callid, dst);
2023}
2024
[8bf1eeb]2025/** Start IPC_M_DATA_READ using the async framework.
2026 *
[79ae36dd]2027 * @param exch Exchange for sending the message.
2028 * @param dst Address of the beginning of the destination buffer.
2029 * @param size Size of the destination buffer (in bytes).
[8bf1eeb]2030 * @param dataptr Storage of call data (arg 2 holds actual data size).
[79ae36dd]2031 *
[8bf1eeb]2032 * @return Hash of the sent message or 0 on error.
[79ae36dd]2033 *
[8bf1eeb]2034 */
[79ae36dd]2035aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2036 ipc_call_t *dataptr)
[8bf1eeb]2037{
[79ae36dd]2038 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
[8bf1eeb]2039 (sysarg_t) size, dataptr);
2040}
2041
[47b7006]2042/** Wrapper for IPC_M_DATA_READ calls using the async framework.
[0da4e41]2043 *
[79ae36dd]2044 * @param exch Exchange for sending the message.
2045 * @param dst Address of the beginning of the destination buffer.
2046 * @param size Size of the destination buffer.
[47b7006]2047 *
2048 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2049 *
2050 */
[79ae36dd]2051int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
[0da4e41]2052{
[79ae36dd]2053 if (exch == NULL)
2054 return ENOENT;
2055
2056 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2057 (sysarg_t) size);
[0da4e41]2058}
2059
2060/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2061 *
[47b7006]2062 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2063 * calls so that the user doesn't have to remember the meaning of each IPC
2064 * argument.
[0da4e41]2065 *
2066 * So far, this wrapper is to be used from within a connection fibril.
2067 *
[47b7006]2068 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2069 * @param size Storage for the maximum size. Can be NULL.
2070 *
2071 * @return True on success, false on failure.
[0da4e41]2072 *
2073 */
[47b7006]2074bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
[0da4e41]2075{
2076 assert(callid);
[47b7006]2077
2078 ipc_call_t data;
[0da4e41]2079 *callid = async_get_call(&data);
[47b7006]2080
[228e490]2081 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
[47b7006]2082 return false;
2083
[0da4e41]2084 if (size)
2085 *size = (size_t) IPC_GET_ARG2(data);
[47b7006]2086
2087 return true;
[0da4e41]2088}
2089
2090/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2091 *
[47b7006]2092 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2093 * calls so that the user doesn't have to remember the meaning of each IPC
2094 * argument.
[0da4e41]2095 *
[47b7006]2096 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2097 * @param src Source address for the IPC_M_DATA_READ call.
2098 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2099 * the maximum size announced by the sender.
2100 *
2101 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2102 *
2103 */
2104int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
2105{
2106 return ipc_data_read_finalize(callid, src, size);
2107}
2108
[b4cbef1]2109/** Wrapper for forwarding any read request
2110 *
2111 */
[79ae36dd]2112int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2113 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2114 ipc_call_t *dataptr)
[b4cbef1]2115{
[79ae36dd]2116 if (exch == NULL)
2117 return ENOENT;
2118
[b4cbef1]2119 ipc_callid_t callid;
2120 if (!async_data_read_receive(&callid, NULL)) {
2121 ipc_answer_0(callid, EINVAL);
2122 return EINVAL;
2123 }
2124
[79ae36dd]2125 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]2126 dataptr);
2127 if (msg == 0) {
2128 ipc_answer_0(callid, EINVAL);
2129 return EINVAL;
2130 }
2131
[79ae36dd]2132 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
[b4cbef1]2133 IPC_FF_ROUTE_FROM_ME);
2134 if (retval != EOK) {
[a281fc82]2135 async_wait_for(msg, NULL);
[b4cbef1]2136 ipc_answer_0(callid, retval);
2137 return retval;
2138 }
2139
[96b02eb9]2140 sysarg_t rc;
[b4cbef1]2141 async_wait_for(msg, &rc);
2142
2143 return (int) rc;
2144}
2145
[47b7006]2146/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
[0da4e41]2147 *
[79ae36dd]2148 * @param exch Exchange for sending the message.
2149 * @param src Address of the beginning of the source buffer.
2150 * @param size Size of the source buffer.
[b4cbef1]2151 *
2152 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2153 *
2154 */
[79ae36dd]2155int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
[0da4e41]2156{
[79ae36dd]2157 if (exch == NULL)
2158 return ENOENT;
2159
2160 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2161 (sysarg_t) size);
[0da4e41]2162}
2163
2164/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2165 *
[47b7006]2166 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2167 * calls so that the user doesn't have to remember the meaning of each IPC
2168 * argument.
[0da4e41]2169 *
2170 * So far, this wrapper is to be used from within a connection fibril.
2171 *
[47b7006]2172 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2173 * @param size Storage for the suggested size. May be NULL.
[b4cbef1]2174 *
[47b7006]2175 * @return True on success, false on failure.
[0da4e41]2176 *
2177 */
[47b7006]2178bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
[0da4e41]2179{
2180 assert(callid);
[b4cbef1]2181
[47b7006]2182 ipc_call_t data;
[0da4e41]2183 *callid = async_get_call(&data);
[47b7006]2184
[228e490]2185 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
[47b7006]2186 return false;
[b4cbef1]2187
[0da4e41]2188 if (size)
2189 *size = (size_t) IPC_GET_ARG2(data);
[b4cbef1]2190
[47b7006]2191 return true;
[0da4e41]2192}
2193
2194/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2195 *
[47b7006]2196 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2197 * calls so that the user doesn't have to remember the meaning of each IPC
2198 * argument.
[0da4e41]2199 *
[b4cbef1]2200 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2201 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2202 * @param size Final size for the IPC_M_DATA_WRITE call.
2203 *
2204 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2205 *
2206 */
2207int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
2208{
2209 return ipc_data_write_finalize(callid, dst, size);
2210}
2211
[eda925a]2212/** Wrapper for receiving binary data or strings
[8aa42e3]2213 *
2214 * This wrapper only makes it more comfortable to use async_data_write_*
[eda925a]2215 * functions to receive binary data or strings.
[8aa42e3]2216 *
[472c09d]2217 * @param data Pointer to data pointer (which should be later disposed
2218 * by free()). If the operation fails, the pointer is not
2219 * touched.
[eda925a]2220 * @param nullterm If true then the received data is always zero terminated.
2221 * This also causes to allocate one extra byte beyond the
2222 * raw transmitted data.
[b4cbef1]2223 * @param min_size Minimum size (in bytes) of the data to receive.
[472c09d]2224 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2225 * no limit.
[eda925a]2226 * @param granulariy If non-zero then the size of the received data has to
[472c09d]2227 * be divisible by this value.
2228 * @param received If not NULL, the size of the received data is stored here.
[8aa42e3]2229 *
2230 * @return Zero on success or a value from @ref errno.h on failure.
2231 *
2232 */
[eda925a]2233int async_data_write_accept(void **data, const bool nullterm,
2234 const size_t min_size, const size_t max_size, const size_t granularity,
2235 size_t *received)
[8aa42e3]2236{
[79ae36dd]2237 assert(data);
2238
[8aa42e3]2239 ipc_callid_t callid;
2240 size_t size;
2241 if (!async_data_write_receive(&callid, &size)) {
2242 ipc_answer_0(callid, EINVAL);
2243 return EINVAL;
2244 }
2245
[b4cbef1]2246 if (size < min_size) {
2247 ipc_answer_0(callid, EINVAL);
2248 return EINVAL;
2249 }
2250
[8aa42e3]2251 if ((max_size > 0) && (size > max_size)) {
2252 ipc_answer_0(callid, EINVAL);
2253 return EINVAL;
2254 }
2255
[472c09d]2256 if ((granularity > 0) && ((size % granularity) != 0)) {
2257 ipc_answer_0(callid, EINVAL);
2258 return EINVAL;
2259 }
2260
[eda925a]2261 void *_data;
2262
2263 if (nullterm)
2264 _data = malloc(size + 1);
2265 else
2266 _data = malloc(size);
2267
[472c09d]2268 if (_data == NULL) {
[8aa42e3]2269 ipc_answer_0(callid, ENOMEM);
2270 return ENOMEM;
2271 }
2272
[472c09d]2273 int rc = async_data_write_finalize(callid, _data, size);
[8aa42e3]2274 if (rc != EOK) {
[472c09d]2275 free(_data);
[8aa42e3]2276 return rc;
2277 }
2278
[eda925a]2279 if (nullterm)
2280 ((char *) _data)[size] = 0;
[8aa42e3]2281
[eda925a]2282 *data = _data;
[472c09d]2283 if (received != NULL)
2284 *received = size;
2285
[8aa42e3]2286 return EOK;
2287}
2288
[b4cbef1]2289/** Wrapper for voiding any data that is about to be received
2290 *
2291 * This wrapper can be used to void any pending data
2292 *
2293 * @param retval Error value from @ref errno.h to be returned to the caller.
2294 *
2295 */
[47b7006]2296void async_data_write_void(sysarg_t retval)
[b4cbef1]2297{
2298 ipc_callid_t callid;
2299 async_data_write_receive(&callid, NULL);
2300 ipc_answer_0(callid, retval);
2301}
2302
2303/** Wrapper for forwarding any data that is about to be received
2304 *
2305 */
[79ae36dd]2306int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
2307 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2308 ipc_call_t *dataptr)
[b4cbef1]2309{
[79ae36dd]2310 if (exch == NULL)
2311 return ENOENT;
2312
[b4cbef1]2313 ipc_callid_t callid;
2314 if (!async_data_write_receive(&callid, NULL)) {
2315 ipc_answer_0(callid, EINVAL);
2316 return EINVAL;
2317 }
2318
[79ae36dd]2319 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]2320 dataptr);
2321 if (msg == 0) {
2322 ipc_answer_0(callid, EINVAL);
2323 return EINVAL;
2324 }
2325
[79ae36dd]2326 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
[b4cbef1]2327 IPC_FF_ROUTE_FROM_ME);
2328 if (retval != EOK) {
[a281fc82]2329 async_wait_for(msg, NULL);
[b4cbef1]2330 ipc_answer_0(callid, retval);
2331 return retval;
2332 }
2333
[96b02eb9]2334 sysarg_t rc;
[b4cbef1]2335 async_wait_for(msg, &rc);
2336
2337 return (int) rc;
2338}
2339
[79ae36dd]2340/** Wrapper for sending an exchange over different exchange for cloning
2341 *
2342 * @param exch Exchange to be used for sending.
2343 * @param clone_exch Exchange to be cloned.
2344 *
2345 */
2346int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
2347{
2348 return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
2349}
2350
2351/** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
2352 *
2353 * If the current call is IPC_M_CONNECTION_CLONE then a new
2354 * async session is created for the accepted phone.
2355 *
2356 * @param mgmt Exchange management style.
2357 *
2358 * @return New async session or NULL on failure.
2359 *
2360 */
2361async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
2362{
2363 /* Accept the phone */
2364 ipc_call_t call;
2365 ipc_callid_t callid = async_get_call(&call);
2366 int phone = (int) IPC_GET_ARG1(call);
2367
2368 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
2369 (phone < 0)) {
2370 async_answer_0(callid, EINVAL);
2371 return NULL;
2372 }
2373
2374 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2375 if (sess == NULL) {
2376 async_answer_0(callid, ENOMEM);
2377 return NULL;
2378 }
2379
2380 sess->mgmt = mgmt;
2381 sess->phone = phone;
2382 sess->arg1 = 0;
2383 sess->arg2 = 0;
2384 sess->arg3 = 0;
2385
2386 list_initialize(&sess->exch_list);
2387 fibril_mutex_initialize(&sess->mutex);
2388 atomic_set(&sess->refcnt, 0);
2389
2390 /* Acknowledge the cloned phone */
2391 async_answer_0(callid, EOK);
2392
2393 return sess;
2394}
2395
2396/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2397 *
2398 * If the current call is IPC_M_CONNECT_TO_ME then a new
2399 * async session is created for the accepted phone.
2400 *
2401 * @param mgmt Exchange management style.
2402 *
[8869f7b]2403 * @return New async session.
2404 * @return NULL on failure.
[79ae36dd]2405 *
2406 */
2407async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
2408{
2409 /* Accept the phone */
2410 ipc_call_t call;
2411 ipc_callid_t callid = async_get_call(&call);
2412 int phone = (int) IPC_GET_ARG5(call);
2413
2414 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
2415 (phone < 0)) {
2416 async_answer_0(callid, EINVAL);
2417 return NULL;
2418 }
2419
2420 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2421 if (sess == NULL) {
2422 async_answer_0(callid, ENOMEM);
2423 return NULL;
2424 }
2425
2426 sess->mgmt = mgmt;
2427 sess->phone = phone;
2428 sess->arg1 = 0;
2429 sess->arg2 = 0;
2430 sess->arg3 = 0;
2431
2432 list_initialize(&sess->exch_list);
2433 fibril_mutex_initialize(&sess->mutex);
2434 atomic_set(&sess->refcnt, 0);
2435
2436 /* Acknowledge the connected phone */
2437 async_answer_0(callid, EOK);
2438
2439 return sess;
2440}
2441
[8869f7b]2442/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2443 *
2444 * If the call is IPC_M_CONNECT_TO_ME then a new
2445 * async session is created. However, the phone is
2446 * not accepted automatically.
2447 *
2448 * @param mgmt Exchange management style.
2449 * @param call Call data.
2450 *
2451 * @return New async session.
2452 * @return NULL on failure.
2453 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
2454 *
2455 */
2456async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
2457{
2458 int phone = (int) IPC_GET_ARG5(*call);
2459
2460 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
2461 (phone < 0))
2462 return NULL;
2463
2464 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2465 if (sess == NULL)
2466 return NULL;
2467
2468 sess->mgmt = mgmt;
2469 sess->phone = phone;
2470 sess->arg1 = 0;
2471 sess->arg2 = 0;
2472 sess->arg3 = 0;
2473
2474 list_initialize(&sess->exch_list);
2475 fibril_mutex_initialize(&sess->mutex);
2476 atomic_set(&sess->refcnt, 0);
2477
2478 return sess;
2479}
2480
[2c4aa39]2481int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
2482 sysarg_t arg3, async_exch_t *other_exch)
2483{
2484 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
2485 arg1, arg2, arg3, 0, other_exch->phone);
2486}
2487
2488bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
2489 sysarg_t *arg2, sysarg_t *arg3)
2490{
2491 assert(callid);
2492
2493 ipc_call_t call;
2494 *callid = async_get_call(&call);
2495
2496 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
2497 return false;
2498
2499 if (arg1)
2500 *arg1 = IPC_GET_ARG1(call);
2501 if (arg2)
2502 *arg2 = IPC_GET_ARG2(call);
2503 if (arg3)
2504 *arg3 = IPC_GET_ARG3(call);
2505
2506 return true;
2507}
2508
2509int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
2510{
2511 return ipc_answer_1(callid, EOK, other_exch->phone);
2512}
2513
[a46da63]2514/** @}
[b2951e2]2515 */
Note: See TracBrowser for help on using the repository browser.