source: mainline/uspace/lib/c/generic/async.c@ b272c67a

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

make async_new_connection() static

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