source: mainline/uspace/lib/c/generic/async.c@ 853802e

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

Include task ID in connection hash table operations

The task ID must be included in the hash computation because a phone hash alone
might not be unique while the server is still tracking connections for killed
tasks due to kernel's recycling of phone structures.

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