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

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

unify interface API

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