source: mainline/uspace/lib/c/generic/async.c@ 38d150e

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

Prefer to get memory allocation functions through the standard stdlib header.

  • Property mode set to 100644
File size: 79.6 KB
RevLine 
[06502f7d]1/*
[df4ed85]2 * Copyright (c) 2006 Ondrej Palkovsky
[06502f7d]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[b2951e2]27 */
28
[a46da63]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
32/** @file
[c07544d3]33 */
[06502f7d]34
[80649a91]35/**
36 * Asynchronous library
37 *
[c07544d3]38 * The aim of this library is to provide a facility for writing programs which
39 * utilize the asynchronous nature of HelenOS IPC, yet using a normal way of
40 * programming.
[80649a91]41 *
[79ae36dd]42 * You should be able to write very simple multithreaded programs. The async
43 * framework will automatically take care of most of the synchronization
44 * problems.
[80649a91]45 *
[9591265]46 * Example of use (pseudo C):
[c07544d3]47 *
[80649a91]48 * 1) Multithreaded client application
[9591265]49 *
[c07544d3]50 * fibril_create(fibril1, ...);
51 * fibril_create(fibril2, ...);
52 * ...
53 *
54 * int fibril1(void *arg)
55 * {
[79ae36dd]56 * conn = async_connect_me_to(...);
57 *
58 * exch = async_exchange_begin(conn);
59 * c1 = async_send(exch);
60 * async_exchange_end(exch);
61 *
62 * exch = async_exchange_begin(conn);
63 * c2 = async_send(exch);
64 * async_exchange_end(exch);
65 *
[c07544d3]66 * async_wait_for(c1);
67 * async_wait_for(c2);
68 * ...
69 * }
[80649a91]70 *
71 *
72 * 2) Multithreaded server application
73 *
[c07544d3]74 * main()
75 * {
76 * async_manager();
77 * }
78 *
[01c3bb4]79 * port_handler(ichandle, *icall)
[c07544d3]80 * {
81 * if (want_refuse) {
[01c3bb4]82 * async_answer_0(ichandle, ELIMIT);
[c07544d3]83 * return;
84 * }
[01c3bb4]85 * async_answer_0(ichandle, EOK);
[80649a91]86 *
[01c3bb4]87 * chandle = async_get_call(&call);
88 * somehow_handle_the_call(chandle, call);
89 * async_answer_2(chandle, 1, 2, 3);
[53ca318]90 *
[01c3bb4]91 * chandle = async_get_call(&call);
[c07544d3]92 * ...
93 * }
[a2cd194]94 *
[80649a91]95 */
[9591265]96
[64d2b10]97#define LIBC_ASYNC_C_
98#include <ipc/ipc.h>
[80649a91]99#include <async.h>
[b76a7329]100#include "private/async.h"
[64d2b10]101#undef LIBC_ASYNC_C_
102
[8820544]103#include <ipc/irq.h>
104#include <ipc/event.h>
[64d2b10]105#include <futex.h>
[bc1f1c2]106#include <fibril.h>
[d9c8c81]107#include <adt/hash_table.h>
[853802e]108#include <adt/hash.h>
[d9c8c81]109#include <adt/list.h>
[80649a91]110#include <assert.h>
111#include <errno.h>
[daa90e8]112#include <sys/time.h>
[c0699467]113#include <libarch/barrier.h>
[3e6a98c5]114#include <stdbool.h>
[38d150e]115#include <stdlib.h>
[79ae36dd]116#include <mem.h>
117#include <stdlib.h>
[e2ab36f1]118#include <macros.h>
[101516d]119#include <as.h>
[ae6021d]120#include <abi/mm/as.h>
[d7978525]121#include "private/libc.h"
[80649a91]122
[5da7199]123/** Session data */
124struct async_sess {
125 /** List of inactive exchanges */
126 list_t exch_list;
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
[01c3bb4]187 cap_handle_t chandle;
[80649a91]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. */
[01c3bb4]239 cap_handle_t chandle;
[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. */
[01c3bb4]245 cap_handle_t close_chandle;
[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 *
[01c3bb4]376 * This fallback fibril function gets called on incomming connections that do
377 * not have a specific handler defined.
[47b7006]378 *
[01c3bb4]379 * @param chandle Handle of the incoming call.
380 * @param call Data of the incoming call.
381 * @param arg Local argument
[47b7006]382 *
383 */
[01c3bb4]384static void default_fallback_port_handler(cap_handle_t chandle,
385 ipc_call_t *call, void *arg)
[47b7006]386{
[01c3bb4]387 ipc_answer_0(chandle, ENOENT);
[47b7006]388}
[36c9234]389
[b688fd8]390static async_port_handler_t fallback_port_handler =
391 default_fallback_port_handler;
392static void *fallback_port_data = NULL;
[da0c91e7]393
[566992e1]394static hash_table_t interface_hash_table;
395
396static size_t interface_key_hash(void *key)
397{
398 iface_t iface = *(iface_t *) key;
399 return iface;
400}
401
402static size_t interface_hash(const ht_link_t *item)
403{
404 interface_t *interface = hash_table_get_inst(item, interface_t, link);
405 return interface_key_hash(&interface->iface);
406}
407
408static bool interface_key_equal(void *key, const ht_link_t *item)
409{
410 iface_t iface = *(iface_t *) key;
411 interface_t *interface = hash_table_get_inst(item, interface_t, link);
412 return iface == interface->iface;
413}
414
415/** Operations for the port hash table. */
416static hash_table_ops_t interface_hash_table_ops = {
417 .hash = interface_hash,
418 .key_hash = interface_key_hash,
419 .key_equal = interface_key_equal,
420 .equal = NULL,
421 .remove_callback = NULL
422};
423
424static size_t port_key_hash(void *key)
425{
426 port_id_t port_id = *(port_id_t *) key;
427 return port_id;
428}
429
430static size_t port_hash(const ht_link_t *item)
431{
432 port_t *port = hash_table_get_inst(item, port_t, link);
433 return port_key_hash(&port->id);
434}
435
436static bool port_key_equal(void *key, const ht_link_t *item)
437{
438 port_id_t port_id = *(port_id_t *) key;
439 port_t *port = hash_table_get_inst(item, port_t, link);
440 return port_id == port->id;
441}
442
443/** Operations for the port hash table. */
444static hash_table_ops_t port_hash_table_ops = {
445 .hash = port_hash,
446 .key_hash = port_key_hash,
447 .key_equal = port_key_equal,
448 .equal = NULL,
449 .remove_callback = NULL
450};
451
452static interface_t *async_new_interface(iface_t iface)
453{
454 interface_t *interface =
455 (interface_t *) malloc(sizeof(interface_t));
456 if (!interface)
457 return NULL;
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) {
[01c3bb4]717 ipc_answer_0(fibril_connection->chandle, ENOMEM);
[9ef495f]718 return 0;
719 }
720
721 fibril_connection->client = client;
722
723 /*
724 * Call the connection handler function.
725 */
[01c3bb4]726 fibril_connection->handler(fibril_connection->chandle,
[9ef495f]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);
[01c3bb4]753 ipc_answer_0(msg->chandle, EHANGUP);
[9ef495f]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 */
[01c3bb4]761 if (fibril_connection->close_chandle)
762 ipc_answer_0(fibril_connection->close_chandle, EOK);
[9ef495f]763
764 free(fibril_connection);
765 return 0;
766}
767
768/** Create a new fibril for a new connection.
769 *
[01c3bb4]770 * Create new fibril for connection, fill in connection structures and insert it
771 * into the hash table, so that later we can easily do routing of messages to
772 * particular fibrils.
[9ef495f]773 *
[01c3bb4]774 * @param in_task_id Identification of the incoming connection.
775 * @param in_phone_hash Identification of the incoming connection.
776 * @param chandle Handle of the opening IPC_M_CONNECT_ME_TO call.
777 * If chandle is CAP_NIL, the connection was opened by
778 * accepting the IPC_M_CONNECT_TO_ME call and this
779 * function is called directly by the server.
780 * @param call Call data of the opening call.
781 * @param handler Connection handler.
782 * @param data Client argument to pass to the connection handler.
[9ef495f]783 *
[01c3bb4]784 * @return New fibril id or NULL on failure.
[9ef495f]785 *
786 */
787static fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
[01c3bb4]788 cap_handle_t chandle, ipc_call_t *call, async_port_handler_t handler,
[9ef495f]789 void *data)
790{
791 connection_t *conn = malloc(sizeof(*conn));
792 if (!conn) {
[01c3bb4]793 if (chandle != CAP_NIL)
794 ipc_answer_0(chandle, ENOMEM);
[9ef495f]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);
[01c3bb4]802 conn->chandle = chandle;
803 conn->close_chandle = CAP_NIL;
[9ef495f]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
[01c3bb4]817 if (chandle != CAP_NIL)
818 ipc_answer_0(chandle, ENOMEM);
[9ef495f]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,
[01c3bb4]894 CAP_NIL, NULL, handler, data);
[78bb04b]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 *
[01c3bb4]963 * @param chandle Handle of the incoming call.
964 * @param call Data of the incoming call.
[c07544d3]965 *
966 * @return False if the call doesn't match any connection.
[47b7006]967 * @return True if the call was passed to the respective connection fibril.
[36c9234]968 *
[80649a91]969 */
[01c3bb4]970static bool route_call(cap_handle_t chandle, 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
[01c3bb4]993 msg->chandle = chandle;
[80649a91]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)
[01c3bb4]998 conn->close_chandle = chandle;
[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 call Data of the incoming call.
[58563585]1020 *
[c07544d3]1021 */
[01c3bb4]1022static void process_notification(ipc_call_t *call)
[c07544d3]1023{
[8820544]1024 async_notification_handler_t handler = NULL;
1025 void *data = NULL;
[c170438]1026
1027 assert(call);
[8820544]1028
1029 futex_down(&async_futex);
1030
1031 ht_link_t *link = hash_table_find(&notification_hash_table,
[c170438]1032 &IPC_GET_IMETHOD(*call));
[8820544]1033 if (link) {
1034 notification_t *notification =
1035 hash_table_get_inst(link, notification_t, link);
1036 handler = notification->handler;
1037 data = notification->data;
1038 }
1039
1040 futex_up(&async_futex);
1041
1042 if (handler)
[01c3bb4]1043 handler(call, data);
[80649a91]1044}
1045
[8820544]1046/** Subscribe to IRQ notification.
1047 *
1048 * @param inr IRQ number.
1049 * @param handler Notification handler.
1050 * @param data Notification handler client data.
1051 * @param ucode Top-half pseudocode handler.
1052 *
[3f74275]1053 * @return IRQ capability handle on success.
[e9d15d9]1054 * @return Negative error code.
[8820544]1055 *
1056 */
[24abb85d]1057int async_irq_subscribe(int inr, async_notification_handler_t handler,
1058 void *data, const irq_code_t *ucode)
[8820544]1059{
1060 notification_t *notification =
1061 (notification_t *) malloc(sizeof(notification_t));
1062 if (!notification)
1063 return ENOMEM;
1064
1065 futex_down(&async_futex);
1066
1067 sysarg_t imethod = notification_avail;
1068 notification_avail++;
1069
1070 notification->imethod = imethod;
1071 notification->handler = handler;
1072 notification->data = data;
1073
1074 hash_table_insert(&notification_hash_table, &notification->link);
1075
1076 futex_up(&async_futex);
1077
[24abb85d]1078 return ipc_irq_subscribe(inr, imethod, ucode);
[8820544]1079}
1080
1081/** Unsubscribe from IRQ notification.
1082 *
[3f74275]1083 * @param cap IRQ capability handle.
[8820544]1084 *
1085 * @return Zero on success or a negative error code.
1086 *
1087 */
[e9d15d9]1088int async_irq_unsubscribe(int cap)
[8820544]1089{
1090 // TODO: Remove entry from hash table
1091 // to avoid memory leak
1092
[e9d15d9]1093 return ipc_irq_unsubscribe(cap);
[8820544]1094}
1095
1096/** Subscribe to event notifications.
1097 *
1098 * @param evno Event type to subscribe.
1099 * @param handler Notification handler.
1100 * @param data Notification handler client data.
1101 *
1102 * @return Zero on success or a negative error code.
1103 *
1104 */
1105int async_event_subscribe(event_type_t evno,
1106 async_notification_handler_t handler, void *data)
1107{
1108 notification_t *notification =
1109 (notification_t *) malloc(sizeof(notification_t));
1110 if (!notification)
1111 return ENOMEM;
1112
1113 futex_down(&async_futex);
1114
1115 sysarg_t imethod = notification_avail;
1116 notification_avail++;
1117
1118 notification->imethod = imethod;
1119 notification->handler = handler;
1120 notification->data = data;
1121
1122 hash_table_insert(&notification_hash_table, &notification->link);
1123
1124 futex_up(&async_futex);
1125
1126 return ipc_event_subscribe(evno, imethod);
1127}
1128
1129/** Subscribe to task event notifications.
1130 *
1131 * @param evno Event type to subscribe.
1132 * @param handler Notification handler.
1133 * @param data Notification handler client data.
1134 *
1135 * @return Zero on success or a negative error code.
1136 *
1137 */
1138int async_event_task_subscribe(event_task_type_t evno,
1139 async_notification_handler_t handler, void *data)
1140{
1141 notification_t *notification =
1142 (notification_t *) malloc(sizeof(notification_t));
1143 if (!notification)
1144 return ENOMEM;
1145
1146 futex_down(&async_futex);
1147
1148 sysarg_t imethod = notification_avail;
1149 notification_avail++;
1150
1151 notification->imethod = imethod;
1152 notification->handler = handler;
1153 notification->data = data;
1154
1155 hash_table_insert(&notification_hash_table, &notification->link);
1156
1157 futex_up(&async_futex);
1158
1159 return ipc_event_task_subscribe(evno, imethod);
1160}
1161
1162/** Unmask event notifications.
1163 *
1164 * @param evno Event type to unmask.
1165 *
1166 * @return Value returned by the kernel.
1167 *
1168 */
1169int async_event_unmask(event_type_t evno)
1170{
1171 return ipc_event_unmask(evno);
1172}
1173
1174/** Unmask task event notifications.
1175 *
1176 * @param evno Event type to unmask.
1177 *
1178 * @return Value returned by the kernel.
1179 *
1180 */
1181int async_event_task_unmask(event_task_type_t evno)
1182{
1183 return ipc_event_task_unmask(evno);
1184}
1185
[e70bfa5]1186/** Return new incoming message for the current (fibril-local) connection.
1187 *
[01c3bb4]1188 * @param call Storage where the incoming call data will be stored.
1189 * @param usecs Timeout in microseconds. Zero denotes no timeout.
[e70bfa5]1190 *
[01c3bb4]1191 * @return If no timeout was specified, then a handle of the incoming call is
1192 * returned. If a timeout is specified, then a handle of the incoming
1193 * call is returned unless the timeout expires prior to receiving a
1194 * message. In that case zero CAP_NIL is returned.
[e70bfa5]1195 */
[01c3bb4]1196cap_handle_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
[80649a91]1197{
[79ae36dd]1198 assert(call);
1199 assert(fibril_connection);
[c07544d3]1200
1201 /* Why doing this?
[79ae36dd]1202 * GCC 4.1.0 coughs on fibril_connection-> dereference.
[6c46350]1203 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
[c07544d3]1204 * I would never expect to find so many errors in
1205 * a compiler.
[6c46350]1206 */
[79ae36dd]1207 connection_t *conn = fibril_connection;
[c07544d3]1208
[01ff41c]1209 futex_down(&async_futex);
[c07544d3]1210
[49d072e]1211 if (usecs) {
[45cbcaf4]1212 getuptime(&conn->wdata.to_event.expires);
[7f9d97f3]1213 tv_add_diff(&conn->wdata.to_event.expires, usecs);
[c07544d3]1214 } else
[f53cc81]1215 conn->wdata.to_event.inlist = false;
[c07544d3]1216
[e70bfa5]1217 /* If nothing in queue, wait until something arrives */
[6c46350]1218 while (list_empty(&conn->msg_queue)) {
[01c3bb4]1219 if (conn->close_chandle) {
[8c8f8d6]1220 /*
1221 * Handle the case when the connection was already
1222 * closed by the client but the server did not notice
1223 * the first IPC_M_PHONE_HUNGUP call and continues to
1224 * call async_get_call_timeout(). Repeat
[47b7006]1225 * IPC_M_PHONE_HUNGUP until the caller notices.
[8c8f8d6]1226 */
1227 memset(call, 0, sizeof(ipc_call_t));
[228e490]1228 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
[8c8f8d6]1229 futex_up(&async_futex);
[01c3bb4]1230 return conn->close_chandle;
[8c8f8d6]1231 }
[47b7006]1232
[085bd54]1233 if (usecs)
[b6ee5b1]1234 async_insert_timeout(&conn->wdata);
[c07544d3]1235
1236 conn->wdata.active = false;
1237
[c7509e5]1238 /*
1239 * Note: the current fibril will be rescheduled either due to a
1240 * timeout or due to an arriving message destined to it. In the
1241 * former case, handle_expired_timeouts() and, in the latter
1242 * case, route_call() will perform the wakeup.
1243 */
[116d3f6f]1244 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1245
[e70bfa5]1246 /*
[c07544d3]1247 * Futex is up after getting back from async_manager.
1248 * Get it again.
[c7509e5]1249 */
[49d072e]1250 futex_down(&async_futex);
[f53cc81]1251 if ((usecs) && (conn->wdata.to_event.occurred)
[c07544d3]1252 && (list_empty(&conn->msg_queue))) {
[e70bfa5]1253 /* If we timed out -> exit */
[49d072e]1254 futex_up(&async_futex);
[01c3bb4]1255 return CAP_NIL;
[49d072e]1256 }
[450cd3a]1257 }
1258
[57dea62]1259 msg_t *msg = list_get_instance(list_first(&conn->msg_queue),
1260 msg_t, link);
[80649a91]1261 list_remove(&msg->link);
[c07544d3]1262
[01c3bb4]1263 cap_handle_t chandle = msg->chandle;
[80649a91]1264 *call = msg->call;
1265 free(msg);
1266
[01ff41c]1267 futex_up(&async_futex);
[01c3bb4]1268 return chandle;
[80649a91]1269}
1270
[455f190]1271void *async_get_client_data(void)
1272{
1273 assert(fibril_connection);
1274 return fibril_connection->client->data;
1275}
1276
[e2ab36f1]1277void *async_get_client_data_by_id(task_id_t client_id)
[455f190]1278{
[e2ab36f1]1279 client_t *client = async_client_get(client_id, false);
[455f190]1280 if (!client)
1281 return NULL;
[57dea62]1282
[455f190]1283 if (!client->data) {
1284 async_client_put(client);
1285 return NULL;
1286 }
[57dea62]1287
[455f190]1288 return client->data;
1289}
1290
[e2ab36f1]1291void async_put_client_data_by_id(task_id_t client_id)
[455f190]1292{
[e2ab36f1]1293 client_t *client = async_client_get(client_id, false);
[57dea62]1294
[455f190]1295 assert(client);
1296 assert(client->data);
[57dea62]1297
[cdc8ee2d]1298 /* Drop the reference we got in async_get_client_data_by_hash(). */
1299 async_client_put(client);
[57dea62]1300
[cdc8ee2d]1301 /* Drop our own reference we got at the beginning of this function. */
[455f190]1302 async_client_put(client);
1303}
1304
[566992e1]1305static port_t *async_find_port(iface_t iface, port_id_t port_id)
1306{
1307 port_t *port = NULL;
1308
1309 futex_down(&async_futex);
1310
1311 ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
1312 if (link) {
1313 interface_t *interface =
1314 hash_table_get_inst(link, interface_t, link);
1315
1316 link = hash_table_find(&interface->port_hash_table, &port_id);
1317 if (link)
1318 port = hash_table_get_inst(link, port_t, link);
1319 }
1320
1321 futex_up(&async_futex);
1322
1323 return port;
1324}
1325
[36c9234]1326/** Handle a call that was received.
1327 *
1328 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
1329 * Otherwise the call is routed to its connection fibril.
1330 *
[01c3bb4]1331 * @param chandle Handle of the incoming call.
1332 * @param call Data of the incoming call.
[6b21292]1333 *
[36c9234]1334 */
[01c3bb4]1335static void handle_call(cap_handle_t chandle, ipc_call_t *call)
[80649a91]1336{
[79ae36dd]1337 assert(call);
1338
[b688fd8]1339 /* Kernel notification */
[01c3bb4]1340 if ((chandle == CAP_NIL) && (call->flags & IPC_CALLID_NOTIFICATION)) {
[c170438]1341 fibril_t *fibril = (fibril_t *) __tcb_get()->fibril_data;
1342 unsigned oldsw = fibril->switches;
[58563585]1343
[01c3bb4]1344 process_notification(call);
[58563585]1345
[c170438]1346 if (oldsw != fibril->switches) {
1347 /*
1348 * The notification handler did not execute atomically
1349 * and so the current manager fibril assumed the role of
1350 * a notification fibril. While waiting for its
1351 * resources, it switched to another manager fibril that
1352 * had already existed or it created a new one. We
1353 * therefore know there is at least yet another
1354 * manager fibril that can take over. We now kill the
1355 * current 'notification' fibril to prevent fibril
1356 * population explosion.
1357 */
1358 futex_down(&async_futex);
1359 fibril_switch(FIBRIL_FROM_DEAD);
1360 }
[58563585]1361
[47b7006]1362 return;
[6b21292]1363 }
1364
[566992e1]1365 /* New connection */
1366 if (IPC_GET_IMETHOD(*call) == IPC_M_CONNECT_ME_TO) {
1367 iface_t iface = (iface_t) IPC_GET_ARG1(*call);
1368 sysarg_t in_phone_hash = IPC_GET_ARG5(*call);
1369
[01c3bb4]1370 async_port_handler_t handler = fallback_port_handler;
[566992e1]1371 void *data = fallback_port_data;
1372
1373 // TODO: Currently ignores all ports but the first one
1374 port_t *port = async_find_port(iface, 0);
1375 if (port) {
1376 handler = port->handler;
1377 data = port->data;
1378 }
1379
[01c3bb4]1380 async_new_connection(call->in_task_id, in_phone_hash, chandle,
[566992e1]1381 call, handler, data);
1382 return;
1383 }
1384
[36c9234]1385 /* Try to route the call through the connection hash table */
[01c3bb4]1386 if (route_call(chandle, call))
[47b7006]1387 return;
[6b21292]1388
[44c6d88d]1389 /* Unknown call from unknown phone - hang it up */
[01c3bb4]1390 ipc_answer_0(chandle, EHANGUP);
[450cd3a]1391}
1392
[f2f0392]1393/** Fire all timeouts that expired. */
[c042bdd]1394static void handle_expired_timeouts(void)
1395{
1396 struct timeval tv;
[45cbcaf4]1397 getuptime(&tv);
[c07544d3]1398
[c042bdd]1399 futex_down(&async_futex);
[c07544d3]1400
[b72efe8]1401 link_t *cur = list_first(&timeout_list);
1402 while (cur != NULL) {
[47b7006]1403 awaiter_t *waiter =
1404 list_get_instance(cur, awaiter_t, to_event.link);
[c07544d3]1405
[f53cc81]1406 if (tv_gt(&waiter->to_event.expires, &tv))
[c042bdd]1407 break;
[47b7006]1408
[f53cc81]1409 list_remove(&waiter->to_event.link);
1410 waiter->to_event.inlist = false;
1411 waiter->to_event.occurred = true;
[c07544d3]1412
[36c9234]1413 /*
[c07544d3]1414 * Redundant condition?
1415 * The fibril should not be active when it gets here.
[c042bdd]1416 */
[49d072e]1417 if (!waiter->active) {
[c07544d3]1418 waiter->active = true;
[bc1f1c2]1419 fibril_add_ready(waiter->fid);
[c042bdd]1420 }
[b72efe8]1421
1422 cur = list_first(&timeout_list);
[c042bdd]1423 }
[c07544d3]1424
[c042bdd]1425 futex_up(&async_futex);
1426}
1427
[36c9234]1428/** Endless loop dispatching incoming calls and answers.
1429 *
[c07544d3]1430 * @return Never returns.
1431 *
[36c9234]1432 */
[085bd54]1433static int async_manager_worker(void)
[80649a91]1434{
[c07544d3]1435 while (true) {
[116d3f6f]1436 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
[47b7006]1437 futex_up(&async_futex);
[36c9234]1438 /*
1439 * async_futex is always held when entering a manager
1440 * fibril.
[a46da63]1441 */
[80649a91]1442 continue;
1443 }
[c07544d3]1444
[c042bdd]1445 futex_down(&async_futex);
[c07544d3]1446
1447 suseconds_t timeout;
[1db6dfd]1448 unsigned int flags = SYNCH_FLAGS_NONE;
[c042bdd]1449 if (!list_empty(&timeout_list)) {
[b72efe8]1450 awaiter_t *waiter = list_get_instance(
1451 list_first(&timeout_list), awaiter_t, to_event.link);
[c07544d3]1452
1453 struct timeval tv;
[45cbcaf4]1454 getuptime(&tv);
[c07544d3]1455
[f53cc81]1456 if (tv_gteq(&tv, &waiter->to_event.expires)) {
[6c46350]1457 futex_up(&async_futex);
[c042bdd]1458 handle_expired_timeouts();
[1db6dfd]1459 /*
1460 * Notice that even if the event(s) already
1461 * expired (and thus the other fibril was
1462 * supposed to be running already),
1463 * we check for incoming IPC.
1464 *
1465 * Otherwise, a fibril that continuously
1466 * creates (almost) expired events could
1467 * prevent IPC retrieval from the kernel.
1468 */
1469 timeout = 0;
1470 flags = SYNCH_FLAGS_NON_BLOCKING;
1471
1472 } else {
[7f9d97f3]1473 timeout = tv_sub_diff(&waiter->to_event.expires,
1474 &tv);
[1db6dfd]1475 futex_up(&async_futex);
1476 }
1477 } else {
1478 futex_up(&async_futex);
[0b99e40]1479 timeout = SYNCH_NO_TIMEOUT;
[1db6dfd]1480 }
[47b7006]1481
[8619f25]1482 atomic_inc(&threads_in_ipc_wait);
[c07544d3]1483
1484 ipc_call_t call;
[01c3bb4]1485 cap_handle_t chandle = ipc_wait_cycle(&call, timeout, flags);
[c07544d3]1486
[8619f25]1487 atomic_dec(&threads_in_ipc_wait);
[47b7006]1488
[01c3bb4]1489 assert(chandle >= 0);
1490
1491 if (chandle == CAP_NIL) {
1492 if (call.flags == 0) {
1493 /* This neither a notification nor an answer. */
1494 handle_expired_timeouts();
1495 continue;
1496 }
[0b99e40]1497 }
[01c3bb4]1498
[503ffce]1499 if (call.flags & IPC_CALLID_ANSWERED)
[80649a91]1500 continue;
[01c3bb4]1501
1502 handle_call(chandle, &call);
[80649a91]1503 }
[01c3bb4]1504
[a46da63]1505 return 0;
[80649a91]1506}
1507
[36c9234]1508/** Function to start async_manager as a standalone fibril.
[c07544d3]1509 *
[36c9234]1510 * When more kernel threads are used, one async manager should exist per thread.
1511 *
[c07544d3]1512 * @param arg Unused.
1513 * @return Never returns.
[36c9234]1514 *
[a2cd194]1515 */
[9591265]1516static int async_manager_fibril(void *arg)
[80649a91]1517{
[a46da63]1518 futex_up(&async_futex);
[c07544d3]1519
[36c9234]1520 /*
1521 * async_futex is always locked when entering manager
1522 */
[085bd54]1523 async_manager_worker();
[a46da63]1524
1525 return 0;
[80649a91]1526}
[450cd3a]1527
[36c9234]1528/** Add one manager to manager list. */
[80649a91]1529void async_create_manager(void)
[450cd3a]1530{
[c170438]1531 fid_t fid = fibril_create_generic(async_manager_fibril, NULL, PAGE_SIZE);
[86d7bfa]1532 if (fid != 0)
1533 fibril_add_manager(fid);
[80649a91]1534}
1535
1536/** Remove one manager from manager list */
1537void async_destroy_manager(void)
1538{
[bc1f1c2]1539 fibril_remove_manager();
[80649a91]1540}
1541
[36c9234]1542/** Initialize the async framework.
1543 *
1544 */
[47b7006]1545void __async_init(void)
[80649a91]1546{
[566992e1]1547 if (!hash_table_create(&interface_hash_table, 0, 0,
1548 &interface_hash_table_ops))
1549 abort();
1550
[062d900]1551 if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
[47b7006]1552 abort();
[80649a91]1553
[062d900]1554 if (!hash_table_create(&conn_hash_table, 0, 0, &conn_hash_table_ops))
[47b7006]1555 abort();
[79ae36dd]1556
[8820544]1557 if (!hash_table_create(&notification_hash_table, 0, 0,
1558 &notification_hash_table_ops))
1559 abort();
1560
[79ae36dd]1561 session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
1562 if (session_ns == NULL)
1563 abort();
1564
[566992e1]1565 session_ns->iface = 0;
[79ae36dd]1566 session_ns->mgmt = EXCHANGE_ATOMIC;
1567 session_ns->phone = PHONE_NS;
1568 session_ns->arg1 = 0;
1569 session_ns->arg2 = 0;
1570 session_ns->arg3 = 0;
1571
[58cbf8d5]1572 fibril_mutex_initialize(&session_ns->remote_state_mtx);
1573 session_ns->remote_state_data = NULL;
1574
[79ae36dd]1575 list_initialize(&session_ns->exch_list);
1576 fibril_mutex_initialize(&session_ns->mutex);
1577 atomic_set(&session_ns->refcnt, 0);
[450cd3a]1578}
[01ff41c]1579
[36c9234]1580/** Reply received callback.
[01ff41c]1581 *
[36c9234]1582 * This function is called whenever a reply for an asynchronous message sent out
1583 * by the asynchronous framework is received.
1584 *
1585 * Notify the fibril which is waiting for this message that it has arrived.
1586 *
[c07544d3]1587 * @param arg Pointer to the asynchronous message record.
1588 * @param retval Value returned in the answer.
1589 * @param data Call data of the answer.
[47b7006]1590 *
[01ff41c]1591 */
[79ae36dd]1592void reply_received(void *arg, int retval, ipc_call_t *data)
[01ff41c]1593{
[79ae36dd]1594 assert(arg);
1595
[9db9b10]1596 futex_down(&async_futex);
1597
[c07544d3]1598 amsg_t *msg = (amsg_t *) arg;
[01ff41c]1599 msg->retval = retval;
[c07544d3]1600
[36c9234]1601 /* Copy data after futex_down, just in case the call was detached */
[9db9b10]1602 if ((msg->dataptr) && (data))
[c07544d3]1603 *msg->dataptr = *data;
1604
[c042bdd]1605 write_barrier();
[c07544d3]1606
[c042bdd]1607 /* Remove message from timeout list */
[f53cc81]1608 if (msg->wdata.to_event.inlist)
1609 list_remove(&msg->wdata.to_event.link);
[c07544d3]1610
1611 msg->done = true;
[57dea62]1612
[47c9a8c]1613 if (msg->forget) {
1614 assert(msg->wdata.active);
1615 amsg_destroy(msg);
1616 } else if (!msg->wdata.active) {
[c07544d3]1617 msg->wdata.active = true;
[bc1f1c2]1618 fibril_add_ready(msg->wdata.fid);
[01ff41c]1619 }
[57dea62]1620
[01ff41c]1621 futex_up(&async_futex);
1622}
1623
[36c9234]1624/** Send message and return id of the sent message.
1625 *
1626 * The return value can be used as input for async_wait() to wait for
1627 * completion.
[01ff41c]1628 *
[79ae36dd]1629 * @param exch Exchange for sending the message.
1630 * @param imethod Service-defined interface and method.
[c07544d3]1631 * @param arg1 Service-defined payload argument.
1632 * @param arg2 Service-defined payload argument.
1633 * @param arg3 Service-defined payload argument.
1634 * @param arg4 Service-defined payload argument.
[01c3bb4]1635 * @param dataptr If non-NULL, storage where the reply data will be stored.
[c07544d3]1636 *
1637 * @return Hash of the sent message or 0 on error.
[36c9234]1638 *
[01ff41c]1639 */
[79ae36dd]1640aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1641 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[01ff41c]1642{
[79ae36dd]1643 if (exch == NULL)
1644 return 0;
[c07544d3]1645
[47c9a8c]1646 amsg_t *msg = amsg_create();
[79ae36dd]1647 if (msg == NULL)
[c07544d3]1648 return 0;
[6b21292]1649
[01ff41c]1650 msg->dataptr = dataptr;
[c07544d3]1651 msg->wdata.active = true;
1652
[79ae36dd]1653 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
[dcc150cb]1654 reply_received);
[6b21292]1655
[01ff41c]1656 return (aid_t) msg;
1657}
1658
[90f5d64]1659/** Send message and return id of the sent message
1660 *
[36c9234]1661 * The return value can be used as input for async_wait() to wait for
1662 * completion.
1663 *
[79ae36dd]1664 * @param exch Exchange for sending the message.
1665 * @param imethod Service-defined interface and method.
[c07544d3]1666 * @param arg1 Service-defined payload argument.
1667 * @param arg2 Service-defined payload argument.
1668 * @param arg3 Service-defined payload argument.
1669 * @param arg4 Service-defined payload argument.
1670 * @param arg5 Service-defined payload argument.
1671 * @param dataptr If non-NULL, storage where the reply data will be
1672 * stored.
1673 *
1674 * @return Hash of the sent message or 0 on error.
[36c9234]1675 *
[90f5d64]1676 */
[79ae36dd]1677aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1678 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[0cc4313]1679 ipc_call_t *dataptr)
[90f5d64]1680{
[79ae36dd]1681 if (exch == NULL)
1682 return 0;
1683
[47c9a8c]1684 amsg_t *msg = amsg_create();
[79ae36dd]1685 if (msg == NULL)
[c07544d3]1686 return 0;
1687
[90f5d64]1688 msg->dataptr = dataptr;
[c07544d3]1689 msg->wdata.active = true;
[6b21292]1690
[79ae36dd]1691 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
[dcc150cb]1692 msg, reply_received);
[6b21292]1693
[90f5d64]1694 return (aid_t) msg;
1695}
1696
[36c9234]1697/** Wait for a message sent by the async framework.
[01ff41c]1698 *
[c07544d3]1699 * @param amsgid Hash of the message to wait for.
1700 * @param retval Pointer to storage where the retval of the answer will
1701 * be stored.
1702 *
[01ff41c]1703 */
[96b02eb9]1704void async_wait_for(aid_t amsgid, sysarg_t *retval)
[01ff41c]1705{
[79ae36dd]1706 assert(amsgid);
1707
[01ff41c]1708 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]1709
[01ff41c]1710 futex_down(&async_futex);
[57dea62]1711
[47c9a8c]1712 assert(!msg->forget);
1713 assert(!msg->destroyed);
[57dea62]1714
[01ff41c]1715 if (msg->done) {
1716 futex_up(&async_futex);
1717 goto done;
1718 }
[c07544d3]1719
[bc1f1c2]1720 msg->wdata.fid = fibril_get_id();
[c07544d3]1721 msg->wdata.active = false;
[f53cc81]1722 msg->wdata.to_event.inlist = false;
[c07544d3]1723
[36c9234]1724 /* Leave the async_futex locked when entering this function */
[116d3f6f]1725 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1726
1727 /* Futex is up automatically after fibril_switch */
1728
[01ff41c]1729done:
1730 if (retval)
1731 *retval = msg->retval;
[c07544d3]1732
[47c9a8c]1733 amsg_destroy(msg);
[01ff41c]1734}
[0b99e40]1735
[36c9234]1736/** Wait for a message sent by the async framework, timeout variant.
[47c9a8c]1737 *
1738 * If the wait times out, the caller may choose to either wait again by calling
1739 * async_wait_for() or async_wait_timeout(), or forget the message via
1740 * async_forget().
[c042bdd]1741 *
[c07544d3]1742 * @param amsgid Hash of the message to wait for.
1743 * @param retval Pointer to storage where the retval of the answer will
1744 * be stored.
1745 * @param timeout Timeout in microseconds.
1746 *
1747 * @return Zero on success, ETIMEOUT if the timeout has expired.
[c042bdd]1748 *
1749 */
[96b02eb9]1750int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
[c042bdd]1751{
[79ae36dd]1752 assert(amsgid);
1753
[c042bdd]1754 amsg_t *msg = (amsg_t *) amsgid;
[57dea62]1755
[c042bdd]1756 futex_down(&async_futex);
[57dea62]1757
[47c9a8c]1758 assert(!msg->forget);
1759 assert(!msg->destroyed);
[57dea62]1760
[c042bdd]1761 if (msg->done) {
1762 futex_up(&async_futex);
1763 goto done;
1764 }
[c07544d3]1765
[1db6dfd]1766 /*
1767 * Negative timeout is converted to zero timeout to avoid
1768 * using tv_add with negative augmenter.
1769 */
1770 if (timeout < 0)
1771 timeout = 0;
[57dea62]1772
[45cbcaf4]1773 getuptime(&msg->wdata.to_event.expires);
[7f9d97f3]1774 tv_add_diff(&msg->wdata.to_event.expires, timeout);
[c07544d3]1775
[1db6dfd]1776 /*
1777 * Current fibril is inserted as waiting regardless of the
1778 * "size" of the timeout.
1779 *
1780 * Checking for msg->done and immediately bailing out when
1781 * timeout == 0 would mean that the manager fibril would never
1782 * run (consider single threaded program).
1783 * Thus the IPC answer would be never retrieved from the kernel.
1784 *
1785 * Notice that the actual delay would be very small because we
1786 * - switch to manager fibril
1787 * - the manager sees expired timeout
1788 * - and thus adds us back to ready queue
1789 * - manager switches back to some ready fibril
1790 * (prior it, it checks for incoming IPC).
1791 *
1792 */
[bc1f1c2]1793 msg->wdata.fid = fibril_get_id();
[c07544d3]1794 msg->wdata.active = false;
[b6ee5b1]1795 async_insert_timeout(&msg->wdata);
[c07544d3]1796
[36c9234]1797 /* Leave the async_futex locked when entering this function */
[116d3f6f]1798 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1799
1800 /* Futex is up automatically after fibril_switch */
1801
[c042bdd]1802 if (!msg->done)
1803 return ETIMEOUT;
[c07544d3]1804
[c042bdd]1805done:
1806 if (retval)
1807 *retval = msg->retval;
[c07544d3]1808
[47c9a8c]1809 amsg_destroy(msg);
[c07544d3]1810
[c042bdd]1811 return 0;
1812}
[47c9a8c]1813
1814/** Discard the message / reply on arrival.
1815 *
1816 * The message will be marked to be discarded once the reply arrives in
1817 * reply_received(). It is not allowed to call async_wait_for() or
1818 * async_wait_timeout() on this message after a call to this function.
1819 *
1820 * @param amsgid Hash of the message to forget.
1821 */
1822void async_forget(aid_t amsgid)
1823{
1824 amsg_t *msg = (amsg_t *) amsgid;
[57dea62]1825
[47c9a8c]1826 assert(msg);
1827 assert(!msg->forget);
1828 assert(!msg->destroyed);
[57dea62]1829
[47c9a8c]1830 futex_down(&async_futex);
[57dea62]1831
[375e501]1832 if (msg->done) {
[47c9a8c]1833 amsg_destroy(msg);
[375e501]1834 } else {
1835 msg->dataptr = NULL;
[47c9a8c]1836 msg->forget = true;
[375e501]1837 }
[57dea62]1838
[47c9a8c]1839 futex_up(&async_futex);
1840}
[0b99e40]1841
[36c9234]1842/** Wait for specified time.
[44c6d88d]1843 *
[36c9234]1844 * The current fibril is suspended but the thread continues to execute.
1845 *
[c07544d3]1846 * @param timeout Duration of the wait in microseconds.
1847 *
[44c6d88d]1848 */
1849void async_usleep(suseconds_t timeout)
1850{
[47c9a8c]1851 amsg_t *msg = amsg_create();
[44c6d88d]1852 if (!msg)
1853 return;
[6b21292]1854
[bc1f1c2]1855 msg->wdata.fid = fibril_get_id();
[6b21292]1856
[45cbcaf4]1857 getuptime(&msg->wdata.to_event.expires);
[7f9d97f3]1858 tv_add_diff(&msg->wdata.to_event.expires, timeout);
[6b21292]1859
[44c6d88d]1860 futex_down(&async_futex);
[c07544d3]1861
[b6ee5b1]1862 async_insert_timeout(&msg->wdata);
[c07544d3]1863
[36c9234]1864 /* Leave the async_futex locked when entering this function */
[116d3f6f]1865 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1866
1867 /* Futex is up automatically after fibril_switch() */
1868
[47c9a8c]1869 amsg_destroy(msg);
[44c6d88d]1870}
[da0c91e7]1871
[39026d7c]1872/** Delay execution for the specified number of seconds
1873 *
1874 * @param sec Number of seconds to sleep
1875 */
1876void async_sleep(unsigned int sec)
1877{
1878 /*
1879 * Sleep in 1000 second steps to support
1880 * full argument range
1881 */
1882
1883 while (sec > 0) {
1884 unsigned int period = (sec > 1000) ? 1000 : sec;
1885
1886 async_usleep(period * 1000000);
1887 sec -= period;
1888 }
1889}
1890
[0cc4313]1891/** Pseudo-synchronous message sending - fast version.
1892 *
1893 * Send message asynchronously and return only after the reply arrives.
1894 *
1895 * This function can only transfer 4 register payload arguments. For
1896 * transferring more arguments, see the slower async_req_slow().
1897 *
[79ae36dd]1898 * @param exch Exchange for sending the message.
1899 * @param imethod Interface and method of the call.
[c07544d3]1900 * @param arg1 Service-defined payload argument.
1901 * @param arg2 Service-defined payload argument.
1902 * @param arg3 Service-defined payload argument.
1903 * @param arg4 Service-defined payload argument.
1904 * @param r1 If non-NULL, storage for the 1st reply argument.
1905 * @param r2 If non-NULL, storage for the 2nd reply argument.
1906 * @param r3 If non-NULL, storage for the 3rd reply argument.
1907 * @param r4 If non-NULL, storage for the 4th reply argument.
1908 * @param r5 If non-NULL, storage for the 5th reply argument.
1909 *
1910 * @return Return code of the reply or a negative error code.
1911 *
[0cc4313]1912 */
[79ae36dd]1913sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1914 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1915 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1916{
[79ae36dd]1917 if (exch == NULL)
1918 return ENOENT;
1919
[0cc4313]1920 ipc_call_t result;
[79ae36dd]1921 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
[0cc4313]1922 &result);
[c07544d3]1923
[96b02eb9]1924 sysarg_t rc;
[79ae36dd]1925 async_wait_for(aid, &rc);
[c07544d3]1926
1927 if (r1)
[0cc4313]1928 *r1 = IPC_GET_ARG1(result);
[c07544d3]1929
[0cc4313]1930 if (r2)
1931 *r2 = IPC_GET_ARG2(result);
[c07544d3]1932
[0cc4313]1933 if (r3)
1934 *r3 = IPC_GET_ARG3(result);
[c07544d3]1935
[0cc4313]1936 if (r4)
1937 *r4 = IPC_GET_ARG4(result);
[c07544d3]1938
[0cc4313]1939 if (r5)
1940 *r5 = IPC_GET_ARG5(result);
[c07544d3]1941
[0cc4313]1942 return rc;
[085bd54]1943}
1944
[0cc4313]1945/** Pseudo-synchronous message sending - slow version.
1946 *
1947 * Send message asynchronously and return only after the reply arrives.
1948 *
[79ae36dd]1949 * @param exch Exchange for sending the message.
1950 * @param imethod Interface and method of the call.
[c07544d3]1951 * @param arg1 Service-defined payload argument.
1952 * @param arg2 Service-defined payload argument.
1953 * @param arg3 Service-defined payload argument.
1954 * @param arg4 Service-defined payload argument.
1955 * @param arg5 Service-defined payload argument.
1956 * @param r1 If non-NULL, storage for the 1st reply argument.
1957 * @param r2 If non-NULL, storage for the 2nd reply argument.
1958 * @param r3 If non-NULL, storage for the 3rd reply argument.
1959 * @param r4 If non-NULL, storage for the 4th reply argument.
1960 * @param r5 If non-NULL, storage for the 5th reply argument.
1961 *
1962 * @return Return code of the reply or a negative error code.
1963 *
[0cc4313]1964 */
[79ae36dd]1965sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1966 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1967 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1968{
[79ae36dd]1969 if (exch == NULL)
1970 return ENOENT;
1971
[0cc4313]1972 ipc_call_t result;
[79ae36dd]1973 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
[0cc4313]1974 &result);
[c07544d3]1975
[96b02eb9]1976 sysarg_t rc;
[79ae36dd]1977 async_wait_for(aid, &rc);
[c07544d3]1978
1979 if (r1)
[0cc4313]1980 *r1 = IPC_GET_ARG1(result);
[c07544d3]1981
[0cc4313]1982 if (r2)
1983 *r2 = IPC_GET_ARG2(result);
[c07544d3]1984
[0cc4313]1985 if (r3)
1986 *r3 = IPC_GET_ARG3(result);
[c07544d3]1987
[0cc4313]1988 if (r4)
1989 *r4 = IPC_GET_ARG4(result);
[c07544d3]1990
[0cc4313]1991 if (r5)
1992 *r5 = IPC_GET_ARG5(result);
[c07544d3]1993
[0cc4313]1994 return rc;
[085bd54]1995}
[b2951e2]1996
[79ae36dd]1997void async_msg_0(async_exch_t *exch, sysarg_t imethod)
[64d2b10]1998{
[79ae36dd]1999 if (exch != NULL)
[dcc150cb]2000 ipc_call_async_0(exch->phone, imethod, NULL, NULL);
[64d2b10]2001}
2002
[79ae36dd]2003void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
[64d2b10]2004{
[79ae36dd]2005 if (exch != NULL)
[dcc150cb]2006 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL);
[64d2b10]2007}
2008
[79ae36dd]2009void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2010 sysarg_t arg2)
[64d2b10]2011{
[79ae36dd]2012 if (exch != NULL)
[dcc150cb]2013 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL);
[64d2b10]2014}
2015
[79ae36dd]2016void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2017 sysarg_t arg2, sysarg_t arg3)
[64d2b10]2018{
[79ae36dd]2019 if (exch != NULL)
2020 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
[dcc150cb]2021 NULL);
[64d2b10]2022}
2023
[79ae36dd]2024void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2025 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[64d2b10]2026{
[79ae36dd]2027 if (exch != NULL)
2028 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
[dcc150cb]2029 NULL, NULL);
[64d2b10]2030}
2031
[79ae36dd]2032void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2033 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
[64d2b10]2034{
[79ae36dd]2035 if (exch != NULL)
2036 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
[dcc150cb]2037 arg5, NULL, NULL);
[64d2b10]2038}
2039
[01c3bb4]2040sysarg_t async_answer_0(cap_handle_t chandle, sysarg_t retval)
[64d2b10]2041{
[01c3bb4]2042 return ipc_answer_0(chandle, retval);
[64d2b10]2043}
2044
[01c3bb4]2045sysarg_t async_answer_1(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1)
[64d2b10]2046{
[01c3bb4]2047 return ipc_answer_1(chandle, retval, arg1);
[64d2b10]2048}
2049
[01c3bb4]2050sysarg_t async_answer_2(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
[64d2b10]2051 sysarg_t arg2)
2052{
[01c3bb4]2053 return ipc_answer_2(chandle, retval, arg1, arg2);
[64d2b10]2054}
2055
[01c3bb4]2056sysarg_t async_answer_3(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
[64d2b10]2057 sysarg_t arg2, sysarg_t arg3)
2058{
[01c3bb4]2059 return ipc_answer_3(chandle, retval, arg1, arg2, arg3);
[64d2b10]2060}
2061
[01c3bb4]2062sysarg_t async_answer_4(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
[64d2b10]2063 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
2064{
[01c3bb4]2065 return ipc_answer_4(chandle, retval, arg1, arg2, arg3, arg4);
[64d2b10]2066}
2067
[01c3bb4]2068sysarg_t async_answer_5(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
[64d2b10]2069 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
2070{
[01c3bb4]2071 return ipc_answer_5(chandle, retval, arg1, arg2, arg3, arg4, arg5);
[64d2b10]2072}
2073
[01c3bb4]2074int async_forward_fast(cap_handle_t chandle, async_exch_t *exch,
[79ae36dd]2075 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[64d2b10]2076{
[79ae36dd]2077 if (exch == NULL)
2078 return ENOENT;
2079
[01c3bb4]2080 return ipc_forward_fast(chandle, exch->phone, imethod, arg1, arg2, mode);
[64d2b10]2081}
2082
[01c3bb4]2083int async_forward_slow(cap_handle_t chandle, async_exch_t *exch,
[79ae36dd]2084 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
2085 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
[64d2b10]2086{
[79ae36dd]2087 if (exch == NULL)
2088 return ENOENT;
2089
[01c3bb4]2090 return ipc_forward_slow(chandle, exch->phone, imethod, arg1, arg2, arg3,
[79ae36dd]2091 arg4, arg5, mode);
[64d2b10]2092}
2093
[007e6efa]2094/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
2095 *
2096 * Ask through phone for a new connection to some service.
2097 *
[79ae36dd]2098 * @param exch Exchange for sending the message.
[007e6efa]2099 * @param arg1 User defined argument.
2100 * @param arg2 User defined argument.
2101 * @param arg3 User defined argument.
2102 *
[79ae36dd]2103 * @return Zero on success or a negative error code.
[007e6efa]2104 *
2105 */
[79ae36dd]2106int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
[f9b2cb4c]2107 sysarg_t arg3)
[007e6efa]2108{
[79ae36dd]2109 if (exch == NULL)
2110 return ENOENT;
2111
[ab34cc9]2112 ipc_call_t answer;
[f9b2cb4c]2113 aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
[ab34cc9]2114 &answer);
[f9b2cb4c]2115
2116 sysarg_t rc;
[ab34cc9]2117 async_wait_for(req, &rc);
[007e6efa]2118 if (rc != EOK)
[ab34cc9]2119 return (int) rc;
[007e6efa]2120
2121 return EOK;
2122}
2123
[79ae36dd]2124static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
2125 sysarg_t arg3, sysarg_t arg4)
[f74392f]2126{
[79ae36dd]2127 ipc_call_t result;
2128
[47c9a8c]2129 amsg_t *msg = amsg_create();
2130 if (!msg)
[79ae36dd]2131 return ENOENT;
2132
2133 msg->dataptr = &result;
2134 msg->wdata.active = true;
2135
2136 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
[dcc150cb]2137 msg, reply_received);
[79ae36dd]2138
2139 sysarg_t rc;
2140 async_wait_for((aid_t) msg, &rc);
[f74392f]2141
[007e6efa]2142 if (rc != EOK)
[f74392f]2143 return rc;
[007e6efa]2144
[79ae36dd]2145 return (int) IPC_GET_ARG5(result);
2146}
2147
2148/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2149 *
2150 * Ask through for a new connection to some service.
2151 *
2152 * @param mgmt Exchange management style.
2153 * @param exch Exchange for sending the message.
2154 * @param arg1 User defined argument.
2155 * @param arg2 User defined argument.
2156 * @param arg3 User defined argument.
2157 *
2158 * @return New session on success or NULL on error.
2159 *
2160 */
2161async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
2162 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
2163{
2164 if (exch == NULL) {
2165 errno = ENOENT;
2166 return NULL;
2167 }
2168
2169 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2170 if (sess == NULL) {
2171 errno = ENOMEM;
2172 return NULL;
2173 }
2174
2175 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
2176 0);
2177 if (phone < 0) {
2178 errno = phone;
2179 free(sess);
2180 return NULL;
2181 }
2182
[566992e1]2183 sess->iface = 0;
[79ae36dd]2184 sess->mgmt = mgmt;
2185 sess->phone = phone;
2186 sess->arg1 = arg1;
2187 sess->arg2 = arg2;
2188 sess->arg3 = arg3;
2189
[58cbf8d5]2190 fibril_mutex_initialize(&sess->remote_state_mtx);
2191 sess->remote_state_data = NULL;
2192
[79ae36dd]2193 list_initialize(&sess->exch_list);
2194 fibril_mutex_initialize(&sess->mutex);
2195 atomic_set(&sess->refcnt, 0);
2196
2197 return sess;
[f74392f]2198}
2199
[0dd16778]2200/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2201 *
2202 * Ask through phone for a new connection to some service and block until
2203 * success.
2204 *
2205 * @param exch Exchange for sending the message.
2206 * @param iface Connection interface.
2207 * @param arg2 User defined argument.
2208 * @param arg3 User defined argument.
2209 *
2210 * @return New session on success or NULL on error.
2211 *
2212 */
2213async_sess_t *async_connect_me_to_iface(async_exch_t *exch, iface_t iface,
2214 sysarg_t arg2, sysarg_t arg3)
2215{
2216 if (exch == NULL) {
2217 errno = ENOENT;
2218 return NULL;
2219 }
2220
2221 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2222 if (sess == NULL) {
2223 errno = ENOMEM;
2224 return NULL;
2225 }
2226
2227 int phone = async_connect_me_to_internal(exch->phone, iface, arg2,
2228 arg3, 0);
2229 if (phone < 0) {
2230 errno = phone;
2231 free(sess);
2232 return NULL;
2233 }
2234
2235 sess->iface = iface;
2236 sess->phone = phone;
2237 sess->arg1 = iface;
2238 sess->arg2 = arg2;
2239 sess->arg3 = arg3;
2240
2241 fibril_mutex_initialize(&sess->remote_state_mtx);
2242 sess->remote_state_data = NULL;
2243
2244 list_initialize(&sess->exch_list);
2245 fibril_mutex_initialize(&sess->mutex);
2246 atomic_set(&sess->refcnt, 0);
2247
2248 return sess;
2249}
2250
[93ad49a8]2251/** Set arguments for new connections.
[0f4532e]2252 *
2253 * FIXME This is an ugly hack to work around the problem that parallel
2254 * exchanges are implemented using parallel connections. When we create
[93ad49a8]2255 * a callback session, the framework does not know arguments for the new
2256 * connections.
[0f4532e]2257 *
2258 * The proper solution seems to be to implement parallel exchanges using
2259 * tagging.
2260 */
[93ad49a8]2261void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
2262 sysarg_t arg3)
[0f4532e]2263{
[93ad49a8]2264 sess->arg1 = arg1;
2265 sess->arg2 = arg2;
2266 sess->arg3 = arg3;
[0f4532e]2267}
2268
[f74392f]2269/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
[007e6efa]2270 *
[f74392f]2271 * Ask through phone for a new connection to some service and block until
2272 * success.
2273 *
[79ae36dd]2274 * @param mgmt Exchange management style.
2275 * @param exch Exchange for sending the message.
2276 * @param arg1 User defined argument.
2277 * @param arg2 User defined argument.
2278 * @param arg3 User defined argument.
[007e6efa]2279 *
[79ae36dd]2280 * @return New session on success or NULL on error.
[f74392f]2281 *
2282 */
[79ae36dd]2283async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
2284 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
[f74392f]2285{
[79ae36dd]2286 if (exch == NULL) {
2287 errno = ENOENT;
2288 return NULL;
2289 }
[f74392f]2290
[79ae36dd]2291 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2292 if (sess == NULL) {
2293 errno = ENOMEM;
2294 return NULL;
2295 }
[007e6efa]2296
[79ae36dd]2297 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
2298 IPC_FLAG_BLOCKING);
2299
2300 if (phone < 0) {
2301 errno = phone;
2302 free(sess);
2303 return NULL;
2304 }
2305
[566992e1]2306 sess->iface = 0;
[79ae36dd]2307 sess->mgmt = mgmt;
2308 sess->phone = phone;
2309 sess->arg1 = arg1;
2310 sess->arg2 = arg2;
2311 sess->arg3 = arg3;
2312
[58cbf8d5]2313 fibril_mutex_initialize(&sess->remote_state_mtx);
2314 sess->remote_state_data = NULL;
2315
[79ae36dd]2316 list_initialize(&sess->exch_list);
2317 fibril_mutex_initialize(&sess->mutex);
2318 atomic_set(&sess->refcnt, 0);
2319
2320 return sess;
[f74392f]2321}
2322
[566992e1]2323/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2324 *
2325 * Ask through phone for a new connection to some service and block until
2326 * success.
2327 *
2328 * @param exch Exchange for sending the message.
2329 * @param iface Connection interface.
2330 * @param arg2 User defined argument.
2331 * @param arg3 User defined argument.
2332 *
2333 * @return New session on success or NULL on error.
2334 *
2335 */
2336async_sess_t *async_connect_me_to_blocking_iface(async_exch_t *exch, iface_t iface,
2337 sysarg_t arg2, sysarg_t arg3)
2338{
2339 if (exch == NULL) {
2340 errno = ENOENT;
2341 return NULL;
2342 }
2343
2344 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2345 if (sess == NULL) {
2346 errno = ENOMEM;
2347 return NULL;
2348 }
2349
2350 int phone = async_connect_me_to_internal(exch->phone, iface, arg2,
2351 arg3, IPC_FLAG_BLOCKING);
2352 if (phone < 0) {
2353 errno = phone;
2354 free(sess);
2355 return NULL;
2356 }
2357
2358 sess->iface = iface;
2359 sess->phone = phone;
2360 sess->arg1 = iface;
2361 sess->arg2 = arg2;
2362 sess->arg3 = arg3;
2363
2364 fibril_mutex_initialize(&sess->remote_state_mtx);
2365 sess->remote_state_data = NULL;
2366
2367 list_initialize(&sess->exch_list);
2368 fibril_mutex_initialize(&sess->mutex);
2369 atomic_set(&sess->refcnt, 0);
2370
2371 return sess;
2372}
2373
[64d2b10]2374/** Connect to a task specified by id.
2375 *
2376 */
[79ae36dd]2377async_sess_t *async_connect_kbox(task_id_t id)
[64d2b10]2378{
[79ae36dd]2379 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2380 if (sess == NULL) {
2381 errno = ENOMEM;
2382 return NULL;
2383 }
2384
2385 int phone = ipc_connect_kbox(id);
2386 if (phone < 0) {
2387 errno = phone;
2388 free(sess);
2389 return NULL;
2390 }
2391
[566992e1]2392 sess->iface = 0;
[79ae36dd]2393 sess->mgmt = EXCHANGE_ATOMIC;
2394 sess->phone = phone;
2395 sess->arg1 = 0;
2396 sess->arg2 = 0;
2397 sess->arg3 = 0;
2398
[58cbf8d5]2399 fibril_mutex_initialize(&sess->remote_state_mtx);
2400 sess->remote_state_data = NULL;
2401
[79ae36dd]2402 list_initialize(&sess->exch_list);
2403 fibril_mutex_initialize(&sess->mutex);
2404 atomic_set(&sess->refcnt, 0);
2405
2406 return sess;
2407}
2408
2409static int async_hangup_internal(int phone)
2410{
2411 return ipc_hangup(phone);
[64d2b10]2412}
2413
2414/** Wrapper for ipc_hangup.
2415 *
[79ae36dd]2416 * @param sess Session to hung up.
[64d2b10]2417 *
2418 * @return Zero on success or a negative error code.
2419 *
2420 */
[79ae36dd]2421int async_hangup(async_sess_t *sess)
[64d2b10]2422{
[36e2b55]2423 async_exch_t *exch;
2424
[79ae36dd]2425 assert(sess);
2426
2427 if (atomic_get(&sess->refcnt) > 0)
2428 return EBUSY;
2429
[36e2b55]2430 fibril_mutex_lock(&async_sess_mutex);
[972c60ce]2431
[cff3fb6]2432 int rc = async_hangup_internal(sess->phone);
[36e2b55]2433
2434 while (!list_empty(&sess->exch_list)) {
2435 exch = (async_exch_t *)
2436 list_get_instance(list_first(&sess->exch_list),
2437 async_exch_t, sess_link);
2438
2439 list_remove(&exch->sess_link);
2440 list_remove(&exch->global_link);
2441 async_hangup_internal(exch->phone);
2442 free(exch);
2443 }
[4c50c8d]2444
2445 free(sess);
[36e2b55]2446
2447 fibril_mutex_unlock(&async_sess_mutex);
2448
[79ae36dd]2449 return rc;
[64d2b10]2450}
2451
2452/** Interrupt one thread of this task from waiting for IPC. */
2453void async_poke(void)
2454{
2455 ipc_poke();
2456}
2457
[79ae36dd]2458/** Start new exchange in a session.
2459 *
2460 * @param session Session.
2461 *
2462 * @return New exchange or NULL on error.
2463 *
2464 */
2465async_exch_t *async_exchange_begin(async_sess_t *sess)
2466{
2467 if (sess == NULL)
2468 return NULL;
2469
[566992e1]2470 exch_mgmt_t mgmt = sess->mgmt;
2471 if (sess->iface != 0)
2472 mgmt = sess->iface & IFACE_EXCHANGE_MASK;
2473
2474 async_exch_t *exch = NULL;
[79ae36dd]2475
2476 fibril_mutex_lock(&async_sess_mutex);
2477
2478 if (!list_empty(&sess->exch_list)) {
2479 /*
2480 * There are inactive exchanges in the session.
2481 */
2482 exch = (async_exch_t *)
[b72efe8]2483 list_get_instance(list_first(&sess->exch_list),
2484 async_exch_t, sess_link);
2485
[79ae36dd]2486 list_remove(&exch->sess_link);
2487 list_remove(&exch->global_link);
2488 } else {
2489 /*
2490 * There are no available exchanges in the session.
2491 */
2492
[566992e1]2493 if ((mgmt == EXCHANGE_ATOMIC) ||
2494 (mgmt == EXCHANGE_SERIALIZE)) {
[79ae36dd]2495 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2496 if (exch != NULL) {
[b72efe8]2497 link_initialize(&exch->sess_link);
2498 link_initialize(&exch->global_link);
[79ae36dd]2499 exch->sess = sess;
2500 exch->phone = sess->phone;
2501 }
[566992e1]2502 } else if (mgmt == EXCHANGE_PARALLEL) {
2503 int phone;
2504
2505 retry:
[79ae36dd]2506 /*
2507 * Make a one-time attempt to connect a new data phone.
2508 */
2509 phone = async_connect_me_to_internal(sess->phone, sess->arg1,
2510 sess->arg2, sess->arg3, 0);
2511 if (phone >= 0) {
2512 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2513 if (exch != NULL) {
[b72efe8]2514 link_initialize(&exch->sess_link);
2515 link_initialize(&exch->global_link);
[79ae36dd]2516 exch->sess = sess;
2517 exch->phone = phone;
2518 } else
2519 async_hangup_internal(phone);
2520 } else if (!list_empty(&inactive_exch_list)) {
2521 /*
2522 * We did not manage to connect a new phone. But we
2523 * can try to close some of the currently inactive
2524 * connections in other sessions and try again.
2525 */
2526 exch = (async_exch_t *)
[b72efe8]2527 list_get_instance(list_first(&inactive_exch_list),
2528 async_exch_t, global_link);
2529
[79ae36dd]2530 list_remove(&exch->sess_link);
2531 list_remove(&exch->global_link);
2532 async_hangup_internal(exch->phone);
2533 free(exch);
2534 goto retry;
2535 } else {
2536 /*
2537 * Wait for a phone to become available.
2538 */
2539 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
2540 goto retry;
2541 }
2542 }
2543 }
2544
2545 fibril_mutex_unlock(&async_sess_mutex);
2546
2547 if (exch != NULL) {
2548 atomic_inc(&sess->refcnt);
2549
[566992e1]2550 if (mgmt == EXCHANGE_SERIALIZE)
[79ae36dd]2551 fibril_mutex_lock(&sess->mutex);
2552 }
2553
2554 return exch;
2555}
2556
2557/** Finish an exchange.
2558 *
2559 * @param exch Exchange to finish.
2560 *
2561 */
2562void async_exchange_end(async_exch_t *exch)
2563{
2564 if (exch == NULL)
2565 return;
2566
2567 async_sess_t *sess = exch->sess;
[3ca2e36]2568 assert(sess != NULL);
[79ae36dd]2569
[566992e1]2570 exch_mgmt_t mgmt = sess->mgmt;
2571 if (sess->iface != 0)
2572 mgmt = sess->iface & IFACE_EXCHANGE_MASK;
2573
[1c6436a]2574 atomic_dec(&sess->refcnt);
2575
[566992e1]2576 if (mgmt == EXCHANGE_SERIALIZE)
[79ae36dd]2577 fibril_mutex_unlock(&sess->mutex);
2578
2579 fibril_mutex_lock(&async_sess_mutex);
2580
2581 list_append(&exch->sess_link, &sess->exch_list);
2582 list_append(&exch->global_link, &inactive_exch_list);
2583 fibril_condvar_signal(&avail_phone_cv);
2584
2585 fibril_mutex_unlock(&async_sess_mutex);
2586}
2587
[47b7006]2588/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
2589 *
[79ae36dd]2590 * @param exch Exchange for sending the message.
2591 * @param size Size of the destination address space area.
2592 * @param arg User defined argument.
2593 * @param flags Storage for the received flags. Can be NULL.
[df956b9b]2594 * @param dst Address of the storage for the destination address space area
2595 * base address. Cannot be NULL.
[0da4e41]2596 *
[47b7006]2597 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2598 *
2599 */
[fbcdeb8]2600int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
2601 unsigned int *flags, void **dst)
[0da4e41]2602{
[79ae36dd]2603 if (exch == NULL)
2604 return ENOENT;
2605
[fbcdeb8]2606 sysarg_t _flags = 0;
2607 sysarg_t _dst = (sysarg_t) -1;
2608 int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
2609 arg, NULL, &_flags, NULL, &_dst);
[47b7006]2610
[0da4e41]2611 if (flags)
[fbcdeb8]2612 *flags = (unsigned int) _flags;
[47b7006]2613
[fbcdeb8]2614 *dst = (void *) _dst;
[0da4e41]2615 return res;
2616}
2617
2618/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
2619 *
[47b7006]2620 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
2621 * calls so that the user doesn't have to remember the meaning of each IPC
2622 * argument.
[0da4e41]2623 *
2624 * So far, this wrapper is to be used from within a connection fibril.
2625 *
[01c3bb4]2626 * @param chandle Storage for the handle of the IPC_M_SHARE_IN call.
2627 * @param size Destination address space area size.
[47b7006]2628 *
2629 * @return True on success, false on failure.
[0da4e41]2630 *
2631 */
[01c3bb4]2632bool async_share_in_receive(cap_handle_t *chandle, size_t *size)
[0da4e41]2633{
[01c3bb4]2634 assert(chandle);
[0da4e41]2635 assert(size);
[47b7006]2636
2637 ipc_call_t data;
[01c3bb4]2638 *chandle = async_get_call(&data);
[47b7006]2639
[228e490]2640 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
[47b7006]2641 return false;
2642
[fbcdeb8]2643 *size = (size_t) IPC_GET_ARG1(data);
[47b7006]2644 return true;
[0da4e41]2645}
2646
2647/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
2648 *
[fbcdeb8]2649 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
[47b7006]2650 * calls so that the user doesn't have to remember the meaning of each IPC
2651 * argument.
[0da4e41]2652 *
[01c3bb4]2653 * @param chandle Handle of the IPC_M_DATA_READ call to answer.
2654 * @param src Source address space base.
2655 * @param flags Flags to be used for sharing. Bits can be only cleared.
[47b7006]2656 *
2657 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2658 *
2659 */
[01c3bb4]2660int async_share_in_finalize(cap_handle_t chandle, void *src, unsigned int flags)
[0da4e41]2661{
[01c3bb4]2662 return ipc_answer_3(chandle, EOK, (sysarg_t) src, (sysarg_t) flags,
[d7978525]2663 (sysarg_t) __entry);
[0da4e41]2664}
2665
[47b7006]2666/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
[0da4e41]2667 *
[79ae36dd]2668 * @param exch Exchange for sending the message.
2669 * @param src Source address space area base address.
2670 * @param flags Flags to be used for sharing. Bits can be only cleared.
[47b7006]2671 *
2672 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2673 *
2674 */
[79ae36dd]2675int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
[0da4e41]2676{
[79ae36dd]2677 if (exch == NULL)
2678 return ENOENT;
2679
2680 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
[96b02eb9]2681 (sysarg_t) flags);
[0da4e41]2682}
2683
2684/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
2685 *
[47b7006]2686 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
2687 * calls so that the user doesn't have to remember the meaning of each IPC
2688 * argument.
[0da4e41]2689 *
2690 * So far, this wrapper is to be used from within a connection fibril.
2691 *
[01c3bb4]2692 * @param chandle Storage for the hash of the IPC_M_SHARE_OUT call.
2693 * @param size Storage for the source address space area size.
2694 * @param flags Storage for the sharing flags.
[47b7006]2695 *
2696 * @return True on success, false on failure.
[0da4e41]2697 *
2698 */
[01c3bb4]2699bool async_share_out_receive(cap_handle_t *chandle, size_t *size,
2700 unsigned int *flags)
[0da4e41]2701{
[01c3bb4]2702 assert(chandle);
[0da4e41]2703 assert(size);
2704 assert(flags);
[47b7006]2705
2706 ipc_call_t data;
[01c3bb4]2707 *chandle = async_get_call(&data);
[47b7006]2708
[228e490]2709 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
[47b7006]2710 return false;
2711
[0da4e41]2712 *size = (size_t) IPC_GET_ARG2(data);
[47b7006]2713 *flags = (unsigned int) IPC_GET_ARG3(data);
2714 return true;
[0da4e41]2715}
2716
2717/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2718 *
[47b7006]2719 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2720 * calls so that the user doesn't have to remember the meaning of each IPC
2721 * argument.
[0da4e41]2722 *
[01c3bb4]2723 * @param chandle Handle of the IPC_M_DATA_WRITE call to answer.
2724 * @param dst Address of the storage for the destination address space area
2725 * base address.
[47b7006]2726 *
[01c3bb4]2727 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2728 *
2729 */
[01c3bb4]2730int async_share_out_finalize(cap_handle_t chandle, void **dst)
[0da4e41]2731{
[01c3bb4]2732 return ipc_answer_2(chandle, EOK, (sysarg_t) __entry, (sysarg_t) dst);
[0da4e41]2733}
2734
[8bf1eeb]2735/** Start IPC_M_DATA_READ using the async framework.
2736 *
[79ae36dd]2737 * @param exch Exchange for sending the message.
2738 * @param dst Address of the beginning of the destination buffer.
2739 * @param size Size of the destination buffer (in bytes).
[8bf1eeb]2740 * @param dataptr Storage of call data (arg 2 holds actual data size).
[79ae36dd]2741 *
[8bf1eeb]2742 * @return Hash of the sent message or 0 on error.
[79ae36dd]2743 *
[8bf1eeb]2744 */
[79ae36dd]2745aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2746 ipc_call_t *dataptr)
[8bf1eeb]2747{
[79ae36dd]2748 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
[8bf1eeb]2749 (sysarg_t) size, dataptr);
2750}
2751
[47b7006]2752/** Wrapper for IPC_M_DATA_READ calls using the async framework.
[0da4e41]2753 *
[79ae36dd]2754 * @param exch Exchange for sending the message.
2755 * @param dst Address of the beginning of the destination buffer.
2756 * @param size Size of the destination buffer.
[47b7006]2757 *
2758 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2759 *
2760 */
[79ae36dd]2761int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
[0da4e41]2762{
[79ae36dd]2763 if (exch == NULL)
2764 return ENOENT;
2765
2766 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2767 (sysarg_t) size);
[0da4e41]2768}
2769
2770/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2771 *
[47b7006]2772 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2773 * calls so that the user doesn't have to remember the meaning of each IPC
2774 * argument.
[0da4e41]2775 *
2776 * So far, this wrapper is to be used from within a connection fibril.
2777 *
[01c3bb4]2778 * @param chandle Storage for the handle of the IPC_M_DATA_READ.
2779 * @param size Storage for the maximum size. Can be NULL.
[47b7006]2780 *
2781 * @return True on success, false on failure.
[0da4e41]2782 *
2783 */
[01c3bb4]2784bool async_data_read_receive(cap_handle_t *chandle, size_t *size)
[d768d4c8]2785{
2786 ipc_call_t data;
[01c3bb4]2787 return async_data_read_receive_call(chandle, &data, size);
[d768d4c8]2788}
2789
2790/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2791 *
2792 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2793 * calls so that the user doesn't have to remember the meaning of each IPC
2794 * argument.
2795 *
2796 * So far, this wrapper is to be used from within a connection fibril.
2797 *
[01c3bb4]2798 * @param chandle Storage for the handle of the IPC_M_DATA_READ.
2799 * @param size Storage for the maximum size. Can be NULL.
[d768d4c8]2800 *
2801 * @return True on success, false on failure.
2802 *
2803 */
[01c3bb4]2804bool async_data_read_receive_call(cap_handle_t *chandle, ipc_call_t *data,
[d768d4c8]2805 size_t *size)
[0da4e41]2806{
[01c3bb4]2807 assert(chandle);
[d768d4c8]2808 assert(data);
[47b7006]2809
[01c3bb4]2810 *chandle = async_get_call(data);
[47b7006]2811
[d768d4c8]2812 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_READ)
[47b7006]2813 return false;
2814
[0da4e41]2815 if (size)
[d768d4c8]2816 *size = (size_t) IPC_GET_ARG2(*data);
[47b7006]2817
2818 return true;
[0da4e41]2819}
2820
2821/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2822 *
[47b7006]2823 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2824 * calls so that the user doesn't have to remember the meaning of each IPC
2825 * argument.
[0da4e41]2826 *
[01c3bb4]2827 * @param chandle Handle of the IPC_M_DATA_READ call to answer.
2828 * @param src Source address for the IPC_M_DATA_READ call.
2829 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2830 * the maximum size announced by the sender.
[47b7006]2831 *
[01c3bb4]2832 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2833 *
2834 */
[01c3bb4]2835int async_data_read_finalize(cap_handle_t chandle, const void *src, size_t size)
[0da4e41]2836{
[01c3bb4]2837 return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) size);
[0da4e41]2838}
2839
[b4cbef1]2840/** Wrapper for forwarding any read request
2841 *
2842 */
[79ae36dd]2843int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2844 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2845 ipc_call_t *dataptr)
[b4cbef1]2846{
[79ae36dd]2847 if (exch == NULL)
2848 return ENOENT;
2849
[01c3bb4]2850 cap_handle_t chandle;
2851 if (!async_data_read_receive(&chandle, NULL)) {
2852 ipc_answer_0(chandle, EINVAL);
[b4cbef1]2853 return EINVAL;
2854 }
2855
[79ae36dd]2856 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]2857 dataptr);
2858 if (msg == 0) {
[01c3bb4]2859 ipc_answer_0(chandle, EINVAL);
[b4cbef1]2860 return EINVAL;
2861 }
2862
[01c3bb4]2863 int retval = ipc_forward_fast(chandle, exch->phone, 0, 0, 0,
[b4cbef1]2864 IPC_FF_ROUTE_FROM_ME);
2865 if (retval != EOK) {
[ab9f443]2866 async_forget(msg);
[01c3bb4]2867 ipc_answer_0(chandle, retval);
[b4cbef1]2868 return retval;
2869 }
2870
[96b02eb9]2871 sysarg_t rc;
[b4cbef1]2872 async_wait_for(msg, &rc);
2873
2874 return (int) rc;
2875}
2876
[47b7006]2877/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
[0da4e41]2878 *
[79ae36dd]2879 * @param exch Exchange for sending the message.
2880 * @param src Address of the beginning of the source buffer.
2881 * @param size Size of the source buffer.
[b4cbef1]2882 *
2883 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2884 *
2885 */
[79ae36dd]2886int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
[0da4e41]2887{
[79ae36dd]2888 if (exch == NULL)
2889 return ENOENT;
2890
2891 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2892 (sysarg_t) size);
[0da4e41]2893}
2894
2895/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2896 *
[47b7006]2897 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2898 * calls so that the user doesn't have to remember the meaning of each IPC
2899 * argument.
[0da4e41]2900 *
2901 * So far, this wrapper is to be used from within a connection fibril.
2902 *
[01c3bb4]2903 * @param chandle Storage for the handle of the IPC_M_DATA_WRITE.
2904 * @param size Storage for the suggested size. May be NULL.
[b4cbef1]2905 *
[01c3bb4]2906 * @return True on success, false on failure.
[0da4e41]2907 *
2908 */
[01c3bb4]2909bool async_data_write_receive(cap_handle_t *chandle, size_t *size)
[5ae1c51]2910{
2911 ipc_call_t data;
[01c3bb4]2912 return async_data_write_receive_call(chandle, &data, size);
[5ae1c51]2913}
2914
2915/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2916 *
2917 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2918 * calls so that the user doesn't have to remember the meaning of each IPC
2919 * argument.
2920 *
2921 * So far, this wrapper is to be used from within a connection fibril.
2922 *
[01c3bb4]2923 * @param chandle Storage for the handle of the IPC_M_DATA_WRITE.
2924 * @param data Storage for the ipc call data.
2925 * @param size Storage for the suggested size. May be NULL.
[5ae1c51]2926 *
2927 * @return True on success, false on failure.
2928 *
2929 */
[01c3bb4]2930bool async_data_write_receive_call(cap_handle_t *chandle, ipc_call_t *data,
[5ae1c51]2931 size_t *size)
[0da4e41]2932{
[01c3bb4]2933 assert(chandle);
[5ae1c51]2934 assert(data);
[b4cbef1]2935
[01c3bb4]2936 *chandle = async_get_call(data);
[47b7006]2937
[5ae1c51]2938 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_WRITE)
[47b7006]2939 return false;
[b4cbef1]2940
[0da4e41]2941 if (size)
[5ae1c51]2942 *size = (size_t) IPC_GET_ARG2(*data);
[b4cbef1]2943
[47b7006]2944 return true;
[0da4e41]2945}
2946
2947/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2948 *
[47b7006]2949 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2950 * calls so that the user doesn't have to remember the meaning of each IPC
2951 * argument.
[0da4e41]2952 *
[01c3bb4]2953 * @param chandle Handle of the IPC_M_DATA_WRITE call to answer.
2954 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2955 * @param size Final size for the IPC_M_DATA_WRITE call.
[b4cbef1]2956 *
[01c3bb4]2957 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2958 *
2959 */
[01c3bb4]2960int async_data_write_finalize(cap_handle_t chandle, void *dst, size_t size)
[0da4e41]2961{
[01c3bb4]2962 return ipc_answer_2(chandle, EOK, (sysarg_t) dst, (sysarg_t) size);
[0da4e41]2963}
2964
[eda925a]2965/** Wrapper for receiving binary data or strings
[8aa42e3]2966 *
2967 * This wrapper only makes it more comfortable to use async_data_write_*
[eda925a]2968 * functions to receive binary data or strings.
[8aa42e3]2969 *
[472c09d]2970 * @param data Pointer to data pointer (which should be later disposed
2971 * by free()). If the operation fails, the pointer is not
2972 * touched.
[eda925a]2973 * @param nullterm If true then the received data is always zero terminated.
2974 * This also causes to allocate one extra byte beyond the
2975 * raw transmitted data.
[b4cbef1]2976 * @param min_size Minimum size (in bytes) of the data to receive.
[472c09d]2977 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2978 * no limit.
[eda925a]2979 * @param granulariy If non-zero then the size of the received data has to
[472c09d]2980 * be divisible by this value.
2981 * @param received If not NULL, the size of the received data is stored here.
[8aa42e3]2982 *
2983 * @return Zero on success or a value from @ref errno.h on failure.
2984 *
2985 */
[eda925a]2986int async_data_write_accept(void **data, const bool nullterm,
2987 const size_t min_size, const size_t max_size, const size_t granularity,
2988 size_t *received)
[8aa42e3]2989{
[79ae36dd]2990 assert(data);
2991
[01c3bb4]2992 cap_handle_t chandle;
[8aa42e3]2993 size_t size;
[01c3bb4]2994 if (!async_data_write_receive(&chandle, &size)) {
2995 ipc_answer_0(chandle, EINVAL);
[8aa42e3]2996 return EINVAL;
2997 }
2998
[b4cbef1]2999 if (size < min_size) {
[01c3bb4]3000 ipc_answer_0(chandle, EINVAL);
[b4cbef1]3001 return EINVAL;
3002 }
3003
[8aa42e3]3004 if ((max_size > 0) && (size > max_size)) {
[01c3bb4]3005 ipc_answer_0(chandle, EINVAL);
[8aa42e3]3006 return EINVAL;
3007 }
3008
[472c09d]3009 if ((granularity > 0) && ((size % granularity) != 0)) {
[01c3bb4]3010 ipc_answer_0(chandle, EINVAL);
[472c09d]3011 return EINVAL;
3012 }
3013
[57dea62]3014 void *arg_data;
[eda925a]3015
3016 if (nullterm)
[57dea62]3017 arg_data = malloc(size + 1);
[eda925a]3018 else
[57dea62]3019 arg_data = malloc(size);
[eda925a]3020
[57dea62]3021 if (arg_data == NULL) {
[01c3bb4]3022 ipc_answer_0(chandle, ENOMEM);
[8aa42e3]3023 return ENOMEM;
3024 }
3025
[01c3bb4]3026 int rc = async_data_write_finalize(chandle, arg_data, size);
[8aa42e3]3027 if (rc != EOK) {
[57dea62]3028 free(arg_data);
[8aa42e3]3029 return rc;
3030 }
3031
[eda925a]3032 if (nullterm)
[57dea62]3033 ((char *) arg_data)[size] = 0;
[8aa42e3]3034
[57dea62]3035 *data = arg_data;
[472c09d]3036 if (received != NULL)
3037 *received = size;
3038
[8aa42e3]3039 return EOK;
3040}
3041
[b4cbef1]3042/** Wrapper for voiding any data that is about to be received
3043 *
3044 * This wrapper can be used to void any pending data
3045 *
3046 * @param retval Error value from @ref errno.h to be returned to the caller.
3047 *
3048 */
[47b7006]3049void async_data_write_void(sysarg_t retval)
[b4cbef1]3050{
[01c3bb4]3051 cap_handle_t chandle;
3052 async_data_write_receive(&chandle, NULL);
3053 ipc_answer_0(chandle, retval);
[b4cbef1]3054}
3055
3056/** Wrapper for forwarding any data that is about to be received
3057 *
3058 */
[79ae36dd]3059int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
3060 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
3061 ipc_call_t *dataptr)
[b4cbef1]3062{
[79ae36dd]3063 if (exch == NULL)
3064 return ENOENT;
3065
[01c3bb4]3066 cap_handle_t chandle;
3067 if (!async_data_write_receive(&chandle, NULL)) {
3068 ipc_answer_0(chandle, EINVAL);
[b4cbef1]3069 return EINVAL;
3070 }
3071
[79ae36dd]3072 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]3073 dataptr);
3074 if (msg == 0) {
[01c3bb4]3075 ipc_answer_0(chandle, EINVAL);
[b4cbef1]3076 return EINVAL;
3077 }
3078
[01c3bb4]3079 int retval = ipc_forward_fast(chandle, exch->phone, 0, 0, 0,
[b4cbef1]3080 IPC_FF_ROUTE_FROM_ME);
3081 if (retval != EOK) {
[ab9f443]3082 async_forget(msg);
[01c3bb4]3083 ipc_answer_0(chandle, retval);
[b4cbef1]3084 return retval;
3085 }
3086
[96b02eb9]3087 sysarg_t rc;
[b4cbef1]3088 async_wait_for(msg, &rc);
3089
3090 return (int) rc;
3091}
3092
[79ae36dd]3093/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
3094 *
3095 * If the current call is IPC_M_CONNECT_TO_ME then a new
3096 * async session is created for the accepted phone.
3097 *
3098 * @param mgmt Exchange management style.
3099 *
[8869f7b]3100 * @return New async session.
3101 * @return NULL on failure.
[79ae36dd]3102 *
3103 */
3104async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
3105{
3106 /* Accept the phone */
3107 ipc_call_t call;
[01c3bb4]3108 cap_handle_t chandle = async_get_call(&call);
3109 cap_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(call);
[79ae36dd]3110
[01c3bb4]3111 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) || (phandle < 0)) {
3112 async_answer_0(chandle, EINVAL);
[79ae36dd]3113 return NULL;
3114 }
3115
3116 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
3117 if (sess == NULL) {
[01c3bb4]3118 async_answer_0(chandle, ENOMEM);
[79ae36dd]3119 return NULL;
3120 }
3121
[566992e1]3122 sess->iface = 0;
[79ae36dd]3123 sess->mgmt = mgmt;
[01c3bb4]3124 sess->phone = phandle;
[79ae36dd]3125 sess->arg1 = 0;
3126 sess->arg2 = 0;
3127 sess->arg3 = 0;
3128
[58cbf8d5]3129 fibril_mutex_initialize(&sess->remote_state_mtx);
3130 sess->remote_state_data = NULL;
3131
[79ae36dd]3132 list_initialize(&sess->exch_list);
3133 fibril_mutex_initialize(&sess->mutex);
3134 atomic_set(&sess->refcnt, 0);
3135
3136 /* Acknowledge the connected phone */
[01c3bb4]3137 async_answer_0(chandle, EOK);
[79ae36dd]3138
3139 return sess;
3140}
3141
[8869f7b]3142/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
3143 *
3144 * If the call is IPC_M_CONNECT_TO_ME then a new
3145 * async session is created. However, the phone is
3146 * not accepted automatically.
3147 *
3148 * @param mgmt Exchange management style.
3149 * @param call Call data.
3150 *
3151 * @return New async session.
3152 * @return NULL on failure.
3153 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
3154 *
3155 */
3156async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
3157{
[01c3bb4]3158 cap_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(*call);
[8869f7b]3159
[01c3bb4]3160 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) || (phandle < 0))
[8869f7b]3161 return NULL;
3162
3163 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
3164 if (sess == NULL)
3165 return NULL;
3166
[566992e1]3167 sess->iface = 0;
[8869f7b]3168 sess->mgmt = mgmt;
[01c3bb4]3169 sess->phone = phandle;
[8869f7b]3170 sess->arg1 = 0;
3171 sess->arg2 = 0;
3172 sess->arg3 = 0;
3173
[58cbf8d5]3174 fibril_mutex_initialize(&sess->remote_state_mtx);
3175 sess->remote_state_data = NULL;
3176
[8869f7b]3177 list_initialize(&sess->exch_list);
3178 fibril_mutex_initialize(&sess->mutex);
3179 atomic_set(&sess->refcnt, 0);
3180
3181 return sess;
3182}
3183
[2c4aa39]3184int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
3185 sysarg_t arg3, async_exch_t *other_exch)
3186{
3187 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
3188 arg1, arg2, arg3, 0, other_exch->phone);
3189}
3190
[01c3bb4]3191bool async_state_change_receive(cap_handle_t *chandle, sysarg_t *arg1,
[2c4aa39]3192 sysarg_t *arg2, sysarg_t *arg3)
3193{
[01c3bb4]3194 assert(chandle);
[57dea62]3195
[2c4aa39]3196 ipc_call_t call;
[01c3bb4]3197 *chandle = async_get_call(&call);
[57dea62]3198
[2c4aa39]3199 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
3200 return false;
3201
3202 if (arg1)
3203 *arg1 = IPC_GET_ARG1(call);
3204 if (arg2)
3205 *arg2 = IPC_GET_ARG2(call);
3206 if (arg3)
3207 *arg3 = IPC_GET_ARG3(call);
[57dea62]3208
[2c4aa39]3209 return true;
3210}
3211
[01c3bb4]3212int async_state_change_finalize(cap_handle_t chandle, async_exch_t *other_exch)
[2c4aa39]3213{
[01c3bb4]3214 return ipc_answer_1(chandle, EOK, other_exch->phone);
[2c4aa39]3215}
3216
[58cbf8d5]3217/** Lock and get session remote state
3218 *
3219 * Lock and get the local replica of the remote state
3220 * in stateful sessions. The call should be paired
3221 * with async_remote_state_release*().
3222 *
3223 * @param[in] sess Stateful session.
3224 *
3225 * @return Local replica of the remote state.
3226 *
3227 */
3228void *async_remote_state_acquire(async_sess_t *sess)
3229{
3230 fibril_mutex_lock(&sess->remote_state_mtx);
3231 return sess->remote_state_data;
3232}
3233
3234/** Update the session remote state
3235 *
3236 * Update the local replica of the remote state
3237 * in stateful sessions. The remote state must
3238 * be already locked.
3239 *
3240 * @param[in] sess Stateful session.
3241 * @param[in] state New local replica of the remote state.
3242 *
3243 */
3244void async_remote_state_update(async_sess_t *sess, void *state)
3245{
3246 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3247 sess->remote_state_data = state;
3248}
3249
3250/** Release the session remote state
3251 *
3252 * Unlock the local replica of the remote state
3253 * in stateful sessions.
3254 *
3255 * @param[in] sess Stateful session.
3256 *
3257 */
3258void async_remote_state_release(async_sess_t *sess)
3259{
3260 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3261
3262 fibril_mutex_unlock(&sess->remote_state_mtx);
3263}
3264
3265/** Release the session remote state and end an exchange
3266 *
3267 * Unlock the local replica of the remote state
3268 * in stateful sessions. This is convenience function
3269 * which gets the session pointer from the exchange
3270 * and also ends the exchange.
3271 *
3272 * @param[in] exch Stateful session's exchange.
3273 *
3274 */
3275void async_remote_state_release_exchange(async_exch_t *exch)
3276{
3277 if (exch == NULL)
3278 return;
3279
3280 async_sess_t *sess = exch->sess;
3281 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3282
3283 async_exchange_end(exch);
3284 fibril_mutex_unlock(&sess->remote_state_mtx);
3285}
3286
[101516d]3287void *async_as_area_create(void *base, size_t size, unsigned int flags,
[ae6021d]3288 async_sess_t *pager, sysarg_t id1, sysarg_t id2, sysarg_t id3)
3289{
3290 as_area_pager_info_t pager_info = {
3291 .pager = pager->phone,
3292 .id1 = id1,
3293 .id2 = id2,
3294 .id3 = id3
3295 };
3296 return as_area_create(base, size, flags, &pager_info);
[101516d]3297}
3298
[a46da63]3299/** @}
[b2951e2]3300 */
Note: See TracBrowser for help on using the repository browser.