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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a53ed3a was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

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