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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c2cf033 was cdc8ee2d, checked in by Jakub Jermar <jakub@…>, 14 years ago

Really drop one reference in async_put_client_data_by_hash().

  • Property mode set to 100644
File size: 60.4 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 * my_client_connection(icallid, *icall)
80 * {
81 * if (want_refuse) {
82 * async_answer_0(icallid, ELIMIT);
83 * return;
84 * }
85 * async_answer_0(icallid, EOK);
86 *
87 * callid = async_get_call(&call);
88 * somehow_handle_the_call(callid, call);
89 * async_answer_2(callid, 1, 2, 3);
90 *
91 * callid = async_get_call(&call);
92 * ...
93 * }
94 *
95 */
96
97#define LIBC_ASYNC_C_
98#include <ipc/ipc.h>
99#include <async.h>
100#undef LIBC_ASYNC_C_
101
102#include <futex.h>
103#include <fibril.h>
104#include <adt/hash_table.h>
105#include <adt/list.h>
106#include <assert.h>
107#include <errno.h>
108#include <sys/time.h>
109#include <libarch/barrier.h>
110#include <bool.h>
111#include <malloc.h>
112#include <mem.h>
113#include <stdlib.h>
114#include "private/async.h"
115
116#define CLIENT_HASH_TABLE_BUCKETS 32
117#define CONN_HASH_TABLE_BUCKETS 32
118
119/** Async framework global futex */
120atomic_t async_futex = FUTEX_INITIALIZER;
121
122/** Number of threads waiting for IPC in the kernel. */
123atomic_t threads_in_ipc_wait = { 0 };
124
125/** Naming service session */
126async_sess_t *session_ns;
127
128/** Call data */
129typedef struct {
130 link_t link;
131
132 ipc_callid_t callid;
133 ipc_call_t call;
134} msg_t;
135
136/* Client connection data */
137typedef struct {
138 link_t link;
139
140 sysarg_t in_task_hash;
141 atomic_t refcnt;
142 void *data;
143} client_t;
144
145/* Server connection data */
146typedef struct {
147 awaiter_t wdata;
148
149 /** Hash table link. */
150 link_t link;
151
152 /** Incoming client task hash. */
153 sysarg_t in_task_hash;
154
155 /** Incoming phone hash. */
156 sysarg_t in_phone_hash;
157
158 /** Link to the client tracking structure. */
159 client_t *client;
160
161 /** Messages that should be delivered to this fibril. */
162 list_t msg_queue;
163
164 /** Identification of the opening call. */
165 ipc_callid_t callid;
166 /** Call data of the opening call. */
167 ipc_call_t call;
168 /** Local argument or NULL if none. */
169 void *carg;
170
171 /** Identification of the closing call. */
172 ipc_callid_t close_callid;
173
174 /** Fibril function that will be used to handle the connection. */
175 async_client_conn_t cfibril;
176} connection_t;
177
178/** Identifier of the incoming connection handled by the current fibril. */
179static fibril_local connection_t *fibril_connection;
180
181static void *default_client_data_constructor(void)
182{
183 return NULL;
184}
185
186static void default_client_data_destructor(void *data)
187{
188}
189
190static async_client_data_ctor_t async_client_data_create =
191 default_client_data_constructor;
192static async_client_data_dtor_t async_client_data_destroy =
193 default_client_data_destructor;
194
195void async_set_client_data_constructor(async_client_data_ctor_t ctor)
196{
197 async_client_data_create = ctor;
198}
199
200void async_set_client_data_destructor(async_client_data_dtor_t dtor)
201{
202 async_client_data_destroy = dtor;
203}
204
205/** Default fibril function that gets called to handle new connection.
206 *
207 * This function is defined as a weak symbol - to be redefined in user code.
208 *
209 * @param callid Hash of the incoming call.
210 * @param call Data of the incoming call.
211 * @param arg Local argument
212 *
213 */
214static void default_client_connection(ipc_callid_t callid, ipc_call_t *call,
215 void *arg)
216{
217 ipc_answer_0(callid, ENOENT);
218}
219
220/** Default fibril function that gets called to handle interrupt notifications.
221 *
222 * This function is defined as a weak symbol - to be redefined in user code.
223 *
224 * @param callid Hash of the incoming call.
225 * @param call Data of the incoming call.
226 * @param arg Local argument.
227 *
228 */
229static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
230{
231}
232
233static async_client_conn_t client_connection = default_client_connection;
234static async_interrupt_handler_t interrupt_received = default_interrupt_received;
235
236/** Setter for client_connection function pointer.
237 *
238 * @param conn Function that will implement a new connection fibril.
239 *
240 */
241void async_set_client_connection(async_client_conn_t conn)
242{
243 client_connection = conn;
244}
245
246/** Setter for interrupt_received function pointer.
247 *
248 * @param intr Function that will implement a new interrupt
249 * notification fibril.
250 */
251void async_set_interrupt_received(async_interrupt_handler_t intr)
252{
253 interrupt_received = intr;
254}
255
256/** Mutex protecting inactive_exch_list and avail_phone_cv.
257 *
258 */
259static FIBRIL_MUTEX_INITIALIZE(async_sess_mutex);
260
261/** List of all currently inactive exchanges.
262 *
263 */
264static LIST_INITIALIZE(inactive_exch_list);
265
266/** Condition variable to wait for a phone to become available.
267 *
268 */
269static FIBRIL_CONDVAR_INITIALIZE(avail_phone_cv);
270
271static hash_table_t client_hash_table;
272static hash_table_t conn_hash_table;
273static LIST_INITIALIZE(timeout_list);
274
275static hash_index_t client_hash(unsigned long key[])
276{
277 assert(key);
278
279 return (((key[0]) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
280}
281
282static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
283{
284 assert(key);
285 assert(item);
286
287 client_t *client = hash_table_get_instance(item, client_t, link);
288 return (key[0] == client->in_task_hash);
289}
290
291static void client_remove(link_t *item)
292{
293}
294
295/** Operations for the client hash table. */
296static hash_table_operations_t client_hash_table_ops = {
297 .hash = client_hash,
298 .compare = client_compare,
299 .remove_callback = client_remove
300};
301
302/** Compute hash into the connection hash table based on the source phone hash.
303 *
304 * @param key Pointer to source phone hash.
305 *
306 * @return Index into the connection hash table.
307 *
308 */
309static hash_index_t conn_hash(unsigned long key[])
310{
311 assert(key);
312
313 return (((key[0]) >> 4) % CONN_HASH_TABLE_BUCKETS);
314}
315
316/** Compare hash table item with a key.
317 *
318 * @param key Array containing the source phone hash as the only item.
319 * @param keys Expected 1 but ignored.
320 * @param item Connection hash table item.
321 *
322 * @return True on match, false otherwise.
323 *
324 */
325static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
326{
327 assert(key);
328 assert(item);
329
330 connection_t *conn = hash_table_get_instance(item, connection_t, link);
331 return (key[0] == conn->in_phone_hash);
332}
333
334static void conn_remove(link_t *item)
335{
336}
337
338/** Operations for the connection hash table. */
339static hash_table_operations_t conn_hash_table_ops = {
340 .hash = conn_hash,
341 .compare = conn_compare,
342 .remove_callback = conn_remove
343};
344
345/** Sort in current fibril's timeout request.
346 *
347 * @param wd Wait data of the current fibril.
348 *
349 */
350void async_insert_timeout(awaiter_t *wd)
351{
352 assert(wd);
353
354 wd->to_event.occurred = false;
355 wd->to_event.inlist = true;
356
357 link_t *tmp = timeout_list.head.next;
358 while (tmp != &timeout_list.head) {
359 awaiter_t *cur
360 = list_get_instance(tmp, awaiter_t, to_event.link);
361
362 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
363 break;
364
365 tmp = tmp->next;
366 }
367
368 list_insert_before(&wd->to_event.link, tmp);
369}
370
371/** Try to route a call to an appropriate connection fibril.
372 *
373 * If the proper connection fibril is found, a message with the call is added to
374 * its message queue. If the fibril was not active, it is activated and all
375 * timeouts are unregistered.
376 *
377 * @param callid Hash of the incoming call.
378 * @param call Data of the incoming call.
379 *
380 * @return False if the call doesn't match any connection.
381 * @return True if the call was passed to the respective connection fibril.
382 *
383 */
384static bool route_call(ipc_callid_t callid, ipc_call_t *call)
385{
386 assert(call);
387
388 futex_down(&async_futex);
389
390 unsigned long key = call->in_phone_hash;
391 link_t *hlp = hash_table_find(&conn_hash_table, &key);
392
393 if (!hlp) {
394 futex_up(&async_futex);
395 return false;
396 }
397
398 connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
399
400 msg_t *msg = malloc(sizeof(*msg));
401 if (!msg) {
402 futex_up(&async_futex);
403 return false;
404 }
405
406 msg->callid = callid;
407 msg->call = *call;
408 list_append(&msg->link, &conn->msg_queue);
409
410 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
411 conn->close_callid = callid;
412
413 /* If the connection fibril is waiting for an event, activate it */
414 if (!conn->wdata.active) {
415
416 /* If in timeout list, remove it */
417 if (conn->wdata.to_event.inlist) {
418 conn->wdata.to_event.inlist = false;
419 list_remove(&conn->wdata.to_event.link);
420 }
421
422 conn->wdata.active = true;
423 fibril_add_ready(conn->wdata.fid);
424 }
425
426 futex_up(&async_futex);
427 return true;
428}
429
430/** Notification fibril.
431 *
432 * When a notification arrives, a fibril with this implementing function is
433 * created. It calls interrupt_received() and does the final cleanup.
434 *
435 * @param arg Message structure pointer.
436 *
437 * @return Always zero.
438 *
439 */
440static int notification_fibril(void *arg)
441{
442 assert(arg);
443
444 msg_t *msg = (msg_t *) arg;
445 interrupt_received(msg->callid, &msg->call);
446
447 free(msg);
448 return 0;
449}
450
451/** Process interrupt notification.
452 *
453 * A new fibril is created which would process the notification.
454 *
455 * @param callid Hash of the incoming call.
456 * @param call Data of the incoming call.
457 *
458 * @return False if an error occured.
459 * True if the call was passed to the notification fibril.
460 *
461 */
462static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
463{
464 assert(call);
465
466 futex_down(&async_futex);
467
468 msg_t *msg = malloc(sizeof(*msg));
469 if (!msg) {
470 futex_up(&async_futex);
471 return false;
472 }
473
474 msg->callid = callid;
475 msg->call = *call;
476
477 fid_t fid = fibril_create(notification_fibril, msg);
478 if (fid == 0) {
479 free(msg);
480 futex_up(&async_futex);
481 return false;
482 }
483
484 fibril_add_ready(fid);
485
486 futex_up(&async_futex);
487 return true;
488}
489
490/** Return new incoming message for the current (fibril-local) connection.
491 *
492 * @param call Storage where the incoming call data will be stored.
493 * @param usecs Timeout in microseconds. Zero denotes no timeout.
494 *
495 * @return If no timeout was specified, then a hash of the
496 * incoming call is returned. If a timeout is specified,
497 * then a hash of the incoming call is returned unless
498 * the timeout expires prior to receiving a message. In
499 * that case zero is returned.
500 *
501 */
502ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
503{
504 assert(call);
505 assert(fibril_connection);
506
507 /* Why doing this?
508 * GCC 4.1.0 coughs on fibril_connection-> dereference.
509 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
510 * I would never expect to find so many errors in
511 * a compiler.
512 */
513 connection_t *conn = fibril_connection;
514
515 futex_down(&async_futex);
516
517 if (usecs) {
518 gettimeofday(&conn->wdata.to_event.expires, NULL);
519 tv_add(&conn->wdata.to_event.expires, usecs);
520 } else
521 conn->wdata.to_event.inlist = false;
522
523 /* If nothing in queue, wait until something arrives */
524 while (list_empty(&conn->msg_queue)) {
525 if (conn->close_callid) {
526 /*
527 * Handle the case when the connection was already
528 * closed by the client but the server did not notice
529 * the first IPC_M_PHONE_HUNGUP call and continues to
530 * call async_get_call_timeout(). Repeat
531 * IPC_M_PHONE_HUNGUP until the caller notices.
532 */
533 memset(call, 0, sizeof(ipc_call_t));
534 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
535 futex_up(&async_futex);
536 return conn->close_callid;
537 }
538
539 if (usecs)
540 async_insert_timeout(&conn->wdata);
541
542 conn->wdata.active = false;
543
544 /*
545 * Note: the current fibril will be rescheduled either due to a
546 * timeout or due to an arriving message destined to it. In the
547 * former case, handle_expired_timeouts() and, in the latter
548 * case, route_call() will perform the wakeup.
549 */
550 fibril_switch(FIBRIL_TO_MANAGER);
551
552 /*
553 * Futex is up after getting back from async_manager.
554 * Get it again.
555 */
556 futex_down(&async_futex);
557 if ((usecs) && (conn->wdata.to_event.occurred)
558 && (list_empty(&conn->msg_queue))) {
559 /* If we timed out -> exit */
560 futex_up(&async_futex);
561 return 0;
562 }
563 }
564
565 msg_t *msg = list_get_instance(list_first(&conn->msg_queue), msg_t, link);
566 list_remove(&msg->link);
567
568 ipc_callid_t callid = msg->callid;
569 *call = msg->call;
570 free(msg);
571
572 futex_up(&async_futex);
573 return callid;
574}
575
576static client_t *async_client_get(sysarg_t client_hash, bool create)
577{
578 unsigned long key = client_hash;
579 client_t *client = NULL;
580
581 futex_down(&async_futex);
582 link_t *lnk = hash_table_find(&client_hash_table, &key);
583 if (lnk) {
584 client = hash_table_get_instance(lnk, client_t, link);
585 atomic_inc(&client->refcnt);
586 } else if (create) {
587 client = malloc(sizeof(client_t));
588 if (client) {
589 client->in_task_hash = client_hash;
590 client->data = async_client_data_create();
591
592 atomic_set(&client->refcnt, 1);
593 hash_table_insert(&client_hash_table, &key, &client->link);
594 }
595 }
596
597 futex_up(&async_futex);
598 return client;
599}
600
601static void async_client_put(client_t *client)
602{
603 bool destroy;
604 unsigned long key = client->in_task_hash;
605
606 futex_down(&async_futex);
607
608 if (atomic_predec(&client->refcnt) == 0) {
609 hash_table_remove(&client_hash_table, &key, 1);
610 destroy = true;
611 } else
612 destroy = false;
613
614 futex_up(&async_futex);
615
616 if (destroy) {
617 if (client->data)
618 async_client_data_destroy(client->data);
619
620 free(client);
621 }
622}
623
624void *async_get_client_data(void)
625{
626 assert(fibril_connection);
627 return fibril_connection->client->data;
628}
629
630void *async_get_client_data_by_hash(sysarg_t client_hash)
631{
632 client_t *client = async_client_get(client_hash, false);
633 if (!client)
634 return NULL;
635 if (!client->data) {
636 async_client_put(client);
637 return NULL;
638 }
639
640 return client->data;
641}
642
643void async_put_client_data_by_hash(sysarg_t client_hash)
644{
645 client_t *client = async_client_get(client_hash, false);
646
647 assert(client);
648 assert(client->data);
649
650 /* Drop the reference we got in async_get_client_data_by_hash(). */
651 async_client_put(client);
652
653 /* Drop our own reference we got at the beginning of this function. */
654 async_client_put(client);
655}
656
657/** Wrapper for client connection fibril.
658 *
659 * When a new connection arrives, a fibril with this implementing function is
660 * created. It calls client_connection() and does the final cleanup.
661 *
662 * @param arg Connection structure pointer.
663 *
664 * @return Always zero.
665 *
666 */
667static int connection_fibril(void *arg)
668{
669 assert(arg);
670
671 /*
672 * Setup fibril-local connection pointer.
673 */
674 fibril_connection = (connection_t *) arg;
675
676 /*
677 * Add our reference for the current connection in the client task
678 * tracking structure. If this is the first reference, create and
679 * hash in a new tracking structure.
680 */
681
682 client_t *client = async_client_get(fibril_connection->in_task_hash, true);
683 if (!client) {
684 ipc_answer_0(fibril_connection->callid, ENOMEM);
685 return 0;
686 }
687
688 fibril_connection->client = client;
689
690 /*
691 * Call the connection handler function.
692 */
693 fibril_connection->cfibril(fibril_connection->callid,
694 &fibril_connection->call, fibril_connection->carg);
695
696 /*
697 * Remove the reference for this client task connection.
698 */
699 async_client_put(client);
700
701 /*
702 * Remove myself from the connection hash table.
703 */
704 futex_down(&async_futex);
705 unsigned long key = fibril_connection->in_phone_hash;
706 hash_table_remove(&conn_hash_table, &key, 1);
707 futex_up(&async_futex);
708
709 /*
710 * Answer all remaining messages with EHANGUP.
711 */
712 while (!list_empty(&fibril_connection->msg_queue)) {
713 msg_t *msg =
714 list_get_instance(list_first(&fibril_connection->msg_queue),
715 msg_t, link);
716
717 list_remove(&msg->link);
718 ipc_answer_0(msg->callid, EHANGUP);
719 free(msg);
720 }
721
722 /*
723 * If the connection was hung-up, answer the last call,
724 * i.e. IPC_M_PHONE_HUNGUP.
725 */
726 if (fibril_connection->close_callid)
727 ipc_answer_0(fibril_connection->close_callid, EOK);
728
729 free(fibril_connection);
730 return 0;
731}
732
733/** Create a new fibril for a new connection.
734 *
735 * Create new fibril for connection, fill in connection structures and insert
736 * it into the hash table, so that later we can easily do routing of messages to
737 * particular fibrils.
738 *
739 * @param in_task_hash Identification of the incoming connection.
740 * @param in_phone_hash Identification of the incoming connection.
741 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
742 * If callid is zero, the connection was opened by
743 * accepting the IPC_M_CONNECT_TO_ME call and this function
744 * is called directly by the server.
745 * @param call Call data of the opening call.
746 * @param cfibril Fibril function that should be called upon opening the
747 * connection.
748 * @param carg Extra argument to pass to the connection fibril
749 *
750 * @return New fibril id or NULL on failure.
751 *
752 */
753fid_t async_new_connection(sysarg_t in_task_hash, sysarg_t in_phone_hash,
754 ipc_callid_t callid, ipc_call_t *call,
755 async_client_conn_t cfibril, void *carg)
756{
757 connection_t *conn = malloc(sizeof(*conn));
758 if (!conn) {
759 if (callid)
760 ipc_answer_0(callid, ENOMEM);
761
762 return (uintptr_t) NULL;
763 }
764
765 conn->in_task_hash = in_task_hash;
766 conn->in_phone_hash = in_phone_hash;
767 list_initialize(&conn->msg_queue);
768 conn->callid = callid;
769 conn->close_callid = 0;
770 conn->carg = carg;
771
772 if (call)
773 conn->call = *call;
774
775 /* We will activate the fibril ASAP */
776 conn->wdata.active = true;
777 conn->cfibril = cfibril;
778 conn->wdata.fid = fibril_create(connection_fibril, conn);
779
780 if (conn->wdata.fid == 0) {
781 free(conn);
782
783 if (callid)
784 ipc_answer_0(callid, ENOMEM);
785
786 return (uintptr_t) NULL;
787 }
788
789 /* Add connection to the connection hash table */
790 unsigned long key = conn->in_phone_hash;
791
792 futex_down(&async_futex);
793 hash_table_insert(&conn_hash_table, &key, &conn->link);
794 futex_up(&async_futex);
795
796 fibril_add_ready(conn->wdata.fid);
797
798 return conn->wdata.fid;
799}
800
801/** Handle a call that was received.
802 *
803 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
804 * Otherwise the call is routed to its connection fibril.
805 *
806 * @param callid Hash of the incoming call.
807 * @param call Data of the incoming call.
808 *
809 */
810static void handle_call(ipc_callid_t callid, ipc_call_t *call)
811{
812 assert(call);
813
814 /* Unrouted call - take some default action */
815 if ((callid & IPC_CALLID_NOTIFICATION)) {
816 process_notification(callid, call);
817 return;
818 }
819
820 switch (IPC_GET_IMETHOD(*call)) {
821 case IPC_M_CONNECT_ME:
822 case IPC_M_CONNECT_ME_TO:
823 /* Open new connection with fibril, etc. */
824 async_new_connection(call->in_task_hash, IPC_GET_ARG5(*call),
825 callid, call, client_connection, NULL);
826 return;
827 }
828
829 /* Try to route the call through the connection hash table */
830 if (route_call(callid, call))
831 return;
832
833 /* Unknown call from unknown phone - hang it up */
834 ipc_answer_0(callid, EHANGUP);
835}
836
837/** Fire all timeouts that expired. */
838static void handle_expired_timeouts(void)
839{
840 struct timeval tv;
841 gettimeofday(&tv, NULL);
842
843 futex_down(&async_futex);
844
845 link_t *cur = list_first(&timeout_list);
846 while (cur != NULL) {
847 awaiter_t *waiter =
848 list_get_instance(cur, awaiter_t, to_event.link);
849
850 if (tv_gt(&waiter->to_event.expires, &tv))
851 break;
852
853 list_remove(&waiter->to_event.link);
854 waiter->to_event.inlist = false;
855 waiter->to_event.occurred = true;
856
857 /*
858 * Redundant condition?
859 * The fibril should not be active when it gets here.
860 */
861 if (!waiter->active) {
862 waiter->active = true;
863 fibril_add_ready(waiter->fid);
864 }
865
866 cur = list_first(&timeout_list);
867 }
868
869 futex_up(&async_futex);
870}
871
872/** Endless loop dispatching incoming calls and answers.
873 *
874 * @return Never returns.
875 *
876 */
877static int async_manager_worker(void)
878{
879 while (true) {
880 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
881 futex_up(&async_futex);
882 /*
883 * async_futex is always held when entering a manager
884 * fibril.
885 */
886 continue;
887 }
888
889 futex_down(&async_futex);
890
891 suseconds_t timeout;
892 if (!list_empty(&timeout_list)) {
893 awaiter_t *waiter = list_get_instance(
894 list_first(&timeout_list), awaiter_t, to_event.link);
895
896 struct timeval tv;
897 gettimeofday(&tv, NULL);
898
899 if (tv_gteq(&tv, &waiter->to_event.expires)) {
900 futex_up(&async_futex);
901 handle_expired_timeouts();
902 continue;
903 } else
904 timeout = tv_sub(&waiter->to_event.expires, &tv);
905 } else
906 timeout = SYNCH_NO_TIMEOUT;
907
908 futex_up(&async_futex);
909
910 atomic_inc(&threads_in_ipc_wait);
911
912 ipc_call_t call;
913 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
914 SYNCH_FLAGS_NONE);
915
916 atomic_dec(&threads_in_ipc_wait);
917
918 if (!callid) {
919 handle_expired_timeouts();
920 continue;
921 }
922
923 if (callid & IPC_CALLID_ANSWERED)
924 continue;
925
926 handle_call(callid, &call);
927 }
928
929 return 0;
930}
931
932/** Function to start async_manager as a standalone fibril.
933 *
934 * When more kernel threads are used, one async manager should exist per thread.
935 *
936 * @param arg Unused.
937 * @return Never returns.
938 *
939 */
940static int async_manager_fibril(void *arg)
941{
942 futex_up(&async_futex);
943
944 /*
945 * async_futex is always locked when entering manager
946 */
947 async_manager_worker();
948
949 return 0;
950}
951
952/** Add one manager to manager list. */
953void async_create_manager(void)
954{
955 fid_t fid = fibril_create(async_manager_fibril, NULL);
956 if (fid != 0)
957 fibril_add_manager(fid);
958}
959
960/** Remove one manager from manager list */
961void async_destroy_manager(void)
962{
963 fibril_remove_manager();
964}
965
966/** Initialize the async framework.
967 *
968 */
969void __async_init(void)
970{
971 if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS,
972 1, &client_hash_table_ops))
973 abort();
974
975 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_BUCKETS,
976 1, &conn_hash_table_ops))
977 abort();
978
979 session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
980 if (session_ns == NULL)
981 abort();
982
983 session_ns->mgmt = EXCHANGE_ATOMIC;
984 session_ns->phone = PHONE_NS;
985 session_ns->arg1 = 0;
986 session_ns->arg2 = 0;
987 session_ns->arg3 = 0;
988
989 list_initialize(&session_ns->exch_list);
990 fibril_mutex_initialize(&session_ns->mutex);
991 atomic_set(&session_ns->refcnt, 0);
992}
993
994/** Reply received callback.
995 *
996 * This function is called whenever a reply for an asynchronous message sent out
997 * by the asynchronous framework is received.
998 *
999 * Notify the fibril which is waiting for this message that it has arrived.
1000 *
1001 * @param arg Pointer to the asynchronous message record.
1002 * @param retval Value returned in the answer.
1003 * @param data Call data of the answer.
1004 *
1005 */
1006void reply_received(void *arg, int retval, ipc_call_t *data)
1007{
1008 assert(arg);
1009
1010 futex_down(&async_futex);
1011
1012 amsg_t *msg = (amsg_t *) arg;
1013 msg->retval = retval;
1014
1015 /* Copy data after futex_down, just in case the call was detached */
1016 if ((msg->dataptr) && (data))
1017 *msg->dataptr = *data;
1018
1019 write_barrier();
1020
1021 /* Remove message from timeout list */
1022 if (msg->wdata.to_event.inlist)
1023 list_remove(&msg->wdata.to_event.link);
1024
1025 msg->done = true;
1026 if (!msg->wdata.active) {
1027 msg->wdata.active = true;
1028 fibril_add_ready(msg->wdata.fid);
1029 }
1030
1031 futex_up(&async_futex);
1032}
1033
1034/** Send message and return id of the sent message.
1035 *
1036 * The return value can be used as input for async_wait() to wait for
1037 * completion.
1038 *
1039 * @param exch Exchange for sending the message.
1040 * @param imethod Service-defined interface and method.
1041 * @param arg1 Service-defined payload argument.
1042 * @param arg2 Service-defined payload argument.
1043 * @param arg3 Service-defined payload argument.
1044 * @param arg4 Service-defined payload argument.
1045 * @param dataptr If non-NULL, storage where the reply data will be
1046 * stored.
1047 *
1048 * @return Hash of the sent message or 0 on error.
1049 *
1050 */
1051aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1052 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1053{
1054 if (exch == NULL)
1055 return 0;
1056
1057 amsg_t *msg = malloc(sizeof(amsg_t));
1058 if (msg == NULL)
1059 return 0;
1060
1061 msg->done = false;
1062 msg->dataptr = dataptr;
1063
1064 msg->wdata.to_event.inlist = false;
1065
1066 /*
1067 * We may sleep in the next method,
1068 * but it will use its own means
1069 */
1070 msg->wdata.active = true;
1071
1072 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
1073 reply_received, true);
1074
1075 return (aid_t) msg;
1076}
1077
1078/** Send message and return id of the sent message
1079 *
1080 * The return value can be used as input for async_wait() to wait for
1081 * completion.
1082 *
1083 * @param exch Exchange for sending the message.
1084 * @param imethod Service-defined interface and method.
1085 * @param arg1 Service-defined payload argument.
1086 * @param arg2 Service-defined payload argument.
1087 * @param arg3 Service-defined payload argument.
1088 * @param arg4 Service-defined payload argument.
1089 * @param arg5 Service-defined payload argument.
1090 * @param dataptr If non-NULL, storage where the reply data will be
1091 * stored.
1092 *
1093 * @return Hash of the sent message or 0 on error.
1094 *
1095 */
1096aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1097 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
1098 ipc_call_t *dataptr)
1099{
1100 if (exch == NULL)
1101 return 0;
1102
1103 amsg_t *msg = malloc(sizeof(amsg_t));
1104
1105 if (msg == NULL)
1106 return 0;
1107
1108 msg->done = false;
1109 msg->dataptr = dataptr;
1110
1111 msg->wdata.to_event.inlist = false;
1112
1113 /*
1114 * We may sleep in the next method,
1115 * but it will use its own means
1116 */
1117 msg->wdata.active = true;
1118
1119 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
1120 msg, reply_received, true);
1121
1122 return (aid_t) msg;
1123}
1124
1125/** Wait for a message sent by the async framework.
1126 *
1127 * @param amsgid Hash of the message to wait for.
1128 * @param retval Pointer to storage where the retval of the answer will
1129 * be stored.
1130 *
1131 */
1132void async_wait_for(aid_t amsgid, sysarg_t *retval)
1133{
1134 assert(amsgid);
1135
1136 amsg_t *msg = (amsg_t *) amsgid;
1137
1138 futex_down(&async_futex);
1139 if (msg->done) {
1140 futex_up(&async_futex);
1141 goto done;
1142 }
1143
1144 msg->wdata.fid = fibril_get_id();
1145 msg->wdata.active = false;
1146 msg->wdata.to_event.inlist = false;
1147
1148 /* Leave the async_futex locked when entering this function */
1149 fibril_switch(FIBRIL_TO_MANAGER);
1150
1151 /* Futex is up automatically after fibril_switch */
1152
1153done:
1154 if (retval)
1155 *retval = msg->retval;
1156
1157 free(msg);
1158}
1159
1160/** Wait for a message sent by the async framework, timeout variant.
1161 *
1162 * @param amsgid Hash of the message to wait for.
1163 * @param retval Pointer to storage where the retval of the answer will
1164 * be stored.
1165 * @param timeout Timeout in microseconds.
1166 *
1167 * @return Zero on success, ETIMEOUT if the timeout has expired.
1168 *
1169 */
1170int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
1171{
1172 assert(amsgid);
1173
1174 amsg_t *msg = (amsg_t *) amsgid;
1175
1176 /* TODO: Let it go through the event read at least once */
1177 if (timeout < 0)
1178 return ETIMEOUT;
1179
1180 futex_down(&async_futex);
1181 if (msg->done) {
1182 futex_up(&async_futex);
1183 goto done;
1184 }
1185
1186 gettimeofday(&msg->wdata.to_event.expires, NULL);
1187 tv_add(&msg->wdata.to_event.expires, timeout);
1188
1189 msg->wdata.fid = fibril_get_id();
1190 msg->wdata.active = false;
1191 async_insert_timeout(&msg->wdata);
1192
1193 /* Leave the async_futex locked when entering this function */
1194 fibril_switch(FIBRIL_TO_MANAGER);
1195
1196 /* Futex is up automatically after fibril_switch */
1197
1198 if (!msg->done)
1199 return ETIMEOUT;
1200
1201done:
1202 if (retval)
1203 *retval = msg->retval;
1204
1205 free(msg);
1206
1207 return 0;
1208}
1209
1210/** Wait for specified time.
1211 *
1212 * The current fibril is suspended but the thread continues to execute.
1213 *
1214 * @param timeout Duration of the wait in microseconds.
1215 *
1216 */
1217void async_usleep(suseconds_t timeout)
1218{
1219 amsg_t *msg = malloc(sizeof(amsg_t));
1220
1221 if (!msg)
1222 return;
1223
1224 msg->wdata.fid = fibril_get_id();
1225 msg->wdata.active = false;
1226
1227 gettimeofday(&msg->wdata.to_event.expires, NULL);
1228 tv_add(&msg->wdata.to_event.expires, timeout);
1229
1230 futex_down(&async_futex);
1231
1232 async_insert_timeout(&msg->wdata);
1233
1234 /* Leave the async_futex locked when entering this function */
1235 fibril_switch(FIBRIL_TO_MANAGER);
1236
1237 /* Futex is up automatically after fibril_switch() */
1238
1239 free(msg);
1240}
1241
1242/** Pseudo-synchronous message sending - fast version.
1243 *
1244 * Send message asynchronously and return only after the reply arrives.
1245 *
1246 * This function can only transfer 4 register payload arguments. For
1247 * transferring more arguments, see the slower async_req_slow().
1248 *
1249 * @param exch Exchange for sending the message.
1250 * @param imethod Interface and method of the call.
1251 * @param arg1 Service-defined payload argument.
1252 * @param arg2 Service-defined payload argument.
1253 * @param arg3 Service-defined payload argument.
1254 * @param arg4 Service-defined payload argument.
1255 * @param r1 If non-NULL, storage for the 1st reply argument.
1256 * @param r2 If non-NULL, storage for the 2nd reply argument.
1257 * @param r3 If non-NULL, storage for the 3rd reply argument.
1258 * @param r4 If non-NULL, storage for the 4th reply argument.
1259 * @param r5 If non-NULL, storage for the 5th reply argument.
1260 *
1261 * @return Return code of the reply or a negative error code.
1262 *
1263 */
1264sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1265 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1266 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1267{
1268 if (exch == NULL)
1269 return ENOENT;
1270
1271 ipc_call_t result;
1272 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
1273 &result);
1274
1275 sysarg_t rc;
1276 async_wait_for(aid, &rc);
1277
1278 if (r1)
1279 *r1 = IPC_GET_ARG1(result);
1280
1281 if (r2)
1282 *r2 = IPC_GET_ARG2(result);
1283
1284 if (r3)
1285 *r3 = IPC_GET_ARG3(result);
1286
1287 if (r4)
1288 *r4 = IPC_GET_ARG4(result);
1289
1290 if (r5)
1291 *r5 = IPC_GET_ARG5(result);
1292
1293 return rc;
1294}
1295
1296/** Pseudo-synchronous message sending - slow version.
1297 *
1298 * Send message asynchronously and return only after the reply arrives.
1299 *
1300 * @param exch Exchange for sending the message.
1301 * @param imethod Interface and method of the call.
1302 * @param arg1 Service-defined payload argument.
1303 * @param arg2 Service-defined payload argument.
1304 * @param arg3 Service-defined payload argument.
1305 * @param arg4 Service-defined payload argument.
1306 * @param arg5 Service-defined payload argument.
1307 * @param r1 If non-NULL, storage for the 1st reply argument.
1308 * @param r2 If non-NULL, storage for the 2nd reply argument.
1309 * @param r3 If non-NULL, storage for the 3rd reply argument.
1310 * @param r4 If non-NULL, storage for the 4th reply argument.
1311 * @param r5 If non-NULL, storage for the 5th reply argument.
1312 *
1313 * @return Return code of the reply or a negative error code.
1314 *
1315 */
1316sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1317 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1318 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1319{
1320 if (exch == NULL)
1321 return ENOENT;
1322
1323 ipc_call_t result;
1324 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
1325 &result);
1326
1327 sysarg_t rc;
1328 async_wait_for(aid, &rc);
1329
1330 if (r1)
1331 *r1 = IPC_GET_ARG1(result);
1332
1333 if (r2)
1334 *r2 = IPC_GET_ARG2(result);
1335
1336 if (r3)
1337 *r3 = IPC_GET_ARG3(result);
1338
1339 if (r4)
1340 *r4 = IPC_GET_ARG4(result);
1341
1342 if (r5)
1343 *r5 = IPC_GET_ARG5(result);
1344
1345 return rc;
1346}
1347
1348void async_msg_0(async_exch_t *exch, sysarg_t imethod)
1349{
1350 if (exch != NULL)
1351 ipc_call_async_0(exch->phone, imethod, NULL, NULL, true);
1352}
1353
1354void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
1355{
1356 if (exch != NULL)
1357 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL, true);
1358}
1359
1360void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1361 sysarg_t arg2)
1362{
1363 if (exch != NULL)
1364 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL,
1365 true);
1366}
1367
1368void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1369 sysarg_t arg2, sysarg_t arg3)
1370{
1371 if (exch != NULL)
1372 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
1373 NULL, true);
1374}
1375
1376void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1377 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1378{
1379 if (exch != NULL)
1380 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
1381 NULL, NULL, true);
1382}
1383
1384void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1385 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1386{
1387 if (exch != NULL)
1388 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
1389 arg5, NULL, NULL, true);
1390}
1391
1392sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
1393{
1394 return ipc_answer_0(callid, retval);
1395}
1396
1397sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
1398{
1399 return ipc_answer_1(callid, retval, arg1);
1400}
1401
1402sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1403 sysarg_t arg2)
1404{
1405 return ipc_answer_2(callid, retval, arg1, arg2);
1406}
1407
1408sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1409 sysarg_t arg2, sysarg_t arg3)
1410{
1411 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
1412}
1413
1414sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1415 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1416{
1417 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
1418}
1419
1420sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1421 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1422{
1423 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
1424}
1425
1426int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
1427 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
1428{
1429 if (exch == NULL)
1430 return ENOENT;
1431
1432 return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
1433}
1434
1435int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
1436 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
1437 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
1438{
1439 if (exch == NULL)
1440 return ENOENT;
1441
1442 return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
1443 arg4, arg5, mode);
1444}
1445
1446/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1447 *
1448 * Ask through phone for a new connection to some service.
1449 *
1450 * @param exch Exchange for sending the message.
1451 * @param arg1 User defined argument.
1452 * @param arg2 User defined argument.
1453 * @param arg3 User defined argument.
1454 * @param client_receiver Connection handing routine.
1455 *
1456 * @return Zero on success or a negative error code.
1457 *
1458 */
1459int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
1460 sysarg_t arg3, async_client_conn_t client_receiver, void *carg)
1461{
1462 if (exch == NULL)
1463 return ENOENT;
1464
1465 sysarg_t task_hash;
1466 sysarg_t phone_hash;
1467 int rc = async_req_3_5(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1468 NULL, NULL, NULL, &task_hash, &phone_hash);
1469 if (rc != EOK)
1470 return rc;
1471
1472 if (client_receiver != NULL)
1473 async_new_connection(task_hash, phone_hash, 0, NULL,
1474 client_receiver, carg);
1475
1476 return EOK;
1477}
1478
1479/** Wrapper for making IPC_M_CONNECT_ME calls using the async framework.
1480 *
1481 * Ask through for a cloned connection to some service.
1482 *
1483 * @param mgmt Exchange management style.
1484 * @param exch Exchange for sending the message.
1485 *
1486 * @return New session on success or NULL on error.
1487 *
1488 */
1489async_sess_t *async_connect_me(exch_mgmt_t mgmt, async_exch_t *exch)
1490{
1491 if (exch == NULL) {
1492 errno = ENOENT;
1493 return NULL;
1494 }
1495
1496 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1497 if (sess == NULL) {
1498 errno = ENOMEM;
1499 return NULL;
1500 }
1501
1502 ipc_call_t result;
1503
1504 amsg_t *msg = malloc(sizeof(amsg_t));
1505 if (msg == NULL) {
1506 free(sess);
1507 errno = ENOMEM;
1508 return NULL;
1509 }
1510
1511 msg->done = false;
1512 msg->dataptr = &result;
1513
1514 msg->wdata.to_event.inlist = false;
1515
1516 /*
1517 * We may sleep in the next method,
1518 * but it will use its own means
1519 */
1520 msg->wdata.active = true;
1521
1522 ipc_call_async_0(exch->phone, IPC_M_CONNECT_ME, msg,
1523 reply_received, true);
1524
1525 sysarg_t rc;
1526 async_wait_for((aid_t) msg, &rc);
1527
1528 if (rc != EOK) {
1529 errno = rc;
1530 free(sess);
1531 return NULL;
1532 }
1533
1534 int phone = (int) IPC_GET_ARG5(result);
1535
1536 if (phone < 0) {
1537 errno = phone;
1538 free(sess);
1539 return NULL;
1540 }
1541
1542 sess->mgmt = mgmt;
1543 sess->phone = phone;
1544 sess->arg1 = 0;
1545 sess->arg2 = 0;
1546 sess->arg3 = 0;
1547
1548 list_initialize(&sess->exch_list);
1549 fibril_mutex_initialize(&sess->mutex);
1550 atomic_set(&sess->refcnt, 0);
1551
1552 return sess;
1553}
1554
1555static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
1556 sysarg_t arg3, sysarg_t arg4)
1557{
1558 ipc_call_t result;
1559
1560 amsg_t *msg = malloc(sizeof(amsg_t));
1561 if (msg == NULL)
1562 return ENOENT;
1563
1564 msg->done = false;
1565 msg->dataptr = &result;
1566
1567 msg->wdata.to_event.inlist = false;
1568
1569 /*
1570 * We may sleep in the next method,
1571 * but it will use its own means
1572 */
1573 msg->wdata.active = true;
1574
1575 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
1576 msg, reply_received, true);
1577
1578 sysarg_t rc;
1579 async_wait_for((aid_t) msg, &rc);
1580
1581 if (rc != EOK)
1582 return rc;
1583
1584 return (int) IPC_GET_ARG5(result);
1585}
1586
1587/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1588 *
1589 * Ask through for a new connection to some service.
1590 *
1591 * @param mgmt Exchange management style.
1592 * @param exch Exchange for sending the message.
1593 * @param arg1 User defined argument.
1594 * @param arg2 User defined argument.
1595 * @param arg3 User defined argument.
1596 *
1597 * @return New session on success or NULL on error.
1598 *
1599 */
1600async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
1601 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1602{
1603 if (exch == NULL) {
1604 errno = ENOENT;
1605 return NULL;
1606 }
1607
1608 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1609 if (sess == NULL) {
1610 errno = ENOMEM;
1611 return NULL;
1612 }
1613
1614 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1615 0);
1616
1617 if (phone < 0) {
1618 errno = phone;
1619 free(sess);
1620 return NULL;
1621 }
1622
1623 sess->mgmt = mgmt;
1624 sess->phone = phone;
1625 sess->arg1 = arg1;
1626 sess->arg2 = arg2;
1627 sess->arg3 = arg3;
1628
1629 list_initialize(&sess->exch_list);
1630 fibril_mutex_initialize(&sess->mutex);
1631 atomic_set(&sess->refcnt, 0);
1632
1633 return sess;
1634}
1635
1636/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1637 *
1638 * Ask through phone for a new connection to some service and block until
1639 * success.
1640 *
1641 * @param mgmt Exchange management style.
1642 * @param exch Exchange for sending the message.
1643 * @param arg1 User defined argument.
1644 * @param arg2 User defined argument.
1645 * @param arg3 User defined argument.
1646 *
1647 * @return New session on success or NULL on error.
1648 *
1649 */
1650async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
1651 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1652{
1653 if (exch == NULL) {
1654 errno = ENOENT;
1655 return NULL;
1656 }
1657
1658 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1659 if (sess == NULL) {
1660 errno = ENOMEM;
1661 return NULL;
1662 }
1663
1664 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1665 IPC_FLAG_BLOCKING);
1666
1667 if (phone < 0) {
1668 errno = phone;
1669 free(sess);
1670 return NULL;
1671 }
1672
1673 sess->mgmt = mgmt;
1674 sess->phone = phone;
1675 sess->arg1 = arg1;
1676 sess->arg2 = arg2;
1677 sess->arg3 = arg3;
1678
1679 list_initialize(&sess->exch_list);
1680 fibril_mutex_initialize(&sess->mutex);
1681 atomic_set(&sess->refcnt, 0);
1682
1683 return sess;
1684}
1685
1686/** Connect to a task specified by id.
1687 *
1688 */
1689async_sess_t *async_connect_kbox(task_id_t id)
1690{
1691 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1692 if (sess == NULL) {
1693 errno = ENOMEM;
1694 return NULL;
1695 }
1696
1697 int phone = ipc_connect_kbox(id);
1698 if (phone < 0) {
1699 errno = phone;
1700 free(sess);
1701 return NULL;
1702 }
1703
1704 sess->mgmt = EXCHANGE_ATOMIC;
1705 sess->phone = phone;
1706 sess->arg1 = 0;
1707 sess->arg2 = 0;
1708 sess->arg3 = 0;
1709
1710 list_initialize(&sess->exch_list);
1711 fibril_mutex_initialize(&sess->mutex);
1712 atomic_set(&sess->refcnt, 0);
1713
1714 return sess;
1715}
1716
1717static int async_hangup_internal(int phone)
1718{
1719 return ipc_hangup(phone);
1720}
1721
1722/** Wrapper for ipc_hangup.
1723 *
1724 * @param sess Session to hung up.
1725 *
1726 * @return Zero on success or a negative error code.
1727 *
1728 */
1729int async_hangup(async_sess_t *sess)
1730{
1731 assert(sess);
1732
1733 if (atomic_get(&sess->refcnt) > 0)
1734 return EBUSY;
1735
1736 int rc = async_hangup_internal(sess->phone);
1737 if (rc == EOK)
1738 free(sess);
1739
1740 return rc;
1741}
1742
1743/** Interrupt one thread of this task from waiting for IPC. */
1744void async_poke(void)
1745{
1746 ipc_poke();
1747}
1748
1749/** Start new exchange in a session.
1750 *
1751 * @param session Session.
1752 *
1753 * @return New exchange or NULL on error.
1754 *
1755 */
1756async_exch_t *async_exchange_begin(async_sess_t *sess)
1757{
1758 if (sess == NULL)
1759 return NULL;
1760
1761 async_exch_t *exch;
1762
1763 fibril_mutex_lock(&async_sess_mutex);
1764
1765 if (!list_empty(&sess->exch_list)) {
1766 /*
1767 * There are inactive exchanges in the session.
1768 */
1769 exch = (async_exch_t *)
1770 list_get_instance(list_first(&sess->exch_list),
1771 async_exch_t, sess_link);
1772
1773 list_remove(&exch->sess_link);
1774 list_remove(&exch->global_link);
1775 } else {
1776 /*
1777 * There are no available exchanges in the session.
1778 */
1779
1780 if ((sess->mgmt == EXCHANGE_ATOMIC) ||
1781 (sess->mgmt == EXCHANGE_SERIALIZE)) {
1782 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
1783 if (exch != NULL) {
1784 link_initialize(&exch->sess_link);
1785 link_initialize(&exch->global_link);
1786 exch->sess = sess;
1787 exch->phone = sess->phone;
1788 }
1789 } else { /* EXCHANGE_PARALLEL */
1790 /*
1791 * Make a one-time attempt to connect a new data phone.
1792 */
1793
1794 int phone;
1795
1796retry:
1797 phone = async_connect_me_to_internal(sess->phone, sess->arg1,
1798 sess->arg2, sess->arg3, 0);
1799 if (phone >= 0) {
1800 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
1801 if (exch != NULL) {
1802 link_initialize(&exch->sess_link);
1803 link_initialize(&exch->global_link);
1804 exch->sess = sess;
1805 exch->phone = phone;
1806 } else
1807 async_hangup_internal(phone);
1808 } else if (!list_empty(&inactive_exch_list)) {
1809 /*
1810 * We did not manage to connect a new phone. But we
1811 * can try to close some of the currently inactive
1812 * connections in other sessions and try again.
1813 */
1814 exch = (async_exch_t *)
1815 list_get_instance(list_first(&inactive_exch_list),
1816 async_exch_t, global_link);
1817
1818 list_remove(&exch->sess_link);
1819 list_remove(&exch->global_link);
1820 async_hangup_internal(exch->phone);
1821 free(exch);
1822 goto retry;
1823 } else {
1824 /*
1825 * Wait for a phone to become available.
1826 */
1827 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
1828 goto retry;
1829 }
1830 }
1831 }
1832
1833 fibril_mutex_unlock(&async_sess_mutex);
1834
1835 if (exch != NULL) {
1836 atomic_inc(&sess->refcnt);
1837
1838 if (sess->mgmt == EXCHANGE_SERIALIZE)
1839 fibril_mutex_lock(&sess->mutex);
1840 }
1841
1842 return exch;
1843}
1844
1845/** Finish an exchange.
1846 *
1847 * @param exch Exchange to finish.
1848 *
1849 */
1850void async_exchange_end(async_exch_t *exch)
1851{
1852 if (exch == NULL)
1853 return;
1854
1855 async_sess_t *sess = exch->sess;
1856
1857 atomic_dec(&sess->refcnt);
1858
1859 if (sess->mgmt == EXCHANGE_SERIALIZE)
1860 fibril_mutex_unlock(&sess->mutex);
1861
1862 fibril_mutex_lock(&async_sess_mutex);
1863
1864 list_append(&exch->sess_link, &sess->exch_list);
1865 list_append(&exch->global_link, &inactive_exch_list);
1866 fibril_condvar_signal(&avail_phone_cv);
1867
1868 fibril_mutex_unlock(&async_sess_mutex);
1869}
1870
1871/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
1872 *
1873 * @param exch Exchange for sending the message.
1874 * @param dst Destination address space area base.
1875 * @param size Size of the destination address space area.
1876 * @param arg User defined argument.
1877 * @param flags Storage for the received flags. Can be NULL.
1878 *
1879 * @return Zero on success or a negative error code from errno.h.
1880 *
1881 */
1882int async_share_in_start(async_exch_t *exch, void *dst, size_t size,
1883 sysarg_t arg, unsigned int *flags)
1884{
1885 if (exch == NULL)
1886 return ENOENT;
1887
1888 sysarg_t tmp_flags;
1889 int res = async_req_3_2(exch, IPC_M_SHARE_IN, (sysarg_t) dst,
1890 (sysarg_t) size, arg, NULL, &tmp_flags);
1891
1892 if (flags)
1893 *flags = (unsigned int) tmp_flags;
1894
1895 return res;
1896}
1897
1898/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1899 *
1900 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
1901 * calls so that the user doesn't have to remember the meaning of each IPC
1902 * argument.
1903 *
1904 * So far, this wrapper is to be used from within a connection fibril.
1905 *
1906 * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
1907 * @param size Destination address space area size.
1908 *
1909 * @return True on success, false on failure.
1910 *
1911 */
1912bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
1913{
1914 assert(callid);
1915 assert(size);
1916
1917 ipc_call_t data;
1918 *callid = async_get_call(&data);
1919
1920 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
1921 return false;
1922
1923 *size = (size_t) IPC_GET_ARG2(data);
1924 return true;
1925}
1926
1927/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1928 *
1929 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
1930 * calls so that the user doesn't have to remember the meaning of each IPC
1931 * argument.
1932 *
1933 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1934 * @param src Source address space base.
1935 * @param flags Flags to be used for sharing. Bits can be only cleared.
1936 *
1937 * @return Zero on success or a value from @ref errno.h on failure.
1938 *
1939 */
1940int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
1941{
1942 return ipc_share_in_finalize(callid, src, flags);
1943}
1944
1945/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
1946 *
1947 * @param exch Exchange for sending the message.
1948 * @param src Source address space area base address.
1949 * @param flags Flags to be used for sharing. Bits can be only cleared.
1950 *
1951 * @return Zero on success or a negative error code from errno.h.
1952 *
1953 */
1954int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
1955{
1956 if (exch == NULL)
1957 return ENOENT;
1958
1959 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
1960 (sysarg_t) flags);
1961}
1962
1963/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1964 *
1965 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
1966 * calls so that the user doesn't have to remember the meaning of each IPC
1967 * argument.
1968 *
1969 * So far, this wrapper is to be used from within a connection fibril.
1970 *
1971 * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
1972 * @param size Storage for the source address space area size.
1973 * @param flags Storage for the sharing flags.
1974 *
1975 * @return True on success, false on failure.
1976 *
1977 */
1978bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
1979{
1980 assert(callid);
1981 assert(size);
1982 assert(flags);
1983
1984 ipc_call_t data;
1985 *callid = async_get_call(&data);
1986
1987 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
1988 return false;
1989
1990 *size = (size_t) IPC_GET_ARG2(data);
1991 *flags = (unsigned int) IPC_GET_ARG3(data);
1992 return true;
1993}
1994
1995/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
1996 *
1997 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
1998 * calls so that the user doesn't have to remember the meaning of each IPC
1999 * argument.
2000 *
2001 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2002 * @param dst Destination address space area base address.
2003 *
2004 * @return Zero on success or a value from @ref errno.h on failure.
2005 *
2006 */
2007int async_share_out_finalize(ipc_callid_t callid, void *dst)
2008{
2009 return ipc_share_out_finalize(callid, dst);
2010}
2011
2012/** Start IPC_M_DATA_READ using the async framework.
2013 *
2014 * @param exch Exchange for sending the message.
2015 * @param dst Address of the beginning of the destination buffer.
2016 * @param size Size of the destination buffer (in bytes).
2017 * @param dataptr Storage of call data (arg 2 holds actual data size).
2018 *
2019 * @return Hash of the sent message or 0 on error.
2020 *
2021 */
2022aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2023 ipc_call_t *dataptr)
2024{
2025 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2026 (sysarg_t) size, dataptr);
2027}
2028
2029/** Wrapper for IPC_M_DATA_READ calls using the async framework.
2030 *
2031 * @param exch Exchange for sending the message.
2032 * @param dst Address of the beginning of the destination buffer.
2033 * @param size Size of the destination buffer.
2034 *
2035 * @return Zero on success or a negative error code from errno.h.
2036 *
2037 */
2038int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
2039{
2040 if (exch == NULL)
2041 return ENOENT;
2042
2043 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2044 (sysarg_t) size);
2045}
2046
2047/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2048 *
2049 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2050 * calls so that the user doesn't have to remember the meaning of each IPC
2051 * argument.
2052 *
2053 * So far, this wrapper is to be used from within a connection fibril.
2054 *
2055 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2056 * @param size Storage for the maximum size. Can be NULL.
2057 *
2058 * @return True on success, false on failure.
2059 *
2060 */
2061bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
2062{
2063 assert(callid);
2064
2065 ipc_call_t data;
2066 *callid = async_get_call(&data);
2067
2068 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
2069 return false;
2070
2071 if (size)
2072 *size = (size_t) IPC_GET_ARG2(data);
2073
2074 return true;
2075}
2076
2077/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2078 *
2079 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2080 * calls so that the user doesn't have to remember the meaning of each IPC
2081 * argument.
2082 *
2083 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2084 * @param src Source address for the IPC_M_DATA_READ call.
2085 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2086 * the maximum size announced by the sender.
2087 *
2088 * @return Zero on success or a value from @ref errno.h on failure.
2089 *
2090 */
2091int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
2092{
2093 return ipc_data_read_finalize(callid, src, size);
2094}
2095
2096/** Wrapper for forwarding any read request
2097 *
2098 */
2099int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2100 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2101 ipc_call_t *dataptr)
2102{
2103 if (exch == NULL)
2104 return ENOENT;
2105
2106 ipc_callid_t callid;
2107 if (!async_data_read_receive(&callid, NULL)) {
2108 ipc_answer_0(callid, EINVAL);
2109 return EINVAL;
2110 }
2111
2112 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2113 dataptr);
2114 if (msg == 0) {
2115 ipc_answer_0(callid, EINVAL);
2116 return EINVAL;
2117 }
2118
2119 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
2120 IPC_FF_ROUTE_FROM_ME);
2121 if (retval != EOK) {
2122 async_wait_for(msg, NULL);
2123 ipc_answer_0(callid, retval);
2124 return retval;
2125 }
2126
2127 sysarg_t rc;
2128 async_wait_for(msg, &rc);
2129
2130 return (int) rc;
2131}
2132
2133/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
2134 *
2135 * @param exch Exchange for sending the message.
2136 * @param src Address of the beginning of the source buffer.
2137 * @param size Size of the source buffer.
2138 *
2139 * @return Zero on success or a negative error code from errno.h.
2140 *
2141 */
2142int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
2143{
2144 if (exch == NULL)
2145 return ENOENT;
2146
2147 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2148 (sysarg_t) size);
2149}
2150
2151/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2152 *
2153 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2154 * calls so that the user doesn't have to remember the meaning of each IPC
2155 * argument.
2156 *
2157 * So far, this wrapper is to be used from within a connection fibril.
2158 *
2159 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2160 * @param size Storage for the suggested size. May be NULL.
2161 *
2162 * @return True on success, false on failure.
2163 *
2164 */
2165bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
2166{
2167 assert(callid);
2168
2169 ipc_call_t data;
2170 *callid = async_get_call(&data);
2171
2172 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
2173 return false;
2174
2175 if (size)
2176 *size = (size_t) IPC_GET_ARG2(data);
2177
2178 return true;
2179}
2180
2181/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2182 *
2183 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2184 * calls so that the user doesn't have to remember the meaning of each IPC
2185 * argument.
2186 *
2187 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2188 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2189 * @param size Final size for the IPC_M_DATA_WRITE call.
2190 *
2191 * @return Zero on success or a value from @ref errno.h on failure.
2192 *
2193 */
2194int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
2195{
2196 return ipc_data_write_finalize(callid, dst, size);
2197}
2198
2199/** Wrapper for receiving binary data or strings
2200 *
2201 * This wrapper only makes it more comfortable to use async_data_write_*
2202 * functions to receive binary data or strings.
2203 *
2204 * @param data Pointer to data pointer (which should be later disposed
2205 * by free()). If the operation fails, the pointer is not
2206 * touched.
2207 * @param nullterm If true then the received data is always zero terminated.
2208 * This also causes to allocate one extra byte beyond the
2209 * raw transmitted data.
2210 * @param min_size Minimum size (in bytes) of the data to receive.
2211 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2212 * no limit.
2213 * @param granulariy If non-zero then the size of the received data has to
2214 * be divisible by this value.
2215 * @param received If not NULL, the size of the received data is stored here.
2216 *
2217 * @return Zero on success or a value from @ref errno.h on failure.
2218 *
2219 */
2220int async_data_write_accept(void **data, const bool nullterm,
2221 const size_t min_size, const size_t max_size, const size_t granularity,
2222 size_t *received)
2223{
2224 assert(data);
2225
2226 ipc_callid_t callid;
2227 size_t size;
2228 if (!async_data_write_receive(&callid, &size)) {
2229 ipc_answer_0(callid, EINVAL);
2230 return EINVAL;
2231 }
2232
2233 if (size < min_size) {
2234 ipc_answer_0(callid, EINVAL);
2235 return EINVAL;
2236 }
2237
2238 if ((max_size > 0) && (size > max_size)) {
2239 ipc_answer_0(callid, EINVAL);
2240 return EINVAL;
2241 }
2242
2243 if ((granularity > 0) && ((size % granularity) != 0)) {
2244 ipc_answer_0(callid, EINVAL);
2245 return EINVAL;
2246 }
2247
2248 void *_data;
2249
2250 if (nullterm)
2251 _data = malloc(size + 1);
2252 else
2253 _data = malloc(size);
2254
2255 if (_data == NULL) {
2256 ipc_answer_0(callid, ENOMEM);
2257 return ENOMEM;
2258 }
2259
2260 int rc = async_data_write_finalize(callid, _data, size);
2261 if (rc != EOK) {
2262 free(_data);
2263 return rc;
2264 }
2265
2266 if (nullterm)
2267 ((char *) _data)[size] = 0;
2268
2269 *data = _data;
2270 if (received != NULL)
2271 *received = size;
2272
2273 return EOK;
2274}
2275
2276/** Wrapper for voiding any data that is about to be received
2277 *
2278 * This wrapper can be used to void any pending data
2279 *
2280 * @param retval Error value from @ref errno.h to be returned to the caller.
2281 *
2282 */
2283void async_data_write_void(sysarg_t retval)
2284{
2285 ipc_callid_t callid;
2286 async_data_write_receive(&callid, NULL);
2287 ipc_answer_0(callid, retval);
2288}
2289
2290/** Wrapper for forwarding any data that is about to be received
2291 *
2292 */
2293int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
2294 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2295 ipc_call_t *dataptr)
2296{
2297 if (exch == NULL)
2298 return ENOENT;
2299
2300 ipc_callid_t callid;
2301 if (!async_data_write_receive(&callid, NULL)) {
2302 ipc_answer_0(callid, EINVAL);
2303 return EINVAL;
2304 }
2305
2306 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2307 dataptr);
2308 if (msg == 0) {
2309 ipc_answer_0(callid, EINVAL);
2310 return EINVAL;
2311 }
2312
2313 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
2314 IPC_FF_ROUTE_FROM_ME);
2315 if (retval != EOK) {
2316 async_wait_for(msg, NULL);
2317 ipc_answer_0(callid, retval);
2318 return retval;
2319 }
2320
2321 sysarg_t rc;
2322 async_wait_for(msg, &rc);
2323
2324 return (int) rc;
2325}
2326
2327/** Wrapper for sending an exchange over different exchange for cloning
2328 *
2329 * @param exch Exchange to be used for sending.
2330 * @param clone_exch Exchange to be cloned.
2331 *
2332 */
2333int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
2334{
2335 return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
2336}
2337
2338/** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
2339 *
2340 * If the current call is IPC_M_CONNECTION_CLONE then a new
2341 * async session is created for the accepted phone.
2342 *
2343 * @param mgmt Exchange management style.
2344 *
2345 * @return New async session or NULL on failure.
2346 *
2347 */
2348async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
2349{
2350 /* Accept the phone */
2351 ipc_call_t call;
2352 ipc_callid_t callid = async_get_call(&call);
2353 int phone = (int) IPC_GET_ARG1(call);
2354
2355 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
2356 (phone < 0)) {
2357 async_answer_0(callid, EINVAL);
2358 return NULL;
2359 }
2360
2361 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2362 if (sess == NULL) {
2363 async_answer_0(callid, ENOMEM);
2364 return NULL;
2365 }
2366
2367 sess->mgmt = mgmt;
2368 sess->phone = phone;
2369 sess->arg1 = 0;
2370 sess->arg2 = 0;
2371 sess->arg3 = 0;
2372
2373 list_initialize(&sess->exch_list);
2374 fibril_mutex_initialize(&sess->mutex);
2375 atomic_set(&sess->refcnt, 0);
2376
2377 /* Acknowledge the cloned phone */
2378 async_answer_0(callid, EOK);
2379
2380 return sess;
2381}
2382
2383/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2384 *
2385 * If the current call is IPC_M_CONNECT_TO_ME then a new
2386 * async session is created for the accepted phone.
2387 *
2388 * @param mgmt Exchange management style.
2389 *
2390 * @return New async session.
2391 * @return NULL on failure.
2392 *
2393 */
2394async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
2395{
2396 /* Accept the phone */
2397 ipc_call_t call;
2398 ipc_callid_t callid = async_get_call(&call);
2399 int phone = (int) IPC_GET_ARG5(call);
2400
2401 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
2402 (phone < 0)) {
2403 async_answer_0(callid, EINVAL);
2404 return NULL;
2405 }
2406
2407 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2408 if (sess == NULL) {
2409 async_answer_0(callid, ENOMEM);
2410 return NULL;
2411 }
2412
2413 sess->mgmt = mgmt;
2414 sess->phone = phone;
2415 sess->arg1 = 0;
2416 sess->arg2 = 0;
2417 sess->arg3 = 0;
2418
2419 list_initialize(&sess->exch_list);
2420 fibril_mutex_initialize(&sess->mutex);
2421 atomic_set(&sess->refcnt, 0);
2422
2423 /* Acknowledge the connected phone */
2424 async_answer_0(callid, EOK);
2425
2426 return sess;
2427}
2428
2429/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2430 *
2431 * If the call is IPC_M_CONNECT_TO_ME then a new
2432 * async session is created. However, the phone is
2433 * not accepted automatically.
2434 *
2435 * @param mgmt Exchange management style.
2436 * @param call Call data.
2437 *
2438 * @return New async session.
2439 * @return NULL on failure.
2440 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
2441 *
2442 */
2443async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
2444{
2445 int phone = (int) IPC_GET_ARG5(*call);
2446
2447 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
2448 (phone < 0))
2449 return NULL;
2450
2451 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2452 if (sess == NULL)
2453 return NULL;
2454
2455 sess->mgmt = mgmt;
2456 sess->phone = phone;
2457 sess->arg1 = 0;
2458 sess->arg2 = 0;
2459 sess->arg3 = 0;
2460
2461 list_initialize(&sess->exch_list);
2462 fibril_mutex_initialize(&sess->mutex);
2463 atomic_set(&sess->refcnt, 0);
2464
2465 return sess;
2466}
2467
2468int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
2469 sysarg_t arg3, async_exch_t *other_exch)
2470{
2471 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
2472 arg1, arg2, arg3, 0, other_exch->phone);
2473}
2474
2475bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
2476 sysarg_t *arg2, sysarg_t *arg3)
2477{
2478 assert(callid);
2479
2480 ipc_call_t call;
2481 *callid = async_get_call(&call);
2482
2483 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
2484 return false;
2485
2486 if (arg1)
2487 *arg1 = IPC_GET_ARG1(call);
2488 if (arg2)
2489 *arg2 = IPC_GET_ARG2(call);
2490 if (arg3)
2491 *arg3 = IPC_GET_ARG3(call);
2492
2493 return true;
2494}
2495
2496int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
2497{
2498 return ipc_answer_1(callid, EOK, other_exch->phone);
2499}
2500
2501/** @}
2502 */
Note: See TracBrowser for help on using the repository browser.