source: mainline/uspace/lib/c/generic/async.c@ 0dd16778

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

start migrating devman to interfaces

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