source: mainline/uspace/lib/c/generic/async.c@ 19fc04c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 19fc04c was 93ad49a8, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Make the hack more generic to work around problems with callback
sessions as well.

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