source: mainline/uspace/lib/c/generic/async.c@ 78bb04b

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

introduce ports for callback connections

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