source: mainline/uspace/lib/c/generic/async.c@ 10cb47e

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

code review and cstyle cleanup (no change in functionality)

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