source: mainline/uspace/lib/c/generic/async.c@ 1db6dfd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1db6dfd was 1db6dfd, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Fix starving IPC problem

Check for incoming IPC regardless of expired timeouts.

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