source: mainline/uspace/lib/c/generic/async.c@ 566992e1

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

extremely rudimentary support for interfaces and ports
(does not do much, but it is backward and forward compatible)

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