source: mainline/uspace/lib/c/generic/async.c@ 22a8a9b

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

Use async_forget() in two error paths when waiting for the answer can
happen asynchronously.

  • Property mode set to 100644
File size: 66.4 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35/**
36 * Asynchronous library
37 *
38 * The aim of this library is to provide a facility for writing programs which
39 * utilize the asynchronous nature of HelenOS IPC, yet using a normal way of
40 * programming.
41 *
42 * You should be able to write very simple multithreaded programs. The async
43 * framework will automatically take care of most of the synchronization
44 * problems.
45 *
46 * Example of use (pseudo C):
47 *
48 * 1) Multithreaded client application
49 *
50 * fibril_create(fibril1, ...);
51 * fibril_create(fibril2, ...);
52 * ...
53 *
54 * int fibril1(void *arg)
55 * {
56 * conn = async_connect_me_to(...);
57 *
58 * exch = async_exchange_begin(conn);
59 * c1 = async_send(exch);
60 * async_exchange_end(exch);
61 *
62 * exch = async_exchange_begin(conn);
63 * c2 = async_send(exch);
64 * async_exchange_end(exch);
65 *
66 * async_wait_for(c1);
67 * async_wait_for(c2);
68 * ...
69 * }
70 *
71 *
72 * 2) Multithreaded server application
73 *
74 * main()
75 * {
76 * async_manager();
77 * }
78 *
79 * my_client_connection(icallid, *icall)
80 * {
81 * if (want_refuse) {
82 * async_answer_0(icallid, ELIMIT);
83 * return;
84 * }
85 * async_answer_0(icallid, EOK);
86 *
87 * callid = async_get_call(&call);
88 * somehow_handle_the_call(callid, call);
89 * async_answer_2(callid, 1, 2, 3);
90 *
91 * callid = async_get_call(&call);
92 * ...
93 * }
94 *
95 */
96
97#define LIBC_ASYNC_C_
98#include <ipc/ipc.h>
99#include <async.h>
100#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 };
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 if (!list_empty(&timeout_list)) {
1020 awaiter_t *waiter = list_get_instance(
1021 list_first(&timeout_list), awaiter_t, to_event.link);
1022
1023 struct timeval tv;
1024 gettimeofday(&tv, NULL);
1025
1026 if (tv_gteq(&tv, &waiter->to_event.expires)) {
1027 futex_up(&async_futex);
1028 handle_expired_timeouts();
1029 continue;
1030 } else
1031 timeout = tv_sub(&waiter->to_event.expires, &tv);
1032 } else
1033 timeout = SYNCH_NO_TIMEOUT;
1034
1035 futex_up(&async_futex);
1036
1037 atomic_inc(&threads_in_ipc_wait);
1038
1039 ipc_call_t call;
1040 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
1041 SYNCH_FLAGS_NONE);
1042
1043 atomic_dec(&threads_in_ipc_wait);
1044
1045 if (!callid) {
1046 handle_expired_timeouts();
1047 continue;
1048 }
1049
1050 if (callid & IPC_CALLID_ANSWERED)
1051 continue;
1052
1053 handle_call(callid, &call);
1054 }
1055
1056 return 0;
1057}
1058
1059/** Function to start async_manager as a standalone fibril.
1060 *
1061 * When more kernel threads are used, one async manager should exist per thread.
1062 *
1063 * @param arg Unused.
1064 * @return Never returns.
1065 *
1066 */
1067static int async_manager_fibril(void *arg)
1068{
1069 futex_up(&async_futex);
1070
1071 /*
1072 * async_futex is always locked when entering manager
1073 */
1074 async_manager_worker();
1075
1076 return 0;
1077}
1078
1079/** Add one manager to manager list. */
1080void async_create_manager(void)
1081{
1082 fid_t fid = fibril_create(async_manager_fibril, NULL);
1083 if (fid != 0)
1084 fibril_add_manager(fid);
1085}
1086
1087/** Remove one manager from manager list */
1088void async_destroy_manager(void)
1089{
1090 fibril_remove_manager();
1091}
1092
1093/** Initialize the async framework.
1094 *
1095 */
1096void __async_init(void)
1097{
1098 if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS,
1099 2, &client_hash_table_ops))
1100 abort();
1101
1102 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_BUCKETS,
1103 1, &conn_hash_table_ops))
1104 abort();
1105
1106 session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
1107 if (session_ns == NULL)
1108 abort();
1109
1110 session_ns->mgmt = EXCHANGE_ATOMIC;
1111 session_ns->phone = PHONE_NS;
1112 session_ns->arg1 = 0;
1113 session_ns->arg2 = 0;
1114 session_ns->arg3 = 0;
1115
1116 fibril_mutex_initialize(&session_ns->remote_state_mtx);
1117 session_ns->remote_state_data = NULL;
1118
1119 list_initialize(&session_ns->exch_list);
1120 fibril_mutex_initialize(&session_ns->mutex);
1121 atomic_set(&session_ns->refcnt, 0);
1122}
1123
1124/** Reply received callback.
1125 *
1126 * This function is called whenever a reply for an asynchronous message sent out
1127 * by the asynchronous framework is received.
1128 *
1129 * Notify the fibril which is waiting for this message that it has arrived.
1130 *
1131 * @param arg Pointer to the asynchronous message record.
1132 * @param retval Value returned in the answer.
1133 * @param data Call data of the answer.
1134 *
1135 */
1136void reply_received(void *arg, int retval, ipc_call_t *data)
1137{
1138 assert(arg);
1139
1140 futex_down(&async_futex);
1141
1142 amsg_t *msg = (amsg_t *) arg;
1143 msg->retval = retval;
1144
1145 /* Copy data after futex_down, just in case the call was detached */
1146 if ((msg->dataptr) && (data))
1147 *msg->dataptr = *data;
1148
1149 write_barrier();
1150
1151 /* Remove message from timeout list */
1152 if (msg->wdata.to_event.inlist)
1153 list_remove(&msg->wdata.to_event.link);
1154
1155 msg->done = true;
1156
1157 if (msg->forget) {
1158 assert(msg->wdata.active);
1159 amsg_destroy(msg);
1160 } else if (!msg->wdata.active) {
1161 msg->wdata.active = true;
1162 fibril_add_ready(msg->wdata.fid);
1163 }
1164
1165 futex_up(&async_futex);
1166}
1167
1168/** Send message and return id of the sent message.
1169 *
1170 * The return value can be used as input for async_wait() to wait for
1171 * completion.
1172 *
1173 * @param exch Exchange for sending the message.
1174 * @param imethod Service-defined interface and method.
1175 * @param arg1 Service-defined payload argument.
1176 * @param arg2 Service-defined payload argument.
1177 * @param arg3 Service-defined payload argument.
1178 * @param arg4 Service-defined payload argument.
1179 * @param dataptr If non-NULL, storage where the reply data will be
1180 * stored.
1181 *
1182 * @return Hash of the sent message or 0 on error.
1183 *
1184 */
1185aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1186 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1187{
1188 if (exch == NULL)
1189 return 0;
1190
1191 amsg_t *msg = amsg_create();
1192 if (msg == NULL)
1193 return 0;
1194
1195 msg->dataptr = dataptr;
1196 msg->wdata.active = true;
1197
1198 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
1199 reply_received, true);
1200
1201 return (aid_t) msg;
1202}
1203
1204/** Send message and return id of the sent message
1205 *
1206 * The return value can be used as input for async_wait() to wait for
1207 * completion.
1208 *
1209 * @param exch Exchange for sending the message.
1210 * @param imethod Service-defined interface and method.
1211 * @param arg1 Service-defined payload argument.
1212 * @param arg2 Service-defined payload argument.
1213 * @param arg3 Service-defined payload argument.
1214 * @param arg4 Service-defined payload argument.
1215 * @param arg5 Service-defined payload argument.
1216 * @param dataptr If non-NULL, storage where the reply data will be
1217 * stored.
1218 *
1219 * @return Hash of the sent message or 0 on error.
1220 *
1221 */
1222aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1223 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
1224 ipc_call_t *dataptr)
1225{
1226 if (exch == NULL)
1227 return 0;
1228
1229 amsg_t *msg = amsg_create();
1230 if (msg == NULL)
1231 return 0;
1232
1233 msg->dataptr = dataptr;
1234 msg->wdata.active = true;
1235
1236 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
1237 msg, reply_received, true);
1238
1239 return (aid_t) msg;
1240}
1241
1242/** Wait for a message sent by the async framework.
1243 *
1244 * @param amsgid Hash of the message to wait for.
1245 * @param retval Pointer to storage where the retval of the answer will
1246 * be stored.
1247 *
1248 */
1249void async_wait_for(aid_t amsgid, sysarg_t *retval)
1250{
1251 assert(amsgid);
1252
1253 amsg_t *msg = (amsg_t *) amsgid;
1254
1255 futex_down(&async_futex);
1256
1257 assert(!msg->forget);
1258 assert(!msg->destroyed);
1259
1260 if (msg->done) {
1261 futex_up(&async_futex);
1262 goto done;
1263 }
1264
1265 msg->wdata.fid = fibril_get_id();
1266 msg->wdata.active = false;
1267 msg->wdata.to_event.inlist = false;
1268
1269 /* Leave the async_futex locked when entering this function */
1270 fibril_switch(FIBRIL_TO_MANAGER);
1271
1272 /* Futex is up automatically after fibril_switch */
1273
1274done:
1275 if (retval)
1276 *retval = msg->retval;
1277
1278 amsg_destroy(msg);
1279}
1280
1281/** Wait for a message sent by the async framework, timeout variant.
1282 *
1283 * If the wait times out, the caller may choose to either wait again by calling
1284 * async_wait_for() or async_wait_timeout(), or forget the message via
1285 * async_forget().
1286 *
1287 * @param amsgid Hash of the message to wait for.
1288 * @param retval Pointer to storage where the retval of the answer will
1289 * be stored.
1290 * @param timeout Timeout in microseconds.
1291 *
1292 * @return Zero on success, ETIMEOUT if the timeout has expired.
1293 *
1294 */
1295int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
1296{
1297 assert(amsgid);
1298
1299 amsg_t *msg = (amsg_t *) amsgid;
1300
1301 /* TODO: Let it go through the event read at least once */
1302 if (timeout < 0)
1303 return ETIMEOUT;
1304
1305 futex_down(&async_futex);
1306
1307 assert(!msg->forget);
1308 assert(!msg->destroyed);
1309
1310 if (msg->done) {
1311 futex_up(&async_futex);
1312 goto done;
1313 }
1314
1315 gettimeofday(&msg->wdata.to_event.expires, NULL);
1316 tv_add(&msg->wdata.to_event.expires, timeout);
1317
1318 msg->wdata.fid = fibril_get_id();
1319 msg->wdata.active = false;
1320 async_insert_timeout(&msg->wdata);
1321
1322 /* Leave the async_futex locked when entering this function */
1323 fibril_switch(FIBRIL_TO_MANAGER);
1324
1325 /* Futex is up automatically after fibril_switch */
1326
1327 if (!msg->done)
1328 return ETIMEOUT;
1329
1330done:
1331 if (retval)
1332 *retval = msg->retval;
1333
1334 amsg_destroy(msg);
1335
1336 return 0;
1337}
1338
1339/** Discard the message / reply on arrival.
1340 *
1341 * The message will be marked to be discarded once the reply arrives in
1342 * reply_received(). It is not allowed to call async_wait_for() or
1343 * async_wait_timeout() on this message after a call to this function.
1344 *
1345 * @param amsgid Hash of the message to forget.
1346 */
1347void async_forget(aid_t amsgid)
1348{
1349 amsg_t *msg = (amsg_t *) amsgid;
1350
1351 assert(msg);
1352 assert(!msg->forget);
1353 assert(!msg->destroyed);
1354
1355 futex_down(&async_futex);
1356 if (msg->done)
1357 amsg_destroy(msg);
1358 else
1359 msg->forget = true;
1360 futex_up(&async_futex);
1361}
1362
1363/** Wait for specified time.
1364 *
1365 * The current fibril is suspended but the thread continues to execute.
1366 *
1367 * @param timeout Duration of the wait in microseconds.
1368 *
1369 */
1370void async_usleep(suseconds_t timeout)
1371{
1372 amsg_t *msg = amsg_create();
1373 if (!msg)
1374 return;
1375
1376 msg->wdata.fid = fibril_get_id();
1377
1378 gettimeofday(&msg->wdata.to_event.expires, NULL);
1379 tv_add(&msg->wdata.to_event.expires, timeout);
1380
1381 futex_down(&async_futex);
1382
1383 async_insert_timeout(&msg->wdata);
1384
1385 /* Leave the async_futex locked when entering this function */
1386 fibril_switch(FIBRIL_TO_MANAGER);
1387
1388 /* Futex is up automatically after fibril_switch() */
1389
1390 amsg_destroy(msg);
1391}
1392
1393/** Pseudo-synchronous message sending - fast version.
1394 *
1395 * Send message asynchronously and return only after the reply arrives.
1396 *
1397 * This function can only transfer 4 register payload arguments. For
1398 * transferring more arguments, see the slower async_req_slow().
1399 *
1400 * @param exch Exchange for sending the message.
1401 * @param imethod Interface and method of the call.
1402 * @param arg1 Service-defined payload argument.
1403 * @param arg2 Service-defined payload argument.
1404 * @param arg3 Service-defined payload argument.
1405 * @param arg4 Service-defined payload argument.
1406 * @param r1 If non-NULL, storage for the 1st reply argument.
1407 * @param r2 If non-NULL, storage for the 2nd reply argument.
1408 * @param r3 If non-NULL, storage for the 3rd reply argument.
1409 * @param r4 If non-NULL, storage for the 4th reply argument.
1410 * @param r5 If non-NULL, storage for the 5th reply argument.
1411 *
1412 * @return Return code of the reply or a negative error code.
1413 *
1414 */
1415sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1416 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1417 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1418{
1419 if (exch == NULL)
1420 return ENOENT;
1421
1422 ipc_call_t result;
1423 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
1424 &result);
1425
1426 sysarg_t rc;
1427 async_wait_for(aid, &rc);
1428
1429 if (r1)
1430 *r1 = IPC_GET_ARG1(result);
1431
1432 if (r2)
1433 *r2 = IPC_GET_ARG2(result);
1434
1435 if (r3)
1436 *r3 = IPC_GET_ARG3(result);
1437
1438 if (r4)
1439 *r4 = IPC_GET_ARG4(result);
1440
1441 if (r5)
1442 *r5 = IPC_GET_ARG5(result);
1443
1444 return rc;
1445}
1446
1447/** Pseudo-synchronous message sending - slow version.
1448 *
1449 * Send message asynchronously and return only after the reply arrives.
1450 *
1451 * @param exch Exchange for sending the message.
1452 * @param imethod Interface and method of the call.
1453 * @param arg1 Service-defined payload argument.
1454 * @param arg2 Service-defined payload argument.
1455 * @param arg3 Service-defined payload argument.
1456 * @param arg4 Service-defined payload argument.
1457 * @param arg5 Service-defined payload argument.
1458 * @param r1 If non-NULL, storage for the 1st reply argument.
1459 * @param r2 If non-NULL, storage for the 2nd reply argument.
1460 * @param r3 If non-NULL, storage for the 3rd reply argument.
1461 * @param r4 If non-NULL, storage for the 4th reply argument.
1462 * @param r5 If non-NULL, storage for the 5th reply argument.
1463 *
1464 * @return Return code of the reply or a negative error code.
1465 *
1466 */
1467sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1468 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1469 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1470{
1471 if (exch == NULL)
1472 return ENOENT;
1473
1474 ipc_call_t result;
1475 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
1476 &result);
1477
1478 sysarg_t rc;
1479 async_wait_for(aid, &rc);
1480
1481 if (r1)
1482 *r1 = IPC_GET_ARG1(result);
1483
1484 if (r2)
1485 *r2 = IPC_GET_ARG2(result);
1486
1487 if (r3)
1488 *r3 = IPC_GET_ARG3(result);
1489
1490 if (r4)
1491 *r4 = IPC_GET_ARG4(result);
1492
1493 if (r5)
1494 *r5 = IPC_GET_ARG5(result);
1495
1496 return rc;
1497}
1498
1499void async_msg_0(async_exch_t *exch, sysarg_t imethod)
1500{
1501 if (exch != NULL)
1502 ipc_call_async_0(exch->phone, imethod, NULL, NULL, true);
1503}
1504
1505void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
1506{
1507 if (exch != NULL)
1508 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL, true);
1509}
1510
1511void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1512 sysarg_t arg2)
1513{
1514 if (exch != NULL)
1515 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL,
1516 true);
1517}
1518
1519void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1520 sysarg_t arg2, sysarg_t arg3)
1521{
1522 if (exch != NULL)
1523 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
1524 NULL, true);
1525}
1526
1527void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1528 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1529{
1530 if (exch != NULL)
1531 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
1532 NULL, NULL, true);
1533}
1534
1535void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1536 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1537{
1538 if (exch != NULL)
1539 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
1540 arg5, NULL, NULL, true);
1541}
1542
1543sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
1544{
1545 return ipc_answer_0(callid, retval);
1546}
1547
1548sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
1549{
1550 return ipc_answer_1(callid, retval, arg1);
1551}
1552
1553sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1554 sysarg_t arg2)
1555{
1556 return ipc_answer_2(callid, retval, arg1, arg2);
1557}
1558
1559sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1560 sysarg_t arg2, sysarg_t arg3)
1561{
1562 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
1563}
1564
1565sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1566 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1567{
1568 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
1569}
1570
1571sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1572 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1573{
1574 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
1575}
1576
1577int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
1578 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
1579{
1580 if (exch == NULL)
1581 return ENOENT;
1582
1583 return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
1584}
1585
1586int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
1587 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
1588 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
1589{
1590 if (exch == NULL)
1591 return ENOENT;
1592
1593 return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
1594 arg4, arg5, mode);
1595}
1596
1597/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1598 *
1599 * Ask through phone for a new connection to some service.
1600 *
1601 * @param exch Exchange for sending the message.
1602 * @param arg1 User defined argument.
1603 * @param arg2 User defined argument.
1604 * @param arg3 User defined argument.
1605 * @param client_receiver Connection handing routine.
1606 *
1607 * @return Zero on success or a negative error code.
1608 *
1609 */
1610int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
1611 sysarg_t arg3, async_client_conn_t client_receiver, void *carg)
1612{
1613 if (exch == NULL)
1614 return ENOENT;
1615
1616 sysarg_t phone_hash;
1617 sysarg_t rc;
1618
1619 aid_t req;
1620 ipc_call_t answer;
1621 req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1622 &answer);
1623 async_wait_for(req, &rc);
1624 if (rc != EOK)
1625 return (int) rc;
1626
1627 phone_hash = IPC_GET_ARG5(answer);
1628
1629 if (client_receiver != NULL)
1630 async_new_connection(answer.in_task_id, phone_hash, 0, NULL,
1631 client_receiver, carg);
1632
1633 return EOK;
1634}
1635
1636/** Wrapper for making IPC_M_CONNECT_ME calls using the async framework.
1637 *
1638 * Ask through for a cloned connection to some service.
1639 *
1640 * @param mgmt Exchange management style.
1641 * @param exch Exchange for sending the message.
1642 *
1643 * @return New session on success or NULL on error.
1644 *
1645 */
1646async_sess_t *async_connect_me(exch_mgmt_t mgmt, async_exch_t *exch)
1647{
1648 if (exch == NULL) {
1649 errno = ENOENT;
1650 return NULL;
1651 }
1652
1653 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1654 if (sess == NULL) {
1655 errno = ENOMEM;
1656 return NULL;
1657 }
1658
1659 ipc_call_t result;
1660
1661 amsg_t *msg = amsg_create();
1662 if (!msg) {
1663 free(sess);
1664 errno = ENOMEM;
1665 return NULL;
1666 }
1667
1668 msg->dataptr = &result;
1669 msg->wdata.active = true;
1670
1671 ipc_call_async_0(exch->phone, IPC_M_CONNECT_ME, msg,
1672 reply_received, true);
1673
1674 sysarg_t rc;
1675 async_wait_for((aid_t) msg, &rc);
1676
1677 if (rc != EOK) {
1678 errno = rc;
1679 free(sess);
1680 return NULL;
1681 }
1682
1683 int phone = (int) IPC_GET_ARG5(result);
1684
1685 if (phone < 0) {
1686 errno = phone;
1687 free(sess);
1688 return NULL;
1689 }
1690
1691 sess->mgmt = mgmt;
1692 sess->phone = phone;
1693 sess->arg1 = 0;
1694 sess->arg2 = 0;
1695 sess->arg3 = 0;
1696
1697 fibril_mutex_initialize(&sess->remote_state_mtx);
1698 sess->remote_state_data = NULL;
1699
1700 list_initialize(&sess->exch_list);
1701 fibril_mutex_initialize(&sess->mutex);
1702 atomic_set(&sess->refcnt, 0);
1703
1704 return sess;
1705}
1706
1707static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
1708 sysarg_t arg3, sysarg_t arg4)
1709{
1710 ipc_call_t result;
1711
1712 amsg_t *msg = amsg_create();
1713 if (!msg)
1714 return ENOENT;
1715
1716 msg->dataptr = &result;
1717 msg->wdata.active = true;
1718
1719 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
1720 msg, reply_received, true);
1721
1722 sysarg_t rc;
1723 async_wait_for((aid_t) msg, &rc);
1724
1725 if (rc != EOK)
1726 return rc;
1727
1728 return (int) IPC_GET_ARG5(result);
1729}
1730
1731/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1732 *
1733 * Ask through for a new connection to some service.
1734 *
1735 * @param mgmt Exchange management style.
1736 * @param exch Exchange for sending the message.
1737 * @param arg1 User defined argument.
1738 * @param arg2 User defined argument.
1739 * @param arg3 User defined argument.
1740 *
1741 * @return New session on success or NULL on error.
1742 *
1743 */
1744async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
1745 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1746{
1747 if (exch == NULL) {
1748 errno = ENOENT;
1749 return NULL;
1750 }
1751
1752 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1753 if (sess == NULL) {
1754 errno = ENOMEM;
1755 return NULL;
1756 }
1757
1758 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1759 0);
1760
1761 if (phone < 0) {
1762 errno = phone;
1763 free(sess);
1764 return NULL;
1765 }
1766
1767 sess->mgmt = mgmt;
1768 sess->phone = phone;
1769 sess->arg1 = arg1;
1770 sess->arg2 = arg2;
1771 sess->arg3 = arg3;
1772
1773 fibril_mutex_initialize(&sess->remote_state_mtx);
1774 sess->remote_state_data = NULL;
1775
1776 list_initialize(&sess->exch_list);
1777 fibril_mutex_initialize(&sess->mutex);
1778 atomic_set(&sess->refcnt, 0);
1779
1780 return sess;
1781}
1782
1783/** Set arguments for new connections.
1784 *
1785 * FIXME This is an ugly hack to work around the problem that parallel
1786 * exchanges are implemented using parallel connections. When we create
1787 * a callback session, the framework does not know arguments for the new
1788 * connections.
1789 *
1790 * The proper solution seems to be to implement parallel exchanges using
1791 * tagging.
1792 */
1793void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
1794 sysarg_t arg3)
1795{
1796 sess->arg1 = arg1;
1797 sess->arg2 = arg2;
1798 sess->arg3 = arg3;
1799}
1800
1801/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1802 *
1803 * Ask through phone for a new connection to some service and block until
1804 * success.
1805 *
1806 * @param mgmt Exchange management style.
1807 * @param exch Exchange for sending the message.
1808 * @param arg1 User defined argument.
1809 * @param arg2 User defined argument.
1810 * @param arg3 User defined argument.
1811 *
1812 * @return New session on success or NULL on error.
1813 *
1814 */
1815async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
1816 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1817{
1818 if (exch == NULL) {
1819 errno = ENOENT;
1820 return NULL;
1821 }
1822
1823 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1824 if (sess == NULL) {
1825 errno = ENOMEM;
1826 return NULL;
1827 }
1828
1829 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1830 IPC_FLAG_BLOCKING);
1831
1832 if (phone < 0) {
1833 errno = phone;
1834 free(sess);
1835 return NULL;
1836 }
1837
1838 sess->mgmt = mgmt;
1839 sess->phone = phone;
1840 sess->arg1 = arg1;
1841 sess->arg2 = arg2;
1842 sess->arg3 = arg3;
1843
1844 fibril_mutex_initialize(&sess->remote_state_mtx);
1845 sess->remote_state_data = NULL;
1846
1847 list_initialize(&sess->exch_list);
1848 fibril_mutex_initialize(&sess->mutex);
1849 atomic_set(&sess->refcnt, 0);
1850
1851 return sess;
1852}
1853
1854/** Connect to a task specified by id.
1855 *
1856 */
1857async_sess_t *async_connect_kbox(task_id_t id)
1858{
1859 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1860 if (sess == NULL) {
1861 errno = ENOMEM;
1862 return NULL;
1863 }
1864
1865 int phone = ipc_connect_kbox(id);
1866 if (phone < 0) {
1867 errno = phone;
1868 free(sess);
1869 return NULL;
1870 }
1871
1872 sess->mgmt = EXCHANGE_ATOMIC;
1873 sess->phone = phone;
1874 sess->arg1 = 0;
1875 sess->arg2 = 0;
1876 sess->arg3 = 0;
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
1888static int async_hangup_internal(int phone)
1889{
1890 return ipc_hangup(phone);
1891}
1892
1893/** Wrapper for ipc_hangup.
1894 *
1895 * @param sess Session to hung up.
1896 *
1897 * @return Zero on success or a negative error code.
1898 *
1899 */
1900int async_hangup(async_sess_t *sess)
1901{
1902 async_exch_t *exch;
1903
1904 assert(sess);
1905
1906 if (atomic_get(&sess->refcnt) > 0)
1907 return EBUSY;
1908
1909 fibril_mutex_lock(&async_sess_mutex);
1910
1911 int rc = async_hangup_internal(sess->phone);
1912
1913 while (!list_empty(&sess->exch_list)) {
1914 exch = (async_exch_t *)
1915 list_get_instance(list_first(&sess->exch_list),
1916 async_exch_t, sess_link);
1917
1918 list_remove(&exch->sess_link);
1919 list_remove(&exch->global_link);
1920 async_hangup_internal(exch->phone);
1921 free(exch);
1922 }
1923
1924 free(sess);
1925
1926 fibril_mutex_unlock(&async_sess_mutex);
1927
1928 return rc;
1929}
1930
1931/** Interrupt one thread of this task from waiting for IPC. */
1932void async_poke(void)
1933{
1934 ipc_poke();
1935}
1936
1937/** Start new exchange in a session.
1938 *
1939 * @param session Session.
1940 *
1941 * @return New exchange or NULL on error.
1942 *
1943 */
1944async_exch_t *async_exchange_begin(async_sess_t *sess)
1945{
1946 if (sess == NULL)
1947 return NULL;
1948
1949 async_exch_t *exch;
1950
1951 fibril_mutex_lock(&async_sess_mutex);
1952
1953 if (!list_empty(&sess->exch_list)) {
1954 /*
1955 * There are inactive exchanges in the session.
1956 */
1957 exch = (async_exch_t *)
1958 list_get_instance(list_first(&sess->exch_list),
1959 async_exch_t, sess_link);
1960
1961 list_remove(&exch->sess_link);
1962 list_remove(&exch->global_link);
1963 } else {
1964 /*
1965 * There are no available exchanges in the session.
1966 */
1967
1968 if ((sess->mgmt == EXCHANGE_ATOMIC) ||
1969 (sess->mgmt == EXCHANGE_SERIALIZE)) {
1970 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
1971 if (exch != NULL) {
1972 link_initialize(&exch->sess_link);
1973 link_initialize(&exch->global_link);
1974 exch->sess = sess;
1975 exch->phone = sess->phone;
1976 }
1977 } else { /* EXCHANGE_PARALLEL */
1978 /*
1979 * Make a one-time attempt to connect a new data phone.
1980 */
1981
1982 int phone;
1983
1984retry:
1985 phone = async_connect_me_to_internal(sess->phone, sess->arg1,
1986 sess->arg2, sess->arg3, 0);
1987 if (phone >= 0) {
1988 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
1989 if (exch != NULL) {
1990 link_initialize(&exch->sess_link);
1991 link_initialize(&exch->global_link);
1992 exch->sess = sess;
1993 exch->phone = phone;
1994 } else
1995 async_hangup_internal(phone);
1996 } else if (!list_empty(&inactive_exch_list)) {
1997 /*
1998 * We did not manage to connect a new phone. But we
1999 * can try to close some of the currently inactive
2000 * connections in other sessions and try again.
2001 */
2002 exch = (async_exch_t *)
2003 list_get_instance(list_first(&inactive_exch_list),
2004 async_exch_t, global_link);
2005
2006 list_remove(&exch->sess_link);
2007 list_remove(&exch->global_link);
2008 async_hangup_internal(exch->phone);
2009 free(exch);
2010 goto retry;
2011 } else {
2012 /*
2013 * Wait for a phone to become available.
2014 */
2015 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
2016 goto retry;
2017 }
2018 }
2019 }
2020
2021 fibril_mutex_unlock(&async_sess_mutex);
2022
2023 if (exch != NULL) {
2024 atomic_inc(&sess->refcnt);
2025
2026 if (sess->mgmt == EXCHANGE_SERIALIZE)
2027 fibril_mutex_lock(&sess->mutex);
2028 }
2029
2030 return exch;
2031}
2032
2033/** Finish an exchange.
2034 *
2035 * @param exch Exchange to finish.
2036 *
2037 */
2038void async_exchange_end(async_exch_t *exch)
2039{
2040 if (exch == NULL)
2041 return;
2042
2043 async_sess_t *sess = exch->sess;
2044
2045 atomic_dec(&sess->refcnt);
2046
2047 if (sess->mgmt == EXCHANGE_SERIALIZE)
2048 fibril_mutex_unlock(&sess->mutex);
2049
2050 fibril_mutex_lock(&async_sess_mutex);
2051
2052 list_append(&exch->sess_link, &sess->exch_list);
2053 list_append(&exch->global_link, &inactive_exch_list);
2054 fibril_condvar_signal(&avail_phone_cv);
2055
2056 fibril_mutex_unlock(&async_sess_mutex);
2057}
2058
2059/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
2060 *
2061 * @param exch Exchange for sending the message.
2062 * @param size Size of the destination address space area.
2063 * @param arg User defined argument.
2064 * @param flags Storage for the received flags. Can be NULL.
2065 * @param dst Destination address space area base. Cannot be NULL.
2066 *
2067 * @return Zero on success or a negative error code from errno.h.
2068 *
2069 */
2070int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
2071 unsigned int *flags, void **dst)
2072{
2073 if (exch == NULL)
2074 return ENOENT;
2075
2076 sysarg_t _flags = 0;
2077 sysarg_t _dst = (sysarg_t) -1;
2078 int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
2079 arg, NULL, &_flags, NULL, &_dst);
2080
2081 if (flags)
2082 *flags = (unsigned int) _flags;
2083
2084 *dst = (void *) _dst;
2085 return res;
2086}
2087
2088/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
2089 *
2090 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
2091 * calls so that the user doesn't have to remember the meaning of each IPC
2092 * argument.
2093 *
2094 * So far, this wrapper is to be used from within a connection fibril.
2095 *
2096 * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
2097 * @param size Destination address space area size.
2098 *
2099 * @return True on success, false on failure.
2100 *
2101 */
2102bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
2103{
2104 assert(callid);
2105 assert(size);
2106
2107 ipc_call_t data;
2108 *callid = async_get_call(&data);
2109
2110 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
2111 return false;
2112
2113 *size = (size_t) IPC_GET_ARG1(data);
2114 return true;
2115}
2116
2117/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
2118 *
2119 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
2120 * calls so that the user doesn't have to remember the meaning of each IPC
2121 * argument.
2122 *
2123 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2124 * @param src Source address space base.
2125 * @param flags Flags to be used for sharing. Bits can be only cleared.
2126 *
2127 * @return Zero on success or a value from @ref errno.h on failure.
2128 *
2129 */
2130int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
2131{
2132 return ipc_share_in_finalize(callid, src, flags);
2133}
2134
2135/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
2136 *
2137 * @param exch Exchange for sending the message.
2138 * @param src Source address space area base address.
2139 * @param flags Flags to be used for sharing. Bits can be only cleared.
2140 *
2141 * @return Zero on success or a negative error code from errno.h.
2142 *
2143 */
2144int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
2145{
2146 if (exch == NULL)
2147 return ENOENT;
2148
2149 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
2150 (sysarg_t) flags);
2151}
2152
2153/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
2154 *
2155 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
2156 * calls so that the user doesn't have to remember the meaning of each IPC
2157 * argument.
2158 *
2159 * So far, this wrapper is to be used from within a connection fibril.
2160 *
2161 * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
2162 * @param size Storage for the source address space area size.
2163 * @param flags Storage for the sharing flags.
2164 *
2165 * @return True on success, false on failure.
2166 *
2167 */
2168bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
2169{
2170 assert(callid);
2171 assert(size);
2172 assert(flags);
2173
2174 ipc_call_t data;
2175 *callid = async_get_call(&data);
2176
2177 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
2178 return false;
2179
2180 *size = (size_t) IPC_GET_ARG2(data);
2181 *flags = (unsigned int) IPC_GET_ARG3(data);
2182 return true;
2183}
2184
2185/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2186 *
2187 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2188 * calls so that the user doesn't have to remember the meaning of each IPC
2189 * argument.
2190 *
2191 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2192 * @param dst Destination address space area base address.
2193 *
2194 * @return Zero on success or a value from @ref errno.h on failure.
2195 *
2196 */
2197int async_share_out_finalize(ipc_callid_t callid, void **dst)
2198{
2199 return ipc_share_out_finalize(callid, dst);
2200}
2201
2202/** Start IPC_M_DATA_READ using the async framework.
2203 *
2204 * @param exch Exchange for sending the message.
2205 * @param dst Address of the beginning of the destination buffer.
2206 * @param size Size of the destination buffer (in bytes).
2207 * @param dataptr Storage of call data (arg 2 holds actual data size).
2208 *
2209 * @return Hash of the sent message or 0 on error.
2210 *
2211 */
2212aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2213 ipc_call_t *dataptr)
2214{
2215 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2216 (sysarg_t) size, dataptr);
2217}
2218
2219/** Wrapper for IPC_M_DATA_READ calls using the async framework.
2220 *
2221 * @param exch Exchange for sending the message.
2222 * @param dst Address of the beginning of the destination buffer.
2223 * @param size Size of the destination buffer.
2224 *
2225 * @return Zero on success or a negative error code from errno.h.
2226 *
2227 */
2228int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
2229{
2230 if (exch == NULL)
2231 return ENOENT;
2232
2233 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2234 (sysarg_t) size);
2235}
2236
2237/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2238 *
2239 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2240 * calls so that the user doesn't have to remember the meaning of each IPC
2241 * argument.
2242 *
2243 * So far, this wrapper is to be used from within a connection fibril.
2244 *
2245 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2246 * @param size Storage for the maximum size. Can be NULL.
2247 *
2248 * @return True on success, false on failure.
2249 *
2250 */
2251bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
2252{
2253 assert(callid);
2254
2255 ipc_call_t data;
2256 *callid = async_get_call(&data);
2257
2258 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
2259 return false;
2260
2261 if (size)
2262 *size = (size_t) IPC_GET_ARG2(data);
2263
2264 return true;
2265}
2266
2267/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2268 *
2269 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2270 * calls so that the user doesn't have to remember the meaning of each IPC
2271 * argument.
2272 *
2273 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2274 * @param src Source address for the IPC_M_DATA_READ call.
2275 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2276 * the maximum size announced by the sender.
2277 *
2278 * @return Zero on success or a value from @ref errno.h on failure.
2279 *
2280 */
2281int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
2282{
2283 return ipc_data_read_finalize(callid, src, size);
2284}
2285
2286/** Wrapper for forwarding any read request
2287 *
2288 */
2289int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2290 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2291 ipc_call_t *dataptr)
2292{
2293 if (exch == NULL)
2294 return ENOENT;
2295
2296 ipc_callid_t callid;
2297 if (!async_data_read_receive(&callid, NULL)) {
2298 ipc_answer_0(callid, EINVAL);
2299 return EINVAL;
2300 }
2301
2302 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2303 dataptr);
2304 if (msg == 0) {
2305 ipc_answer_0(callid, EINVAL);
2306 return EINVAL;
2307 }
2308
2309 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
2310 IPC_FF_ROUTE_FROM_ME);
2311 if (retval != EOK) {
2312 async_forget(msg);
2313 ipc_answer_0(callid, retval);
2314 return retval;
2315 }
2316
2317 sysarg_t rc;
2318 async_wait_for(msg, &rc);
2319
2320 return (int) rc;
2321}
2322
2323/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
2324 *
2325 * @param exch Exchange for sending the message.
2326 * @param src Address of the beginning of the source buffer.
2327 * @param size Size of the source buffer.
2328 *
2329 * @return Zero on success or a negative error code from errno.h.
2330 *
2331 */
2332int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
2333{
2334 if (exch == NULL)
2335 return ENOENT;
2336
2337 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2338 (sysarg_t) size);
2339}
2340
2341/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2342 *
2343 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2344 * calls so that the user doesn't have to remember the meaning of each IPC
2345 * argument.
2346 *
2347 * So far, this wrapper is to be used from within a connection fibril.
2348 *
2349 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2350 * @param size Storage for the suggested size. May be NULL.
2351 *
2352 * @return True on success, false on failure.
2353 *
2354 */
2355bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
2356{
2357 assert(callid);
2358
2359 ipc_call_t data;
2360 *callid = async_get_call(&data);
2361
2362 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
2363 return false;
2364
2365 if (size)
2366 *size = (size_t) IPC_GET_ARG2(data);
2367
2368 return true;
2369}
2370
2371/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2372 *
2373 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2374 * calls so that the user doesn't have to remember the meaning of each IPC
2375 * argument.
2376 *
2377 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2378 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2379 * @param size Final size for the IPC_M_DATA_WRITE call.
2380 *
2381 * @return Zero on success or a value from @ref errno.h on failure.
2382 *
2383 */
2384int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
2385{
2386 return ipc_data_write_finalize(callid, dst, size);
2387}
2388
2389/** Wrapper for receiving binary data or strings
2390 *
2391 * This wrapper only makes it more comfortable to use async_data_write_*
2392 * functions to receive binary data or strings.
2393 *
2394 * @param data Pointer to data pointer (which should be later disposed
2395 * by free()). If the operation fails, the pointer is not
2396 * touched.
2397 * @param nullterm If true then the received data is always zero terminated.
2398 * This also causes to allocate one extra byte beyond the
2399 * raw transmitted data.
2400 * @param min_size Minimum size (in bytes) of the data to receive.
2401 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2402 * no limit.
2403 * @param granulariy If non-zero then the size of the received data has to
2404 * be divisible by this value.
2405 * @param received If not NULL, the size of the received data is stored here.
2406 *
2407 * @return Zero on success or a value from @ref errno.h on failure.
2408 *
2409 */
2410int async_data_write_accept(void **data, const bool nullterm,
2411 const size_t min_size, const size_t max_size, const size_t granularity,
2412 size_t *received)
2413{
2414 assert(data);
2415
2416 ipc_callid_t callid;
2417 size_t size;
2418 if (!async_data_write_receive(&callid, &size)) {
2419 ipc_answer_0(callid, EINVAL);
2420 return EINVAL;
2421 }
2422
2423 if (size < min_size) {
2424 ipc_answer_0(callid, EINVAL);
2425 return EINVAL;
2426 }
2427
2428 if ((max_size > 0) && (size > max_size)) {
2429 ipc_answer_0(callid, EINVAL);
2430 return EINVAL;
2431 }
2432
2433 if ((granularity > 0) && ((size % granularity) != 0)) {
2434 ipc_answer_0(callid, EINVAL);
2435 return EINVAL;
2436 }
2437
2438 void *_data;
2439
2440 if (nullterm)
2441 _data = malloc(size + 1);
2442 else
2443 _data = malloc(size);
2444
2445 if (_data == NULL) {
2446 ipc_answer_0(callid, ENOMEM);
2447 return ENOMEM;
2448 }
2449
2450 int rc = async_data_write_finalize(callid, _data, size);
2451 if (rc != EOK) {
2452 free(_data);
2453 return rc;
2454 }
2455
2456 if (nullterm)
2457 ((char *) _data)[size] = 0;
2458
2459 *data = _data;
2460 if (received != NULL)
2461 *received = size;
2462
2463 return EOK;
2464}
2465
2466/** Wrapper for voiding any data that is about to be received
2467 *
2468 * This wrapper can be used to void any pending data
2469 *
2470 * @param retval Error value from @ref errno.h to be returned to the caller.
2471 *
2472 */
2473void async_data_write_void(sysarg_t retval)
2474{
2475 ipc_callid_t callid;
2476 async_data_write_receive(&callid, NULL);
2477 ipc_answer_0(callid, retval);
2478}
2479
2480/** Wrapper for forwarding any data that is about to be received
2481 *
2482 */
2483int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
2484 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2485 ipc_call_t *dataptr)
2486{
2487 if (exch == NULL)
2488 return ENOENT;
2489
2490 ipc_callid_t callid;
2491 if (!async_data_write_receive(&callid, NULL)) {
2492 ipc_answer_0(callid, EINVAL);
2493 return EINVAL;
2494 }
2495
2496 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2497 dataptr);
2498 if (msg == 0) {
2499 ipc_answer_0(callid, EINVAL);
2500 return EINVAL;
2501 }
2502
2503 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
2504 IPC_FF_ROUTE_FROM_ME);
2505 if (retval != EOK) {
2506 async_forget(msg);
2507 ipc_answer_0(callid, retval);
2508 return retval;
2509 }
2510
2511 sysarg_t rc;
2512 async_wait_for(msg, &rc);
2513
2514 return (int) rc;
2515}
2516
2517/** Wrapper for sending an exchange over different exchange for cloning
2518 *
2519 * @param exch Exchange to be used for sending.
2520 * @param clone_exch Exchange to be cloned.
2521 *
2522 */
2523int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
2524{
2525 return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
2526}
2527
2528/** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
2529 *
2530 * If the current call is IPC_M_CONNECTION_CLONE then a new
2531 * async session is created for the accepted phone.
2532 *
2533 * @param mgmt Exchange management style.
2534 *
2535 * @return New async session or NULL on failure.
2536 *
2537 */
2538async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
2539{
2540 /* Accept the phone */
2541 ipc_call_t call;
2542 ipc_callid_t callid = async_get_call(&call);
2543 int phone = (int) IPC_GET_ARG1(call);
2544
2545 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
2546 (phone < 0)) {
2547 async_answer_0(callid, EINVAL);
2548 return NULL;
2549 }
2550
2551 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2552 if (sess == NULL) {
2553 async_answer_0(callid, ENOMEM);
2554 return NULL;
2555 }
2556
2557 sess->mgmt = mgmt;
2558 sess->phone = phone;
2559 sess->arg1 = 0;
2560 sess->arg2 = 0;
2561 sess->arg3 = 0;
2562
2563 fibril_mutex_initialize(&sess->remote_state_mtx);
2564 sess->remote_state_data = NULL;
2565
2566 list_initialize(&sess->exch_list);
2567 fibril_mutex_initialize(&sess->mutex);
2568 atomic_set(&sess->refcnt, 0);
2569
2570 /* Acknowledge the cloned phone */
2571 async_answer_0(callid, EOK);
2572
2573 return sess;
2574}
2575
2576/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2577 *
2578 * If the current call is IPC_M_CONNECT_TO_ME then a new
2579 * async session is created for the accepted phone.
2580 *
2581 * @param mgmt Exchange management style.
2582 *
2583 * @return New async session.
2584 * @return NULL on failure.
2585 *
2586 */
2587async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
2588{
2589 /* Accept the phone */
2590 ipc_call_t call;
2591 ipc_callid_t callid = async_get_call(&call);
2592 int phone = (int) IPC_GET_ARG5(call);
2593
2594 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
2595 (phone < 0)) {
2596 async_answer_0(callid, EINVAL);
2597 return NULL;
2598 }
2599
2600 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2601 if (sess == NULL) {
2602 async_answer_0(callid, ENOMEM);
2603 return NULL;
2604 }
2605
2606 sess->mgmt = mgmt;
2607 sess->phone = phone;
2608 sess->arg1 = 0;
2609 sess->arg2 = 0;
2610 sess->arg3 = 0;
2611
2612 fibril_mutex_initialize(&sess->remote_state_mtx);
2613 sess->remote_state_data = NULL;
2614
2615 list_initialize(&sess->exch_list);
2616 fibril_mutex_initialize(&sess->mutex);
2617 atomic_set(&sess->refcnt, 0);
2618
2619 /* Acknowledge the connected phone */
2620 async_answer_0(callid, EOK);
2621
2622 return sess;
2623}
2624
2625/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2626 *
2627 * If the call is IPC_M_CONNECT_TO_ME then a new
2628 * async session is created. However, the phone is
2629 * not accepted automatically.
2630 *
2631 * @param mgmt Exchange management style.
2632 * @param call Call data.
2633 *
2634 * @return New async session.
2635 * @return NULL on failure.
2636 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
2637 *
2638 */
2639async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
2640{
2641 int phone = (int) IPC_GET_ARG5(*call);
2642
2643 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
2644 (phone < 0))
2645 return NULL;
2646
2647 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2648 if (sess == NULL)
2649 return NULL;
2650
2651 sess->mgmt = mgmt;
2652 sess->phone = phone;
2653 sess->arg1 = 0;
2654 sess->arg2 = 0;
2655 sess->arg3 = 0;
2656
2657 fibril_mutex_initialize(&sess->remote_state_mtx);
2658 sess->remote_state_data = NULL;
2659
2660 list_initialize(&sess->exch_list);
2661 fibril_mutex_initialize(&sess->mutex);
2662 atomic_set(&sess->refcnt, 0);
2663
2664 return sess;
2665}
2666
2667int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
2668 sysarg_t arg3, async_exch_t *other_exch)
2669{
2670 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
2671 arg1, arg2, arg3, 0, other_exch->phone);
2672}
2673
2674bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
2675 sysarg_t *arg2, sysarg_t *arg3)
2676{
2677 assert(callid);
2678
2679 ipc_call_t call;
2680 *callid = async_get_call(&call);
2681
2682 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
2683 return false;
2684
2685 if (arg1)
2686 *arg1 = IPC_GET_ARG1(call);
2687 if (arg2)
2688 *arg2 = IPC_GET_ARG2(call);
2689 if (arg3)
2690 *arg3 = IPC_GET_ARG3(call);
2691
2692 return true;
2693}
2694
2695int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
2696{
2697 return ipc_answer_1(callid, EOK, other_exch->phone);
2698}
2699
2700/** Lock and get session remote state
2701 *
2702 * Lock and get the local replica of the remote state
2703 * in stateful sessions. The call should be paired
2704 * with async_remote_state_release*().
2705 *
2706 * @param[in] sess Stateful session.
2707 *
2708 * @return Local replica of the remote state.
2709 *
2710 */
2711void *async_remote_state_acquire(async_sess_t *sess)
2712{
2713 fibril_mutex_lock(&sess->remote_state_mtx);
2714 return sess->remote_state_data;
2715}
2716
2717/** Update the session remote state
2718 *
2719 * Update the local replica of the remote state
2720 * in stateful sessions. The remote state must
2721 * be already locked.
2722 *
2723 * @param[in] sess Stateful session.
2724 * @param[in] state New local replica of the remote state.
2725 *
2726 */
2727void async_remote_state_update(async_sess_t *sess, void *state)
2728{
2729 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2730 sess->remote_state_data = state;
2731}
2732
2733/** Release the session remote state
2734 *
2735 * Unlock the local replica of the remote state
2736 * in stateful sessions.
2737 *
2738 * @param[in] sess Stateful session.
2739 *
2740 */
2741void async_remote_state_release(async_sess_t *sess)
2742{
2743 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2744
2745 fibril_mutex_unlock(&sess->remote_state_mtx);
2746}
2747
2748/** Release the session remote state and end an exchange
2749 *
2750 * Unlock the local replica of the remote state
2751 * in stateful sessions. This is convenience function
2752 * which gets the session pointer from the exchange
2753 * and also ends the exchange.
2754 *
2755 * @param[in] exch Stateful session's exchange.
2756 *
2757 */
2758void async_remote_state_release_exchange(async_exch_t *exch)
2759{
2760 if (exch == NULL)
2761 return;
2762
2763 async_sess_t *sess = exch->sess;
2764 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2765
2766 async_exchange_end(exch);
2767 fibril_mutex_unlock(&sess->remote_state_mtx);
2768}
2769
2770/** @}
2771 */
Note: See TracBrowser for help on using the repository browser.