source: mainline/uspace/lib/c/generic/async.c@ 848e880f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 848e880f was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 80.1 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 *
[01c3bb4]79 * port_handler(ichandle, *icall)
[c07544d3]80 * {
81 * if (want_refuse) {
[01c3bb4]82 * async_answer_0(ichandle, ELIMIT);
[c07544d3]83 * return;
84 * }
[01c3bb4]85 * async_answer_0(ichandle, EOK);
[80649a91]86 *
[01c3bb4]87 * chandle = async_get_call(&call);
88 * somehow_handle_the_call(chandle, call);
89 * async_answer_2(chandle, 1, 2, 3);
[53ca318]90 *
[01c3bb4]91 * chandle = async_get_call(&call);
[c07544d3]92 * ...
93 * }
[a2cd194]94 *
[80649a91]95 */
[9591265]96
[64d2b10]97#define LIBC_ASYNC_C_
98#include <ipc/ipc.h>
[80649a91]99#include <async.h>
[b76a7329]100#include "private/async.h"
[64d2b10]101#undef LIBC_ASYNC_C_
102
[8820544]103#include <ipc/irq.h>
104#include <ipc/event.h>
[64d2b10]105#include <futex.h>
[bc1f1c2]106#include <fibril.h>
[d9c8c81]107#include <adt/hash_table.h>
[853802e]108#include <adt/hash.h>
[d9c8c81]109#include <adt/list.h>
[80649a91]110#include <assert.h>
111#include <errno.h>
[daa90e8]112#include <sys/time.h>
[c0699467]113#include <libarch/barrier.h>
[3e6a98c5]114#include <stdbool.h>
[38d150e]115#include <stdlib.h>
[79ae36dd]116#include <mem.h>
117#include <stdlib.h>
[e2ab36f1]118#include <macros.h>
[101516d]119#include <as.h>
[ae6021d]120#include <abi/mm/as.h>
[d7978525]121#include "private/libc.h"
[80649a91]122
[5da7199]123/** Session data */
124struct async_sess {
125 /** List of inactive exchanges */
126 list_t exch_list;
[a35b458]127
[566992e1]128 /** Session interface */
129 iface_t iface;
[a35b458]130
[5da7199]131 /** Exchange management style */
132 exch_mgmt_t mgmt;
[a35b458]133
[5da7199]134 /** Session identification */
[eadaeae8]135 cap_phone_handle_t phone;
[a35b458]136
[5da7199]137 /** First clone connection argument */
138 sysarg_t arg1;
[a35b458]139
[5da7199]140 /** Second clone connection argument */
141 sysarg_t arg2;
[a35b458]142
[5da7199]143 /** Third clone connection argument */
144 sysarg_t arg3;
[a35b458]145
[5da7199]146 /** Exchange mutex */
147 fibril_mutex_t mutex;
[a35b458]148
[5da7199]149 /** Number of opened exchanges */
150 atomic_t refcnt;
[a35b458]151
[5da7199]152 /** Mutex for stateful connections */
153 fibril_mutex_t remote_state_mtx;
[a35b458]154
[5da7199]155 /** Data for stateful connections */
156 void *remote_state_data;
157};
158
159/** Exchange data */
160struct async_exch {
161 /** Link into list of inactive exchanges */
162 link_t sess_link;
[a35b458]163
[5da7199]164 /** Link into global list of inactive exchanges */
165 link_t global_link;
[a35b458]166
[5da7199]167 /** Session pointer */
168 async_sess_t *sess;
[a35b458]169
[5da7199]170 /** Exchange identification */
[eadaeae8]171 cap_phone_handle_t phone;
[5da7199]172};
173
[79ae36dd]174/** Async framework global futex */
[927a181e]175futex_t async_futex = FUTEX_INITIALIZER;
[80649a91]176
[8619f25]177/** Number of threads waiting for IPC in the kernel. */
178atomic_t threads_in_ipc_wait = { 0 };
179
[79ae36dd]180/** Naming service session */
181async_sess_t *session_ns;
[01ff41c]182
[79ae36dd]183/** Call data */
[80649a91]184typedef struct {
185 link_t link;
[a35b458]186
[eadaeae8]187 cap_call_handle_t chandle;
[80649a91]188 ipc_call_t call;
189} msg_t;
190
[5da7199]191/** Message data */
192typedef struct {
193 awaiter_t wdata;
[a35b458]194
[5da7199]195 /** If reply was received. */
196 bool done;
[a35b458]197
[47c9a8c]198 /** If the message / reply should be discarded on arrival. */
199 bool forget;
[a35b458]200
[47c9a8c]201 /** If already destroyed. */
202 bool destroyed;
[a35b458]203
[5da7199]204 /** Pointer to where the answer data is stored. */
205 ipc_call_t *dataptr;
[a35b458]206
[b7fd2a0]207 errno_t retval;
[5da7199]208} amsg_t;
209
[79ae36dd]210/* Client connection data */
[c80fdd0]211typedef struct {
[062d900]212 ht_link_t link;
[a35b458]213
[649f087]214 task_id_t in_task_id;
[79ae36dd]215 atomic_t refcnt;
[c80fdd0]216 void *data;
217} client_t;
218
[79ae36dd]219/* Server connection data */
[80649a91]220typedef struct {
[49d072e]221 awaiter_t wdata;
[a35b458]222
[e70bfa5]223 /** Hash table link. */
[062d900]224 ht_link_t link;
[a35b458]225
[e2ab36f1]226 /** Incoming client task ID. */
227 task_id_t in_task_id;
[a35b458]228
[e70bfa5]229 /** Incoming phone hash. */
[96b02eb9]230 sysarg_t in_phone_hash;
[a35b458]231
[23882034]232 /** Link to the client tracking structure. */
233 client_t *client;
[a35b458]234
[e70bfa5]235 /** Messages that should be delivered to this fibril. */
[b72efe8]236 list_t msg_queue;
[a35b458]237
[e70bfa5]238 /** Identification of the opening call. */
[eadaeae8]239 cap_call_handle_t chandle;
[a35b458]240
[e70bfa5]241 /** Call data of the opening call. */
[80649a91]242 ipc_call_t call;
[a35b458]243
[e70bfa5]244 /** Identification of the closing call. */
[eadaeae8]245 cap_call_handle_t close_chandle;
[a35b458]246
[e70bfa5]247 /** Fibril function that will be used to handle the connection. */
[b688fd8]248 async_port_handler_t handler;
[a35b458]249
[b688fd8]250 /** Client data */
251 void *data;
[80649a91]252} connection_t;
253
[566992e1]254/** Interface data */
255typedef struct {
256 ht_link_t link;
[a35b458]257
[566992e1]258 /** Interface ID */
259 iface_t iface;
[a35b458]260
[566992e1]261 /** Futex protecting the hash table */
262 futex_t futex;
[a35b458]263
[566992e1]264 /** Interface ports */
265 hash_table_t port_hash_table;
[a35b458]266
[566992e1]267 /** Next available port ID */
268 port_id_t port_id_avail;
269} interface_t;
270
271/* Port data */
272typedef struct {
273 ht_link_t link;
[a35b458]274
[566992e1]275 /** Port ID */
276 port_id_t id;
[a35b458]277
[566992e1]278 /** Port connection handler */
279 async_port_handler_t handler;
[a35b458]280
[566992e1]281 /** Client data */
282 void *data;
283} port_t;
284
[8820544]285/* Notification data */
286typedef struct {
287 ht_link_t link;
[a35b458]288
[8820544]289 /** Notification method */
290 sysarg_t imethod;
[a35b458]291
[8820544]292 /** Notification handler */
293 async_notification_handler_t handler;
[a35b458]294
[8820544]295 /** Notification data */
296 void *data;
297} notification_t;
298
[bc1f1c2]299/** Identifier of the incoming connection handled by the current fibril. */
[79ae36dd]300static fibril_local connection_t *fibril_connection;
[e70bfa5]301
[47c9a8c]302static void to_event_initialize(to_event_t *to)
303{
[aeeddeb]304 struct timeval tv = { 0, 0 };
[a35b458]305
[47c9a8c]306 to->inlist = false;
307 to->occurred = false;
308 link_initialize(&to->link);
309 to->expires = tv;
310}
311
312static void wu_event_initialize(wu_event_t *wu)
313{
314 wu->inlist = false;
315 link_initialize(&wu->link);
316}
317
318void awaiter_initialize(awaiter_t *aw)
319{
320 aw->fid = 0;
321 aw->active = false;
322 to_event_initialize(&aw->to_event);
323 wu_event_initialize(&aw->wu_event);
324}
325
326static amsg_t *amsg_create(void)
327{
[57dea62]328 amsg_t *msg = malloc(sizeof(amsg_t));
[47c9a8c]329 if (msg) {
330 msg->done = false;
331 msg->forget = false;
332 msg->destroyed = false;
333 msg->dataptr = NULL;
[25a179e]334 msg->retval = EINVAL;
[47c9a8c]335 awaiter_initialize(&msg->wdata);
336 }
[a35b458]337
[47c9a8c]338 return msg;
339}
340
341static void amsg_destroy(amsg_t *msg)
342{
343 assert(!msg->destroyed);
344 msg->destroyed = true;
345 free(msg);
346}
347
[46eec3b]348static void *default_client_data_constructor(void)
349{
350 return NULL;
351}
352
353static void default_client_data_destructor(void *data)
354{
355}
356
357static async_client_data_ctor_t async_client_data_create =
358 default_client_data_constructor;
359static async_client_data_dtor_t async_client_data_destroy =
360 default_client_data_destructor;
361
362void async_set_client_data_constructor(async_client_data_ctor_t ctor)
363{
[f302586]364 assert(async_client_data_create == default_client_data_constructor);
[46eec3b]365 async_client_data_create = ctor;
366}
367
368void async_set_client_data_destructor(async_client_data_dtor_t dtor)
369{
[f302586]370 assert(async_client_data_destroy == default_client_data_destructor);
[46eec3b]371 async_client_data_destroy = dtor;
372}
373
[b688fd8]374/** Default fallback fibril function.
[47b7006]375 *
[01c3bb4]376 * This fallback fibril function gets called on incomming connections that do
377 * not have a specific handler defined.
[47b7006]378 *
[01c3bb4]379 * @param chandle Handle of the incoming call.
380 * @param call Data of the incoming call.
381 * @param arg Local argument
[47b7006]382 *
383 */
[eadaeae8]384static void default_fallback_port_handler(cap_call_handle_t chandle,
[01c3bb4]385 ipc_call_t *call, void *arg)
[47b7006]386{
[01c3bb4]387 ipc_answer_0(chandle, ENOENT);
[47b7006]388}
[36c9234]389
[b688fd8]390static async_port_handler_t fallback_port_handler =
391 default_fallback_port_handler;
392static void *fallback_port_data = NULL;
[da0c91e7]393
[566992e1]394static hash_table_t interface_hash_table;
395
396static size_t interface_key_hash(void *key)
397{
398 iface_t iface = *(iface_t *) key;
399 return iface;
400}
401
402static size_t interface_hash(const ht_link_t *item)
403{
404 interface_t *interface = hash_table_get_inst(item, interface_t, link);
405 return interface_key_hash(&interface->iface);
406}
407
408static bool interface_key_equal(void *key, const ht_link_t *item)
409{
410 iface_t iface = *(iface_t *) key;
411 interface_t *interface = hash_table_get_inst(item, interface_t, link);
412 return iface == interface->iface;
413}
414
415/** Operations for the port hash table. */
416static hash_table_ops_t interface_hash_table_ops = {
417 .hash = interface_hash,
418 .key_hash = interface_key_hash,
419 .key_equal = interface_key_equal,
420 .equal = NULL,
421 .remove_callback = NULL
422};
423
424static size_t port_key_hash(void *key)
425{
426 port_id_t port_id = *(port_id_t *) key;
427 return port_id;
428}
429
430static size_t port_hash(const ht_link_t *item)
431{
432 port_t *port = hash_table_get_inst(item, port_t, link);
433 return port_key_hash(&port->id);
434}
435
436static bool port_key_equal(void *key, const ht_link_t *item)
437{
438 port_id_t port_id = *(port_id_t *) key;
439 port_t *port = hash_table_get_inst(item, port_t, link);
440 return port_id == port->id;
441}
442
443/** Operations for the port hash table. */
444static hash_table_ops_t port_hash_table_ops = {
445 .hash = port_hash,
446 .key_hash = port_key_hash,
447 .key_equal = port_key_equal,
448 .equal = NULL,
449 .remove_callback = NULL
450};
451
452static interface_t *async_new_interface(iface_t iface)
453{
454 interface_t *interface =
455 (interface_t *) malloc(sizeof(interface_t));
456 if (!interface)
457 return NULL;
[a35b458]458
[566992e1]459 bool ret = hash_table_create(&interface->port_hash_table, 0, 0,
460 &port_hash_table_ops);
461 if (!ret) {
462 free(interface);
463 return NULL;
464 }
[a35b458]465
[566992e1]466 interface->iface = iface;
467 futex_initialize(&interface->futex, 1);
468 interface->port_id_avail = 0;
[a35b458]469
[566992e1]470 hash_table_insert(&interface_hash_table, &interface->link);
[a35b458]471
[566992e1]472 return interface;
473}
474
475static port_t *async_new_port(interface_t *interface,
476 async_port_handler_t handler, void *data)
477{
478 port_t *port = (port_t *) malloc(sizeof(port_t));
479 if (!port)
480 return NULL;
[a35b458]481
[566992e1]482 futex_down(&interface->futex);
[a35b458]483
[566992e1]484 port_id_t id = interface->port_id_avail;
485 interface->port_id_avail++;
[a35b458]486
[566992e1]487 port->id = id;
488 port->handler = handler;
489 port->data = data;
[a35b458]490
[566992e1]491 hash_table_insert(&interface->port_hash_table, &port->link);
[a35b458]492
[566992e1]493 futex_up(&interface->futex);
[a35b458]494
[566992e1]495 return port;
496}
497
[79ae36dd]498/** Mutex protecting inactive_exch_list and avail_phone_cv.
499 *
500 */
501static FIBRIL_MUTEX_INITIALIZE(async_sess_mutex);
502
503/** List of all currently inactive exchanges.
504 *
505 */
506static LIST_INITIALIZE(inactive_exch_list);
507
508/** Condition variable to wait for a phone to become available.
509 *
510 */
511static FIBRIL_CONDVAR_INITIALIZE(avail_phone_cv);
512
[b7fd2a0]513errno_t async_create_port(iface_t iface, async_port_handler_t handler,
[566992e1]514 void *data, port_id_t *port_id)
515{
516 if ((iface & IFACE_MOD_MASK) == IFACE_MOD_CALLBACK)
517 return EINVAL;
[a35b458]518
[566992e1]519 interface_t *interface;
[a35b458]520
[566992e1]521 futex_down(&async_futex);
[a35b458]522
[566992e1]523 ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
524 if (link)
525 interface = hash_table_get_inst(link, interface_t, link);
526 else
527 interface = async_new_interface(iface);
[a35b458]528
[566992e1]529 if (!interface) {
530 futex_up(&async_futex);
531 return ENOMEM;
532 }
[a35b458]533
[566992e1]534 port_t *port = async_new_port(interface, handler, data);
535 if (!port) {
536 futex_up(&async_futex);
537 return ENOMEM;
538 }
[a35b458]539
[566992e1]540 *port_id = port->id;
[a35b458]541
[566992e1]542 futex_up(&async_futex);
[a35b458]543
[566992e1]544 return EOK;
545}
546
[b688fd8]547void async_set_fallback_port_handler(async_port_handler_t handler, void *data)
548{
549 assert(handler != NULL);
[a35b458]550
[b688fd8]551 fallback_port_handler = handler;
552 fallback_port_data = data;
553}
554
[c80fdd0]555static hash_table_t client_hash_table;
[c07544d3]556static hash_table_t conn_hash_table;
[8820544]557static hash_table_t notification_hash_table;
[c07544d3]558static LIST_INITIALIZE(timeout_list);
559
[8820544]560static sysarg_t notification_avail = 0;
561
562static size_t client_key_hash(void *key)
[c80fdd0]563{
[8820544]564 task_id_t in_task_id = *(task_id_t *) key;
565 return in_task_id;
[c80fdd0]566}
567
[062d900]568static size_t client_hash(const ht_link_t *item)
[c80fdd0]569{
[062d900]570 client_t *client = hash_table_get_inst(item, client_t, link);
571 return client_key_hash(&client->in_task_id);
[c80fdd0]572}
573
[8820544]574static bool client_key_equal(void *key, const ht_link_t *item)
[c80fdd0]575{
[8820544]576 task_id_t in_task_id = *(task_id_t *) key;
[062d900]577 client_t *client = hash_table_get_inst(item, client_t, link);
[8820544]578 return in_task_id == client->in_task_id;
[c80fdd0]579}
580
581/** Operations for the client hash table. */
[062d900]582static hash_table_ops_t client_hash_table_ops = {
[c80fdd0]583 .hash = client_hash,
[062d900]584 .key_hash = client_key_hash,
585 .key_equal = client_key_equal,
[4e00f87]586 .equal = NULL,
587 .remove_callback = NULL
[c80fdd0]588};
[80649a91]589
[853802e]590typedef struct {
591 task_id_t task_id;
592 sysarg_t phone_hash;
593} conn_key_t;
594
595/** Compute hash into the connection hash table
[e70bfa5]596 *
[853802e]597 * The hash is based on the source task ID and the source phone hash. The task
598 * ID is included in the hash because a phone hash alone might not be unique
599 * while we still track connections for killed tasks due to kernel's recycling
600 * of phone structures.
601 *
602 * @param key Pointer to the connection key structure.
[c07544d3]603 *
604 * @return Index into the connection hash table.
[e70bfa5]605 *
606 */
[062d900]607static size_t conn_key_hash(void *key)
[450cd3a]608{
[853802e]609 conn_key_t *ck = (conn_key_t *) key;
610
611 size_t hash = 0;
612 hash = hash_combine(hash, LOWER32(ck->task_id));
613 hash = hash_combine(hash, UPPER32(ck->task_id));
614 hash = hash_combine(hash, ck->phone_hash);
615 return hash;
[450cd3a]616}
[06502f7d]617
[062d900]618static size_t conn_hash(const ht_link_t *item)
[450cd3a]619{
[062d900]620 connection_t *conn = hash_table_get_inst(item, connection_t, link);
[853802e]621 return conn_key_hash(&(conn_key_t){
622 .task_id = conn->in_task_id,
623 .phone_hash = conn->in_phone_hash
624 });
[450cd3a]625}
[06502f7d]626
[062d900]627static bool conn_key_equal(void *key, const ht_link_t *item)
[450cd3a]628{
[853802e]629 conn_key_t *ck = (conn_key_t *) key;
[062d900]630 connection_t *conn = hash_table_get_inst(item, connection_t, link);
[853802e]631 return ((ck->task_id == conn->in_task_id) &&
632 (ck->phone_hash == conn->in_phone_hash));
[450cd3a]633}
634
[e70bfa5]635/** Operations for the connection hash table. */
[062d900]636static hash_table_ops_t conn_hash_table_ops = {
[80649a91]637 .hash = conn_hash,
[062d900]638 .key_hash = conn_key_hash,
639 .key_equal = conn_key_equal,
[4e00f87]640 .equal = NULL,
641 .remove_callback = NULL
[80649a91]642};
643
[9ef495f]644static client_t *async_client_get(task_id_t client_id, bool create)
645{
646 client_t *client = NULL;
[a35b458]647
[9ef495f]648 futex_down(&async_futex);
649 ht_link_t *link = hash_table_find(&client_hash_table, &client_id);
650 if (link) {
651 client = hash_table_get_inst(link, client_t, link);
652 atomic_inc(&client->refcnt);
653 } else if (create) {
654 client = malloc(sizeof(client_t));
655 if (client) {
656 client->in_task_id = client_id;
657 client->data = async_client_data_create();
[a35b458]658
[9ef495f]659 atomic_set(&client->refcnt, 1);
660 hash_table_insert(&client_hash_table, &client->link);
661 }
662 }
[a35b458]663
[9ef495f]664 futex_up(&async_futex);
665 return client;
666}
667
668static void async_client_put(client_t *client)
669{
670 bool destroy;
[a35b458]671
[9ef495f]672 futex_down(&async_futex);
[a35b458]673
[9ef495f]674 if (atomic_predec(&client->refcnt) == 0) {
675 hash_table_remove(&client_hash_table, &client->in_task_id);
676 destroy = true;
677 } else
678 destroy = false;
[a35b458]679
[9ef495f]680 futex_up(&async_futex);
[a35b458]681
[9ef495f]682 if (destroy) {
683 if (client->data)
684 async_client_data_destroy(client->data);
[a35b458]685
[9ef495f]686 free(client);
687 }
688}
689
690/** Wrapper for client connection fibril.
691 *
692 * When a new connection arrives, a fibril with this implementing
693 * function is created.
694 *
695 * @param arg Connection structure pointer.
696 *
697 * @return Always zero.
698 *
699 */
[b7fd2a0]700static errno_t connection_fibril(void *arg)
[9ef495f]701{
702 assert(arg);
[a35b458]703
[9ef495f]704 /*
705 * Setup fibril-local connection pointer.
706 */
707 fibril_connection = (connection_t *) arg;
[a35b458]708
[9ef495f]709 /*
710 * Add our reference for the current connection in the client task
711 * tracking structure. If this is the first reference, create and
712 * hash in a new tracking structure.
713 */
[a35b458]714
[9ef495f]715 client_t *client = async_client_get(fibril_connection->in_task_id, true);
716 if (!client) {
[01c3bb4]717 ipc_answer_0(fibril_connection->chandle, ENOMEM);
[9ef495f]718 return 0;
719 }
[a35b458]720
[9ef495f]721 fibril_connection->client = client;
[a35b458]722
[9ef495f]723 /*
724 * Call the connection handler function.
725 */
[01c3bb4]726 fibril_connection->handler(fibril_connection->chandle,
[9ef495f]727 &fibril_connection->call, fibril_connection->data);
[a35b458]728
[9ef495f]729 /*
730 * Remove the reference for this client task connection.
731 */
732 async_client_put(client);
[a35b458]733
[9ef495f]734 /*
735 * Remove myself from the connection hash table.
736 */
737 futex_down(&async_futex);
[853802e]738 hash_table_remove(&conn_hash_table, &(conn_key_t){
739 .task_id = fibril_connection->in_task_id,
740 .phone_hash = fibril_connection->in_phone_hash
741 });
[9ef495f]742 futex_up(&async_futex);
[a35b458]743
[9ef495f]744 /*
745 * Answer all remaining messages with EHANGUP.
746 */
747 while (!list_empty(&fibril_connection->msg_queue)) {
748 msg_t *msg =
749 list_get_instance(list_first(&fibril_connection->msg_queue),
750 msg_t, link);
[a35b458]751
[9ef495f]752 list_remove(&msg->link);
[01c3bb4]753 ipc_answer_0(msg->chandle, EHANGUP);
[9ef495f]754 free(msg);
755 }
[a35b458]756
[9ef495f]757 /*
758 * If the connection was hung-up, answer the last call,
759 * i.e. IPC_M_PHONE_HUNGUP.
760 */
[01c3bb4]761 if (fibril_connection->close_chandle)
762 ipc_answer_0(fibril_connection->close_chandle, EOK);
[a35b458]763
[9ef495f]764 free(fibril_connection);
[d5c1051]765 return EOK;
[9ef495f]766}
767
768/** Create a new fibril for a new connection.
769 *
[01c3bb4]770 * Create new fibril for connection, fill in connection structures and insert it
771 * into the hash table, so that later we can easily do routing of messages to
772 * particular fibrils.
[9ef495f]773 *
[01c3bb4]774 * @param in_task_id Identification of the incoming connection.
775 * @param in_phone_hash Identification of the incoming connection.
776 * @param chandle Handle of the opening IPC_M_CONNECT_ME_TO call.
777 * If chandle is CAP_NIL, the connection was opened by
778 * accepting the IPC_M_CONNECT_TO_ME call and this
779 * function is called directly by the server.
780 * @param call Call data of the opening call.
781 * @param handler Connection handler.
782 * @param data Client argument to pass to the connection handler.
[9ef495f]783 *
[01c3bb4]784 * @return New fibril id or NULL on failure.
[9ef495f]785 *
786 */
787static fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
[eadaeae8]788 cap_call_handle_t chandle, ipc_call_t *call, async_port_handler_t handler,
[9ef495f]789 void *data)
790{
791 connection_t *conn = malloc(sizeof(*conn));
792 if (!conn) {
[01c3bb4]793 if (chandle != CAP_NIL)
794 ipc_answer_0(chandle, ENOMEM);
[a35b458]795
[9ef495f]796 return (uintptr_t) NULL;
797 }
[a35b458]798
[9ef495f]799 conn->in_task_id = in_task_id;
800 conn->in_phone_hash = in_phone_hash;
801 list_initialize(&conn->msg_queue);
[01c3bb4]802 conn->chandle = chandle;
803 conn->close_chandle = CAP_NIL;
[9ef495f]804 conn->handler = handler;
805 conn->data = data;
[a35b458]806
[9ef495f]807 if (call)
808 conn->call = *call;
[a35b458]809
[9ef495f]810 /* We will activate the fibril ASAP */
811 conn->wdata.active = true;
812 conn->wdata.fid = fibril_create(connection_fibril, conn);
[a35b458]813
[9ef495f]814 if (conn->wdata.fid == 0) {
815 free(conn);
[a35b458]816
[01c3bb4]817 if (chandle != CAP_NIL)
818 ipc_answer_0(chandle, ENOMEM);
[a35b458]819
[9ef495f]820 return (uintptr_t) NULL;
821 }
[a35b458]822
[9ef495f]823 /* Add connection to the connection hash table */
[a35b458]824
[9ef495f]825 futex_down(&async_futex);
826 hash_table_insert(&conn_hash_table, &conn->link);
827 futex_up(&async_futex);
[a35b458]828
[9ef495f]829 fibril_add_ready(conn->wdata.fid);
[a35b458]830
[9ef495f]831 return conn->wdata.fid;
832}
833
[78bb04b]834/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
835 *
836 * Ask through phone for a new connection to some service.
837 *
838 * @param exch Exchange for sending the message.
839 * @param iface Callback interface.
840 * @param arg1 User defined argument.
841 * @param arg2 User defined argument.
842 * @param handler Callback handler.
843 * @param data Handler data.
844 * @param port_id ID of the newly created port.
845 *
[cde999a]846 * @return Zero on success or an error code.
[78bb04b]847 *
848 */
[b7fd2a0]849errno_t async_create_callback_port(async_exch_t *exch, iface_t iface, sysarg_t arg1,
[78bb04b]850 sysarg_t arg2, async_port_handler_t handler, void *data, port_id_t *port_id)
851{
852 if ((iface & IFACE_MOD_CALLBACK) != IFACE_MOD_CALLBACK)
853 return EINVAL;
[a35b458]854
[78bb04b]855 if (exch == NULL)
856 return ENOENT;
[a35b458]857
[78bb04b]858 ipc_call_t answer;
859 aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, iface, arg1, arg2,
860 &answer);
[a35b458]861
[b7fd2a0]862 errno_t ret;
[78bb04b]863 async_wait_for(req, &ret);
864 if (ret != EOK)
[b7fd2a0]865 return (errno_t) ret;
[a35b458]866
[78bb04b]867 sysarg_t phone_hash = IPC_GET_ARG5(answer);
868 interface_t *interface;
[a35b458]869
[78bb04b]870 futex_down(&async_futex);
[a35b458]871
[78bb04b]872 ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
873 if (link)
874 interface = hash_table_get_inst(link, interface_t, link);
875 else
876 interface = async_new_interface(iface);
[a35b458]877
[78bb04b]878 if (!interface) {
879 futex_up(&async_futex);
880 return ENOMEM;
881 }
[a35b458]882
[78bb04b]883 port_t *port = async_new_port(interface, handler, data);
884 if (!port) {
885 futex_up(&async_futex);
886 return ENOMEM;
887 }
[a35b458]888
[78bb04b]889 *port_id = port->id;
[a35b458]890
[78bb04b]891 futex_up(&async_futex);
[a35b458]892
[78bb04b]893 fid_t fid = async_new_connection(answer.in_task_id, phone_hash,
[01c3bb4]894 CAP_NIL, NULL, handler, data);
[78bb04b]895 if (fid == (uintptr_t) NULL)
896 return ENOMEM;
[a35b458]897
[78bb04b]898 return EOK;
899}
900
[8820544]901static size_t notification_key_hash(void *key)
902{
903 sysarg_t id = *(sysarg_t *) key;
904 return id;
905}
906
907static size_t notification_hash(const ht_link_t *item)
908{
909 notification_t *notification =
910 hash_table_get_inst(item, notification_t, link);
911 return notification_key_hash(&notification->imethod);
912}
913
914static bool notification_key_equal(void *key, const ht_link_t *item)
915{
916 sysarg_t id = *(sysarg_t *) key;
917 notification_t *notification =
918 hash_table_get_inst(item, notification_t, link);
919 return id == notification->imethod;
920}
921
922/** Operations for the notification hash table. */
923static hash_table_ops_t notification_hash_table_ops = {
924 .hash = notification_hash,
925 .key_hash = notification_key_hash,
926 .key_equal = notification_key_equal,
927 .equal = NULL,
928 .remove_callback = NULL
929};
930
[e70bfa5]931/** Sort in current fibril's timeout request.
[49d072e]932 *
[c07544d3]933 * @param wd Wait data of the current fibril.
934 *
[49d072e]935 */
[b6ee5b1]936void async_insert_timeout(awaiter_t *wd)
[49d072e]937{
[79ae36dd]938 assert(wd);
[a35b458]939
[f53cc81]940 wd->to_event.occurred = false;
941 wd->to_event.inlist = true;
[a35b458]942
[b72efe8]943 link_t *tmp = timeout_list.head.next;
944 while (tmp != &timeout_list.head) {
[3bacee1]945 awaiter_t *cur =
946 list_get_instance(tmp, awaiter_t, to_event.link);
[a35b458]947
[f53cc81]948 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
[49d072e]949 break;
[a35b458]950
[49d072e]951 tmp = tmp->next;
952 }
[a35b458]953
[b72efe8]954 list_insert_before(&wd->to_event.link, tmp);
[49d072e]955}
956
[e70bfa5]957/** Try to route a call to an appropriate connection fibril.
[80649a91]958 *
[36c9234]959 * If the proper connection fibril is found, a message with the call is added to
960 * its message queue. If the fibril was not active, it is activated and all
961 * timeouts are unregistered.
962 *
[01c3bb4]963 * @param chandle Handle of the incoming call.
964 * @param call Data of the incoming call.
[c07544d3]965 *
966 * @return False if the call doesn't match any connection.
[47b7006]967 * @return True if the call was passed to the respective connection fibril.
[36c9234]968 *
[80649a91]969 */
[eadaeae8]970static bool route_call(cap_call_handle_t chandle, ipc_call_t *call)
[450cd3a]971{
[79ae36dd]972 assert(call);
[a35b458]973
[01ff41c]974 futex_down(&async_futex);
[a35b458]975
[853802e]976 ht_link_t *link = hash_table_find(&conn_hash_table, &(conn_key_t){
977 .task_id = call->in_task_id,
978 .phone_hash = call->in_phone_hash
979 });
[8820544]980 if (!link) {
[01ff41c]981 futex_up(&async_futex);
[c07544d3]982 return false;
[450cd3a]983 }
[a35b458]984
[8820544]985 connection_t *conn = hash_table_get_inst(link, connection_t, link);
[a35b458]986
[c07544d3]987 msg_t *msg = malloc(sizeof(*msg));
988 if (!msg) {
989 futex_up(&async_futex);
990 return false;
991 }
[a35b458]992
[01c3bb4]993 msg->chandle = chandle;
[80649a91]994 msg->call = *call;
995 list_append(&msg->link, &conn->msg_queue);
[a35b458]996
[228e490]997 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
[01c3bb4]998 conn->close_chandle = chandle;
[a35b458]999
[36c9234]1000 /* If the connection fibril is waiting for an event, activate it */
[49d072e]1001 if (!conn->wdata.active) {
[a35b458]1002
[49d072e]1003 /* If in timeout list, remove it */
[f53cc81]1004 if (conn->wdata.to_event.inlist) {
1005 conn->wdata.to_event.inlist = false;
1006 list_remove(&conn->wdata.to_event.link);
[49d072e]1007 }
[a35b458]1008
[c07544d3]1009 conn->wdata.active = true;
[bc1f1c2]1010 fibril_add_ready(conn->wdata.fid);
[80649a91]1011 }
[a35b458]1012
[01ff41c]1013 futex_up(&async_futex);
[c07544d3]1014 return true;
1015}
[80649a91]1016
[c170438]1017/** Process notification.
[c07544d3]1018 *
[c170438]1019 * @param call Data of the incoming call.
[58563585]1020 *
[c07544d3]1021 */
[01c3bb4]1022static void process_notification(ipc_call_t *call)
[c07544d3]1023{
[8820544]1024 async_notification_handler_t handler = NULL;
1025 void *data = NULL;
[c170438]1026
1027 assert(call);
[a35b458]1028
[8820544]1029 futex_down(&async_futex);
[a35b458]1030
[8820544]1031 ht_link_t *link = hash_table_find(&notification_hash_table,
[c170438]1032 &IPC_GET_IMETHOD(*call));
[8820544]1033 if (link) {
1034 notification_t *notification =
1035 hash_table_get_inst(link, notification_t, link);
1036 handler = notification->handler;
1037 data = notification->data;
1038 }
[a35b458]1039
[8820544]1040 futex_up(&async_futex);
[a35b458]1041
[8820544]1042 if (handler)
[01c3bb4]1043 handler(call, data);
[80649a91]1044}
1045
[8820544]1046/** Subscribe to IRQ notification.
1047 *
1048 * @param inr IRQ number.
1049 * @param handler Notification handler.
1050 * @param data Notification handler client data.
1051 * @param ucode Top-half pseudocode handler.
1052 *
[071a1ddb]1053 * @param[out] handle IRQ capability handle on success.
1054 *
[cde999a]1055 * @return An error code.
[8820544]1056 *
1057 */
[b7fd2a0]1058errno_t async_irq_subscribe(int inr, async_notification_handler_t handler,
[eadaeae8]1059 void *data, const irq_code_t *ucode, cap_irq_handle_t *handle)
[8820544]1060{
1061 notification_t *notification =
1062 (notification_t *) malloc(sizeof(notification_t));
1063 if (!notification)
1064 return ENOMEM;
[a35b458]1065
[8820544]1066 futex_down(&async_futex);
[a35b458]1067
[8820544]1068 sysarg_t imethod = notification_avail;
1069 notification_avail++;
[a35b458]1070
[8820544]1071 notification->imethod = imethod;
1072 notification->handler = handler;
1073 notification->data = data;
[a35b458]1074
[8820544]1075 hash_table_insert(&notification_hash_table, &notification->link);
[a35b458]1076
[8820544]1077 futex_up(&async_futex);
[a35b458]1078
[eadaeae8]1079 cap_irq_handle_t ihandle;
1080 errno_t rc = ipc_irq_subscribe(inr, imethod, ucode, &ihandle);
[071a1ddb]1081 if (rc == EOK && handle != NULL) {
[eadaeae8]1082 *handle = ihandle;
[9233e9d]1083 }
[071a1ddb]1084 return rc;
[8820544]1085}
1086
1087/** Unsubscribe from IRQ notification.
1088 *
[eadaeae8]1089 * @param handle IRQ capability handle.
[8820544]1090 *
[cde999a]1091 * @return Zero on success or an error code.
[8820544]1092 *
1093 */
[eadaeae8]1094errno_t async_irq_unsubscribe(cap_irq_handle_t ihandle)
[8820544]1095{
1096 // TODO: Remove entry from hash table
1097 // to avoid memory leak
[a35b458]1098
[eadaeae8]1099 return ipc_irq_unsubscribe(ihandle);
[8820544]1100}
1101
1102/** Subscribe to event notifications.
1103 *
1104 * @param evno Event type to subscribe.
1105 * @param handler Notification handler.
1106 * @param data Notification handler client data.
1107 *
[cde999a]1108 * @return Zero on success or an error code.
[8820544]1109 *
1110 */
[b7fd2a0]1111errno_t async_event_subscribe(event_type_t evno,
[8820544]1112 async_notification_handler_t handler, void *data)
1113{
1114 notification_t *notification =
1115 (notification_t *) malloc(sizeof(notification_t));
1116 if (!notification)
1117 return ENOMEM;
[a35b458]1118
[8820544]1119 futex_down(&async_futex);
[a35b458]1120
[8820544]1121 sysarg_t imethod = notification_avail;
1122 notification_avail++;
[a35b458]1123
[8820544]1124 notification->imethod = imethod;
1125 notification->handler = handler;
1126 notification->data = data;
[a35b458]1127
[8820544]1128 hash_table_insert(&notification_hash_table, &notification->link);
[a35b458]1129
[8820544]1130 futex_up(&async_futex);
[a35b458]1131
[8820544]1132 return ipc_event_subscribe(evno, imethod);
1133}
1134
1135/** Subscribe to task event notifications.
1136 *
1137 * @param evno Event type to subscribe.
1138 * @param handler Notification handler.
1139 * @param data Notification handler client data.
1140 *
[cde999a]1141 * @return Zero on success or an error code.
[8820544]1142 *
1143 */
[b7fd2a0]1144errno_t async_event_task_subscribe(event_task_type_t evno,
[8820544]1145 async_notification_handler_t handler, void *data)
1146{
1147 notification_t *notification =
1148 (notification_t *) malloc(sizeof(notification_t));
1149 if (!notification)
1150 return ENOMEM;
[a35b458]1151
[8820544]1152 futex_down(&async_futex);
[a35b458]1153
[8820544]1154 sysarg_t imethod = notification_avail;
1155 notification_avail++;
[a35b458]1156
[8820544]1157 notification->imethod = imethod;
1158 notification->handler = handler;
1159 notification->data = data;
[a35b458]1160
[8820544]1161 hash_table_insert(&notification_hash_table, &notification->link);
[a35b458]1162
[8820544]1163 futex_up(&async_futex);
[a35b458]1164
[8820544]1165 return ipc_event_task_subscribe(evno, imethod);
1166}
1167
1168/** Unmask event notifications.
1169 *
1170 * @param evno Event type to unmask.
1171 *
1172 * @return Value returned by the kernel.
1173 *
1174 */
[b7fd2a0]1175errno_t async_event_unmask(event_type_t evno)
[8820544]1176{
1177 return ipc_event_unmask(evno);
1178}
1179
1180/** Unmask task event notifications.
1181 *
1182 * @param evno Event type to unmask.
1183 *
1184 * @return Value returned by the kernel.
1185 *
1186 */
[b7fd2a0]1187errno_t async_event_task_unmask(event_task_type_t evno)
[8820544]1188{
1189 return ipc_event_task_unmask(evno);
1190}
1191
[e70bfa5]1192/** Return new incoming message for the current (fibril-local) connection.
1193 *
[01c3bb4]1194 * @param call Storage where the incoming call data will be stored.
1195 * @param usecs Timeout in microseconds. Zero denotes no timeout.
[e70bfa5]1196 *
[01c3bb4]1197 * @return If no timeout was specified, then a handle of the incoming call is
1198 * returned. If a timeout is specified, then a handle of the incoming
1199 * call is returned unless the timeout expires prior to receiving a
1200 * message. In that case zero CAP_NIL is returned.
[e70bfa5]1201 */
[eadaeae8]1202cap_call_handle_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
[80649a91]1203{
[79ae36dd]1204 assert(call);
1205 assert(fibril_connection);
[a35b458]1206
[7c3fb9b]1207 /*
1208 * Why doing this?
[79ae36dd]1209 * GCC 4.1.0 coughs on fibril_connection-> dereference.
[6c46350]1210 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
[c07544d3]1211 * I would never expect to find so many errors in
1212 * a compiler.
[6c46350]1213 */
[79ae36dd]1214 connection_t *conn = fibril_connection;
[a35b458]1215
[01ff41c]1216 futex_down(&async_futex);
[a35b458]1217
[49d072e]1218 if (usecs) {
[45cbcaf4]1219 getuptime(&conn->wdata.to_event.expires);
[7f9d97f3]1220 tv_add_diff(&conn->wdata.to_event.expires, usecs);
[c07544d3]1221 } else
[f53cc81]1222 conn->wdata.to_event.inlist = false;
[a35b458]1223
[e70bfa5]1224 /* If nothing in queue, wait until something arrives */
[6c46350]1225 while (list_empty(&conn->msg_queue)) {
[01c3bb4]1226 if (conn->close_chandle) {
[8c8f8d6]1227 /*
1228 * Handle the case when the connection was already
1229 * closed by the client but the server did not notice
1230 * the first IPC_M_PHONE_HUNGUP call and continues to
1231 * call async_get_call_timeout(). Repeat
[47b7006]1232 * IPC_M_PHONE_HUNGUP until the caller notices.
[8c8f8d6]1233 */
1234 memset(call, 0, sizeof(ipc_call_t));
[228e490]1235 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
[8c8f8d6]1236 futex_up(&async_futex);
[01c3bb4]1237 return conn->close_chandle;
[8c8f8d6]1238 }
[a35b458]1239
[085bd54]1240 if (usecs)
[b6ee5b1]1241 async_insert_timeout(&conn->wdata);
[a35b458]1242
[c07544d3]1243 conn->wdata.active = false;
[a35b458]1244
[c7509e5]1245 /*
1246 * Note: the current fibril will be rescheduled either due to a
1247 * timeout or due to an arriving message destined to it. In the
1248 * former case, handle_expired_timeouts() and, in the latter
1249 * case, route_call() will perform the wakeup.
1250 */
[116d3f6f]1251 fibril_switch(FIBRIL_TO_MANAGER);
[a35b458]1252
[e70bfa5]1253 /*
[c07544d3]1254 * Futex is up after getting back from async_manager.
1255 * Get it again.
[c7509e5]1256 */
[49d072e]1257 futex_down(&async_futex);
[3bacee1]1258 if ((usecs) && (conn->wdata.to_event.occurred) &&
1259 (list_empty(&conn->msg_queue))) {
[e70bfa5]1260 /* If we timed out -> exit */
[49d072e]1261 futex_up(&async_futex);
[01c3bb4]1262 return CAP_NIL;
[49d072e]1263 }
[450cd3a]1264 }
[a35b458]1265
[57dea62]1266 msg_t *msg = list_get_instance(list_first(&conn->msg_queue),
1267 msg_t, link);
[80649a91]1268 list_remove(&msg->link);
[a35b458]1269
[eadaeae8]1270 cap_call_handle_t chandle = msg->chandle;
[80649a91]1271 *call = msg->call;
1272 free(msg);
[a35b458]1273
[01ff41c]1274 futex_up(&async_futex);
[01c3bb4]1275 return chandle;
[80649a91]1276}
1277
[455f190]1278void *async_get_client_data(void)
1279{
1280 assert(fibril_connection);
1281 return fibril_connection->client->data;
1282}
1283
[e2ab36f1]1284void *async_get_client_data_by_id(task_id_t client_id)
[455f190]1285{
[e2ab36f1]1286 client_t *client = async_client_get(client_id, false);
[455f190]1287 if (!client)
1288 return NULL;
[a35b458]1289
[455f190]1290 if (!client->data) {
1291 async_client_put(client);
1292 return NULL;
1293 }
[a35b458]1294
[455f190]1295 return client->data;
1296}
1297
[e2ab36f1]1298void async_put_client_data_by_id(task_id_t client_id)
[455f190]1299{
[e2ab36f1]1300 client_t *client = async_client_get(client_id, false);
[a35b458]1301
[455f190]1302 assert(client);
1303 assert(client->data);
[a35b458]1304
[cdc8ee2d]1305 /* Drop the reference we got in async_get_client_data_by_hash(). */
1306 async_client_put(client);
[a35b458]1307
[cdc8ee2d]1308 /* Drop our own reference we got at the beginning of this function. */
[455f190]1309 async_client_put(client);
1310}
1311
[566992e1]1312static port_t *async_find_port(iface_t iface, port_id_t port_id)
1313{
1314 port_t *port = NULL;
[a35b458]1315
[566992e1]1316 futex_down(&async_futex);
[a35b458]1317
[566992e1]1318 ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
1319 if (link) {
1320 interface_t *interface =
1321 hash_table_get_inst(link, interface_t, link);
[a35b458]1322
[566992e1]1323 link = hash_table_find(&interface->port_hash_table, &port_id);
1324 if (link)
1325 port = hash_table_get_inst(link, port_t, link);
1326 }
[a35b458]1327
[566992e1]1328 futex_up(&async_futex);
[a35b458]1329
[566992e1]1330 return port;
1331}
1332
[36c9234]1333/** Handle a call that was received.
1334 *
1335 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
1336 * Otherwise the call is routed to its connection fibril.
1337 *
[01c3bb4]1338 * @param chandle Handle of the incoming call.
1339 * @param call Data of the incoming call.
[6b21292]1340 *
[36c9234]1341 */
[eadaeae8]1342static void handle_call(cap_call_handle_t chandle, ipc_call_t *call)
[80649a91]1343{
[79ae36dd]1344 assert(call);
[a35b458]1345
[b688fd8]1346 /* Kernel notification */
[addbce4]1347 if ((chandle == CAP_NIL) && (call->flags & IPC_CALL_NOTIF)) {
[c170438]1348 fibril_t *fibril = (fibril_t *) __tcb_get()->fibril_data;
1349 unsigned oldsw = fibril->switches;
[a35b458]1350
[01c3bb4]1351 process_notification(call);
[a35b458]1352
[c170438]1353 if (oldsw != fibril->switches) {
1354 /*
1355 * The notification handler did not execute atomically
1356 * and so the current manager fibril assumed the role of
1357 * a notification fibril. While waiting for its
1358 * resources, it switched to another manager fibril that
1359 * had already existed or it created a new one. We
1360 * therefore know there is at least yet another
1361 * manager fibril that can take over. We now kill the
1362 * current 'notification' fibril to prevent fibril
1363 * population explosion.
1364 */
1365 futex_down(&async_futex);
1366 fibril_switch(FIBRIL_FROM_DEAD);
1367 }
[a35b458]1368
[47b7006]1369 return;
[6b21292]1370 }
[a35b458]1371
[566992e1]1372 /* New connection */
1373 if (IPC_GET_IMETHOD(*call) == IPC_M_CONNECT_ME_TO) {
1374 iface_t iface = (iface_t) IPC_GET_ARG1(*call);
1375 sysarg_t in_phone_hash = IPC_GET_ARG5(*call);
[a35b458]1376
[01c3bb4]1377 async_port_handler_t handler = fallback_port_handler;
[566992e1]1378 void *data = fallback_port_data;
[a35b458]1379
[566992e1]1380 // TODO: Currently ignores all ports but the first one
1381 port_t *port = async_find_port(iface, 0);
1382 if (port) {
1383 handler = port->handler;
1384 data = port->data;
1385 }
[a35b458]1386
[01c3bb4]1387 async_new_connection(call->in_task_id, in_phone_hash, chandle,
[566992e1]1388 call, handler, data);
1389 return;
1390 }
[a35b458]1391
[36c9234]1392 /* Try to route the call through the connection hash table */
[01c3bb4]1393 if (route_call(chandle, call))
[47b7006]1394 return;
[a35b458]1395
[44c6d88d]1396 /* Unknown call from unknown phone - hang it up */
[01c3bb4]1397 ipc_answer_0(chandle, EHANGUP);
[450cd3a]1398}
1399
[f2f0392]1400/** Fire all timeouts that expired. */
[c042bdd]1401static void handle_expired_timeouts(void)
1402{
1403 struct timeval tv;
[45cbcaf4]1404 getuptime(&tv);
[a35b458]1405
[c042bdd]1406 futex_down(&async_futex);
[a35b458]1407
[b72efe8]1408 link_t *cur = list_first(&timeout_list);
1409 while (cur != NULL) {
[47b7006]1410 awaiter_t *waiter =
1411 list_get_instance(cur, awaiter_t, to_event.link);
[a35b458]1412
[f53cc81]1413 if (tv_gt(&waiter->to_event.expires, &tv))
[c042bdd]1414 break;
[a35b458]1415
[f53cc81]1416 list_remove(&waiter->to_event.link);
1417 waiter->to_event.inlist = false;
1418 waiter->to_event.occurred = true;
[a35b458]1419
[36c9234]1420 /*
[c07544d3]1421 * Redundant condition?
1422 * The fibril should not be active when it gets here.
[c042bdd]1423 */
[49d072e]1424 if (!waiter->active) {
[c07544d3]1425 waiter->active = true;
[bc1f1c2]1426 fibril_add_ready(waiter->fid);
[c042bdd]1427 }
[a35b458]1428
[b72efe8]1429 cur = list_first(&timeout_list);
[c042bdd]1430 }
[a35b458]1431
[c042bdd]1432 futex_up(&async_futex);
1433}
1434
[36c9234]1435/** Endless loop dispatching incoming calls and answers.
1436 *
[c07544d3]1437 * @return Never returns.
1438 *
[36c9234]1439 */
[b7fd2a0]1440static errno_t async_manager_worker(void)
[80649a91]1441{
[c07544d3]1442 while (true) {
[116d3f6f]1443 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
[47b7006]1444 futex_up(&async_futex);
[36c9234]1445 /*
1446 * async_futex is always held when entering a manager
1447 * fibril.
[a46da63]1448 */
[80649a91]1449 continue;
1450 }
[a35b458]1451
[c042bdd]1452 futex_down(&async_futex);
[a35b458]1453
[c07544d3]1454 suseconds_t timeout;
[1db6dfd]1455 unsigned int flags = SYNCH_FLAGS_NONE;
[c042bdd]1456 if (!list_empty(&timeout_list)) {
[b72efe8]1457 awaiter_t *waiter = list_get_instance(
1458 list_first(&timeout_list), awaiter_t, to_event.link);
[a35b458]1459
[c07544d3]1460 struct timeval tv;
[45cbcaf4]1461 getuptime(&tv);
[a35b458]1462
[f53cc81]1463 if (tv_gteq(&tv, &waiter->to_event.expires)) {
[6c46350]1464 futex_up(&async_futex);
[c042bdd]1465 handle_expired_timeouts();
[1db6dfd]1466 /*
1467 * Notice that even if the event(s) already
1468 * expired (and thus the other fibril was
1469 * supposed to be running already),
1470 * we check for incoming IPC.
1471 *
1472 * Otherwise, a fibril that continuously
1473 * creates (almost) expired events could
1474 * prevent IPC retrieval from the kernel.
1475 */
1476 timeout = 0;
1477 flags = SYNCH_FLAGS_NON_BLOCKING;
1478
1479 } else {
[7f9d97f3]1480 timeout = tv_sub_diff(&waiter->to_event.expires,
1481 &tv);
[1db6dfd]1482 futex_up(&async_futex);
1483 }
1484 } else {
1485 futex_up(&async_futex);
[0b99e40]1486 timeout = SYNCH_NO_TIMEOUT;
[1db6dfd]1487 }
[a35b458]1488
[8619f25]1489 atomic_inc(&threads_in_ipc_wait);
[a35b458]1490
[c07544d3]1491 ipc_call_t call;
[b7fd2a0]1492 errno_t rc = ipc_wait_cycle(&call, timeout, flags);
[a35b458]1493
[8619f25]1494 atomic_dec(&threads_in_ipc_wait);
[a35b458]1495
[6deb2cd]1496 assert(rc == EOK);
[01c3bb4]1497
[6deb2cd]1498 if (call.cap_handle == CAP_NIL) {
[a1026da]1499 if ((call.flags &
1500 (IPC_CALL_NOTIF | IPC_CALL_ANSWERED)) == 0) {
1501 /* Neither a notification nor an answer. */
[01c3bb4]1502 handle_expired_timeouts();
1503 continue;
1504 }
[0b99e40]1505 }
[01c3bb4]1506
[addbce4]1507 if (call.flags & IPC_CALL_ANSWERED)
[80649a91]1508 continue;
[01c3bb4]1509
[6deb2cd]1510 handle_call(call.cap_handle, &call);
[80649a91]1511 }
[01c3bb4]1512
[a46da63]1513 return 0;
[80649a91]1514}
1515
[36c9234]1516/** Function to start async_manager as a standalone fibril.
[c07544d3]1517 *
[36c9234]1518 * When more kernel threads are used, one async manager should exist per thread.
1519 *
[c07544d3]1520 * @param arg Unused.
1521 * @return Never returns.
[36c9234]1522 *
[a2cd194]1523 */
[b7fd2a0]1524static errno_t async_manager_fibril(void *arg)
[80649a91]1525{
[a46da63]1526 futex_up(&async_futex);
[a35b458]1527
[36c9234]1528 /*
1529 * async_futex is always locked when entering manager
1530 */
[085bd54]1531 async_manager_worker();
[a35b458]1532
[a46da63]1533 return 0;
[80649a91]1534}
[450cd3a]1535
[36c9234]1536/** Add one manager to manager list. */
[80649a91]1537void async_create_manager(void)
[450cd3a]1538{
[c170438]1539 fid_t fid = fibril_create_generic(async_manager_fibril, NULL, PAGE_SIZE);
[86d7bfa]1540 if (fid != 0)
1541 fibril_add_manager(fid);
[80649a91]1542}
1543
1544/** Remove one manager from manager list */
1545void async_destroy_manager(void)
1546{
[bc1f1c2]1547 fibril_remove_manager();
[80649a91]1548}
1549
[36c9234]1550/** Initialize the async framework.
1551 *
1552 */
[47b7006]1553void __async_init(void)
[80649a91]1554{
[566992e1]1555 if (!hash_table_create(&interface_hash_table, 0, 0,
1556 &interface_hash_table_ops))
1557 abort();
[a35b458]1558
[062d900]1559 if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
[47b7006]1560 abort();
[a35b458]1561
[062d900]1562 if (!hash_table_create(&conn_hash_table, 0, 0, &conn_hash_table_ops))
[47b7006]1563 abort();
[a35b458]1564
[8820544]1565 if (!hash_table_create(&notification_hash_table, 0, 0,
1566 &notification_hash_table_ops))
1567 abort();
[a35b458]1568
[79ae36dd]1569 session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
1570 if (session_ns == NULL)
1571 abort();
[a35b458]1572
[566992e1]1573 session_ns->iface = 0;
[79ae36dd]1574 session_ns->mgmt = EXCHANGE_ATOMIC;
1575 session_ns->phone = PHONE_NS;
1576 session_ns->arg1 = 0;
1577 session_ns->arg2 = 0;
1578 session_ns->arg3 = 0;
[a35b458]1579
[58cbf8d5]1580 fibril_mutex_initialize(&session_ns->remote_state_mtx);
1581 session_ns->remote_state_data = NULL;
[a35b458]1582
[79ae36dd]1583 list_initialize(&session_ns->exch_list);
1584 fibril_mutex_initialize(&session_ns->mutex);
1585 atomic_set(&session_ns->refcnt, 0);
[450cd3a]1586}
[01ff41c]1587
[36c9234]1588/** Reply received callback.
[01ff41c]1589 *
[36c9234]1590 * This function is called whenever a reply for an asynchronous message sent out
1591 * by the asynchronous framework is received.
1592 *
1593 * Notify the fibril which is waiting for this message that it has arrived.
1594 *
[c07544d3]1595 * @param arg Pointer to the asynchronous message record.
1596 * @param retval Value returned in the answer.
1597 * @param data Call data of the answer.
[47b7006]1598 *
[01ff41c]1599 */
[b7fd2a0]1600void reply_received(void *arg, errno_t retval, ipc_call_t *data)
[01ff41c]1601{
[79ae36dd]1602 assert(arg);
[a35b458]1603
[9db9b10]1604 futex_down(&async_futex);
[a35b458]1605
[c07544d3]1606 amsg_t *msg = (amsg_t *) arg;
[01ff41c]1607 msg->retval = retval;
[a35b458]1608
[36c9234]1609 /* Copy data after futex_down, just in case the call was detached */
[9db9b10]1610 if ((msg->dataptr) && (data))
[c07544d3]1611 *msg->dataptr = *data;
[a35b458]1612
[c042bdd]1613 write_barrier();
[a35b458]1614
[c042bdd]1615 /* Remove message from timeout list */
[f53cc81]1616 if (msg->wdata.to_event.inlist)
1617 list_remove(&msg->wdata.to_event.link);
[a35b458]1618
[c07544d3]1619 msg->done = true;
[a35b458]1620
[47c9a8c]1621 if (msg->forget) {
1622 assert(msg->wdata.active);
1623 amsg_destroy(msg);
1624 } else if (!msg->wdata.active) {
[c07544d3]1625 msg->wdata.active = true;
[bc1f1c2]1626 fibril_add_ready(msg->wdata.fid);
[01ff41c]1627 }
[a35b458]1628
[01ff41c]1629 futex_up(&async_futex);
1630}
1631
[36c9234]1632/** Send message and return id of the sent message.
1633 *
1634 * The return value can be used as input for async_wait() to wait for
1635 * completion.
[01ff41c]1636 *
[79ae36dd]1637 * @param exch Exchange for sending the message.
1638 * @param imethod Service-defined interface and method.
[c07544d3]1639 * @param arg1 Service-defined payload argument.
1640 * @param arg2 Service-defined payload argument.
1641 * @param arg3 Service-defined payload argument.
1642 * @param arg4 Service-defined payload argument.
[01c3bb4]1643 * @param dataptr If non-NULL, storage where the reply data will be stored.
[c07544d3]1644 *
1645 * @return Hash of the sent message or 0 on error.
[36c9234]1646 *
[01ff41c]1647 */
[79ae36dd]1648aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1649 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[01ff41c]1650{
[79ae36dd]1651 if (exch == NULL)
1652 return 0;
[a35b458]1653
[47c9a8c]1654 amsg_t *msg = amsg_create();
[79ae36dd]1655 if (msg == NULL)
[c07544d3]1656 return 0;
[a35b458]1657
[01ff41c]1658 msg->dataptr = dataptr;
[c07544d3]1659 msg->wdata.active = true;
[a35b458]1660
[79ae36dd]1661 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
[dcc150cb]1662 reply_received);
[a35b458]1663
[01ff41c]1664 return (aid_t) msg;
1665}
1666
[90f5d64]1667/** Send message and return id of the sent message
1668 *
[36c9234]1669 * The return value can be used as input for async_wait() to wait for
1670 * completion.
1671 *
[79ae36dd]1672 * @param exch Exchange for sending the message.
1673 * @param imethod Service-defined interface and method.
[c07544d3]1674 * @param arg1 Service-defined payload argument.
1675 * @param arg2 Service-defined payload argument.
1676 * @param arg3 Service-defined payload argument.
1677 * @param arg4 Service-defined payload argument.
1678 * @param arg5 Service-defined payload argument.
1679 * @param dataptr If non-NULL, storage where the reply data will be
1680 * stored.
1681 *
1682 * @return Hash of the sent message or 0 on error.
[36c9234]1683 *
[90f5d64]1684 */
[79ae36dd]1685aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1686 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[0cc4313]1687 ipc_call_t *dataptr)
[90f5d64]1688{
[79ae36dd]1689 if (exch == NULL)
1690 return 0;
[a35b458]1691
[47c9a8c]1692 amsg_t *msg = amsg_create();
[79ae36dd]1693 if (msg == NULL)
[c07544d3]1694 return 0;
[a35b458]1695
[90f5d64]1696 msg->dataptr = dataptr;
[c07544d3]1697 msg->wdata.active = true;
[a35b458]1698
[79ae36dd]1699 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
[dcc150cb]1700 msg, reply_received);
[a35b458]1701
[90f5d64]1702 return (aid_t) msg;
1703}
1704
[36c9234]1705/** Wait for a message sent by the async framework.
[01ff41c]1706 *
[c07544d3]1707 * @param amsgid Hash of the message to wait for.
1708 * @param retval Pointer to storage where the retval of the answer will
1709 * be stored.
1710 *
[01ff41c]1711 */
[b7fd2a0]1712void async_wait_for(aid_t amsgid, errno_t *retval)
[01ff41c]1713{
[79ae36dd]1714 assert(amsgid);
[a35b458]1715
[01ff41c]1716 amsg_t *msg = (amsg_t *) amsgid;
[a35b458]1717
[01ff41c]1718 futex_down(&async_futex);
[a35b458]1719
[47c9a8c]1720 assert(!msg->forget);
1721 assert(!msg->destroyed);
[a35b458]1722
[01ff41c]1723 if (msg->done) {
1724 futex_up(&async_futex);
1725 goto done;
1726 }
[a35b458]1727
[bc1f1c2]1728 msg->wdata.fid = fibril_get_id();
[c07544d3]1729 msg->wdata.active = false;
[f53cc81]1730 msg->wdata.to_event.inlist = false;
[a35b458]1731
[36c9234]1732 /* Leave the async_futex locked when entering this function */
[116d3f6f]1733 fibril_switch(FIBRIL_TO_MANAGER);
[a35b458]1734
[c07544d3]1735 /* Futex is up automatically after fibril_switch */
[a35b458]1736
[01ff41c]1737done:
1738 if (retval)
1739 *retval = msg->retval;
[a35b458]1740
[47c9a8c]1741 amsg_destroy(msg);
[01ff41c]1742}
[0b99e40]1743
[36c9234]1744/** Wait for a message sent by the async framework, timeout variant.
[47c9a8c]1745 *
1746 * If the wait times out, the caller may choose to either wait again by calling
1747 * async_wait_for() or async_wait_timeout(), or forget the message via
1748 * async_forget().
[c042bdd]1749 *
[c07544d3]1750 * @param amsgid Hash of the message to wait for.
1751 * @param retval Pointer to storage where the retval of the answer will
1752 * be stored.
1753 * @param timeout Timeout in microseconds.
1754 *
1755 * @return Zero on success, ETIMEOUT if the timeout has expired.
[c042bdd]1756 *
1757 */
[b7fd2a0]1758errno_t async_wait_timeout(aid_t amsgid, errno_t *retval, suseconds_t timeout)
[c042bdd]1759{
[79ae36dd]1760 assert(amsgid);
[a35b458]1761
[c042bdd]1762 amsg_t *msg = (amsg_t *) amsgid;
[a35b458]1763
[c042bdd]1764 futex_down(&async_futex);
[a35b458]1765
[47c9a8c]1766 assert(!msg->forget);
1767 assert(!msg->destroyed);
[a35b458]1768
[c042bdd]1769 if (msg->done) {
1770 futex_up(&async_futex);
1771 goto done;
1772 }
[a35b458]1773
[1db6dfd]1774 /*
1775 * Negative timeout is converted to zero timeout to avoid
1776 * using tv_add with negative augmenter.
1777 */
1778 if (timeout < 0)
1779 timeout = 0;
[a35b458]1780
[45cbcaf4]1781 getuptime(&msg->wdata.to_event.expires);
[7f9d97f3]1782 tv_add_diff(&msg->wdata.to_event.expires, timeout);
[a35b458]1783
[1db6dfd]1784 /*
1785 * Current fibril is inserted as waiting regardless of the
1786 * "size" of the timeout.
1787 *
1788 * Checking for msg->done and immediately bailing out when
1789 * timeout == 0 would mean that the manager fibril would never
1790 * run (consider single threaded program).
1791 * Thus the IPC answer would be never retrieved from the kernel.
1792 *
1793 * Notice that the actual delay would be very small because we
1794 * - switch to manager fibril
1795 * - the manager sees expired timeout
1796 * - and thus adds us back to ready queue
1797 * - manager switches back to some ready fibril
1798 * (prior it, it checks for incoming IPC).
1799 *
1800 */
[bc1f1c2]1801 msg->wdata.fid = fibril_get_id();
[c07544d3]1802 msg->wdata.active = false;
[b6ee5b1]1803 async_insert_timeout(&msg->wdata);
[a35b458]1804
[36c9234]1805 /* Leave the async_futex locked when entering this function */
[116d3f6f]1806 fibril_switch(FIBRIL_TO_MANAGER);
[a35b458]1807
[c07544d3]1808 /* Futex is up automatically after fibril_switch */
[a35b458]1809
[c042bdd]1810 if (!msg->done)
1811 return ETIMEOUT;
[a35b458]1812
[c042bdd]1813done:
1814 if (retval)
1815 *retval = msg->retval;
[a35b458]1816
[47c9a8c]1817 amsg_destroy(msg);
[a35b458]1818
[c042bdd]1819 return 0;
1820}
[a35b458]1821
[47c9a8c]1822/** Discard the message / reply on arrival.
1823 *
1824 * The message will be marked to be discarded once the reply arrives in
1825 * reply_received(). It is not allowed to call async_wait_for() or
1826 * async_wait_timeout() on this message after a call to this function.
1827 *
1828 * @param amsgid Hash of the message to forget.
1829 */
1830void async_forget(aid_t amsgid)
1831{
1832 amsg_t *msg = (amsg_t *) amsgid;
[a35b458]1833
[47c9a8c]1834 assert(msg);
1835 assert(!msg->forget);
1836 assert(!msg->destroyed);
[a35b458]1837
[47c9a8c]1838 futex_down(&async_futex);
[a35b458]1839
[375e501]1840 if (msg->done) {
[47c9a8c]1841 amsg_destroy(msg);
[375e501]1842 } else {
1843 msg->dataptr = NULL;
[47c9a8c]1844 msg->forget = true;
[375e501]1845 }
[a35b458]1846
[47c9a8c]1847 futex_up(&async_futex);
1848}
[0b99e40]1849
[36c9234]1850/** Wait for specified time.
[44c6d88d]1851 *
[36c9234]1852 * The current fibril is suspended but the thread continues to execute.
1853 *
[c07544d3]1854 * @param timeout Duration of the wait in microseconds.
1855 *
[44c6d88d]1856 */
1857void async_usleep(suseconds_t timeout)
1858{
[6a5d05b]1859 awaiter_t awaiter;
1860 awaiter_initialize(&awaiter);
[a35b458]1861
[6a5d05b]1862 awaiter.fid = fibril_get_id();
[a35b458]1863
[6a5d05b]1864 getuptime(&awaiter.to_event.expires);
1865 tv_add_diff(&awaiter.to_event.expires, timeout);
[a35b458]1866
[44c6d88d]1867 futex_down(&async_futex);
[a35b458]1868
[6a5d05b]1869 async_insert_timeout(&awaiter);
[a35b458]1870
[36c9234]1871 /* Leave the async_futex locked when entering this function */
[116d3f6f]1872 fibril_switch(FIBRIL_TO_MANAGER);
[a35b458]1873
[c07544d3]1874 /* Futex is up automatically after fibril_switch() */
[44c6d88d]1875}
[da0c91e7]1876
[39026d7c]1877/** Delay execution for the specified number of seconds
1878 *
1879 * @param sec Number of seconds to sleep
1880 */
1881void async_sleep(unsigned int sec)
1882{
1883 /*
1884 * Sleep in 1000 second steps to support
1885 * full argument range
1886 */
1887
1888 while (sec > 0) {
1889 unsigned int period = (sec > 1000) ? 1000 : sec;
1890
1891 async_usleep(period * 1000000);
1892 sec -= period;
1893 }
1894}
1895
[0cc4313]1896/** Pseudo-synchronous message sending - fast version.
1897 *
1898 * Send message asynchronously and return only after the reply arrives.
1899 *
1900 * This function can only transfer 4 register payload arguments. For
1901 * transferring more arguments, see the slower async_req_slow().
1902 *
[79ae36dd]1903 * @param exch Exchange for sending the message.
1904 * @param imethod Interface and method of the call.
[c07544d3]1905 * @param arg1 Service-defined payload argument.
1906 * @param arg2 Service-defined payload argument.
1907 * @param arg3 Service-defined payload argument.
1908 * @param arg4 Service-defined payload argument.
1909 * @param r1 If non-NULL, storage for the 1st reply argument.
1910 * @param r2 If non-NULL, storage for the 2nd reply argument.
1911 * @param r3 If non-NULL, storage for the 3rd reply argument.
1912 * @param r4 If non-NULL, storage for the 4th reply argument.
1913 * @param r5 If non-NULL, storage for the 5th reply argument.
1914 *
[cde999a]1915 * @return Return code of the reply or an error code.
[c07544d3]1916 *
[0cc4313]1917 */
[b7fd2a0]1918errno_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1919 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1920 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1921{
[79ae36dd]1922 if (exch == NULL)
1923 return ENOENT;
[a35b458]1924
[0cc4313]1925 ipc_call_t result;
[79ae36dd]1926 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
[0cc4313]1927 &result);
[a35b458]1928
[b7fd2a0]1929 errno_t rc;
[79ae36dd]1930 async_wait_for(aid, &rc);
[a35b458]1931
[c07544d3]1932 if (r1)
[0cc4313]1933 *r1 = IPC_GET_ARG1(result);
[a35b458]1934
[0cc4313]1935 if (r2)
1936 *r2 = IPC_GET_ARG2(result);
[a35b458]1937
[0cc4313]1938 if (r3)
1939 *r3 = IPC_GET_ARG3(result);
[a35b458]1940
[0cc4313]1941 if (r4)
1942 *r4 = IPC_GET_ARG4(result);
[a35b458]1943
[0cc4313]1944 if (r5)
1945 *r5 = IPC_GET_ARG5(result);
[a35b458]1946
[0cc4313]1947 return rc;
[085bd54]1948}
1949
[0cc4313]1950/** Pseudo-synchronous message sending - slow version.
1951 *
1952 * Send message asynchronously and return only after the reply arrives.
1953 *
[79ae36dd]1954 * @param exch Exchange for sending the message.
1955 * @param imethod Interface and method of the call.
[c07544d3]1956 * @param arg1 Service-defined payload argument.
1957 * @param arg2 Service-defined payload argument.
1958 * @param arg3 Service-defined payload argument.
1959 * @param arg4 Service-defined payload argument.
1960 * @param arg5 Service-defined payload argument.
1961 * @param r1 If non-NULL, storage for the 1st reply argument.
1962 * @param r2 If non-NULL, storage for the 2nd reply argument.
1963 * @param r3 If non-NULL, storage for the 3rd reply argument.
1964 * @param r4 If non-NULL, storage for the 4th reply argument.
1965 * @param r5 If non-NULL, storage for the 5th reply argument.
1966 *
[cde999a]1967 * @return Return code of the reply or an error code.
[c07544d3]1968 *
[0cc4313]1969 */
[b7fd2a0]1970errno_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1971 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1972 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1973{
[79ae36dd]1974 if (exch == NULL)
1975 return ENOENT;
[a35b458]1976
[0cc4313]1977 ipc_call_t result;
[79ae36dd]1978 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
[0cc4313]1979 &result);
[a35b458]1980
[b7fd2a0]1981 errno_t rc;
[79ae36dd]1982 async_wait_for(aid, &rc);
[a35b458]1983
[c07544d3]1984 if (r1)
[0cc4313]1985 *r1 = IPC_GET_ARG1(result);
[a35b458]1986
[0cc4313]1987 if (r2)
1988 *r2 = IPC_GET_ARG2(result);
[a35b458]1989
[0cc4313]1990 if (r3)
1991 *r3 = IPC_GET_ARG3(result);
[a35b458]1992
[0cc4313]1993 if (r4)
1994 *r4 = IPC_GET_ARG4(result);
[a35b458]1995
[0cc4313]1996 if (r5)
1997 *r5 = IPC_GET_ARG5(result);
[a35b458]1998
[0cc4313]1999 return rc;
[085bd54]2000}
[b2951e2]2001
[79ae36dd]2002void async_msg_0(async_exch_t *exch, sysarg_t imethod)
[64d2b10]2003{
[79ae36dd]2004 if (exch != NULL)
[dcc150cb]2005 ipc_call_async_0(exch->phone, imethod, NULL, NULL);
[64d2b10]2006}
2007
[79ae36dd]2008void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
[64d2b10]2009{
[79ae36dd]2010 if (exch != NULL)
[dcc150cb]2011 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL);
[64d2b10]2012}
2013
[79ae36dd]2014void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2015 sysarg_t arg2)
[64d2b10]2016{
[79ae36dd]2017 if (exch != NULL)
[dcc150cb]2018 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL);
[64d2b10]2019}
2020
[79ae36dd]2021void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2022 sysarg_t arg2, sysarg_t arg3)
[64d2b10]2023{
[79ae36dd]2024 if (exch != NULL)
2025 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
[dcc150cb]2026 NULL);
[64d2b10]2027}
2028
[79ae36dd]2029void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2030 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[64d2b10]2031{
[79ae36dd]2032 if (exch != NULL)
2033 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
[dcc150cb]2034 NULL, NULL);
[64d2b10]2035}
2036
[79ae36dd]2037void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2038 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
[64d2b10]2039{
[79ae36dd]2040 if (exch != NULL)
2041 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
[dcc150cb]2042 arg5, NULL, NULL);
[64d2b10]2043}
2044
[eadaeae8]2045errno_t async_answer_0(cap_call_handle_t chandle, errno_t retval)
[64d2b10]2046{
[01c3bb4]2047 return ipc_answer_0(chandle, retval);
[64d2b10]2048}
2049
[eadaeae8]2050errno_t async_answer_1(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1)
[64d2b10]2051{
[01c3bb4]2052 return ipc_answer_1(chandle, retval, arg1);
[64d2b10]2053}
2054
[eadaeae8]2055errno_t async_answer_2(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
[64d2b10]2056 sysarg_t arg2)
2057{
[01c3bb4]2058 return ipc_answer_2(chandle, retval, arg1, arg2);
[64d2b10]2059}
2060
[eadaeae8]2061errno_t async_answer_3(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
[64d2b10]2062 sysarg_t arg2, sysarg_t arg3)
2063{
[01c3bb4]2064 return ipc_answer_3(chandle, retval, arg1, arg2, arg3);
[64d2b10]2065}
2066
[eadaeae8]2067errno_t async_answer_4(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
[64d2b10]2068 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
2069{
[01c3bb4]2070 return ipc_answer_4(chandle, retval, arg1, arg2, arg3, arg4);
[64d2b10]2071}
2072
[eadaeae8]2073errno_t async_answer_5(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
[64d2b10]2074 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
2075{
[01c3bb4]2076 return ipc_answer_5(chandle, retval, arg1, arg2, arg3, arg4, arg5);
[64d2b10]2077}
2078
[eadaeae8]2079errno_t async_forward_fast(cap_call_handle_t chandle, async_exch_t *exch,
[79ae36dd]2080 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[64d2b10]2081{
[79ae36dd]2082 if (exch == NULL)
2083 return ENOENT;
[a35b458]2084
[01c3bb4]2085 return ipc_forward_fast(chandle, exch->phone, imethod, arg1, arg2, mode);
[64d2b10]2086}
2087
[eadaeae8]2088errno_t async_forward_slow(cap_call_handle_t chandle, async_exch_t *exch,
[79ae36dd]2089 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
2090 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
[64d2b10]2091{
[79ae36dd]2092 if (exch == NULL)
2093 return ENOENT;
[a35b458]2094
[01c3bb4]2095 return ipc_forward_slow(chandle, exch->phone, imethod, arg1, arg2, arg3,
[79ae36dd]2096 arg4, arg5, mode);
[64d2b10]2097}
2098
[007e6efa]2099/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
2100 *
2101 * Ask through phone for a new connection to some service.
2102 *
[79ae36dd]2103 * @param exch Exchange for sending the message.
[007e6efa]2104 * @param arg1 User defined argument.
2105 * @param arg2 User defined argument.
2106 * @param arg3 User defined argument.
2107 *
[cde999a]2108 * @return Zero on success or an error code.
[007e6efa]2109 *
2110 */
[b7fd2a0]2111errno_t async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
[f9b2cb4c]2112 sysarg_t arg3)
[007e6efa]2113{
[79ae36dd]2114 if (exch == NULL)
2115 return ENOENT;
[a35b458]2116
[ab34cc9]2117 ipc_call_t answer;
[f9b2cb4c]2118 aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
[ab34cc9]2119 &answer);
[a35b458]2120
[b7fd2a0]2121 errno_t rc;
[ab34cc9]2122 async_wait_for(req, &rc);
[007e6efa]2123 if (rc != EOK)
[b7fd2a0]2124 return (errno_t) rc;
[a35b458]2125
[007e6efa]2126 return EOK;
2127}
2128
[eadaeae8]2129static errno_t async_connect_me_to_internal(cap_phone_handle_t phone,
2130 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2131 cap_phone_handle_t *out_phone)
[f74392f]2132{
[79ae36dd]2133 ipc_call_t result;
[a35b458]2134
[a99cbc1e]2135 // XXX: Workaround for GCC's inability to infer association between
2136 // rc == EOK and *out_phone being assigned.
[eadaeae8]2137 *out_phone = CAP_NIL;
[a35b458]2138
[47c9a8c]2139 amsg_t *msg = amsg_create();
2140 if (!msg)
[79ae36dd]2141 return ENOENT;
[a35b458]2142
[79ae36dd]2143 msg->dataptr = &result;
2144 msg->wdata.active = true;
[a35b458]2145
[79ae36dd]2146 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
[dcc150cb]2147 msg, reply_received);
[a35b458]2148
[b7fd2a0]2149 errno_t rc;
[79ae36dd]2150 async_wait_for((aid_t) msg, &rc);
[a35b458]2151
[007e6efa]2152 if (rc != EOK)
[f74392f]2153 return rc;
[a35b458]2154
[eadaeae8]2155 *out_phone = (cap_phone_handle_t) IPC_GET_ARG5(result);
[a99cbc1e]2156 return EOK;
[79ae36dd]2157}
2158
2159/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2160 *
2161 * Ask through for a new connection to some service.
2162 *
2163 * @param mgmt Exchange management style.
2164 * @param exch Exchange for sending the message.
2165 * @param arg1 User defined argument.
2166 * @param arg2 User defined argument.
2167 * @param arg3 User defined argument.
2168 *
2169 * @return New session on success or NULL on error.
2170 *
2171 */
2172async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
2173 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
2174{
2175 if (exch == NULL) {
2176 errno = ENOENT;
2177 return NULL;
2178 }
[a35b458]2179
[79ae36dd]2180 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2181 if (sess == NULL) {
2182 errno = ENOMEM;
2183 return NULL;
2184 }
[a35b458]2185
[eadaeae8]2186 cap_phone_handle_t phone;
[b7fd2a0]2187 errno_t rc = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
[a99cbc1e]2188 0, &phone);
2189 if (rc != EOK) {
2190 errno = rc;
[79ae36dd]2191 free(sess);
2192 return NULL;
2193 }
[a35b458]2194
[566992e1]2195 sess->iface = 0;
[79ae36dd]2196 sess->mgmt = mgmt;
2197 sess->phone = phone;
2198 sess->arg1 = arg1;
2199 sess->arg2 = arg2;
2200 sess->arg3 = arg3;
[a35b458]2201
[58cbf8d5]2202 fibril_mutex_initialize(&sess->remote_state_mtx);
2203 sess->remote_state_data = NULL;
[a35b458]2204
[79ae36dd]2205 list_initialize(&sess->exch_list);
2206 fibril_mutex_initialize(&sess->mutex);
2207 atomic_set(&sess->refcnt, 0);
[a35b458]2208
[79ae36dd]2209 return sess;
[f74392f]2210}
2211
[0dd16778]2212/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2213 *
2214 * Ask through phone for a new connection to some service and block until
2215 * success.
2216 *
2217 * @param exch Exchange for sending the message.
2218 * @param iface Connection interface.
2219 * @param arg2 User defined argument.
2220 * @param arg3 User defined argument.
2221 *
2222 * @return New session on success or NULL on error.
2223 *
2224 */
2225async_sess_t *async_connect_me_to_iface(async_exch_t *exch, iface_t iface,
2226 sysarg_t arg2, sysarg_t arg3)
2227{
2228 if (exch == NULL) {
2229 errno = ENOENT;
2230 return NULL;
2231 }
[a35b458]2232
[0dd16778]2233 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2234 if (sess == NULL) {
2235 errno = ENOMEM;
2236 return NULL;
2237 }
[a35b458]2238
[eadaeae8]2239 cap_phone_handle_t phone;
[b7fd2a0]2240 errno_t rc = async_connect_me_to_internal(exch->phone, iface, arg2,
[a99cbc1e]2241 arg3, 0, &phone);
2242 if (rc != EOK) {
2243 errno = rc;
[0dd16778]2244 free(sess);
2245 return NULL;
2246 }
[a35b458]2247
[0dd16778]2248 sess->iface = iface;
2249 sess->phone = phone;
2250 sess->arg1 = iface;
2251 sess->arg2 = arg2;
2252 sess->arg3 = arg3;
[a35b458]2253
[0dd16778]2254 fibril_mutex_initialize(&sess->remote_state_mtx);
2255 sess->remote_state_data = NULL;
[a35b458]2256
[0dd16778]2257 list_initialize(&sess->exch_list);
2258 fibril_mutex_initialize(&sess->mutex);
2259 atomic_set(&sess->refcnt, 0);
[a35b458]2260
[0dd16778]2261 return sess;
2262}
2263
[93ad49a8]2264/** Set arguments for new connections.
[0f4532e]2265 *
2266 * FIXME This is an ugly hack to work around the problem that parallel
2267 * exchanges are implemented using parallel connections. When we create
[93ad49a8]2268 * a callback session, the framework does not know arguments for the new
2269 * connections.
[0f4532e]2270 *
2271 * The proper solution seems to be to implement parallel exchanges using
2272 * tagging.
2273 */
[93ad49a8]2274void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
2275 sysarg_t arg3)
[0f4532e]2276{
[93ad49a8]2277 sess->arg1 = arg1;
2278 sess->arg2 = arg2;
2279 sess->arg3 = arg3;
[0f4532e]2280}
2281
[f74392f]2282/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
[007e6efa]2283 *
[f74392f]2284 * Ask through phone for a new connection to some service and block until
2285 * success.
2286 *
[79ae36dd]2287 * @param mgmt Exchange management style.
2288 * @param exch Exchange for sending the message.
2289 * @param arg1 User defined argument.
2290 * @param arg2 User defined argument.
2291 * @param arg3 User defined argument.
[007e6efa]2292 *
[79ae36dd]2293 * @return New session on success or NULL on error.
[f74392f]2294 *
2295 */
[79ae36dd]2296async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
2297 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
[f74392f]2298{
[79ae36dd]2299 if (exch == NULL) {
2300 errno = ENOENT;
2301 return NULL;
2302 }
[a35b458]2303
[79ae36dd]2304 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2305 if (sess == NULL) {
2306 errno = ENOMEM;
2307 return NULL;
2308 }
[a35b458]2309
[eadaeae8]2310 cap_phone_handle_t phone;
[b7fd2a0]2311 errno_t rc = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
[a99cbc1e]2312 IPC_FLAG_BLOCKING, &phone);
[a35b458]2313
[a99cbc1e]2314 if (rc != EOK) {
2315 errno = rc;
[79ae36dd]2316 free(sess);
2317 return NULL;
2318 }
[a35b458]2319
[566992e1]2320 sess->iface = 0;
[79ae36dd]2321 sess->mgmt = mgmt;
2322 sess->phone = phone;
2323 sess->arg1 = arg1;
2324 sess->arg2 = arg2;
2325 sess->arg3 = arg3;
[a35b458]2326
[58cbf8d5]2327 fibril_mutex_initialize(&sess->remote_state_mtx);
2328 sess->remote_state_data = NULL;
[a35b458]2329
[79ae36dd]2330 list_initialize(&sess->exch_list);
2331 fibril_mutex_initialize(&sess->mutex);
2332 atomic_set(&sess->refcnt, 0);
[a35b458]2333
[79ae36dd]2334 return sess;
[f74392f]2335}
2336
[566992e1]2337/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2338 *
2339 * Ask through phone for a new connection to some service and block until
2340 * success.
2341 *
2342 * @param exch Exchange for sending the message.
2343 * @param iface Connection interface.
2344 * @param arg2 User defined argument.
2345 * @param arg3 User defined argument.
2346 *
2347 * @return New session on success or NULL on error.
2348 *
2349 */
2350async_sess_t *async_connect_me_to_blocking_iface(async_exch_t *exch, iface_t iface,
2351 sysarg_t arg2, sysarg_t arg3)
2352{
2353 if (exch == NULL) {
2354 errno = ENOENT;
2355 return NULL;
2356 }
[a35b458]2357
[566992e1]2358 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2359 if (sess == NULL) {
2360 errno = ENOMEM;
2361 return NULL;
2362 }
[a35b458]2363
[eadaeae8]2364 cap_phone_handle_t phone;
[b7fd2a0]2365 errno_t rc = async_connect_me_to_internal(exch->phone, iface, arg2,
[a99cbc1e]2366 arg3, IPC_FLAG_BLOCKING, &phone);
2367 if (rc != EOK) {
2368 errno = rc;
[566992e1]2369 free(sess);
2370 return NULL;
2371 }
[a35b458]2372
[566992e1]2373 sess->iface = iface;
2374 sess->phone = phone;
2375 sess->arg1 = iface;
2376 sess->arg2 = arg2;
2377 sess->arg3 = arg3;
[a35b458]2378
[566992e1]2379 fibril_mutex_initialize(&sess->remote_state_mtx);
2380 sess->remote_state_data = NULL;
[a35b458]2381
[566992e1]2382 list_initialize(&sess->exch_list);
2383 fibril_mutex_initialize(&sess->mutex);
2384 atomic_set(&sess->refcnt, 0);
[a35b458]2385
[566992e1]2386 return sess;
2387}
2388
[64d2b10]2389/** Connect to a task specified by id.
2390 *
2391 */
[79ae36dd]2392async_sess_t *async_connect_kbox(task_id_t id)
[64d2b10]2393{
[79ae36dd]2394 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2395 if (sess == NULL) {
2396 errno = ENOMEM;
2397 return NULL;
2398 }
[a35b458]2399
[eadaeae8]2400 cap_phone_handle_t phone;
[b7fd2a0]2401 errno_t rc = ipc_connect_kbox(id, &phone);
[569a51a]2402 if (rc != EOK) {
2403 errno = rc;
[79ae36dd]2404 free(sess);
2405 return NULL;
2406 }
[a35b458]2407
[566992e1]2408 sess->iface = 0;
[79ae36dd]2409 sess->mgmt = EXCHANGE_ATOMIC;
2410 sess->phone = phone;
2411 sess->arg1 = 0;
2412 sess->arg2 = 0;
2413 sess->arg3 = 0;
[a35b458]2414
[58cbf8d5]2415 fibril_mutex_initialize(&sess->remote_state_mtx);
2416 sess->remote_state_data = NULL;
[a35b458]2417
[79ae36dd]2418 list_initialize(&sess->exch_list);
2419 fibril_mutex_initialize(&sess->mutex);
2420 atomic_set(&sess->refcnt, 0);
[a35b458]2421
[79ae36dd]2422 return sess;
2423}
2424
[eadaeae8]2425static errno_t async_hangup_internal(cap_phone_handle_t phone)
[79ae36dd]2426{
2427 return ipc_hangup(phone);
[64d2b10]2428}
2429
2430/** Wrapper for ipc_hangup.
2431 *
[79ae36dd]2432 * @param sess Session to hung up.
[64d2b10]2433 *
[cde999a]2434 * @return Zero on success or an error code.
[64d2b10]2435 *
2436 */
[b7fd2a0]2437errno_t async_hangup(async_sess_t *sess)
[64d2b10]2438{
[36e2b55]2439 async_exch_t *exch;
[a35b458]2440
[79ae36dd]2441 assert(sess);
[a35b458]2442
[79ae36dd]2443 if (atomic_get(&sess->refcnt) > 0)
2444 return EBUSY;
[a35b458]2445
[36e2b55]2446 fibril_mutex_lock(&async_sess_mutex);
[a35b458]2447
[b7fd2a0]2448 errno_t rc = async_hangup_internal(sess->phone);
[a35b458]2449
[36e2b55]2450 while (!list_empty(&sess->exch_list)) {
2451 exch = (async_exch_t *)
2452 list_get_instance(list_first(&sess->exch_list),
2453 async_exch_t, sess_link);
[a35b458]2454
[36e2b55]2455 list_remove(&exch->sess_link);
2456 list_remove(&exch->global_link);
2457 async_hangup_internal(exch->phone);
2458 free(exch);
2459 }
[4c50c8d]2460
2461 free(sess);
[a35b458]2462
[36e2b55]2463 fibril_mutex_unlock(&async_sess_mutex);
[a35b458]2464
[79ae36dd]2465 return rc;
[64d2b10]2466}
2467
2468/** Interrupt one thread of this task from waiting for IPC. */
2469void async_poke(void)
2470{
2471 ipc_poke();
2472}
2473
[79ae36dd]2474/** Start new exchange in a session.
2475 *
2476 * @param session Session.
2477 *
2478 * @return New exchange or NULL on error.
2479 *
2480 */
2481async_exch_t *async_exchange_begin(async_sess_t *sess)
2482{
2483 if (sess == NULL)
2484 return NULL;
[a35b458]2485
[566992e1]2486 exch_mgmt_t mgmt = sess->mgmt;
2487 if (sess->iface != 0)
2488 mgmt = sess->iface & IFACE_EXCHANGE_MASK;
[a35b458]2489
[566992e1]2490 async_exch_t *exch = NULL;
[a35b458]2491
[79ae36dd]2492 fibril_mutex_lock(&async_sess_mutex);
[a35b458]2493
[79ae36dd]2494 if (!list_empty(&sess->exch_list)) {
2495 /*
2496 * There are inactive exchanges in the session.
2497 */
2498 exch = (async_exch_t *)
[b72efe8]2499 list_get_instance(list_first(&sess->exch_list),
2500 async_exch_t, sess_link);
[a35b458]2501
[79ae36dd]2502 list_remove(&exch->sess_link);
2503 list_remove(&exch->global_link);
2504 } else {
2505 /*
2506 * There are no available exchanges in the session.
2507 */
[a35b458]2508
[566992e1]2509 if ((mgmt == EXCHANGE_ATOMIC) ||
2510 (mgmt == EXCHANGE_SERIALIZE)) {
[79ae36dd]2511 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2512 if (exch != NULL) {
[b72efe8]2513 link_initialize(&exch->sess_link);
2514 link_initialize(&exch->global_link);
[79ae36dd]2515 exch->sess = sess;
2516 exch->phone = sess->phone;
2517 }
[566992e1]2518 } else if (mgmt == EXCHANGE_PARALLEL) {
[eadaeae8]2519 cap_phone_handle_t phone;
[b7fd2a0]2520 errno_t rc;
[a35b458]2521
[566992e1]2522 retry:
[79ae36dd]2523 /*
2524 * Make a one-time attempt to connect a new data phone.
2525 */
[a99cbc1e]2526 rc = async_connect_me_to_internal(sess->phone, sess->arg1,
2527 sess->arg2, sess->arg3, 0, &phone);
2528 if (rc == EOK) {
[79ae36dd]2529 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2530 if (exch != NULL) {
[b72efe8]2531 link_initialize(&exch->sess_link);
2532 link_initialize(&exch->global_link);
[79ae36dd]2533 exch->sess = sess;
2534 exch->phone = phone;
2535 } else
2536 async_hangup_internal(phone);
2537 } else if (!list_empty(&inactive_exch_list)) {
2538 /*
2539 * We did not manage to connect a new phone. But we
2540 * can try to close some of the currently inactive
2541 * connections in other sessions and try again.
2542 */
2543 exch = (async_exch_t *)
[b72efe8]2544 list_get_instance(list_first(&inactive_exch_list),
2545 async_exch_t, global_link);
[a35b458]2546
[79ae36dd]2547 list_remove(&exch->sess_link);
2548 list_remove(&exch->global_link);
2549 async_hangup_internal(exch->phone);
2550 free(exch);
2551 goto retry;
2552 } else {
2553 /*
2554 * Wait for a phone to become available.
2555 */
2556 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
2557 goto retry;
2558 }
2559 }
2560 }
[a35b458]2561
[79ae36dd]2562 fibril_mutex_unlock(&async_sess_mutex);
[a35b458]2563
[79ae36dd]2564 if (exch != NULL) {
2565 atomic_inc(&sess->refcnt);
[a35b458]2566
[566992e1]2567 if (mgmt == EXCHANGE_SERIALIZE)
[79ae36dd]2568 fibril_mutex_lock(&sess->mutex);
2569 }
[a35b458]2570
[79ae36dd]2571 return exch;
2572}
2573
2574/** Finish an exchange.
2575 *
2576 * @param exch Exchange to finish.
2577 *
2578 */
2579void async_exchange_end(async_exch_t *exch)
2580{
2581 if (exch == NULL)
2582 return;
[a35b458]2583
[79ae36dd]2584 async_sess_t *sess = exch->sess;
[3ca2e36]2585 assert(sess != NULL);
[a35b458]2586
[566992e1]2587 exch_mgmt_t mgmt = sess->mgmt;
2588 if (sess->iface != 0)
2589 mgmt = sess->iface & IFACE_EXCHANGE_MASK;
[a35b458]2590
[1c6436a]2591 atomic_dec(&sess->refcnt);
[a35b458]2592
[566992e1]2593 if (mgmt == EXCHANGE_SERIALIZE)
[79ae36dd]2594 fibril_mutex_unlock(&sess->mutex);
[a35b458]2595
[79ae36dd]2596 fibril_mutex_lock(&async_sess_mutex);
[a35b458]2597
[79ae36dd]2598 list_append(&exch->sess_link, &sess->exch_list);
2599 list_append(&exch->global_link, &inactive_exch_list);
2600 fibril_condvar_signal(&avail_phone_cv);
[a35b458]2601
[79ae36dd]2602 fibril_mutex_unlock(&async_sess_mutex);
2603}
2604
[47b7006]2605/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
2606 *
[79ae36dd]2607 * @param exch Exchange for sending the message.
2608 * @param size Size of the destination address space area.
2609 * @param arg User defined argument.
2610 * @param flags Storage for the received flags. Can be NULL.
[df956b9b]2611 * @param dst Address of the storage for the destination address space area
2612 * base address. Cannot be NULL.
[0da4e41]2613 *
[cde999a]2614 * @return Zero on success or an error code from errno.h.
[0da4e41]2615 *
2616 */
[b7fd2a0]2617errno_t async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
[fbcdeb8]2618 unsigned int *flags, void **dst)
[0da4e41]2619{
[79ae36dd]2620 if (exch == NULL)
2621 return ENOENT;
[a35b458]2622
[fbcdeb8]2623 sysarg_t _flags = 0;
2624 sysarg_t _dst = (sysarg_t) -1;
[b7fd2a0]2625 errno_t res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
[fbcdeb8]2626 arg, NULL, &_flags, NULL, &_dst);
[a35b458]2627
[0da4e41]2628 if (flags)
[fbcdeb8]2629 *flags = (unsigned int) _flags;
[a35b458]2630
[fbcdeb8]2631 *dst = (void *) _dst;
[0da4e41]2632 return res;
2633}
2634
2635/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
2636 *
[47b7006]2637 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
2638 * calls so that the user doesn't have to remember the meaning of each IPC
2639 * argument.
[0da4e41]2640 *
2641 * So far, this wrapper is to be used from within a connection fibril.
2642 *
[01c3bb4]2643 * @param chandle Storage for the handle of the IPC_M_SHARE_IN call.
2644 * @param size Destination address space area size.
[47b7006]2645 *
2646 * @return True on success, false on failure.
[0da4e41]2647 *
2648 */
[eadaeae8]2649bool async_share_in_receive(cap_call_handle_t *chandle, size_t *size)
[0da4e41]2650{
[01c3bb4]2651 assert(chandle);
[0da4e41]2652 assert(size);
[a35b458]2653
[47b7006]2654 ipc_call_t data;
[01c3bb4]2655 *chandle = async_get_call(&data);
[a35b458]2656
[228e490]2657 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
[47b7006]2658 return false;
[a35b458]2659
[fbcdeb8]2660 *size = (size_t) IPC_GET_ARG1(data);
[47b7006]2661 return true;
[0da4e41]2662}
2663
2664/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
2665 *
[fbcdeb8]2666 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
[47b7006]2667 * calls so that the user doesn't have to remember the meaning of each IPC
2668 * argument.
[0da4e41]2669 *
[01c3bb4]2670 * @param chandle Handle of the IPC_M_DATA_READ call to answer.
2671 * @param src Source address space base.
2672 * @param flags Flags to be used for sharing. Bits can be only cleared.
[47b7006]2673 *
2674 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2675 *
2676 */
[eadaeae8]2677errno_t async_share_in_finalize(cap_call_handle_t chandle, void *src,
2678 unsigned int flags)
[0da4e41]2679{
[a69d42e]2680 // FIXME: The source has no business deciding destination address.
[01c3bb4]2681 return ipc_answer_3(chandle, EOK, (sysarg_t) src, (sysarg_t) flags,
[a69d42e]2682 (sysarg_t) _end);
[0da4e41]2683}
2684
[47b7006]2685/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
[0da4e41]2686 *
[79ae36dd]2687 * @param exch Exchange for sending the message.
2688 * @param src Source address space area base address.
2689 * @param flags Flags to be used for sharing. Bits can be only cleared.
[47b7006]2690 *
[cde999a]2691 * @return Zero on success or an error code from errno.h.
[0da4e41]2692 *
2693 */
[b7fd2a0]2694errno_t async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
[0da4e41]2695{
[79ae36dd]2696 if (exch == NULL)
2697 return ENOENT;
[a35b458]2698
[79ae36dd]2699 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
[96b02eb9]2700 (sysarg_t) flags);
[0da4e41]2701}
2702
2703/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
2704 *
[47b7006]2705 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
2706 * calls so that the user doesn't have to remember the meaning of each IPC
2707 * argument.
[0da4e41]2708 *
2709 * So far, this wrapper is to be used from within a connection fibril.
2710 *
[01c3bb4]2711 * @param chandle Storage for the hash of the IPC_M_SHARE_OUT call.
2712 * @param size Storage for the source address space area size.
2713 * @param flags Storage for the sharing flags.
[47b7006]2714 *
2715 * @return True on success, false on failure.
[0da4e41]2716 *
2717 */
[eadaeae8]2718bool async_share_out_receive(cap_call_handle_t *chandle, size_t *size,
[01c3bb4]2719 unsigned int *flags)
[0da4e41]2720{
[01c3bb4]2721 assert(chandle);
[0da4e41]2722 assert(size);
2723 assert(flags);
[a35b458]2724
[47b7006]2725 ipc_call_t data;
[01c3bb4]2726 *chandle = async_get_call(&data);
[a35b458]2727
[228e490]2728 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
[47b7006]2729 return false;
[a35b458]2730
[0da4e41]2731 *size = (size_t) IPC_GET_ARG2(data);
[47b7006]2732 *flags = (unsigned int) IPC_GET_ARG3(data);
2733 return true;
[0da4e41]2734}
2735
2736/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2737 *
[47b7006]2738 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2739 * calls so that the user doesn't have to remember the meaning of each IPC
2740 * argument.
[0da4e41]2741 *
[01c3bb4]2742 * @param chandle Handle of the IPC_M_DATA_WRITE call to answer.
2743 * @param dst Address of the storage for the destination address space area
2744 * base address.
[47b7006]2745 *
[01c3bb4]2746 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2747 *
2748 */
[eadaeae8]2749errno_t async_share_out_finalize(cap_call_handle_t chandle, void **dst)
[0da4e41]2750{
[a69d42e]2751 return ipc_answer_2(chandle, EOK, (sysarg_t) _end, (sysarg_t) dst);
[0da4e41]2752}
2753
[8bf1eeb]2754/** Start IPC_M_DATA_READ using the async framework.
2755 *
[79ae36dd]2756 * @param exch Exchange for sending the message.
2757 * @param dst Address of the beginning of the destination buffer.
2758 * @param size Size of the destination buffer (in bytes).
[8bf1eeb]2759 * @param dataptr Storage of call data (arg 2 holds actual data size).
[79ae36dd]2760 *
[8bf1eeb]2761 * @return Hash of the sent message or 0 on error.
[79ae36dd]2762 *
[8bf1eeb]2763 */
[79ae36dd]2764aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2765 ipc_call_t *dataptr)
[8bf1eeb]2766{
[79ae36dd]2767 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
[8bf1eeb]2768 (sysarg_t) size, dataptr);
2769}
2770
[47b7006]2771/** Wrapper for IPC_M_DATA_READ calls using the async framework.
[0da4e41]2772 *
[79ae36dd]2773 * @param exch Exchange for sending the message.
2774 * @param dst Address of the beginning of the destination buffer.
2775 * @param size Size of the destination buffer.
[47b7006]2776 *
[cde999a]2777 * @return Zero on success or an error code from errno.h.
[0da4e41]2778 *
2779 */
[b7fd2a0]2780errno_t async_data_read_start(async_exch_t *exch, void *dst, size_t size)
[0da4e41]2781{
[79ae36dd]2782 if (exch == NULL)
2783 return ENOENT;
[a35b458]2784
[79ae36dd]2785 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2786 (sysarg_t) size);
[0da4e41]2787}
2788
2789/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2790 *
[47b7006]2791 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2792 * calls so that the user doesn't have to remember the meaning of each IPC
2793 * argument.
[0da4e41]2794 *
2795 * So far, this wrapper is to be used from within a connection fibril.
2796 *
[01c3bb4]2797 * @param chandle Storage for the handle of the IPC_M_DATA_READ.
2798 * @param size Storage for the maximum size. Can be NULL.
[47b7006]2799 *
2800 * @return True on success, false on failure.
[0da4e41]2801 *
2802 */
[eadaeae8]2803bool async_data_read_receive(cap_call_handle_t *chandle, size_t *size)
[d768d4c8]2804{
2805 ipc_call_t data;
[01c3bb4]2806 return async_data_read_receive_call(chandle, &data, size);
[d768d4c8]2807}
2808
2809/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2810 *
2811 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2812 * calls so that the user doesn't have to remember the meaning of each IPC
2813 * argument.
2814 *
2815 * So far, this wrapper is to be used from within a connection fibril.
2816 *
[01c3bb4]2817 * @param chandle Storage for the handle of the IPC_M_DATA_READ.
2818 * @param size Storage for the maximum size. Can be NULL.
[d768d4c8]2819 *
2820 * @return True on success, false on failure.
2821 *
2822 */
[eadaeae8]2823bool async_data_read_receive_call(cap_call_handle_t *chandle, ipc_call_t *data,
[d768d4c8]2824 size_t *size)
[0da4e41]2825{
[01c3bb4]2826 assert(chandle);
[d768d4c8]2827 assert(data);
[a35b458]2828
[01c3bb4]2829 *chandle = async_get_call(data);
[a35b458]2830
[d768d4c8]2831 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_READ)
[47b7006]2832 return false;
[a35b458]2833
[0da4e41]2834 if (size)
[d768d4c8]2835 *size = (size_t) IPC_GET_ARG2(*data);
[a35b458]2836
[47b7006]2837 return true;
[0da4e41]2838}
2839
2840/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2841 *
[47b7006]2842 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2843 * calls so that the user doesn't have to remember the meaning of each IPC
2844 * argument.
[0da4e41]2845 *
[01c3bb4]2846 * @param chandle Handle of the IPC_M_DATA_READ call to answer.
2847 * @param src Source address for the IPC_M_DATA_READ call.
2848 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2849 * the maximum size announced by the sender.
[47b7006]2850 *
[01c3bb4]2851 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2852 *
2853 */
[eadaeae8]2854errno_t async_data_read_finalize(cap_call_handle_t chandle, const void *src,
2855 size_t size)
[0da4e41]2856{
[01c3bb4]2857 return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) size);
[0da4e41]2858}
2859
[b4cbef1]2860/** Wrapper for forwarding any read request
2861 *
2862 */
[b7fd2a0]2863errno_t async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
[79ae36dd]2864 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2865 ipc_call_t *dataptr)
[b4cbef1]2866{
[79ae36dd]2867 if (exch == NULL)
2868 return ENOENT;
[a35b458]2869
[eadaeae8]2870 cap_call_handle_t chandle;
[01c3bb4]2871 if (!async_data_read_receive(&chandle, NULL)) {
2872 ipc_answer_0(chandle, EINVAL);
[b4cbef1]2873 return EINVAL;
2874 }
[a35b458]2875
[79ae36dd]2876 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]2877 dataptr);
2878 if (msg == 0) {
[01c3bb4]2879 ipc_answer_0(chandle, EINVAL);
[b4cbef1]2880 return EINVAL;
2881 }
[a35b458]2882
[b7fd2a0]2883 errno_t retval = ipc_forward_fast(chandle, exch->phone, 0, 0, 0,
[b4cbef1]2884 IPC_FF_ROUTE_FROM_ME);
2885 if (retval != EOK) {
[ab9f443]2886 async_forget(msg);
[01c3bb4]2887 ipc_answer_0(chandle, retval);
[b4cbef1]2888 return retval;
2889 }
[a35b458]2890
[b7fd2a0]2891 errno_t rc;
[b4cbef1]2892 async_wait_for(msg, &rc);
[a35b458]2893
[b7fd2a0]2894 return (errno_t) rc;
[b4cbef1]2895}
2896
[47b7006]2897/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
[0da4e41]2898 *
[79ae36dd]2899 * @param exch Exchange for sending the message.
2900 * @param src Address of the beginning of the source buffer.
2901 * @param size Size of the source buffer.
[b4cbef1]2902 *
[cde999a]2903 * @return Zero on success or an error code from errno.h.
[0da4e41]2904 *
2905 */
[b7fd2a0]2906errno_t async_data_write_start(async_exch_t *exch, const void *src, size_t size)
[0da4e41]2907{
[79ae36dd]2908 if (exch == NULL)
2909 return ENOENT;
[a35b458]2910
[79ae36dd]2911 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2912 (sysarg_t) size);
[0da4e41]2913}
2914
2915/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2916 *
[47b7006]2917 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2918 * calls so that the user doesn't have to remember the meaning of each IPC
2919 * argument.
[0da4e41]2920 *
2921 * So far, this wrapper is to be used from within a connection fibril.
2922 *
[01c3bb4]2923 * @param chandle Storage for the handle of the IPC_M_DATA_WRITE.
2924 * @param size Storage for the suggested size. May be NULL.
[b4cbef1]2925 *
[01c3bb4]2926 * @return True on success, false on failure.
[0da4e41]2927 *
2928 */
[eadaeae8]2929bool async_data_write_receive(cap_call_handle_t *chandle, size_t *size)
[5ae1c51]2930{
2931 ipc_call_t data;
[01c3bb4]2932 return async_data_write_receive_call(chandle, &data, size);
[5ae1c51]2933}
2934
2935/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2936 *
2937 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2938 * calls so that the user doesn't have to remember the meaning of each IPC
2939 * argument.
2940 *
2941 * So far, this wrapper is to be used from within a connection fibril.
2942 *
[01c3bb4]2943 * @param chandle Storage for the handle of the IPC_M_DATA_WRITE.
2944 * @param data Storage for the ipc call data.
2945 * @param size Storage for the suggested size. May be NULL.
[5ae1c51]2946 *
2947 * @return True on success, false on failure.
2948 *
2949 */
[eadaeae8]2950bool async_data_write_receive_call(cap_call_handle_t *chandle, ipc_call_t *data,
[5ae1c51]2951 size_t *size)
[0da4e41]2952{
[01c3bb4]2953 assert(chandle);
[5ae1c51]2954 assert(data);
[a35b458]2955
[01c3bb4]2956 *chandle = async_get_call(data);
[a35b458]2957
[5ae1c51]2958 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_WRITE)
[47b7006]2959 return false;
[a35b458]2960
[0da4e41]2961 if (size)
[5ae1c51]2962 *size = (size_t) IPC_GET_ARG2(*data);
[a35b458]2963
[47b7006]2964 return true;
[0da4e41]2965}
2966
2967/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2968 *
[47b7006]2969 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2970 * calls so that the user doesn't have to remember the meaning of each IPC
2971 * argument.
[0da4e41]2972 *
[01c3bb4]2973 * @param chandle Handle of the IPC_M_DATA_WRITE call to answer.
2974 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2975 * @param size Final size for the IPC_M_DATA_WRITE call.
[b4cbef1]2976 *
[01c3bb4]2977 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2978 *
2979 */
[eadaeae8]2980errno_t async_data_write_finalize(cap_call_handle_t chandle, void *dst,
2981 size_t size)
[0da4e41]2982{
[01c3bb4]2983 return ipc_answer_2(chandle, EOK, (sysarg_t) dst, (sysarg_t) size);
[0da4e41]2984}
2985
[eda925a]2986/** Wrapper for receiving binary data or strings
[8aa42e3]2987 *
2988 * This wrapper only makes it more comfortable to use async_data_write_*
[eda925a]2989 * functions to receive binary data or strings.
[8aa42e3]2990 *
[472c09d]2991 * @param data Pointer to data pointer (which should be later disposed
2992 * by free()). If the operation fails, the pointer is not
2993 * touched.
[eda925a]2994 * @param nullterm If true then the received data is always zero terminated.
2995 * This also causes to allocate one extra byte beyond the
2996 * raw transmitted data.
[b4cbef1]2997 * @param min_size Minimum size (in bytes) of the data to receive.
[472c09d]2998 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2999 * no limit.
[eda925a]3000 * @param granulariy If non-zero then the size of the received data has to
[472c09d]3001 * be divisible by this value.
3002 * @param received If not NULL, the size of the received data is stored here.
[8aa42e3]3003 *
3004 * @return Zero on success or a value from @ref errno.h on failure.
3005 *
3006 */
[b7fd2a0]3007errno_t async_data_write_accept(void **data, const bool nullterm,
[eda925a]3008 const size_t min_size, const size_t max_size, const size_t granularity,
3009 size_t *received)
[8aa42e3]3010{
[79ae36dd]3011 assert(data);
[a35b458]3012
[eadaeae8]3013 cap_call_handle_t chandle;
[8aa42e3]3014 size_t size;
[01c3bb4]3015 if (!async_data_write_receive(&chandle, &size)) {
3016 ipc_answer_0(chandle, EINVAL);
[8aa42e3]3017 return EINVAL;
3018 }
[a35b458]3019
[b4cbef1]3020 if (size < min_size) {
[01c3bb4]3021 ipc_answer_0(chandle, EINVAL);
[b4cbef1]3022 return EINVAL;
3023 }
[a35b458]3024
[8aa42e3]3025 if ((max_size > 0) && (size > max_size)) {
[01c3bb4]3026 ipc_answer_0(chandle, EINVAL);
[8aa42e3]3027 return EINVAL;
3028 }
[a35b458]3029
[472c09d]3030 if ((granularity > 0) && ((size % granularity) != 0)) {
[01c3bb4]3031 ipc_answer_0(chandle, EINVAL);
[472c09d]3032 return EINVAL;
3033 }
[a35b458]3034
[57dea62]3035 void *arg_data;
[a35b458]3036
[eda925a]3037 if (nullterm)
[57dea62]3038 arg_data = malloc(size + 1);
[eda925a]3039 else
[57dea62]3040 arg_data = malloc(size);
[a35b458]3041
[57dea62]3042 if (arg_data == NULL) {
[01c3bb4]3043 ipc_answer_0(chandle, ENOMEM);
[8aa42e3]3044 return ENOMEM;
3045 }
[a35b458]3046
[b7fd2a0]3047 errno_t rc = async_data_write_finalize(chandle, arg_data, size);
[8aa42e3]3048 if (rc != EOK) {
[57dea62]3049 free(arg_data);
[8aa42e3]3050 return rc;
3051 }
[a35b458]3052
[eda925a]3053 if (nullterm)
[57dea62]3054 ((char *) arg_data)[size] = 0;
[a35b458]3055
[57dea62]3056 *data = arg_data;
[472c09d]3057 if (received != NULL)
3058 *received = size;
[a35b458]3059
[8aa42e3]3060 return EOK;
3061}
3062
[b4cbef1]3063/** Wrapper for voiding any data that is about to be received
3064 *
3065 * This wrapper can be used to void any pending data
3066 *
3067 * @param retval Error value from @ref errno.h to be returned to the caller.
3068 *
3069 */
[b7fd2a0]3070void async_data_write_void(errno_t retval)
[b4cbef1]3071{
[eadaeae8]3072 cap_call_handle_t chandle;
[01c3bb4]3073 async_data_write_receive(&chandle, NULL);
3074 ipc_answer_0(chandle, retval);
[b4cbef1]3075}
3076
3077/** Wrapper for forwarding any data that is about to be received
3078 *
3079 */
[b7fd2a0]3080errno_t async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
[79ae36dd]3081 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
3082 ipc_call_t *dataptr)
[b4cbef1]3083{
[79ae36dd]3084 if (exch == NULL)
3085 return ENOENT;
[a35b458]3086
[eadaeae8]3087 cap_call_handle_t chandle;
[01c3bb4]3088 if (!async_data_write_receive(&chandle, NULL)) {
3089 ipc_answer_0(chandle, EINVAL);
[b4cbef1]3090 return EINVAL;
3091 }
[a35b458]3092
[79ae36dd]3093 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]3094 dataptr);
3095 if (msg == 0) {
[01c3bb4]3096 ipc_answer_0(chandle, EINVAL);
[b4cbef1]3097 return EINVAL;
3098 }
[a35b458]3099
[b7fd2a0]3100 errno_t retval = ipc_forward_fast(chandle, exch->phone, 0, 0, 0,
[b4cbef1]3101 IPC_FF_ROUTE_FROM_ME);
3102 if (retval != EOK) {
[ab9f443]3103 async_forget(msg);
[01c3bb4]3104 ipc_answer_0(chandle, retval);
[b4cbef1]3105 return retval;
3106 }
[a35b458]3107
[b7fd2a0]3108 errno_t rc;
[b4cbef1]3109 async_wait_for(msg, &rc);
[a35b458]3110
[b7fd2a0]3111 return (errno_t) rc;
[b4cbef1]3112}
3113
[79ae36dd]3114/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
3115 *
3116 * If the current call is IPC_M_CONNECT_TO_ME then a new
3117 * async session is created for the accepted phone.
3118 *
3119 * @param mgmt Exchange management style.
3120 *
[8869f7b]3121 * @return New async session.
3122 * @return NULL on failure.
[79ae36dd]3123 *
3124 */
3125async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
3126{
3127 /* Accept the phone */
3128 ipc_call_t call;
[eadaeae8]3129 cap_call_handle_t chandle = async_get_call(&call);
3130 cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(call);
[a35b458]3131
[eadaeae8]3132 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
3133 !CAP_HANDLE_VALID((phandle))) {
[01c3bb4]3134 async_answer_0(chandle, EINVAL);
[79ae36dd]3135 return NULL;
3136 }
[a35b458]3137
[79ae36dd]3138 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
3139 if (sess == NULL) {
[01c3bb4]3140 async_answer_0(chandle, ENOMEM);
[79ae36dd]3141 return NULL;
3142 }
[a35b458]3143
[566992e1]3144 sess->iface = 0;
[79ae36dd]3145 sess->mgmt = mgmt;
[01c3bb4]3146 sess->phone = phandle;
[79ae36dd]3147 sess->arg1 = 0;
3148 sess->arg2 = 0;
3149 sess->arg3 = 0;
[a35b458]3150
[58cbf8d5]3151 fibril_mutex_initialize(&sess->remote_state_mtx);
3152 sess->remote_state_data = NULL;
[a35b458]3153
[79ae36dd]3154 list_initialize(&sess->exch_list);
3155 fibril_mutex_initialize(&sess->mutex);
3156 atomic_set(&sess->refcnt, 0);
[a35b458]3157
[79ae36dd]3158 /* Acknowledge the connected phone */
[01c3bb4]3159 async_answer_0(chandle, EOK);
[a35b458]3160
[79ae36dd]3161 return sess;
3162}
3163
[8869f7b]3164/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
3165 *
3166 * If the call is IPC_M_CONNECT_TO_ME then a new
3167 * async session is created. However, the phone is
3168 * not accepted automatically.
3169 *
3170 * @param mgmt Exchange management style.
3171 * @param call Call data.
3172 *
3173 * @return New async session.
3174 * @return NULL on failure.
3175 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
3176 *
3177 */
3178async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
3179{
[eadaeae8]3180 cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(*call);
[a35b458]3181
[eadaeae8]3182 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
3183 !CAP_HANDLE_VALID((phandle)))
[8869f7b]3184 return NULL;
[a35b458]3185
[8869f7b]3186 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
3187 if (sess == NULL)
3188 return NULL;
[a35b458]3189
[566992e1]3190 sess->iface = 0;
[8869f7b]3191 sess->mgmt = mgmt;
[01c3bb4]3192 sess->phone = phandle;
[8869f7b]3193 sess->arg1 = 0;
3194 sess->arg2 = 0;
3195 sess->arg3 = 0;
[a35b458]3196
[58cbf8d5]3197 fibril_mutex_initialize(&sess->remote_state_mtx);
3198 sess->remote_state_data = NULL;
[a35b458]3199
[8869f7b]3200 list_initialize(&sess->exch_list);
3201 fibril_mutex_initialize(&sess->mutex);
3202 atomic_set(&sess->refcnt, 0);
[a35b458]3203
[8869f7b]3204 return sess;
3205}
3206
[b7fd2a0]3207errno_t async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
[2c4aa39]3208 sysarg_t arg3, async_exch_t *other_exch)
3209{
3210 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
[eadaeae8]3211 arg1, arg2, arg3, 0, CAP_HANDLE_RAW(other_exch->phone));
[2c4aa39]3212}
3213
[eadaeae8]3214bool async_state_change_receive(cap_call_handle_t *chandle, sysarg_t *arg1,
[2c4aa39]3215 sysarg_t *arg2, sysarg_t *arg3)
3216{
[01c3bb4]3217 assert(chandle);
[a35b458]3218
[2c4aa39]3219 ipc_call_t call;
[01c3bb4]3220 *chandle = async_get_call(&call);
[a35b458]3221
[2c4aa39]3222 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
3223 return false;
[a35b458]3224
[2c4aa39]3225 if (arg1)
3226 *arg1 = IPC_GET_ARG1(call);
3227 if (arg2)
3228 *arg2 = IPC_GET_ARG2(call);
3229 if (arg3)
3230 *arg3 = IPC_GET_ARG3(call);
[a35b458]3231
[2c4aa39]3232 return true;
3233}
3234
[eadaeae8]3235errno_t async_state_change_finalize(cap_call_handle_t chandle,
3236 async_exch_t *other_exch)
[2c4aa39]3237{
[eadaeae8]3238 return ipc_answer_1(chandle, EOK, CAP_HANDLE_RAW(other_exch->phone));
[2c4aa39]3239}
3240
[58cbf8d5]3241/** Lock and get session remote state
3242 *
3243 * Lock and get the local replica of the remote state
3244 * in stateful sessions. The call should be paired
3245 * with async_remote_state_release*().
3246 *
3247 * @param[in] sess Stateful session.
3248 *
3249 * @return Local replica of the remote state.
3250 *
3251 */
3252void *async_remote_state_acquire(async_sess_t *sess)
3253{
3254 fibril_mutex_lock(&sess->remote_state_mtx);
3255 return sess->remote_state_data;
3256}
3257
3258/** Update the session remote state
3259 *
3260 * Update the local replica of the remote state
3261 * in stateful sessions. The remote state must
3262 * be already locked.
3263 *
3264 * @param[in] sess Stateful session.
3265 * @param[in] state New local replica of the remote state.
3266 *
3267 */
3268void async_remote_state_update(async_sess_t *sess, void *state)
3269{
3270 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3271 sess->remote_state_data = state;
3272}
3273
3274/** Release the session remote state
3275 *
3276 * Unlock the local replica of the remote state
3277 * in stateful sessions.
3278 *
3279 * @param[in] sess Stateful session.
3280 *
3281 */
3282void async_remote_state_release(async_sess_t *sess)
3283{
3284 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
[a35b458]3285
[58cbf8d5]3286 fibril_mutex_unlock(&sess->remote_state_mtx);
3287}
3288
3289/** Release the session remote state and end an exchange
3290 *
3291 * Unlock the local replica of the remote state
3292 * in stateful sessions. This is convenience function
3293 * which gets the session pointer from the exchange
3294 * and also ends the exchange.
3295 *
3296 * @param[in] exch Stateful session's exchange.
3297 *
3298 */
3299void async_remote_state_release_exchange(async_exch_t *exch)
3300{
3301 if (exch == NULL)
3302 return;
[a35b458]3303
[58cbf8d5]3304 async_sess_t *sess = exch->sess;
3305 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
[a35b458]3306
[58cbf8d5]3307 async_exchange_end(exch);
3308 fibril_mutex_unlock(&sess->remote_state_mtx);
3309}
3310
[101516d]3311void *async_as_area_create(void *base, size_t size, unsigned int flags,
[ae6021d]3312 async_sess_t *pager, sysarg_t id1, sysarg_t id2, sysarg_t id3)
3313{
3314 as_area_pager_info_t pager_info = {
3315 .pager = pager->phone,
3316 .id1 = id1,
3317 .id2 = id2,
3318 .id3 = id3
3319 };
3320 return as_area_create(base, size, flags, &pager_info);
[101516d]3321}
3322
[a46da63]3323/** @}
[b2951e2]3324 */
Note: See TracBrowser for help on using the repository browser.