source: mainline/uspace/lib/c/generic/async.c@ 10cb47e

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

code review and cstyle cleanup (no change in functionality)

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