source: mainline/uspace/lib/c/generic/async.c@ 6deb2cd

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

Return capability handle in SYS_IPC_WAIT via call data structure, separately from error codes.

  • Property mode set to 100644
File size: 79.8 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 sysarg_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 = (sysarg_t) 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
513int 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 int 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 0;
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 a negative error code.
847 *
848 */
849int 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 sysarg_t ret;
863 async_wait_for(req, &ret);
864 if (ret != EOK)
865 return (int) 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 * @return IRQ capability handle on success.
1054 * @return Negative error code.
1055 *
1056 */
1057int async_irq_subscribe(int inr, async_notification_handler_t handler,
1058 void *data, const irq_code_t *ucode)
1059{
1060 notification_t *notification =
1061 (notification_t *) malloc(sizeof(notification_t));
1062 if (!notification)
1063 return ENOMEM;
1064
1065 futex_down(&async_futex);
1066
1067 sysarg_t imethod = notification_avail;
1068 notification_avail++;
1069
1070 notification->imethod = imethod;
1071 notification->handler = handler;
1072 notification->data = data;
1073
1074 hash_table_insert(&notification_hash_table, &notification->link);
1075
1076 futex_up(&async_futex);
1077
1078 return ipc_irq_subscribe(inr, imethod, ucode);
1079}
1080
1081/** Unsubscribe from IRQ notification.
1082 *
1083 * @param cap IRQ capability handle.
1084 *
1085 * @return Zero on success or a negative error code.
1086 *
1087 */
1088int async_irq_unsubscribe(int cap)
1089{
1090 // TODO: Remove entry from hash table
1091 // to avoid memory leak
1092
1093 return ipc_irq_unsubscribe(cap);
1094}
1095
1096/** Subscribe to event notifications.
1097 *
1098 * @param evno Event type to subscribe.
1099 * @param handler Notification handler.
1100 * @param data Notification handler client data.
1101 *
1102 * @return Zero on success or a negative error code.
1103 *
1104 */
1105int async_event_subscribe(event_type_t evno,
1106 async_notification_handler_t handler, void *data)
1107{
1108 notification_t *notification =
1109 (notification_t *) malloc(sizeof(notification_t));
1110 if (!notification)
1111 return ENOMEM;
1112
1113 futex_down(&async_futex);
1114
1115 sysarg_t imethod = notification_avail;
1116 notification_avail++;
1117
1118 notification->imethod = imethod;
1119 notification->handler = handler;
1120 notification->data = data;
1121
1122 hash_table_insert(&notification_hash_table, &notification->link);
1123
1124 futex_up(&async_futex);
1125
1126 return ipc_event_subscribe(evno, imethod);
1127}
1128
1129/** Subscribe to task event notifications.
1130 *
1131 * @param evno Event type to subscribe.
1132 * @param handler Notification handler.
1133 * @param data Notification handler client data.
1134 *
1135 * @return Zero on success or a negative error code.
1136 *
1137 */
1138int async_event_task_subscribe(event_task_type_t evno,
1139 async_notification_handler_t handler, void *data)
1140{
1141 notification_t *notification =
1142 (notification_t *) malloc(sizeof(notification_t));
1143 if (!notification)
1144 return ENOMEM;
1145
1146 futex_down(&async_futex);
1147
1148 sysarg_t imethod = notification_avail;
1149 notification_avail++;
1150
1151 notification->imethod = imethod;
1152 notification->handler = handler;
1153 notification->data = data;
1154
1155 hash_table_insert(&notification_hash_table, &notification->link);
1156
1157 futex_up(&async_futex);
1158
1159 return ipc_event_task_subscribe(evno, imethod);
1160}
1161
1162/** Unmask event notifications.
1163 *
1164 * @param evno Event type to unmask.
1165 *
1166 * @return Value returned by the kernel.
1167 *
1168 */
1169int async_event_unmask(event_type_t evno)
1170{
1171 return ipc_event_unmask(evno);
1172}
1173
1174/** Unmask task event notifications.
1175 *
1176 * @param evno Event type to unmask.
1177 *
1178 * @return Value returned by the kernel.
1179 *
1180 */
1181int async_event_task_unmask(event_task_type_t evno)
1182{
1183 return ipc_event_task_unmask(evno);
1184}
1185
1186/** Return new incoming message for the current (fibril-local) connection.
1187 *
1188 * @param call Storage where the incoming call data will be stored.
1189 * @param usecs Timeout in microseconds. Zero denotes no timeout.
1190 *
1191 * @return If no timeout was specified, then a handle of the incoming call is
1192 * returned. If a timeout is specified, then a handle of the incoming
1193 * call is returned unless the timeout expires prior to receiving a
1194 * message. In that case zero CAP_NIL is returned.
1195 */
1196cap_handle_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
1197{
1198 assert(call);
1199 assert(fibril_connection);
1200
1201 /* Why doing this?
1202 * GCC 4.1.0 coughs on fibril_connection-> dereference.
1203 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
1204 * I would never expect to find so many errors in
1205 * a compiler.
1206 */
1207 connection_t *conn = fibril_connection;
1208
1209 futex_down(&async_futex);
1210
1211 if (usecs) {
1212 getuptime(&conn->wdata.to_event.expires);
1213 tv_add_diff(&conn->wdata.to_event.expires, usecs);
1214 } else
1215 conn->wdata.to_event.inlist = false;
1216
1217 /* If nothing in queue, wait until something arrives */
1218 while (list_empty(&conn->msg_queue)) {
1219 if (conn->close_chandle) {
1220 /*
1221 * Handle the case when the connection was already
1222 * closed by the client but the server did not notice
1223 * the first IPC_M_PHONE_HUNGUP call and continues to
1224 * call async_get_call_timeout(). Repeat
1225 * IPC_M_PHONE_HUNGUP until the caller notices.
1226 */
1227 memset(call, 0, sizeof(ipc_call_t));
1228 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
1229 futex_up(&async_futex);
1230 return conn->close_chandle;
1231 }
1232
1233 if (usecs)
1234 async_insert_timeout(&conn->wdata);
1235
1236 conn->wdata.active = false;
1237
1238 /*
1239 * Note: the current fibril will be rescheduled either due to a
1240 * timeout or due to an arriving message destined to it. In the
1241 * former case, handle_expired_timeouts() and, in the latter
1242 * case, route_call() will perform the wakeup.
1243 */
1244 fibril_switch(FIBRIL_TO_MANAGER);
1245
1246 /*
1247 * Futex is up after getting back from async_manager.
1248 * Get it again.
1249 */
1250 futex_down(&async_futex);
1251 if ((usecs) && (conn->wdata.to_event.occurred)
1252 && (list_empty(&conn->msg_queue))) {
1253 /* If we timed out -> exit */
1254 futex_up(&async_futex);
1255 return CAP_NIL;
1256 }
1257 }
1258
1259 msg_t *msg = list_get_instance(list_first(&conn->msg_queue),
1260 msg_t, link);
1261 list_remove(&msg->link);
1262
1263 cap_handle_t chandle = msg->chandle;
1264 *call = msg->call;
1265 free(msg);
1266
1267 futex_up(&async_futex);
1268 return chandle;
1269}
1270
1271void *async_get_client_data(void)
1272{
1273 assert(fibril_connection);
1274 return fibril_connection->client->data;
1275}
1276
1277void *async_get_client_data_by_id(task_id_t client_id)
1278{
1279 client_t *client = async_client_get(client_id, false);
1280 if (!client)
1281 return NULL;
1282
1283 if (!client->data) {
1284 async_client_put(client);
1285 return NULL;
1286 }
1287
1288 return client->data;
1289}
1290
1291void async_put_client_data_by_id(task_id_t client_id)
1292{
1293 client_t *client = async_client_get(client_id, false);
1294
1295 assert(client);
1296 assert(client->data);
1297
1298 /* Drop the reference we got in async_get_client_data_by_hash(). */
1299 async_client_put(client);
1300
1301 /* Drop our own reference we got at the beginning of this function. */
1302 async_client_put(client);
1303}
1304
1305static port_t *async_find_port(iface_t iface, port_id_t port_id)
1306{
1307 port_t *port = NULL;
1308
1309 futex_down(&async_futex);
1310
1311 ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
1312 if (link) {
1313 interface_t *interface =
1314 hash_table_get_inst(link, interface_t, link);
1315
1316 link = hash_table_find(&interface->port_hash_table, &port_id);
1317 if (link)
1318 port = hash_table_get_inst(link, port_t, link);
1319 }
1320
1321 futex_up(&async_futex);
1322
1323 return port;
1324}
1325
1326/** Handle a call that was received.
1327 *
1328 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
1329 * Otherwise the call is routed to its connection fibril.
1330 *
1331 * @param chandle Handle of the incoming call.
1332 * @param call Data of the incoming call.
1333 *
1334 */
1335static void handle_call(cap_handle_t chandle, ipc_call_t *call)
1336{
1337 assert(call);
1338
1339 /* Kernel notification */
1340 if ((chandle == CAP_NIL) && (call->flags & IPC_CALL_NOTIF)) {
1341 fibril_t *fibril = (fibril_t *) __tcb_get()->fibril_data;
1342 unsigned oldsw = fibril->switches;
1343
1344 process_notification(call);
1345
1346 if (oldsw != fibril->switches) {
1347 /*
1348 * The notification handler did not execute atomically
1349 * and so the current manager fibril assumed the role of
1350 * a notification fibril. While waiting for its
1351 * resources, it switched to another manager fibril that
1352 * had already existed or it created a new one. We
1353 * therefore know there is at least yet another
1354 * manager fibril that can take over. We now kill the
1355 * current 'notification' fibril to prevent fibril
1356 * population explosion.
1357 */
1358 futex_down(&async_futex);
1359 fibril_switch(FIBRIL_FROM_DEAD);
1360 }
1361
1362 return;
1363 }
1364
1365 /* New connection */
1366 if (IPC_GET_IMETHOD(*call) == IPC_M_CONNECT_ME_TO) {
1367 iface_t iface = (iface_t) IPC_GET_ARG1(*call);
1368 sysarg_t in_phone_hash = IPC_GET_ARG5(*call);
1369
1370 async_port_handler_t handler = fallback_port_handler;
1371 void *data = fallback_port_data;
1372
1373 // TODO: Currently ignores all ports but the first one
1374 port_t *port = async_find_port(iface, 0);
1375 if (port) {
1376 handler = port->handler;
1377 data = port->data;
1378 }
1379
1380 async_new_connection(call->in_task_id, in_phone_hash, chandle,
1381 call, handler, data);
1382 return;
1383 }
1384
1385 /* Try to route the call through the connection hash table */
1386 if (route_call(chandle, call))
1387 return;
1388
1389 /* Unknown call from unknown phone - hang it up */
1390 ipc_answer_0(chandle, EHANGUP);
1391}
1392
1393/** Fire all timeouts that expired. */
1394static void handle_expired_timeouts(void)
1395{
1396 struct timeval tv;
1397 getuptime(&tv);
1398
1399 futex_down(&async_futex);
1400
1401 link_t *cur = list_first(&timeout_list);
1402 while (cur != NULL) {
1403 awaiter_t *waiter =
1404 list_get_instance(cur, awaiter_t, to_event.link);
1405
1406 if (tv_gt(&waiter->to_event.expires, &tv))
1407 break;
1408
1409 list_remove(&waiter->to_event.link);
1410 waiter->to_event.inlist = false;
1411 waiter->to_event.occurred = true;
1412
1413 /*
1414 * Redundant condition?
1415 * The fibril should not be active when it gets here.
1416 */
1417 if (!waiter->active) {
1418 waiter->active = true;
1419 fibril_add_ready(waiter->fid);
1420 }
1421
1422 cur = list_first(&timeout_list);
1423 }
1424
1425 futex_up(&async_futex);
1426}
1427
1428/** Endless loop dispatching incoming calls and answers.
1429 *
1430 * @return Never returns.
1431 *
1432 */
1433static int async_manager_worker(void)
1434{
1435 while (true) {
1436 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
1437 futex_up(&async_futex);
1438 /*
1439 * async_futex is always held when entering a manager
1440 * fibril.
1441 */
1442 continue;
1443 }
1444
1445 futex_down(&async_futex);
1446
1447 suseconds_t timeout;
1448 unsigned int flags = SYNCH_FLAGS_NONE;
1449 if (!list_empty(&timeout_list)) {
1450 awaiter_t *waiter = list_get_instance(
1451 list_first(&timeout_list), awaiter_t, to_event.link);
1452
1453 struct timeval tv;
1454 getuptime(&tv);
1455
1456 if (tv_gteq(&tv, &waiter->to_event.expires)) {
1457 futex_up(&async_futex);
1458 handle_expired_timeouts();
1459 /*
1460 * Notice that even if the event(s) already
1461 * expired (and thus the other fibril was
1462 * supposed to be running already),
1463 * we check for incoming IPC.
1464 *
1465 * Otherwise, a fibril that continuously
1466 * creates (almost) expired events could
1467 * prevent IPC retrieval from the kernel.
1468 */
1469 timeout = 0;
1470 flags = SYNCH_FLAGS_NON_BLOCKING;
1471
1472 } else {
1473 timeout = tv_sub_diff(&waiter->to_event.expires,
1474 &tv);
1475 futex_up(&async_futex);
1476 }
1477 } else {
1478 futex_up(&async_futex);
1479 timeout = SYNCH_NO_TIMEOUT;
1480 }
1481
1482 atomic_inc(&threads_in_ipc_wait);
1483
1484 ipc_call_t call;
1485 int rc = ipc_wait_cycle(&call, timeout, flags);
1486
1487 atomic_dec(&threads_in_ipc_wait);
1488
1489 assert(rc == EOK);
1490
1491 if (call.cap_handle == CAP_NIL) {
1492 if (call.flags == 0) {
1493 /* This neither a notification nor an answer. */
1494 handle_expired_timeouts();
1495 continue;
1496 }
1497 }
1498
1499 if (call.flags & IPC_CALL_ANSWERED)
1500 continue;
1501
1502 handle_call(call.cap_handle, &call);
1503 }
1504
1505 return 0;
1506}
1507
1508/** Function to start async_manager as a standalone fibril.
1509 *
1510 * When more kernel threads are used, one async manager should exist per thread.
1511 *
1512 * @param arg Unused.
1513 * @return Never returns.
1514 *
1515 */
1516static int async_manager_fibril(void *arg)
1517{
1518 futex_up(&async_futex);
1519
1520 /*
1521 * async_futex is always locked when entering manager
1522 */
1523 async_manager_worker();
1524
1525 return 0;
1526}
1527
1528/** Add one manager to manager list. */
1529void async_create_manager(void)
1530{
1531 fid_t fid = fibril_create_generic(async_manager_fibril, NULL, PAGE_SIZE);
1532 if (fid != 0)
1533 fibril_add_manager(fid);
1534}
1535
1536/** Remove one manager from manager list */
1537void async_destroy_manager(void)
1538{
1539 fibril_remove_manager();
1540}
1541
1542/** Initialize the async framework.
1543 *
1544 */
1545void __async_init(void)
1546{
1547 if (!hash_table_create(&interface_hash_table, 0, 0,
1548 &interface_hash_table_ops))
1549 abort();
1550
1551 if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
1552 abort();
1553
1554 if (!hash_table_create(&conn_hash_table, 0, 0, &conn_hash_table_ops))
1555 abort();
1556
1557 if (!hash_table_create(&notification_hash_table, 0, 0,
1558 &notification_hash_table_ops))
1559 abort();
1560
1561 session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
1562 if (session_ns == NULL)
1563 abort();
1564
1565 session_ns->iface = 0;
1566 session_ns->mgmt = EXCHANGE_ATOMIC;
1567 session_ns->phone = PHONE_NS;
1568 session_ns->arg1 = 0;
1569 session_ns->arg2 = 0;
1570 session_ns->arg3 = 0;
1571
1572 fibril_mutex_initialize(&session_ns->remote_state_mtx);
1573 session_ns->remote_state_data = NULL;
1574
1575 list_initialize(&session_ns->exch_list);
1576 fibril_mutex_initialize(&session_ns->mutex);
1577 atomic_set(&session_ns->refcnt, 0);
1578}
1579
1580/** Reply received callback.
1581 *
1582 * This function is called whenever a reply for an asynchronous message sent out
1583 * by the asynchronous framework is received.
1584 *
1585 * Notify the fibril which is waiting for this message that it has arrived.
1586 *
1587 * @param arg Pointer to the asynchronous message record.
1588 * @param retval Value returned in the answer.
1589 * @param data Call data of the answer.
1590 *
1591 */
1592void reply_received(void *arg, int retval, ipc_call_t *data)
1593{
1594 assert(arg);
1595
1596 futex_down(&async_futex);
1597
1598 amsg_t *msg = (amsg_t *) arg;
1599 msg->retval = retval;
1600
1601 /* Copy data after futex_down, just in case the call was detached */
1602 if ((msg->dataptr) && (data))
1603 *msg->dataptr = *data;
1604
1605 write_barrier();
1606
1607 /* Remove message from timeout list */
1608 if (msg->wdata.to_event.inlist)
1609 list_remove(&msg->wdata.to_event.link);
1610
1611 msg->done = true;
1612
1613 if (msg->forget) {
1614 assert(msg->wdata.active);
1615 amsg_destroy(msg);
1616 } else if (!msg->wdata.active) {
1617 msg->wdata.active = true;
1618 fibril_add_ready(msg->wdata.fid);
1619 }
1620
1621 futex_up(&async_futex);
1622}
1623
1624/** Send message and return id of the sent message.
1625 *
1626 * The return value can be used as input for async_wait() to wait for
1627 * completion.
1628 *
1629 * @param exch Exchange for sending the message.
1630 * @param imethod Service-defined interface and method.
1631 * @param arg1 Service-defined payload argument.
1632 * @param arg2 Service-defined payload argument.
1633 * @param arg3 Service-defined payload argument.
1634 * @param arg4 Service-defined payload argument.
1635 * @param dataptr If non-NULL, storage where the reply data will be stored.
1636 *
1637 * @return Hash of the sent message or 0 on error.
1638 *
1639 */
1640aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1641 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1642{
1643 if (exch == NULL)
1644 return 0;
1645
1646 amsg_t *msg = amsg_create();
1647 if (msg == NULL)
1648 return 0;
1649
1650 msg->dataptr = dataptr;
1651 msg->wdata.active = true;
1652
1653 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
1654 reply_received);
1655
1656 return (aid_t) msg;
1657}
1658
1659/** Send message and return id of the sent message
1660 *
1661 * The return value can be used as input for async_wait() to wait for
1662 * completion.
1663 *
1664 * @param exch Exchange for sending the message.
1665 * @param imethod Service-defined interface and method.
1666 * @param arg1 Service-defined payload argument.
1667 * @param arg2 Service-defined payload argument.
1668 * @param arg3 Service-defined payload argument.
1669 * @param arg4 Service-defined payload argument.
1670 * @param arg5 Service-defined payload argument.
1671 * @param dataptr If non-NULL, storage where the reply data will be
1672 * stored.
1673 *
1674 * @return Hash of the sent message or 0 on error.
1675 *
1676 */
1677aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1678 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
1679 ipc_call_t *dataptr)
1680{
1681 if (exch == NULL)
1682 return 0;
1683
1684 amsg_t *msg = amsg_create();
1685 if (msg == NULL)
1686 return 0;
1687
1688 msg->dataptr = dataptr;
1689 msg->wdata.active = true;
1690
1691 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
1692 msg, reply_received);
1693
1694 return (aid_t) msg;
1695}
1696
1697/** Wait for a message sent by the async framework.
1698 *
1699 * @param amsgid Hash of the message to wait for.
1700 * @param retval Pointer to storage where the retval of the answer will
1701 * be stored.
1702 *
1703 */
1704void async_wait_for(aid_t amsgid, sysarg_t *retval)
1705{
1706 assert(amsgid);
1707
1708 amsg_t *msg = (amsg_t *) amsgid;
1709
1710 futex_down(&async_futex);
1711
1712 assert(!msg->forget);
1713 assert(!msg->destroyed);
1714
1715 if (msg->done) {
1716 futex_up(&async_futex);
1717 goto done;
1718 }
1719
1720 msg->wdata.fid = fibril_get_id();
1721 msg->wdata.active = false;
1722 msg->wdata.to_event.inlist = false;
1723
1724 /* Leave the async_futex locked when entering this function */
1725 fibril_switch(FIBRIL_TO_MANAGER);
1726
1727 /* Futex is up automatically after fibril_switch */
1728
1729done:
1730 if (retval)
1731 *retval = msg->retval;
1732
1733 amsg_destroy(msg);
1734}
1735
1736/** Wait for a message sent by the async framework, timeout variant.
1737 *
1738 * If the wait times out, the caller may choose to either wait again by calling
1739 * async_wait_for() or async_wait_timeout(), or forget the message via
1740 * async_forget().
1741 *
1742 * @param amsgid Hash of the message to wait for.
1743 * @param retval Pointer to storage where the retval of the answer will
1744 * be stored.
1745 * @param timeout Timeout in microseconds.
1746 *
1747 * @return Zero on success, ETIMEOUT if the timeout has expired.
1748 *
1749 */
1750int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
1751{
1752 assert(amsgid);
1753
1754 amsg_t *msg = (amsg_t *) amsgid;
1755
1756 futex_down(&async_futex);
1757
1758 assert(!msg->forget);
1759 assert(!msg->destroyed);
1760
1761 if (msg->done) {
1762 futex_up(&async_futex);
1763 goto done;
1764 }
1765
1766 /*
1767 * Negative timeout is converted to zero timeout to avoid
1768 * using tv_add with negative augmenter.
1769 */
1770 if (timeout < 0)
1771 timeout = 0;
1772
1773 getuptime(&msg->wdata.to_event.expires);
1774 tv_add_diff(&msg->wdata.to_event.expires, timeout);
1775
1776 /*
1777 * Current fibril is inserted as waiting regardless of the
1778 * "size" of the timeout.
1779 *
1780 * Checking for msg->done and immediately bailing out when
1781 * timeout == 0 would mean that the manager fibril would never
1782 * run (consider single threaded program).
1783 * Thus the IPC answer would be never retrieved from the kernel.
1784 *
1785 * Notice that the actual delay would be very small because we
1786 * - switch to manager fibril
1787 * - the manager sees expired timeout
1788 * - and thus adds us back to ready queue
1789 * - manager switches back to some ready fibril
1790 * (prior it, it checks for incoming IPC).
1791 *
1792 */
1793 msg->wdata.fid = fibril_get_id();
1794 msg->wdata.active = false;
1795 async_insert_timeout(&msg->wdata);
1796
1797 /* Leave the async_futex locked when entering this function */
1798 fibril_switch(FIBRIL_TO_MANAGER);
1799
1800 /* Futex is up automatically after fibril_switch */
1801
1802 if (!msg->done)
1803 return ETIMEOUT;
1804
1805done:
1806 if (retval)
1807 *retval = msg->retval;
1808
1809 amsg_destroy(msg);
1810
1811 return 0;
1812}
1813
1814/** Discard the message / reply on arrival.
1815 *
1816 * The message will be marked to be discarded once the reply arrives in
1817 * reply_received(). It is not allowed to call async_wait_for() or
1818 * async_wait_timeout() on this message after a call to this function.
1819 *
1820 * @param amsgid Hash of the message to forget.
1821 */
1822void async_forget(aid_t amsgid)
1823{
1824 amsg_t *msg = (amsg_t *) amsgid;
1825
1826 assert(msg);
1827 assert(!msg->forget);
1828 assert(!msg->destroyed);
1829
1830 futex_down(&async_futex);
1831
1832 if (msg->done) {
1833 amsg_destroy(msg);
1834 } else {
1835 msg->dataptr = NULL;
1836 msg->forget = true;
1837 }
1838
1839 futex_up(&async_futex);
1840}
1841
1842/** Wait for specified time.
1843 *
1844 * The current fibril is suspended but the thread continues to execute.
1845 *
1846 * @param timeout Duration of the wait in microseconds.
1847 *
1848 */
1849void async_usleep(suseconds_t timeout)
1850{
1851 amsg_t *msg = amsg_create();
1852 if (!msg)
1853 return;
1854
1855 msg->wdata.fid = fibril_get_id();
1856
1857 getuptime(&msg->wdata.to_event.expires);
1858 tv_add_diff(&msg->wdata.to_event.expires, timeout);
1859
1860 futex_down(&async_futex);
1861
1862 async_insert_timeout(&msg->wdata);
1863
1864 /* Leave the async_futex locked when entering this function */
1865 fibril_switch(FIBRIL_TO_MANAGER);
1866
1867 /* Futex is up automatically after fibril_switch() */
1868
1869 amsg_destroy(msg);
1870}
1871
1872/** Delay execution for the specified number of seconds
1873 *
1874 * @param sec Number of seconds to sleep
1875 */
1876void async_sleep(unsigned int sec)
1877{
1878 /*
1879 * Sleep in 1000 second steps to support
1880 * full argument range
1881 */
1882
1883 while (sec > 0) {
1884 unsigned int period = (sec > 1000) ? 1000 : sec;
1885
1886 async_usleep(period * 1000000);
1887 sec -= period;
1888 }
1889}
1890
1891/** Pseudo-synchronous message sending - fast version.
1892 *
1893 * Send message asynchronously and return only after the reply arrives.
1894 *
1895 * This function can only transfer 4 register payload arguments. For
1896 * transferring more arguments, see the slower async_req_slow().
1897 *
1898 * @param exch Exchange for sending the message.
1899 * @param imethod Interface and method of the call.
1900 * @param arg1 Service-defined payload argument.
1901 * @param arg2 Service-defined payload argument.
1902 * @param arg3 Service-defined payload argument.
1903 * @param arg4 Service-defined payload argument.
1904 * @param r1 If non-NULL, storage for the 1st reply argument.
1905 * @param r2 If non-NULL, storage for the 2nd reply argument.
1906 * @param r3 If non-NULL, storage for the 3rd reply argument.
1907 * @param r4 If non-NULL, storage for the 4th reply argument.
1908 * @param r5 If non-NULL, storage for the 5th reply argument.
1909 *
1910 * @return Return code of the reply or a negative error code.
1911 *
1912 */
1913sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1914 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1915 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1916{
1917 if (exch == NULL)
1918 return ENOENT;
1919
1920 ipc_call_t result;
1921 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
1922 &result);
1923
1924 sysarg_t rc;
1925 async_wait_for(aid, &rc);
1926
1927 if (r1)
1928 *r1 = IPC_GET_ARG1(result);
1929
1930 if (r2)
1931 *r2 = IPC_GET_ARG2(result);
1932
1933 if (r3)
1934 *r3 = IPC_GET_ARG3(result);
1935
1936 if (r4)
1937 *r4 = IPC_GET_ARG4(result);
1938
1939 if (r5)
1940 *r5 = IPC_GET_ARG5(result);
1941
1942 return rc;
1943}
1944
1945/** Pseudo-synchronous message sending - slow version.
1946 *
1947 * Send message asynchronously and return only after the reply arrives.
1948 *
1949 * @param exch Exchange for sending the message.
1950 * @param imethod Interface and method of the call.
1951 * @param arg1 Service-defined payload argument.
1952 * @param arg2 Service-defined payload argument.
1953 * @param arg3 Service-defined payload argument.
1954 * @param arg4 Service-defined payload argument.
1955 * @param arg5 Service-defined payload argument.
1956 * @param r1 If non-NULL, storage for the 1st reply argument.
1957 * @param r2 If non-NULL, storage for the 2nd reply argument.
1958 * @param r3 If non-NULL, storage for the 3rd reply argument.
1959 * @param r4 If non-NULL, storage for the 4th reply argument.
1960 * @param r5 If non-NULL, storage for the 5th reply argument.
1961 *
1962 * @return Return code of the reply or a negative error code.
1963 *
1964 */
1965sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1966 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1967 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1968{
1969 if (exch == NULL)
1970 return ENOENT;
1971
1972 ipc_call_t result;
1973 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
1974 &result);
1975
1976 sysarg_t rc;
1977 async_wait_for(aid, &rc);
1978
1979 if (r1)
1980 *r1 = IPC_GET_ARG1(result);
1981
1982 if (r2)
1983 *r2 = IPC_GET_ARG2(result);
1984
1985 if (r3)
1986 *r3 = IPC_GET_ARG3(result);
1987
1988 if (r4)
1989 *r4 = IPC_GET_ARG4(result);
1990
1991 if (r5)
1992 *r5 = IPC_GET_ARG5(result);
1993
1994 return rc;
1995}
1996
1997void async_msg_0(async_exch_t *exch, sysarg_t imethod)
1998{
1999 if (exch != NULL)
2000 ipc_call_async_0(exch->phone, imethod, NULL, NULL);
2001}
2002
2003void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
2004{
2005 if (exch != NULL)
2006 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL);
2007}
2008
2009void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2010 sysarg_t arg2)
2011{
2012 if (exch != NULL)
2013 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL);
2014}
2015
2016void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2017 sysarg_t arg2, sysarg_t arg3)
2018{
2019 if (exch != NULL)
2020 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
2021 NULL);
2022}
2023
2024void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2025 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
2026{
2027 if (exch != NULL)
2028 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
2029 NULL, NULL);
2030}
2031
2032void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
2033 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
2034{
2035 if (exch != NULL)
2036 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
2037 arg5, NULL, NULL);
2038}
2039
2040sysarg_t async_answer_0(cap_handle_t chandle, sysarg_t retval)
2041{
2042 return ipc_answer_0(chandle, retval);
2043}
2044
2045sysarg_t async_answer_1(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1)
2046{
2047 return ipc_answer_1(chandle, retval, arg1);
2048}
2049
2050sysarg_t async_answer_2(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
2051 sysarg_t arg2)
2052{
2053 return ipc_answer_2(chandle, retval, arg1, arg2);
2054}
2055
2056sysarg_t async_answer_3(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
2057 sysarg_t arg2, sysarg_t arg3)
2058{
2059 return ipc_answer_3(chandle, retval, arg1, arg2, arg3);
2060}
2061
2062sysarg_t async_answer_4(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
2063 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
2064{
2065 return ipc_answer_4(chandle, retval, arg1, arg2, arg3, arg4);
2066}
2067
2068sysarg_t async_answer_5(cap_handle_t chandle, sysarg_t retval, sysarg_t arg1,
2069 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
2070{
2071 return ipc_answer_5(chandle, retval, arg1, arg2, arg3, arg4, arg5);
2072}
2073
2074int async_forward_fast(cap_handle_t chandle, async_exch_t *exch,
2075 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
2076{
2077 if (exch == NULL)
2078 return ENOENT;
2079
2080 return ipc_forward_fast(chandle, exch->phone, imethod, arg1, arg2, mode);
2081}
2082
2083int async_forward_slow(cap_handle_t chandle, async_exch_t *exch,
2084 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
2085 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
2086{
2087 if (exch == NULL)
2088 return ENOENT;
2089
2090 return ipc_forward_slow(chandle, exch->phone, imethod, arg1, arg2, arg3,
2091 arg4, arg5, mode);
2092}
2093
2094/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
2095 *
2096 * Ask through phone for a new connection to some service.
2097 *
2098 * @param exch Exchange for sending the message.
2099 * @param arg1 User defined argument.
2100 * @param arg2 User defined argument.
2101 * @param arg3 User defined argument.
2102 *
2103 * @return Zero on success or a negative error code.
2104 *
2105 */
2106int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
2107 sysarg_t arg3)
2108{
2109 if (exch == NULL)
2110 return ENOENT;
2111
2112 ipc_call_t answer;
2113 aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
2114 &answer);
2115
2116 sysarg_t rc;
2117 async_wait_for(req, &rc);
2118 if (rc != EOK)
2119 return (int) rc;
2120
2121 return EOK;
2122}
2123
2124static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
2125 sysarg_t arg3, sysarg_t arg4, int *out_phone)
2126{
2127 ipc_call_t result;
2128
2129 // XXX: Workaround for GCC's inability to infer association between
2130 // rc == EOK and *out_phone being assigned.
2131 *out_phone = -1;
2132
2133 amsg_t *msg = amsg_create();
2134 if (!msg)
2135 return ENOENT;
2136
2137 msg->dataptr = &result;
2138 msg->wdata.active = true;
2139
2140 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
2141 msg, reply_received);
2142
2143 sysarg_t rc;
2144 async_wait_for((aid_t) msg, &rc);
2145
2146 if (rc != EOK)
2147 return rc;
2148
2149 *out_phone = (int) IPC_GET_ARG5(result);
2150 return EOK;
2151}
2152
2153/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2154 *
2155 * Ask through for a new connection to some service.
2156 *
2157 * @param mgmt Exchange management style.
2158 * @param exch Exchange for sending the message.
2159 * @param arg1 User defined argument.
2160 * @param arg2 User defined argument.
2161 * @param arg3 User defined argument.
2162 *
2163 * @return New session on success or NULL on error.
2164 *
2165 */
2166async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
2167 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
2168{
2169 if (exch == NULL) {
2170 errno = ENOENT;
2171 return NULL;
2172 }
2173
2174 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2175 if (sess == NULL) {
2176 errno = ENOMEM;
2177 return NULL;
2178 }
2179
2180 int phone;
2181 int rc = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
2182 0, &phone);
2183 if (rc != EOK) {
2184 errno = rc;
2185 free(sess);
2186 return NULL;
2187 }
2188
2189 sess->iface = 0;
2190 sess->mgmt = mgmt;
2191 sess->phone = phone;
2192 sess->arg1 = arg1;
2193 sess->arg2 = arg2;
2194 sess->arg3 = arg3;
2195
2196 fibril_mutex_initialize(&sess->remote_state_mtx);
2197 sess->remote_state_data = NULL;
2198
2199 list_initialize(&sess->exch_list);
2200 fibril_mutex_initialize(&sess->mutex);
2201 atomic_set(&sess->refcnt, 0);
2202
2203 return sess;
2204}
2205
2206/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2207 *
2208 * Ask through phone for a new connection to some service and block until
2209 * success.
2210 *
2211 * @param exch Exchange for sending the message.
2212 * @param iface Connection interface.
2213 * @param arg2 User defined argument.
2214 * @param arg3 User defined argument.
2215 *
2216 * @return New session on success or NULL on error.
2217 *
2218 */
2219async_sess_t *async_connect_me_to_iface(async_exch_t *exch, iface_t iface,
2220 sysarg_t arg2, sysarg_t arg3)
2221{
2222 if (exch == NULL) {
2223 errno = ENOENT;
2224 return NULL;
2225 }
2226
2227 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2228 if (sess == NULL) {
2229 errno = ENOMEM;
2230 return NULL;
2231 }
2232
2233 int phone;
2234 int rc = async_connect_me_to_internal(exch->phone, iface, arg2,
2235 arg3, 0, &phone);
2236 if (rc != EOK) {
2237 errno = rc;
2238 free(sess);
2239 return NULL;
2240 }
2241
2242 sess->iface = iface;
2243 sess->phone = phone;
2244 sess->arg1 = iface;
2245 sess->arg2 = arg2;
2246 sess->arg3 = arg3;
2247
2248 fibril_mutex_initialize(&sess->remote_state_mtx);
2249 sess->remote_state_data = NULL;
2250
2251 list_initialize(&sess->exch_list);
2252 fibril_mutex_initialize(&sess->mutex);
2253 atomic_set(&sess->refcnt, 0);
2254
2255 return sess;
2256}
2257
2258/** Set arguments for new connections.
2259 *
2260 * FIXME This is an ugly hack to work around the problem that parallel
2261 * exchanges are implemented using parallel connections. When we create
2262 * a callback session, the framework does not know arguments for the new
2263 * connections.
2264 *
2265 * The proper solution seems to be to implement parallel exchanges using
2266 * tagging.
2267 */
2268void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
2269 sysarg_t arg3)
2270{
2271 sess->arg1 = arg1;
2272 sess->arg2 = arg2;
2273 sess->arg3 = arg3;
2274}
2275
2276/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2277 *
2278 * Ask through phone for a new connection to some service and block until
2279 * success.
2280 *
2281 * @param mgmt Exchange management style.
2282 * @param exch Exchange for sending the message.
2283 * @param arg1 User defined argument.
2284 * @param arg2 User defined argument.
2285 * @param arg3 User defined argument.
2286 *
2287 * @return New session on success or NULL on error.
2288 *
2289 */
2290async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
2291 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
2292{
2293 if (exch == NULL) {
2294 errno = ENOENT;
2295 return NULL;
2296 }
2297
2298 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2299 if (sess == NULL) {
2300 errno = ENOMEM;
2301 return NULL;
2302 }
2303
2304 int phone;
2305 int rc = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
2306 IPC_FLAG_BLOCKING, &phone);
2307
2308 if (rc != EOK) {
2309 errno = rc;
2310 free(sess);
2311 return NULL;
2312 }
2313
2314 sess->iface = 0;
2315 sess->mgmt = mgmt;
2316 sess->phone = phone;
2317 sess->arg1 = arg1;
2318 sess->arg2 = arg2;
2319 sess->arg3 = arg3;
2320
2321 fibril_mutex_initialize(&sess->remote_state_mtx);
2322 sess->remote_state_data = NULL;
2323
2324 list_initialize(&sess->exch_list);
2325 fibril_mutex_initialize(&sess->mutex);
2326 atomic_set(&sess->refcnt, 0);
2327
2328 return sess;
2329}
2330
2331/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
2332 *
2333 * Ask through phone for a new connection to some service and block until
2334 * success.
2335 *
2336 * @param exch Exchange for sending the message.
2337 * @param iface Connection interface.
2338 * @param arg2 User defined argument.
2339 * @param arg3 User defined argument.
2340 *
2341 * @return New session on success or NULL on error.
2342 *
2343 */
2344async_sess_t *async_connect_me_to_blocking_iface(async_exch_t *exch, iface_t iface,
2345 sysarg_t arg2, sysarg_t arg3)
2346{
2347 if (exch == NULL) {
2348 errno = ENOENT;
2349 return NULL;
2350 }
2351
2352 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2353 if (sess == NULL) {
2354 errno = ENOMEM;
2355 return NULL;
2356 }
2357
2358 int phone;
2359 int rc = async_connect_me_to_internal(exch->phone, iface, arg2,
2360 arg3, IPC_FLAG_BLOCKING, &phone);
2361 if (rc != EOK) {
2362 errno = rc;
2363 free(sess);
2364 return NULL;
2365 }
2366
2367 sess->iface = iface;
2368 sess->phone = phone;
2369 sess->arg1 = iface;
2370 sess->arg2 = arg2;
2371 sess->arg3 = arg3;
2372
2373 fibril_mutex_initialize(&sess->remote_state_mtx);
2374 sess->remote_state_data = NULL;
2375
2376 list_initialize(&sess->exch_list);
2377 fibril_mutex_initialize(&sess->mutex);
2378 atomic_set(&sess->refcnt, 0);
2379
2380 return sess;
2381}
2382
2383/** Connect to a task specified by id.
2384 *
2385 */
2386async_sess_t *async_connect_kbox(task_id_t id)
2387{
2388 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2389 if (sess == NULL) {
2390 errno = ENOMEM;
2391 return NULL;
2392 }
2393
2394 int phone = ipc_connect_kbox(id);
2395 if (phone < 0) {
2396 errno = phone;
2397 free(sess);
2398 return NULL;
2399 }
2400
2401 sess->iface = 0;
2402 sess->mgmt = EXCHANGE_ATOMIC;
2403 sess->phone = phone;
2404 sess->arg1 = 0;
2405 sess->arg2 = 0;
2406 sess->arg3 = 0;
2407
2408 fibril_mutex_initialize(&sess->remote_state_mtx);
2409 sess->remote_state_data = NULL;
2410
2411 list_initialize(&sess->exch_list);
2412 fibril_mutex_initialize(&sess->mutex);
2413 atomic_set(&sess->refcnt, 0);
2414
2415 return sess;
2416}
2417
2418static int async_hangup_internal(int phone)
2419{
2420 return ipc_hangup(phone);
2421}
2422
2423/** Wrapper for ipc_hangup.
2424 *
2425 * @param sess Session to hung up.
2426 *
2427 * @return Zero on success or a negative error code.
2428 *
2429 */
2430int async_hangup(async_sess_t *sess)
2431{
2432 async_exch_t *exch;
2433
2434 assert(sess);
2435
2436 if (atomic_get(&sess->refcnt) > 0)
2437 return EBUSY;
2438
2439 fibril_mutex_lock(&async_sess_mutex);
2440
2441 int rc = async_hangup_internal(sess->phone);
2442
2443 while (!list_empty(&sess->exch_list)) {
2444 exch = (async_exch_t *)
2445 list_get_instance(list_first(&sess->exch_list),
2446 async_exch_t, sess_link);
2447
2448 list_remove(&exch->sess_link);
2449 list_remove(&exch->global_link);
2450 async_hangup_internal(exch->phone);
2451 free(exch);
2452 }
2453
2454 free(sess);
2455
2456 fibril_mutex_unlock(&async_sess_mutex);
2457
2458 return rc;
2459}
2460
2461/** Interrupt one thread of this task from waiting for IPC. */
2462void async_poke(void)
2463{
2464 ipc_poke();
2465}
2466
2467/** Start new exchange in a session.
2468 *
2469 * @param session Session.
2470 *
2471 * @return New exchange or NULL on error.
2472 *
2473 */
2474async_exch_t *async_exchange_begin(async_sess_t *sess)
2475{
2476 if (sess == NULL)
2477 return NULL;
2478
2479 exch_mgmt_t mgmt = sess->mgmt;
2480 if (sess->iface != 0)
2481 mgmt = sess->iface & IFACE_EXCHANGE_MASK;
2482
2483 async_exch_t *exch = NULL;
2484
2485 fibril_mutex_lock(&async_sess_mutex);
2486
2487 if (!list_empty(&sess->exch_list)) {
2488 /*
2489 * There are inactive exchanges in the session.
2490 */
2491 exch = (async_exch_t *)
2492 list_get_instance(list_first(&sess->exch_list),
2493 async_exch_t, sess_link);
2494
2495 list_remove(&exch->sess_link);
2496 list_remove(&exch->global_link);
2497 } else {
2498 /*
2499 * There are no available exchanges in the session.
2500 */
2501
2502 if ((mgmt == EXCHANGE_ATOMIC) ||
2503 (mgmt == EXCHANGE_SERIALIZE)) {
2504 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2505 if (exch != NULL) {
2506 link_initialize(&exch->sess_link);
2507 link_initialize(&exch->global_link);
2508 exch->sess = sess;
2509 exch->phone = sess->phone;
2510 }
2511 } else if (mgmt == EXCHANGE_PARALLEL) {
2512 int phone;
2513 int rc;
2514
2515 retry:
2516 /*
2517 * Make a one-time attempt to connect a new data phone.
2518 */
2519 rc = async_connect_me_to_internal(sess->phone, sess->arg1,
2520 sess->arg2, sess->arg3, 0, &phone);
2521 if (rc == EOK) {
2522 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2523 if (exch != NULL) {
2524 link_initialize(&exch->sess_link);
2525 link_initialize(&exch->global_link);
2526 exch->sess = sess;
2527 exch->phone = phone;
2528 } else
2529 async_hangup_internal(phone);
2530 } else if (!list_empty(&inactive_exch_list)) {
2531 /*
2532 * We did not manage to connect a new phone. But we
2533 * can try to close some of the currently inactive
2534 * connections in other sessions and try again.
2535 */
2536 exch = (async_exch_t *)
2537 list_get_instance(list_first(&inactive_exch_list),
2538 async_exch_t, global_link);
2539
2540 list_remove(&exch->sess_link);
2541 list_remove(&exch->global_link);
2542 async_hangup_internal(exch->phone);
2543 free(exch);
2544 goto retry;
2545 } else {
2546 /*
2547 * Wait for a phone to become available.
2548 */
2549 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
2550 goto retry;
2551 }
2552 }
2553 }
2554
2555 fibril_mutex_unlock(&async_sess_mutex);
2556
2557 if (exch != NULL) {
2558 atomic_inc(&sess->refcnt);
2559
2560 if (mgmt == EXCHANGE_SERIALIZE)
2561 fibril_mutex_lock(&sess->mutex);
2562 }
2563
2564 return exch;
2565}
2566
2567/** Finish an exchange.
2568 *
2569 * @param exch Exchange to finish.
2570 *
2571 */
2572void async_exchange_end(async_exch_t *exch)
2573{
2574 if (exch == NULL)
2575 return;
2576
2577 async_sess_t *sess = exch->sess;
2578 assert(sess != NULL);
2579
2580 exch_mgmt_t mgmt = sess->mgmt;
2581 if (sess->iface != 0)
2582 mgmt = sess->iface & IFACE_EXCHANGE_MASK;
2583
2584 atomic_dec(&sess->refcnt);
2585
2586 if (mgmt == EXCHANGE_SERIALIZE)
2587 fibril_mutex_unlock(&sess->mutex);
2588
2589 fibril_mutex_lock(&async_sess_mutex);
2590
2591 list_append(&exch->sess_link, &sess->exch_list);
2592 list_append(&exch->global_link, &inactive_exch_list);
2593 fibril_condvar_signal(&avail_phone_cv);
2594
2595 fibril_mutex_unlock(&async_sess_mutex);
2596}
2597
2598/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
2599 *
2600 * @param exch Exchange for sending the message.
2601 * @param size Size of the destination address space area.
2602 * @param arg User defined argument.
2603 * @param flags Storage for the received flags. Can be NULL.
2604 * @param dst Address of the storage for the destination address space area
2605 * base address. Cannot be NULL.
2606 *
2607 * @return Zero on success or a negative error code from errno.h.
2608 *
2609 */
2610int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
2611 unsigned int *flags, void **dst)
2612{
2613 if (exch == NULL)
2614 return ENOENT;
2615
2616 sysarg_t _flags = 0;
2617 sysarg_t _dst = (sysarg_t) -1;
2618 int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
2619 arg, NULL, &_flags, NULL, &_dst);
2620
2621 if (flags)
2622 *flags = (unsigned int) _flags;
2623
2624 *dst = (void *) _dst;
2625 return res;
2626}
2627
2628/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
2629 *
2630 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
2631 * calls so that the user doesn't have to remember the meaning of each IPC
2632 * argument.
2633 *
2634 * So far, this wrapper is to be used from within a connection fibril.
2635 *
2636 * @param chandle Storage for the handle of the IPC_M_SHARE_IN call.
2637 * @param size Destination address space area size.
2638 *
2639 * @return True on success, false on failure.
2640 *
2641 */
2642bool async_share_in_receive(cap_handle_t *chandle, size_t *size)
2643{
2644 assert(chandle);
2645 assert(size);
2646
2647 ipc_call_t data;
2648 *chandle = async_get_call(&data);
2649
2650 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
2651 return false;
2652
2653 *size = (size_t) IPC_GET_ARG1(data);
2654 return true;
2655}
2656
2657/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
2658 *
2659 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
2660 * calls so that the user doesn't have to remember the meaning of each IPC
2661 * argument.
2662 *
2663 * @param chandle Handle of the IPC_M_DATA_READ call to answer.
2664 * @param src Source address space base.
2665 * @param flags Flags to be used for sharing. Bits can be only cleared.
2666 *
2667 * @return Zero on success or a value from @ref errno.h on failure.
2668 *
2669 */
2670int async_share_in_finalize(cap_handle_t chandle, void *src, unsigned int flags)
2671{
2672 return ipc_answer_3(chandle, EOK, (sysarg_t) src, (sysarg_t) flags,
2673 (sysarg_t) __entry);
2674}
2675
2676/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
2677 *
2678 * @param exch Exchange for sending the message.
2679 * @param src Source address space area base address.
2680 * @param flags Flags to be used for sharing. Bits can be only cleared.
2681 *
2682 * @return Zero on success or a negative error code from errno.h.
2683 *
2684 */
2685int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
2686{
2687 if (exch == NULL)
2688 return ENOENT;
2689
2690 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
2691 (sysarg_t) flags);
2692}
2693
2694/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
2695 *
2696 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
2697 * calls so that the user doesn't have to remember the meaning of each IPC
2698 * argument.
2699 *
2700 * So far, this wrapper is to be used from within a connection fibril.
2701 *
2702 * @param chandle Storage for the hash of the IPC_M_SHARE_OUT call.
2703 * @param size Storage for the source address space area size.
2704 * @param flags Storage for the sharing flags.
2705 *
2706 * @return True on success, false on failure.
2707 *
2708 */
2709bool async_share_out_receive(cap_handle_t *chandle, size_t *size,
2710 unsigned int *flags)
2711{
2712 assert(chandle);
2713 assert(size);
2714 assert(flags);
2715
2716 ipc_call_t data;
2717 *chandle = async_get_call(&data);
2718
2719 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
2720 return false;
2721
2722 *size = (size_t) IPC_GET_ARG2(data);
2723 *flags = (unsigned int) IPC_GET_ARG3(data);
2724 return true;
2725}
2726
2727/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2728 *
2729 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2730 * calls so that the user doesn't have to remember the meaning of each IPC
2731 * argument.
2732 *
2733 * @param chandle Handle of the IPC_M_DATA_WRITE call to answer.
2734 * @param dst Address of the storage for the destination address space area
2735 * base address.
2736 *
2737 * @return Zero on success or a value from @ref errno.h on failure.
2738 *
2739 */
2740int async_share_out_finalize(cap_handle_t chandle, void **dst)
2741{
2742 return ipc_answer_2(chandle, EOK, (sysarg_t) __entry, (sysarg_t) dst);
2743}
2744
2745/** Start IPC_M_DATA_READ using the async framework.
2746 *
2747 * @param exch Exchange for sending the message.
2748 * @param dst Address of the beginning of the destination buffer.
2749 * @param size Size of the destination buffer (in bytes).
2750 * @param dataptr Storage of call data (arg 2 holds actual data size).
2751 *
2752 * @return Hash of the sent message or 0 on error.
2753 *
2754 */
2755aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2756 ipc_call_t *dataptr)
2757{
2758 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2759 (sysarg_t) size, dataptr);
2760}
2761
2762/** Wrapper for IPC_M_DATA_READ calls using the async framework.
2763 *
2764 * @param exch Exchange for sending the message.
2765 * @param dst Address of the beginning of the destination buffer.
2766 * @param size Size of the destination buffer.
2767 *
2768 * @return Zero on success or a negative error code from errno.h.
2769 *
2770 */
2771int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
2772{
2773 if (exch == NULL)
2774 return ENOENT;
2775
2776 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2777 (sysarg_t) size);
2778}
2779
2780/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2781 *
2782 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2783 * calls so that the user doesn't have to remember the meaning of each IPC
2784 * argument.
2785 *
2786 * So far, this wrapper is to be used from within a connection fibril.
2787 *
2788 * @param chandle Storage for the handle of the IPC_M_DATA_READ.
2789 * @param size Storage for the maximum size. Can be NULL.
2790 *
2791 * @return True on success, false on failure.
2792 *
2793 */
2794bool async_data_read_receive(cap_handle_t *chandle, size_t *size)
2795{
2796 ipc_call_t data;
2797 return async_data_read_receive_call(chandle, &data, size);
2798}
2799
2800/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2801 *
2802 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2803 * calls so that the user doesn't have to remember the meaning of each IPC
2804 * argument.
2805 *
2806 * So far, this wrapper is to be used from within a connection fibril.
2807 *
2808 * @param chandle Storage for the handle of the IPC_M_DATA_READ.
2809 * @param size Storage for the maximum size. Can be NULL.
2810 *
2811 * @return True on success, false on failure.
2812 *
2813 */
2814bool async_data_read_receive_call(cap_handle_t *chandle, ipc_call_t *data,
2815 size_t *size)
2816{
2817 assert(chandle);
2818 assert(data);
2819
2820 *chandle = async_get_call(data);
2821
2822 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_READ)
2823 return false;
2824
2825 if (size)
2826 *size = (size_t) IPC_GET_ARG2(*data);
2827
2828 return true;
2829}
2830
2831/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2832 *
2833 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2834 * calls so that the user doesn't have to remember the meaning of each IPC
2835 * argument.
2836 *
2837 * @param chandle Handle of the IPC_M_DATA_READ call to answer.
2838 * @param src Source address for the IPC_M_DATA_READ call.
2839 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2840 * the maximum size announced by the sender.
2841 *
2842 * @return Zero on success or a value from @ref errno.h on failure.
2843 *
2844 */
2845int async_data_read_finalize(cap_handle_t chandle, const void *src, size_t size)
2846{
2847 return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) size);
2848}
2849
2850/** Wrapper for forwarding any read request
2851 *
2852 */
2853int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2854 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2855 ipc_call_t *dataptr)
2856{
2857 if (exch == NULL)
2858 return ENOENT;
2859
2860 cap_handle_t chandle;
2861 if (!async_data_read_receive(&chandle, NULL)) {
2862 ipc_answer_0(chandle, EINVAL);
2863 return EINVAL;
2864 }
2865
2866 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2867 dataptr);
2868 if (msg == 0) {
2869 ipc_answer_0(chandle, EINVAL);
2870 return EINVAL;
2871 }
2872
2873 int retval = ipc_forward_fast(chandle, exch->phone, 0, 0, 0,
2874 IPC_FF_ROUTE_FROM_ME);
2875 if (retval != EOK) {
2876 async_forget(msg);
2877 ipc_answer_0(chandle, retval);
2878 return retval;
2879 }
2880
2881 sysarg_t rc;
2882 async_wait_for(msg, &rc);
2883
2884 return (int) rc;
2885}
2886
2887/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
2888 *
2889 * @param exch Exchange for sending the message.
2890 * @param src Address of the beginning of the source buffer.
2891 * @param size Size of the source buffer.
2892 *
2893 * @return Zero on success or a negative error code from errno.h.
2894 *
2895 */
2896int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
2897{
2898 if (exch == NULL)
2899 return ENOENT;
2900
2901 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2902 (sysarg_t) size);
2903}
2904
2905/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2906 *
2907 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2908 * calls so that the user doesn't have to remember the meaning of each IPC
2909 * argument.
2910 *
2911 * So far, this wrapper is to be used from within a connection fibril.
2912 *
2913 * @param chandle Storage for the handle of the IPC_M_DATA_WRITE.
2914 * @param size Storage for the suggested size. May be NULL.
2915 *
2916 * @return True on success, false on failure.
2917 *
2918 */
2919bool async_data_write_receive(cap_handle_t *chandle, size_t *size)
2920{
2921 ipc_call_t data;
2922 return async_data_write_receive_call(chandle, &data, size);
2923}
2924
2925/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2926 *
2927 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2928 * calls so that the user doesn't have to remember the meaning of each IPC
2929 * argument.
2930 *
2931 * So far, this wrapper is to be used from within a connection fibril.
2932 *
2933 * @param chandle Storage for the handle of the IPC_M_DATA_WRITE.
2934 * @param data Storage for the ipc call data.
2935 * @param size Storage for the suggested size. May be NULL.
2936 *
2937 * @return True on success, false on failure.
2938 *
2939 */
2940bool async_data_write_receive_call(cap_handle_t *chandle, ipc_call_t *data,
2941 size_t *size)
2942{
2943 assert(chandle);
2944 assert(data);
2945
2946 *chandle = async_get_call(data);
2947
2948 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_WRITE)
2949 return false;
2950
2951 if (size)
2952 *size = (size_t) IPC_GET_ARG2(*data);
2953
2954 return true;
2955}
2956
2957/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2958 *
2959 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2960 * calls so that the user doesn't have to remember the meaning of each IPC
2961 * argument.
2962 *
2963 * @param chandle Handle of the IPC_M_DATA_WRITE call to answer.
2964 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2965 * @param size Final size for the IPC_M_DATA_WRITE call.
2966 *
2967 * @return Zero on success or a value from @ref errno.h on failure.
2968 *
2969 */
2970int async_data_write_finalize(cap_handle_t chandle, void *dst, size_t size)
2971{
2972 return ipc_answer_2(chandle, EOK, (sysarg_t) dst, (sysarg_t) size);
2973}
2974
2975/** Wrapper for receiving binary data or strings
2976 *
2977 * This wrapper only makes it more comfortable to use async_data_write_*
2978 * functions to receive binary data or strings.
2979 *
2980 * @param data Pointer to data pointer (which should be later disposed
2981 * by free()). If the operation fails, the pointer is not
2982 * touched.
2983 * @param nullterm If true then the received data is always zero terminated.
2984 * This also causes to allocate one extra byte beyond the
2985 * raw transmitted data.
2986 * @param min_size Minimum size (in bytes) of the data to receive.
2987 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2988 * no limit.
2989 * @param granulariy If non-zero then the size of the received data has to
2990 * be divisible by this value.
2991 * @param received If not NULL, the size of the received data is stored here.
2992 *
2993 * @return Zero on success or a value from @ref errno.h on failure.
2994 *
2995 */
2996int async_data_write_accept(void **data, const bool nullterm,
2997 const size_t min_size, const size_t max_size, const size_t granularity,
2998 size_t *received)
2999{
3000 assert(data);
3001
3002 cap_handle_t chandle;
3003 size_t size;
3004 if (!async_data_write_receive(&chandle, &size)) {
3005 ipc_answer_0(chandle, EINVAL);
3006 return EINVAL;
3007 }
3008
3009 if (size < min_size) {
3010 ipc_answer_0(chandle, EINVAL);
3011 return EINVAL;
3012 }
3013
3014 if ((max_size > 0) && (size > max_size)) {
3015 ipc_answer_0(chandle, EINVAL);
3016 return EINVAL;
3017 }
3018
3019 if ((granularity > 0) && ((size % granularity) != 0)) {
3020 ipc_answer_0(chandle, EINVAL);
3021 return EINVAL;
3022 }
3023
3024 void *arg_data;
3025
3026 if (nullterm)
3027 arg_data = malloc(size + 1);
3028 else
3029 arg_data = malloc(size);
3030
3031 if (arg_data == NULL) {
3032 ipc_answer_0(chandle, ENOMEM);
3033 return ENOMEM;
3034 }
3035
3036 int rc = async_data_write_finalize(chandle, arg_data, size);
3037 if (rc != EOK) {
3038 free(arg_data);
3039 return rc;
3040 }
3041
3042 if (nullterm)
3043 ((char *) arg_data)[size] = 0;
3044
3045 *data = arg_data;
3046 if (received != NULL)
3047 *received = size;
3048
3049 return EOK;
3050}
3051
3052/** Wrapper for voiding any data that is about to be received
3053 *
3054 * This wrapper can be used to void any pending data
3055 *
3056 * @param retval Error value from @ref errno.h to be returned to the caller.
3057 *
3058 */
3059void async_data_write_void(sysarg_t retval)
3060{
3061 cap_handle_t chandle;
3062 async_data_write_receive(&chandle, NULL);
3063 ipc_answer_0(chandle, retval);
3064}
3065
3066/** Wrapper for forwarding any data that is about to be received
3067 *
3068 */
3069int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
3070 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
3071 ipc_call_t *dataptr)
3072{
3073 if (exch == NULL)
3074 return ENOENT;
3075
3076 cap_handle_t chandle;
3077 if (!async_data_write_receive(&chandle, NULL)) {
3078 ipc_answer_0(chandle, EINVAL);
3079 return EINVAL;
3080 }
3081
3082 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
3083 dataptr);
3084 if (msg == 0) {
3085 ipc_answer_0(chandle, EINVAL);
3086 return EINVAL;
3087 }
3088
3089 int retval = ipc_forward_fast(chandle, exch->phone, 0, 0, 0,
3090 IPC_FF_ROUTE_FROM_ME);
3091 if (retval != EOK) {
3092 async_forget(msg);
3093 ipc_answer_0(chandle, retval);
3094 return retval;
3095 }
3096
3097 sysarg_t rc;
3098 async_wait_for(msg, &rc);
3099
3100 return (int) rc;
3101}
3102
3103/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
3104 *
3105 * If the current call is IPC_M_CONNECT_TO_ME then a new
3106 * async session is created for the accepted phone.
3107 *
3108 * @param mgmt Exchange management style.
3109 *
3110 * @return New async session.
3111 * @return NULL on failure.
3112 *
3113 */
3114async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
3115{
3116 /* Accept the phone */
3117 ipc_call_t call;
3118 cap_handle_t chandle = async_get_call(&call);
3119 cap_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(call);
3120
3121 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) || (phandle < 0)) {
3122 async_answer_0(chandle, EINVAL);
3123 return NULL;
3124 }
3125
3126 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
3127 if (sess == NULL) {
3128 async_answer_0(chandle, ENOMEM);
3129 return NULL;
3130 }
3131
3132 sess->iface = 0;
3133 sess->mgmt = mgmt;
3134 sess->phone = phandle;
3135 sess->arg1 = 0;
3136 sess->arg2 = 0;
3137 sess->arg3 = 0;
3138
3139 fibril_mutex_initialize(&sess->remote_state_mtx);
3140 sess->remote_state_data = NULL;
3141
3142 list_initialize(&sess->exch_list);
3143 fibril_mutex_initialize(&sess->mutex);
3144 atomic_set(&sess->refcnt, 0);
3145
3146 /* Acknowledge the connected phone */
3147 async_answer_0(chandle, EOK);
3148
3149 return sess;
3150}
3151
3152/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
3153 *
3154 * If the call is IPC_M_CONNECT_TO_ME then a new
3155 * async session is created. However, the phone is
3156 * not accepted automatically.
3157 *
3158 * @param mgmt Exchange management style.
3159 * @param call Call data.
3160 *
3161 * @return New async session.
3162 * @return NULL on failure.
3163 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
3164 *
3165 */
3166async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
3167{
3168 cap_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(*call);
3169
3170 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) || (phandle < 0))
3171 return NULL;
3172
3173 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
3174 if (sess == NULL)
3175 return NULL;
3176
3177 sess->iface = 0;
3178 sess->mgmt = mgmt;
3179 sess->phone = phandle;
3180 sess->arg1 = 0;
3181 sess->arg2 = 0;
3182 sess->arg3 = 0;
3183
3184 fibril_mutex_initialize(&sess->remote_state_mtx);
3185 sess->remote_state_data = NULL;
3186
3187 list_initialize(&sess->exch_list);
3188 fibril_mutex_initialize(&sess->mutex);
3189 atomic_set(&sess->refcnt, 0);
3190
3191 return sess;
3192}
3193
3194int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
3195 sysarg_t arg3, async_exch_t *other_exch)
3196{
3197 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
3198 arg1, arg2, arg3, 0, other_exch->phone);
3199}
3200
3201bool async_state_change_receive(cap_handle_t *chandle, sysarg_t *arg1,
3202 sysarg_t *arg2, sysarg_t *arg3)
3203{
3204 assert(chandle);
3205
3206 ipc_call_t call;
3207 *chandle = async_get_call(&call);
3208
3209 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
3210 return false;
3211
3212 if (arg1)
3213 *arg1 = IPC_GET_ARG1(call);
3214 if (arg2)
3215 *arg2 = IPC_GET_ARG2(call);
3216 if (arg3)
3217 *arg3 = IPC_GET_ARG3(call);
3218
3219 return true;
3220}
3221
3222int async_state_change_finalize(cap_handle_t chandle, async_exch_t *other_exch)
3223{
3224 return ipc_answer_1(chandle, EOK, other_exch->phone);
3225}
3226
3227/** Lock and get session remote state
3228 *
3229 * Lock and get the local replica of the remote state
3230 * in stateful sessions. The call should be paired
3231 * with async_remote_state_release*().
3232 *
3233 * @param[in] sess Stateful session.
3234 *
3235 * @return Local replica of the remote state.
3236 *
3237 */
3238void *async_remote_state_acquire(async_sess_t *sess)
3239{
3240 fibril_mutex_lock(&sess->remote_state_mtx);
3241 return sess->remote_state_data;
3242}
3243
3244/** Update the session remote state
3245 *
3246 * Update the local replica of the remote state
3247 * in stateful sessions. The remote state must
3248 * be already locked.
3249 *
3250 * @param[in] sess Stateful session.
3251 * @param[in] state New local replica of the remote state.
3252 *
3253 */
3254void async_remote_state_update(async_sess_t *sess, void *state)
3255{
3256 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3257 sess->remote_state_data = state;
3258}
3259
3260/** Release the session remote state
3261 *
3262 * Unlock the local replica of the remote state
3263 * in stateful sessions.
3264 *
3265 * @param[in] sess Stateful session.
3266 *
3267 */
3268void async_remote_state_release(async_sess_t *sess)
3269{
3270 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3271
3272 fibril_mutex_unlock(&sess->remote_state_mtx);
3273}
3274
3275/** Release the session remote state and end an exchange
3276 *
3277 * Unlock the local replica of the remote state
3278 * in stateful sessions. This is convenience function
3279 * which gets the session pointer from the exchange
3280 * and also ends the exchange.
3281 *
3282 * @param[in] exch Stateful session's exchange.
3283 *
3284 */
3285void async_remote_state_release_exchange(async_exch_t *exch)
3286{
3287 if (exch == NULL)
3288 return;
3289
3290 async_sess_t *sess = exch->sess;
3291 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3292
3293 async_exchange_end(exch);
3294 fibril_mutex_unlock(&sess->remote_state_mtx);
3295}
3296
3297void *async_as_area_create(void *base, size_t size, unsigned int flags,
3298 async_sess_t *pager, sysarg_t id1, sysarg_t id2, sysarg_t id3)
3299{
3300 as_area_pager_info_t pager_info = {
3301 .pager = pager->phone,
3302 .id1 = id1,
3303 .id2 = id2,
3304 .id3 = id3
3305 };
3306 return as_area_create(base, size, flags, &pager_info);
3307}
3308
3309/** @}
3310 */
Note: See TracBrowser for help on using the repository browser.