source: mainline/uspace/lib/c/generic/async.c@ 0dd16778

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0dd16778 was 0dd16778, checked in by Martin Decky <martin@…>, 10 years ago

start migrating devman to interfaces

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