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

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

Fibrils can always be preempted during IPC

  • 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);
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);
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);
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);
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}
2018
2019void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2020 sysarg_t arg2, sysarg_t arg3)
2021{
2022 if (exch != NULL)
2023 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
2024 NULL);
2025}
2026
2027void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2028 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
2029{
2030 if (exch != NULL)
2031 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
2032 NULL, NULL);
2033}
2034
2035void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2036 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
2037{
2038 if (exch != NULL)
2039 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
2040 arg5, NULL, NULL);
2041}
2042
2043sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
2044{
2045 return ipc_answer_0(callid, retval);
2046}
2047
2048sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
2049{
2050 return ipc_answer_1(callid, retval, arg1);
2051}
2052
2053sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
2054 sysarg_t arg2)
2055{
2056 return ipc_answer_2(callid, retval, arg1, arg2);
2057}
2058
2059sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
2060 sysarg_t arg2, sysarg_t arg3)
2061{
2062 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
2063}
2064
2065sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
2066 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
2067{
2068 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
2069}
2070
2071sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
2072 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
2073{
2074 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
2075}
2076
2077int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
2078 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
2079{
2080 if (exch == NULL)
2081 return ENOENT;
2082
2083 return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
2084}
2085
2086int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
2087 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
2088 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
2089{
2090 if (exch == NULL)
2091 return ENOENT;
2092
2093 return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
2094 arg4, arg5, mode);
2095}
2096
2097/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
2098 *
2099 * Ask through phone for a new connection to some service.
2100 *
2101 * @param exch Exchange for sending the message.
2102 * @param arg1 User defined argument.
2103 * @param arg2 User defined argument.
2104 * @param arg3 User defined argument.
2105 *
2106 * @return Zero on success or a negative error code.
2107 *
2108 */
2109int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
2110 sysarg_t arg3)
2111{
2112 if (exch == NULL)
2113 return ENOENT;
2114
2115 ipc_call_t answer;
2116 aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
2117 &answer);
2118
2119 sysarg_t rc;
2120 async_wait_for(req, &rc);
2121 if (rc != EOK)
2122 return (int) rc;
2123
2124 return EOK;
2125}
2126
2127/** Wrapper for making IPC_M_CLONE_ESTABLISH calls using the async framework.
2128 *
2129 * Ask for a cloned connection to some service.
2130 *
2131 * @param mgmt Exchange management style.
2132 * @param exch Exchange for sending the message.
2133 *
2134 * @return New session on success or NULL on error.
2135 *
2136 */
2137async_sess_t *async_clone_establish(exch_mgmt_t mgmt, async_exch_t *exch)
2138{
2139 if (exch == NULL) {
2140 errno = ENOENT;
2141 return NULL;
2142 }
2143
2144 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2145 if (sess == NULL) {
2146 errno = ENOMEM;
2147 return NULL;
2148 }
2149
2150 ipc_call_t result;
2151
2152 amsg_t *msg = amsg_create();
2153 if (!msg) {
2154 free(sess);
2155 errno = ENOMEM;
2156 return NULL;
2157 }
2158
2159 msg->dataptr = &result;
2160 msg->wdata.active = true;
2161
2162 ipc_call_async_0(exch->phone, IPC_M_CLONE_ESTABLISH, msg,
2163 reply_received);
2164
2165 sysarg_t rc;
2166 async_wait_for((aid_t) msg, &rc);
2167
2168 if (rc != EOK) {
2169 errno = rc;
2170 free(sess);
2171 return NULL;
2172 }
2173
2174 int phone = (int) IPC_GET_ARG5(result);
2175
2176 if (phone < 0) {
2177 errno = phone;
2178 free(sess);
2179 return NULL;
2180 }
2181
2182 sess->iface = 0;
2183 sess->mgmt = mgmt;
2184 sess->phone = phone;
2185 sess->arg1 = 0;
2186 sess->arg2 = 0;
2187 sess->arg3 = 0;
2188
2189 fibril_mutex_initialize(&sess->remote_state_mtx);
2190 sess->remote_state_data = NULL;
2191
2192 list_initialize(&sess->exch_list);
2193 fibril_mutex_initialize(&sess->mutex);
2194 atomic_set(&sess->refcnt, 0);
2195
2196 return sess;
2197}
2198
2199static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
2200 sysarg_t arg3, sysarg_t arg4)
2201{
2202 ipc_call_t result;
2203
2204 amsg_t *msg = amsg_create();
2205 if (!msg)
2206 return ENOENT;
2207
2208 msg->dataptr = &result;
2209 msg->wdata.active = true;
2210
2211 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
2212 msg, reply_received);
2213
2214 sysarg_t rc;
2215 async_wait_for((aid_t) msg, &rc);
2216
2217 if (rc != EOK)
2218 return rc;
2219
2220 return (int) IPC_GET_ARG5(result);
2221}
2222
2223/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2224 *
2225 * Ask through for a new connection to some service.
2226 *
2227 * @param mgmt Exchange management style.
2228 * @param exch Exchange for sending the message.
2229 * @param arg1 User defined argument.
2230 * @param arg2 User defined argument.
2231 * @param arg3 User defined argument.
2232 *
2233 * @return New session on success or NULL on error.
2234 *
2235 */
2236async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
2237 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
2238{
2239 if (exch == NULL) {
2240 errno = ENOENT;
2241 return NULL;
2242 }
2243
2244 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2245 if (sess == NULL) {
2246 errno = ENOMEM;
2247 return NULL;
2248 }
2249
2250 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
2251 0);
2252 if (phone < 0) {
2253 errno = phone;
2254 free(sess);
2255 return NULL;
2256 }
2257
2258 sess->iface = 0;
2259 sess->mgmt = mgmt;
2260 sess->phone = phone;
2261 sess->arg1 = arg1;
2262 sess->arg2 = arg2;
2263 sess->arg3 = arg3;
2264
2265 fibril_mutex_initialize(&sess->remote_state_mtx);
2266 sess->remote_state_data = NULL;
2267
2268 list_initialize(&sess->exch_list);
2269 fibril_mutex_initialize(&sess->mutex);
2270 atomic_set(&sess->refcnt, 0);
2271
2272 return sess;
2273}
2274
2275/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2276 *
2277 * Ask through phone for a new connection to some service and block until
2278 * success.
2279 *
2280 * @param exch Exchange for sending the message.
2281 * @param iface Connection interface.
2282 * @param arg2 User defined argument.
2283 * @param arg3 User defined argument.
2284 *
2285 * @return New session on success or NULL on error.
2286 *
2287 */
2288async_sess_t *async_connect_me_to_iface(async_exch_t *exch, iface_t iface,
2289 sysarg_t arg2, sysarg_t arg3)
2290{
2291 if (exch == NULL) {
2292 errno = ENOENT;
2293 return NULL;
2294 }
2295
2296 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2297 if (sess == NULL) {
2298 errno = ENOMEM;
2299 return NULL;
2300 }
2301
2302 int phone = async_connect_me_to_internal(exch->phone, iface, arg2,
2303 arg3, 0);
2304 if (phone < 0) {
2305 errno = phone;
2306 free(sess);
2307 return NULL;
2308 }
2309
2310 sess->iface = iface;
2311 sess->phone = phone;
2312 sess->arg1 = iface;
2313 sess->arg2 = arg2;
2314 sess->arg3 = arg3;
2315
2316 fibril_mutex_initialize(&sess->remote_state_mtx);
2317 sess->remote_state_data = NULL;
2318
2319 list_initialize(&sess->exch_list);
2320 fibril_mutex_initialize(&sess->mutex);
2321 atomic_set(&sess->refcnt, 0);
2322
2323 return sess;
2324}
2325
2326/** Set arguments for new connections.
2327 *
2328 * FIXME This is an ugly hack to work around the problem that parallel
2329 * exchanges are implemented using parallel connections. When we create
2330 * a callback session, the framework does not know arguments for the new
2331 * connections.
2332 *
2333 * The proper solution seems to be to implement parallel exchanges using
2334 * tagging.
2335 */
2336void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
2337 sysarg_t arg3)
2338{
2339 sess->arg1 = arg1;
2340 sess->arg2 = arg2;
2341 sess->arg3 = arg3;
2342}
2343
2344/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2345 *
2346 * Ask through phone for a new connection to some service and block until
2347 * success.
2348 *
2349 * @param mgmt Exchange management style.
2350 * @param exch Exchange for sending the message.
2351 * @param arg1 User defined argument.
2352 * @param arg2 User defined argument.
2353 * @param arg3 User defined argument.
2354 *
2355 * @return New session on success or NULL on error.
2356 *
2357 */
2358async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
2359 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
2360{
2361 if (exch == NULL) {
2362 errno = ENOENT;
2363 return NULL;
2364 }
2365
2366 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2367 if (sess == NULL) {
2368 errno = ENOMEM;
2369 return NULL;
2370 }
2371
2372 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
2373 IPC_FLAG_BLOCKING);
2374
2375 if (phone < 0) {
2376 errno = phone;
2377 free(sess);
2378 return NULL;
2379 }
2380
2381 sess->iface = 0;
2382 sess->mgmt = mgmt;
2383 sess->phone = phone;
2384 sess->arg1 = arg1;
2385 sess->arg2 = arg2;
2386 sess->arg3 = arg3;
2387
2388 fibril_mutex_initialize(&sess->remote_state_mtx);
2389 sess->remote_state_data = NULL;
2390
2391 list_initialize(&sess->exch_list);
2392 fibril_mutex_initialize(&sess->mutex);
2393 atomic_set(&sess->refcnt, 0);
2394
2395 return sess;
2396}
2397
2398/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2399 *
2400 * Ask through phone for a new connection to some service and block until
2401 * success.
2402 *
2403 * @param exch Exchange for sending the message.
2404 * @param iface Connection interface.
2405 * @param arg2 User defined argument.
2406 * @param arg3 User defined argument.
2407 *
2408 * @return New session on success or NULL on error.
2409 *
2410 */
2411async_sess_t *async_connect_me_to_blocking_iface(async_exch_t *exch, iface_t iface,
2412 sysarg_t arg2, sysarg_t arg3)
2413{
2414 if (exch == NULL) {
2415 errno = ENOENT;
2416 return NULL;
2417 }
2418
2419 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2420 if (sess == NULL) {
2421 errno = ENOMEM;
2422 return NULL;
2423 }
2424
2425 int phone = async_connect_me_to_internal(exch->phone, iface, arg2,
2426 arg3, IPC_FLAG_BLOCKING);
2427 if (phone < 0) {
2428 errno = phone;
2429 free(sess);
2430 return NULL;
2431 }
2432
2433 sess->iface = iface;
2434 sess->phone = phone;
2435 sess->arg1 = iface;
2436 sess->arg2 = arg2;
2437 sess->arg3 = arg3;
2438
2439 fibril_mutex_initialize(&sess->remote_state_mtx);
2440 sess->remote_state_data = NULL;
2441
2442 list_initialize(&sess->exch_list);
2443 fibril_mutex_initialize(&sess->mutex);
2444 atomic_set(&sess->refcnt, 0);
2445
2446 return sess;
2447}
2448
2449/** Connect to a task specified by id.
2450 *
2451 */
2452async_sess_t *async_connect_kbox(task_id_t id)
2453{
2454 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2455 if (sess == NULL) {
2456 errno = ENOMEM;
2457 return NULL;
2458 }
2459
2460 int phone = ipc_connect_kbox(id);
2461 if (phone < 0) {
2462 errno = phone;
2463 free(sess);
2464 return NULL;
2465 }
2466
2467 sess->iface = 0;
2468 sess->mgmt = EXCHANGE_ATOMIC;
2469 sess->phone = phone;
2470 sess->arg1 = 0;
2471 sess->arg2 = 0;
2472 sess->arg3 = 0;
2473
2474 fibril_mutex_initialize(&sess->remote_state_mtx);
2475 sess->remote_state_data = NULL;
2476
2477 list_initialize(&sess->exch_list);
2478 fibril_mutex_initialize(&sess->mutex);
2479 atomic_set(&sess->refcnt, 0);
2480
2481 return sess;
2482}
2483
2484static int async_hangup_internal(int phone)
2485{
2486 return ipc_hangup(phone);
2487}
2488
2489/** Wrapper for ipc_hangup.
2490 *
2491 * @param sess Session to hung up.
2492 *
2493 * @return Zero on success or a negative error code.
2494 *
2495 */
2496int async_hangup(async_sess_t *sess)
2497{
2498 async_exch_t *exch;
2499
2500 assert(sess);
2501
2502 if (atomic_get(&sess->refcnt) > 0)
2503 return EBUSY;
2504
2505 fibril_mutex_lock(&async_sess_mutex);
2506
2507 int rc = async_hangup_internal(sess->phone);
2508
2509 while (!list_empty(&sess->exch_list)) {
2510 exch = (async_exch_t *)
2511 list_get_instance(list_first(&sess->exch_list),
2512 async_exch_t, sess_link);
2513
2514 list_remove(&exch->sess_link);
2515 list_remove(&exch->global_link);
2516 async_hangup_internal(exch->phone);
2517 free(exch);
2518 }
2519
2520 free(sess);
2521
2522 fibril_mutex_unlock(&async_sess_mutex);
2523
2524 return rc;
2525}
2526
2527/** Interrupt one thread of this task from waiting for IPC. */
2528void async_poke(void)
2529{
2530 ipc_poke();
2531}
2532
2533/** Start new exchange in a session.
2534 *
2535 * @param session Session.
2536 *
2537 * @return New exchange or NULL on error.
2538 *
2539 */
2540async_exch_t *async_exchange_begin(async_sess_t *sess)
2541{
2542 if (sess == NULL)
2543 return NULL;
2544
2545 exch_mgmt_t mgmt = sess->mgmt;
2546 if (sess->iface != 0)
2547 mgmt = sess->iface & IFACE_EXCHANGE_MASK;
2548
2549 async_exch_t *exch = NULL;
2550
2551 fibril_mutex_lock(&async_sess_mutex);
2552
2553 if (!list_empty(&sess->exch_list)) {
2554 /*
2555 * There are inactive exchanges in the session.
2556 */
2557 exch = (async_exch_t *)
2558 list_get_instance(list_first(&sess->exch_list),
2559 async_exch_t, sess_link);
2560
2561 list_remove(&exch->sess_link);
2562 list_remove(&exch->global_link);
2563 } else {
2564 /*
2565 * There are no available exchanges in the session.
2566 */
2567
2568 if ((mgmt == EXCHANGE_ATOMIC) ||
2569 (mgmt == EXCHANGE_SERIALIZE)) {
2570 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2571 if (exch != NULL) {
2572 link_initialize(&exch->sess_link);
2573 link_initialize(&exch->global_link);
2574 exch->sess = sess;
2575 exch->phone = sess->phone;
2576 }
2577 } else if (mgmt == EXCHANGE_PARALLEL) {
2578 int phone;
2579
2580 retry:
2581 /*
2582 * Make a one-time attempt to connect a new data phone.
2583 */
2584 phone = async_connect_me_to_internal(sess->phone, sess->arg1,
2585 sess->arg2, sess->arg3, 0);
2586 if (phone >= 0) {
2587 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2588 if (exch != NULL) {
2589 link_initialize(&exch->sess_link);
2590 link_initialize(&exch->global_link);
2591 exch->sess = sess;
2592 exch->phone = phone;
2593 } else
2594 async_hangup_internal(phone);
2595 } else if (!list_empty(&inactive_exch_list)) {
2596 /*
2597 * We did not manage to connect a new phone. But we
2598 * can try to close some of the currently inactive
2599 * connections in other sessions and try again.
2600 */
2601 exch = (async_exch_t *)
2602 list_get_instance(list_first(&inactive_exch_list),
2603 async_exch_t, global_link);
2604
2605 list_remove(&exch->sess_link);
2606 list_remove(&exch->global_link);
2607 async_hangup_internal(exch->phone);
2608 free(exch);
2609 goto retry;
2610 } else {
2611 /*
2612 * Wait for a phone to become available.
2613 */
2614 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
2615 goto retry;
2616 }
2617 }
2618 }
2619
2620 fibril_mutex_unlock(&async_sess_mutex);
2621
2622 if (exch != NULL) {
2623 atomic_inc(&sess->refcnt);
2624
2625 if (mgmt == EXCHANGE_SERIALIZE)
2626 fibril_mutex_lock(&sess->mutex);
2627 }
2628
2629 return exch;
2630}
2631
2632/** Finish an exchange.
2633 *
2634 * @param exch Exchange to finish.
2635 *
2636 */
2637void async_exchange_end(async_exch_t *exch)
2638{
2639 if (exch == NULL)
2640 return;
2641
2642 async_sess_t *sess = exch->sess;
2643 assert(sess != NULL);
2644
2645 exch_mgmt_t mgmt = sess->mgmt;
2646 if (sess->iface != 0)
2647 mgmt = sess->iface & IFACE_EXCHANGE_MASK;
2648
2649 atomic_dec(&sess->refcnt);
2650
2651 if (mgmt == EXCHANGE_SERIALIZE)
2652 fibril_mutex_unlock(&sess->mutex);
2653
2654 fibril_mutex_lock(&async_sess_mutex);
2655
2656 list_append(&exch->sess_link, &sess->exch_list);
2657 list_append(&exch->global_link, &inactive_exch_list);
2658 fibril_condvar_signal(&avail_phone_cv);
2659
2660 fibril_mutex_unlock(&async_sess_mutex);
2661}
2662
2663/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
2664 *
2665 * @param exch Exchange for sending the message.
2666 * @param size Size of the destination address space area.
2667 * @param arg User defined argument.
2668 * @param flags Storage for the received flags. Can be NULL.
2669 * @param dst Address of the storage for the destination address space area
2670 * base address. Cannot be NULL.
2671 *
2672 * @return Zero on success or a negative error code from errno.h.
2673 *
2674 */
2675int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
2676 unsigned int *flags, void **dst)
2677{
2678 if (exch == NULL)
2679 return ENOENT;
2680
2681 sysarg_t _flags = 0;
2682 sysarg_t _dst = (sysarg_t) -1;
2683 int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
2684 arg, NULL, &_flags, NULL, &_dst);
2685
2686 if (flags)
2687 *flags = (unsigned int) _flags;
2688
2689 *dst = (void *) _dst;
2690 return res;
2691}
2692
2693/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
2694 *
2695 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
2696 * calls so that the user doesn't have to remember the meaning of each IPC
2697 * argument.
2698 *
2699 * So far, this wrapper is to be used from within a connection fibril.
2700 *
2701 * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
2702 * @param size Destination address space area size.
2703 *
2704 * @return True on success, false on failure.
2705 *
2706 */
2707bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
2708{
2709 assert(callid);
2710 assert(size);
2711
2712 ipc_call_t data;
2713 *callid = async_get_call(&data);
2714
2715 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
2716 return false;
2717
2718 *size = (size_t) IPC_GET_ARG1(data);
2719 return true;
2720}
2721
2722/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
2723 *
2724 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
2725 * calls so that the user doesn't have to remember the meaning of each IPC
2726 * argument.
2727 *
2728 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2729 * @param src Source address space base.
2730 * @param flags Flags to be used for sharing. Bits can be only cleared.
2731 *
2732 * @return Zero on success or a value from @ref errno.h on failure.
2733 *
2734 */
2735int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
2736{
2737 return ipc_answer_3(callid, EOK, (sysarg_t) src, (sysarg_t) flags,
2738 (sysarg_t) __entry);
2739}
2740
2741/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
2742 *
2743 * @param exch Exchange for sending the message.
2744 * @param src Source address space area base address.
2745 * @param flags Flags to be used for sharing. Bits can be only cleared.
2746 *
2747 * @return Zero on success or a negative error code from errno.h.
2748 *
2749 */
2750int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
2751{
2752 if (exch == NULL)
2753 return ENOENT;
2754
2755 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
2756 (sysarg_t) flags);
2757}
2758
2759/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
2760 *
2761 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
2762 * calls so that the user doesn't have to remember the meaning of each IPC
2763 * argument.
2764 *
2765 * So far, this wrapper is to be used from within a connection fibril.
2766 *
2767 * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
2768 * @param size Storage for the source address space area size.
2769 * @param flags Storage for the sharing flags.
2770 *
2771 * @return True on success, false on failure.
2772 *
2773 */
2774bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
2775{
2776 assert(callid);
2777 assert(size);
2778 assert(flags);
2779
2780 ipc_call_t data;
2781 *callid = async_get_call(&data);
2782
2783 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
2784 return false;
2785
2786 *size = (size_t) IPC_GET_ARG2(data);
2787 *flags = (unsigned int) IPC_GET_ARG3(data);
2788 return true;
2789}
2790
2791/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2792 *
2793 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2794 * calls so that the user doesn't have to remember the meaning of each IPC
2795 * argument.
2796 *
2797 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2798 * @param dst Address of the storage for the destination address space area
2799 * base address.
2800 *
2801 * @return Zero on success or a value from @ref errno.h on failure.
2802 *
2803 */
2804int async_share_out_finalize(ipc_callid_t callid, void **dst)
2805{
2806 return ipc_answer_2(callid, EOK, (sysarg_t) __entry, (sysarg_t) dst);
2807}
2808
2809/** Start IPC_M_DATA_READ using the async framework.
2810 *
2811 * @param exch Exchange for sending the message.
2812 * @param dst Address of the beginning of the destination buffer.
2813 * @param size Size of the destination buffer (in bytes).
2814 * @param dataptr Storage of call data (arg 2 holds actual data size).
2815 *
2816 * @return Hash of the sent message or 0 on error.
2817 *
2818 */
2819aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2820 ipc_call_t *dataptr)
2821{
2822 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2823 (sysarg_t) size, dataptr);
2824}
2825
2826/** Wrapper for IPC_M_DATA_READ calls using the async framework.
2827 *
2828 * @param exch Exchange for sending the message.
2829 * @param dst Address of the beginning of the destination buffer.
2830 * @param size Size of the destination buffer.
2831 *
2832 * @return Zero on success or a negative error code from errno.h.
2833 *
2834 */
2835int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
2836{
2837 if (exch == NULL)
2838 return ENOENT;
2839
2840 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2841 (sysarg_t) size);
2842}
2843
2844/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2845 *
2846 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2847 * calls so that the user doesn't have to remember the meaning of each IPC
2848 * argument.
2849 *
2850 * So far, this wrapper is to be used from within a connection fibril.
2851 *
2852 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2853 * @param size Storage for the maximum size. Can be NULL.
2854 *
2855 * @return True on success, false on failure.
2856 *
2857 */
2858bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
2859{
2860 ipc_call_t data;
2861 return async_data_read_receive_call(callid, &data, size);
2862}
2863
2864/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2865 *
2866 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2867 * calls so that the user doesn't have to remember the meaning of each IPC
2868 * argument.
2869 *
2870 * So far, this wrapper is to be used from within a connection fibril.
2871 *
2872 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2873 * @param size Storage for the maximum size. Can be NULL.
2874 *
2875 * @return True on success, false on failure.
2876 *
2877 */
2878bool async_data_read_receive_call(ipc_callid_t *callid, ipc_call_t *data,
2879 size_t *size)
2880{
2881 assert(callid);
2882 assert(data);
2883
2884 *callid = async_get_call(data);
2885
2886 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_READ)
2887 return false;
2888
2889 if (size)
2890 *size = (size_t) IPC_GET_ARG2(*data);
2891
2892 return true;
2893}
2894
2895/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2896 *
2897 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2898 * calls so that the user doesn't have to remember the meaning of each IPC
2899 * argument.
2900 *
2901 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2902 * @param src Source address for the IPC_M_DATA_READ call.
2903 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2904 * the maximum size announced by the sender.
2905 *
2906 * @return Zero on success or a value from @ref errno.h on failure.
2907 *
2908 */
2909int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
2910{
2911 return ipc_answer_2(callid, EOK, (sysarg_t) src, (sysarg_t) size);
2912}
2913
2914/** Wrapper for forwarding any read request
2915 *
2916 */
2917int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2918 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2919 ipc_call_t *dataptr)
2920{
2921 if (exch == NULL)
2922 return ENOENT;
2923
2924 ipc_callid_t callid;
2925 if (!async_data_read_receive(&callid, NULL)) {
2926 ipc_answer_0(callid, EINVAL);
2927 return EINVAL;
2928 }
2929
2930 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2931 dataptr);
2932 if (msg == 0) {
2933 ipc_answer_0(callid, EINVAL);
2934 return EINVAL;
2935 }
2936
2937 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
2938 IPC_FF_ROUTE_FROM_ME);
2939 if (retval != EOK) {
2940 async_forget(msg);
2941 ipc_answer_0(callid, retval);
2942 return retval;
2943 }
2944
2945 sysarg_t rc;
2946 async_wait_for(msg, &rc);
2947
2948 return (int) rc;
2949}
2950
2951/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
2952 *
2953 * @param exch Exchange for sending the message.
2954 * @param src Address of the beginning of the source buffer.
2955 * @param size Size of the source buffer.
2956 *
2957 * @return Zero on success or a negative error code from errno.h.
2958 *
2959 */
2960int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
2961{
2962 if (exch == NULL)
2963 return ENOENT;
2964
2965 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2966 (sysarg_t) size);
2967}
2968
2969/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2970 *
2971 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2972 * calls so that the user doesn't have to remember the meaning of each IPC
2973 * argument.
2974 *
2975 * So far, this wrapper is to be used from within a connection fibril.
2976 *
2977 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2978 * @param size Storage for the suggested size. May be NULL.
2979 *
2980 * @return True on success, false on failure.
2981 *
2982 */
2983bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
2984{
2985 ipc_call_t data;
2986 return async_data_write_receive_call(callid, &data, size);
2987}
2988
2989/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2990 *
2991 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2992 * calls so that the user doesn't have to remember the meaning of each IPC
2993 * argument.
2994 *
2995 * So far, this wrapper is to be used from within a connection fibril.
2996 *
2997 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2998 * @param data Storage for the ipc call data.
2999 * @param size Storage for the suggested size. May be NULL.
3000 *
3001 * @return True on success, false on failure.
3002 *
3003 */
3004bool async_data_write_receive_call(ipc_callid_t *callid, ipc_call_t *data,
3005 size_t *size)
3006{
3007 assert(callid);
3008 assert(data);
3009
3010 *callid = async_get_call(data);
3011
3012 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_WRITE)
3013 return false;
3014
3015 if (size)
3016 *size = (size_t) IPC_GET_ARG2(*data);
3017
3018 return true;
3019}
3020
3021/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
3022 *
3023 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
3024 * calls so that the user doesn't have to remember the meaning of each IPC
3025 * argument.
3026 *
3027 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
3028 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
3029 * @param size Final size for the IPC_M_DATA_WRITE call.
3030 *
3031 * @return Zero on success or a value from @ref errno.h on failure.
3032 *
3033 */
3034int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
3035{
3036 return ipc_answer_2(callid, EOK, (sysarg_t) dst, (sysarg_t) size);
3037}
3038
3039/** Wrapper for receiving binary data or strings
3040 *
3041 * This wrapper only makes it more comfortable to use async_data_write_*
3042 * functions to receive binary data or strings.
3043 *
3044 * @param data Pointer to data pointer (which should be later disposed
3045 * by free()). If the operation fails, the pointer is not
3046 * touched.
3047 * @param nullterm If true then the received data is always zero terminated.
3048 * This also causes to allocate one extra byte beyond the
3049 * raw transmitted data.
3050 * @param min_size Minimum size (in bytes) of the data to receive.
3051 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
3052 * no limit.
3053 * @param granulariy If non-zero then the size of the received data has to
3054 * be divisible by this value.
3055 * @param received If not NULL, the size of the received data is stored here.
3056 *
3057 * @return Zero on success or a value from @ref errno.h on failure.
3058 *
3059 */
3060int async_data_write_accept(void **data, const bool nullterm,
3061 const size_t min_size, const size_t max_size, const size_t granularity,
3062 size_t *received)
3063{
3064 assert(data);
3065
3066 ipc_callid_t callid;
3067 size_t size;
3068 if (!async_data_write_receive(&callid, &size)) {
3069 ipc_answer_0(callid, EINVAL);
3070 return EINVAL;
3071 }
3072
3073 if (size < min_size) {
3074 ipc_answer_0(callid, EINVAL);
3075 return EINVAL;
3076 }
3077
3078 if ((max_size > 0) && (size > max_size)) {
3079 ipc_answer_0(callid, EINVAL);
3080 return EINVAL;
3081 }
3082
3083 if ((granularity > 0) && ((size % granularity) != 0)) {
3084 ipc_answer_0(callid, EINVAL);
3085 return EINVAL;
3086 }
3087
3088 void *arg_data;
3089
3090 if (nullterm)
3091 arg_data = malloc(size + 1);
3092 else
3093 arg_data = malloc(size);
3094
3095 if (arg_data == NULL) {
3096 ipc_answer_0(callid, ENOMEM);
3097 return ENOMEM;
3098 }
3099
3100 int rc = async_data_write_finalize(callid, arg_data, size);
3101 if (rc != EOK) {
3102 free(arg_data);
3103 return rc;
3104 }
3105
3106 if (nullterm)
3107 ((char *) arg_data)[size] = 0;
3108
3109 *data = arg_data;
3110 if (received != NULL)
3111 *received = size;
3112
3113 return EOK;
3114}
3115
3116/** Wrapper for voiding any data that is about to be received
3117 *
3118 * This wrapper can be used to void any pending data
3119 *
3120 * @param retval Error value from @ref errno.h to be returned to the caller.
3121 *
3122 */
3123void async_data_write_void(sysarg_t retval)
3124{
3125 ipc_callid_t callid;
3126 async_data_write_receive(&callid, NULL);
3127 ipc_answer_0(callid, retval);
3128}
3129
3130/** Wrapper for forwarding any data that is about to be received
3131 *
3132 */
3133int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
3134 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
3135 ipc_call_t *dataptr)
3136{
3137 if (exch == NULL)
3138 return ENOENT;
3139
3140 ipc_callid_t callid;
3141 if (!async_data_write_receive(&callid, NULL)) {
3142 ipc_answer_0(callid, EINVAL);
3143 return EINVAL;
3144 }
3145
3146 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
3147 dataptr);
3148 if (msg == 0) {
3149 ipc_answer_0(callid, EINVAL);
3150 return EINVAL;
3151 }
3152
3153 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
3154 IPC_FF_ROUTE_FROM_ME);
3155 if (retval != EOK) {
3156 async_forget(msg);
3157 ipc_answer_0(callid, retval);
3158 return retval;
3159 }
3160
3161 sysarg_t rc;
3162 async_wait_for(msg, &rc);
3163
3164 return (int) rc;
3165}
3166
3167/** Wrapper for sending an exchange over different exchange for cloning
3168 *
3169 * @param exch Exchange to be used for sending.
3170 * @param clone_exch Exchange to be cloned.
3171 *
3172 */
3173int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
3174{
3175 return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
3176}
3177
3178/** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
3179 *
3180 * If the current call is IPC_M_CONNECTION_CLONE then a new
3181 * async session is created for the accepted phone.
3182 *
3183 * @param mgmt Exchange management style.
3184 *
3185 * @return New async session or NULL on failure.
3186 *
3187 */
3188async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
3189{
3190 /* Accept the phone */
3191 ipc_call_t call;
3192 ipc_callid_t callid = async_get_call(&call);
3193 int phone = (int) IPC_GET_ARG1(call);
3194
3195 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
3196 (phone < 0)) {
3197 async_answer_0(callid, EINVAL);
3198 return NULL;
3199 }
3200
3201 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
3202 if (sess == NULL) {
3203 async_answer_0(callid, ENOMEM);
3204 return NULL;
3205 }
3206
3207 sess->iface = 0;
3208 sess->mgmt = mgmt;
3209 sess->phone = phone;
3210 sess->arg1 = 0;
3211 sess->arg2 = 0;
3212 sess->arg3 = 0;
3213
3214 fibril_mutex_initialize(&sess->remote_state_mtx);
3215 sess->remote_state_data = NULL;
3216
3217 list_initialize(&sess->exch_list);
3218 fibril_mutex_initialize(&sess->mutex);
3219 atomic_set(&sess->refcnt, 0);
3220
3221 /* Acknowledge the cloned phone */
3222 async_answer_0(callid, EOK);
3223
3224 return sess;
3225}
3226
3227/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
3228 *
3229 * If the current call is IPC_M_CONNECT_TO_ME then a new
3230 * async session is created for the accepted phone.
3231 *
3232 * @param mgmt Exchange management style.
3233 *
3234 * @return New async session.
3235 * @return NULL on failure.
3236 *
3237 */
3238async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
3239{
3240 /* Accept the phone */
3241 ipc_call_t call;
3242 ipc_callid_t callid = async_get_call(&call);
3243 int phone = (int) IPC_GET_ARG5(call);
3244
3245 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
3246 (phone < 0)) {
3247 async_answer_0(callid, EINVAL);
3248 return NULL;
3249 }
3250
3251 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
3252 if (sess == NULL) {
3253 async_answer_0(callid, ENOMEM);
3254 return NULL;
3255 }
3256
3257 sess->iface = 0;
3258 sess->mgmt = mgmt;
3259 sess->phone = phone;
3260 sess->arg1 = 0;
3261 sess->arg2 = 0;
3262 sess->arg3 = 0;
3263
3264 fibril_mutex_initialize(&sess->remote_state_mtx);
3265 sess->remote_state_data = NULL;
3266
3267 list_initialize(&sess->exch_list);
3268 fibril_mutex_initialize(&sess->mutex);
3269 atomic_set(&sess->refcnt, 0);
3270
3271 /* Acknowledge the connected phone */
3272 async_answer_0(callid, EOK);
3273
3274 return sess;
3275}
3276
3277/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
3278 *
3279 * If the call is IPC_M_CONNECT_TO_ME then a new
3280 * async session is created. However, the phone is
3281 * not accepted automatically.
3282 *
3283 * @param mgmt Exchange management style.
3284 * @param call Call data.
3285 *
3286 * @return New async session.
3287 * @return NULL on failure.
3288 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
3289 *
3290 */
3291async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
3292{
3293 int phone = (int) IPC_GET_ARG5(*call);
3294
3295 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
3296 (phone < 0))
3297 return NULL;
3298
3299 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
3300 if (sess == NULL)
3301 return NULL;
3302
3303 sess->iface = 0;
3304 sess->mgmt = mgmt;
3305 sess->phone = phone;
3306 sess->arg1 = 0;
3307 sess->arg2 = 0;
3308 sess->arg3 = 0;
3309
3310 fibril_mutex_initialize(&sess->remote_state_mtx);
3311 sess->remote_state_data = NULL;
3312
3313 list_initialize(&sess->exch_list);
3314 fibril_mutex_initialize(&sess->mutex);
3315 atomic_set(&sess->refcnt, 0);
3316
3317 return sess;
3318}
3319
3320int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
3321 sysarg_t arg3, async_exch_t *other_exch)
3322{
3323 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
3324 arg1, arg2, arg3, 0, other_exch->phone);
3325}
3326
3327bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
3328 sysarg_t *arg2, sysarg_t *arg3)
3329{
3330 assert(callid);
3331
3332 ipc_call_t call;
3333 *callid = async_get_call(&call);
3334
3335 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
3336 return false;
3337
3338 if (arg1)
3339 *arg1 = IPC_GET_ARG1(call);
3340 if (arg2)
3341 *arg2 = IPC_GET_ARG2(call);
3342 if (arg3)
3343 *arg3 = IPC_GET_ARG3(call);
3344
3345 return true;
3346}
3347
3348int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
3349{
3350 return ipc_answer_1(callid, EOK, other_exch->phone);
3351}
3352
3353/** Lock and get session remote state
3354 *
3355 * Lock and get the local replica of the remote state
3356 * in stateful sessions. The call should be paired
3357 * with async_remote_state_release*().
3358 *
3359 * @param[in] sess Stateful session.
3360 *
3361 * @return Local replica of the remote state.
3362 *
3363 */
3364void *async_remote_state_acquire(async_sess_t *sess)
3365{
3366 fibril_mutex_lock(&sess->remote_state_mtx);
3367 return sess->remote_state_data;
3368}
3369
3370/** Update the session remote state
3371 *
3372 * Update the local replica of the remote state
3373 * in stateful sessions. The remote state must
3374 * be already locked.
3375 *
3376 * @param[in] sess Stateful session.
3377 * @param[in] state New local replica of the remote state.
3378 *
3379 */
3380void async_remote_state_update(async_sess_t *sess, void *state)
3381{
3382 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3383 sess->remote_state_data = state;
3384}
3385
3386/** Release the session remote state
3387 *
3388 * Unlock the local replica of the remote state
3389 * in stateful sessions.
3390 *
3391 * @param[in] sess Stateful session.
3392 *
3393 */
3394void async_remote_state_release(async_sess_t *sess)
3395{
3396 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3397
3398 fibril_mutex_unlock(&sess->remote_state_mtx);
3399}
3400
3401/** Release the session remote state and end an exchange
3402 *
3403 * Unlock the local replica of the remote state
3404 * in stateful sessions. This is convenience function
3405 * which gets the session pointer from the exchange
3406 * and also ends the exchange.
3407 *
3408 * @param[in] exch Stateful session's exchange.
3409 *
3410 */
3411void async_remote_state_release_exchange(async_exch_t *exch)
3412{
3413 if (exch == NULL)
3414 return;
3415
3416 async_sess_t *sess = exch->sess;
3417 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3418
3419 async_exchange_end(exch);
3420 fibril_mutex_unlock(&sess->remote_state_mtx);
3421}
3422
3423/** @}
3424 */
Note: See TracBrowser for help on using the repository browser.