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

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

Really drop one reference in async_put_client_data_by_hash().

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