source: mainline/uspace/lib/c/generic/async.c@ 45cbcaf4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 45cbcaf4 was 45cbcaf4, checked in by Maurizio Lombardi <m.lombardi85@…>, 13 years ago

avoid deadlocks by replacing gettimeofday() with getuptime() in lib/async.c and lib/fibril_sync.c

  • 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 getuptime(&conn->wdata.to_event.expires);
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_CLONE_ESTABLISH:
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 getuptime(&tv);
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 getuptime(&tv);
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 getuptime(&msg->wdata.to_event.expires);
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->dataptr = NULL;
1394 msg->forget = true;
1395 }
1396 futex_up(&async_futex);
1397}
1398
1399/** Wait for specified time.
1400 *
1401 * The current fibril is suspended but the thread continues to execute.
1402 *
1403 * @param timeout Duration of the wait in microseconds.
1404 *
1405 */
1406void async_usleep(suseconds_t timeout)
1407{
1408 amsg_t *msg = amsg_create();
1409 if (!msg)
1410 return;
1411
1412 msg->wdata.fid = fibril_get_id();
1413
1414 getuptime(&msg->wdata.to_event.expires);
1415 tv_add(&msg->wdata.to_event.expires, timeout);
1416
1417 futex_down(&async_futex);
1418
1419 async_insert_timeout(&msg->wdata);
1420
1421 /* Leave the async_futex locked when entering this function */
1422 fibril_switch(FIBRIL_TO_MANAGER);
1423
1424 /* Futex is up automatically after fibril_switch() */
1425
1426 amsg_destroy(msg);
1427}
1428
1429/** Pseudo-synchronous message sending - fast version.
1430 *
1431 * Send message asynchronously and return only after the reply arrives.
1432 *
1433 * This function can only transfer 4 register payload arguments. For
1434 * transferring more arguments, see the slower async_req_slow().
1435 *
1436 * @param exch Exchange for sending the message.
1437 * @param imethod Interface and method of the call.
1438 * @param arg1 Service-defined payload argument.
1439 * @param arg2 Service-defined payload argument.
1440 * @param arg3 Service-defined payload argument.
1441 * @param arg4 Service-defined payload argument.
1442 * @param r1 If non-NULL, storage for the 1st reply argument.
1443 * @param r2 If non-NULL, storage for the 2nd reply argument.
1444 * @param r3 If non-NULL, storage for the 3rd reply argument.
1445 * @param r4 If non-NULL, storage for the 4th reply argument.
1446 * @param r5 If non-NULL, storage for the 5th reply argument.
1447 *
1448 * @return Return code of the reply or a negative error code.
1449 *
1450 */
1451sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1452 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1453 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1454{
1455 if (exch == NULL)
1456 return ENOENT;
1457
1458 ipc_call_t result;
1459 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
1460 &result);
1461
1462 sysarg_t rc;
1463 async_wait_for(aid, &rc);
1464
1465 if (r1)
1466 *r1 = IPC_GET_ARG1(result);
1467
1468 if (r2)
1469 *r2 = IPC_GET_ARG2(result);
1470
1471 if (r3)
1472 *r3 = IPC_GET_ARG3(result);
1473
1474 if (r4)
1475 *r4 = IPC_GET_ARG4(result);
1476
1477 if (r5)
1478 *r5 = IPC_GET_ARG5(result);
1479
1480 return rc;
1481}
1482
1483/** Pseudo-synchronous message sending - slow version.
1484 *
1485 * Send message asynchronously and return only after the reply arrives.
1486 *
1487 * @param exch Exchange for sending the message.
1488 * @param imethod Interface and method of the call.
1489 * @param arg1 Service-defined payload argument.
1490 * @param arg2 Service-defined payload argument.
1491 * @param arg3 Service-defined payload argument.
1492 * @param arg4 Service-defined payload argument.
1493 * @param arg5 Service-defined payload argument.
1494 * @param r1 If non-NULL, storage for the 1st reply argument.
1495 * @param r2 If non-NULL, storage for the 2nd reply argument.
1496 * @param r3 If non-NULL, storage for the 3rd reply argument.
1497 * @param r4 If non-NULL, storage for the 4th reply argument.
1498 * @param r5 If non-NULL, storage for the 5th reply argument.
1499 *
1500 * @return Return code of the reply or a negative error code.
1501 *
1502 */
1503sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1504 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1505 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1506{
1507 if (exch == NULL)
1508 return ENOENT;
1509
1510 ipc_call_t result;
1511 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
1512 &result);
1513
1514 sysarg_t rc;
1515 async_wait_for(aid, &rc);
1516
1517 if (r1)
1518 *r1 = IPC_GET_ARG1(result);
1519
1520 if (r2)
1521 *r2 = IPC_GET_ARG2(result);
1522
1523 if (r3)
1524 *r3 = IPC_GET_ARG3(result);
1525
1526 if (r4)
1527 *r4 = IPC_GET_ARG4(result);
1528
1529 if (r5)
1530 *r5 = IPC_GET_ARG5(result);
1531
1532 return rc;
1533}
1534
1535void async_msg_0(async_exch_t *exch, sysarg_t imethod)
1536{
1537 if (exch != NULL)
1538 ipc_call_async_0(exch->phone, imethod, NULL, NULL, true);
1539}
1540
1541void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
1542{
1543 if (exch != NULL)
1544 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL, true);
1545}
1546
1547void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1548 sysarg_t arg2)
1549{
1550 if (exch != NULL)
1551 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL,
1552 true);
1553}
1554
1555void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1556 sysarg_t arg2, sysarg_t arg3)
1557{
1558 if (exch != NULL)
1559 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
1560 NULL, true);
1561}
1562
1563void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1564 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1565{
1566 if (exch != NULL)
1567 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
1568 NULL, NULL, true);
1569}
1570
1571void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1572 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1573{
1574 if (exch != NULL)
1575 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
1576 arg5, NULL, NULL, true);
1577}
1578
1579sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
1580{
1581 return ipc_answer_0(callid, retval);
1582}
1583
1584sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
1585{
1586 return ipc_answer_1(callid, retval, arg1);
1587}
1588
1589sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1590 sysarg_t arg2)
1591{
1592 return ipc_answer_2(callid, retval, arg1, arg2);
1593}
1594
1595sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1596 sysarg_t arg2, sysarg_t arg3)
1597{
1598 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
1599}
1600
1601sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1602 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1603{
1604 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
1605}
1606
1607sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1608 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1609{
1610 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
1611}
1612
1613int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
1614 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
1615{
1616 if (exch == NULL)
1617 return ENOENT;
1618
1619 return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
1620}
1621
1622int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
1623 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
1624 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
1625{
1626 if (exch == NULL)
1627 return ENOENT;
1628
1629 return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
1630 arg4, arg5, mode);
1631}
1632
1633/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1634 *
1635 * Ask through phone for a new connection to some service.
1636 *
1637 * @param exch Exchange for sending the message.
1638 * @param arg1 User defined argument.
1639 * @param arg2 User defined argument.
1640 * @param arg3 User defined argument.
1641 * @param client_receiver Connection handing routine.
1642 *
1643 * @return Zero on success or a negative error code.
1644 *
1645 */
1646int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
1647 sysarg_t arg3, async_client_conn_t client_receiver, void *carg)
1648{
1649 if (exch == NULL)
1650 return ENOENT;
1651
1652 sysarg_t phone_hash;
1653 sysarg_t rc;
1654
1655 aid_t req;
1656 ipc_call_t answer;
1657 req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1658 &answer);
1659 async_wait_for(req, &rc);
1660 if (rc != EOK)
1661 return (int) rc;
1662
1663 phone_hash = IPC_GET_ARG5(answer);
1664
1665 if (client_receiver != NULL)
1666 async_new_connection(answer.in_task_id, phone_hash, 0, NULL,
1667 client_receiver, carg);
1668
1669 return EOK;
1670}
1671
1672/** Wrapper for making IPC_M_CLONE_ESTABLISH calls using the async framework.
1673 *
1674 * Ask for a cloned connection to some service.
1675 *
1676 * @param mgmt Exchange management style.
1677 * @param exch Exchange for sending the message.
1678 *
1679 * @return New session on success or NULL on error.
1680 *
1681 */
1682async_sess_t *async_clone_establish(exch_mgmt_t mgmt, async_exch_t *exch)
1683{
1684 if (exch == NULL) {
1685 errno = ENOENT;
1686 return NULL;
1687 }
1688
1689 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1690 if (sess == NULL) {
1691 errno = ENOMEM;
1692 return NULL;
1693 }
1694
1695 ipc_call_t result;
1696
1697 amsg_t *msg = amsg_create();
1698 if (!msg) {
1699 free(sess);
1700 errno = ENOMEM;
1701 return NULL;
1702 }
1703
1704 msg->dataptr = &result;
1705 msg->wdata.active = true;
1706
1707 ipc_call_async_0(exch->phone, IPC_M_CLONE_ESTABLISH, msg,
1708 reply_received, true);
1709
1710 sysarg_t rc;
1711 async_wait_for((aid_t) msg, &rc);
1712
1713 if (rc != EOK) {
1714 errno = rc;
1715 free(sess);
1716 return NULL;
1717 }
1718
1719 int phone = (int) IPC_GET_ARG5(result);
1720
1721 if (phone < 0) {
1722 errno = phone;
1723 free(sess);
1724 return NULL;
1725 }
1726
1727 sess->mgmt = mgmt;
1728 sess->phone = phone;
1729 sess->arg1 = 0;
1730 sess->arg2 = 0;
1731 sess->arg3 = 0;
1732
1733 fibril_mutex_initialize(&sess->remote_state_mtx);
1734 sess->remote_state_data = NULL;
1735
1736 list_initialize(&sess->exch_list);
1737 fibril_mutex_initialize(&sess->mutex);
1738 atomic_set(&sess->refcnt, 0);
1739
1740 return sess;
1741}
1742
1743static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
1744 sysarg_t arg3, sysarg_t arg4)
1745{
1746 ipc_call_t result;
1747
1748 amsg_t *msg = amsg_create();
1749 if (!msg)
1750 return ENOENT;
1751
1752 msg->dataptr = &result;
1753 msg->wdata.active = true;
1754
1755 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
1756 msg, reply_received, true);
1757
1758 sysarg_t rc;
1759 async_wait_for((aid_t) msg, &rc);
1760
1761 if (rc != EOK)
1762 return rc;
1763
1764 return (int) IPC_GET_ARG5(result);
1765}
1766
1767/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1768 *
1769 * Ask through for a new connection to some service.
1770 *
1771 * @param mgmt Exchange management style.
1772 * @param exch Exchange for sending the message.
1773 * @param arg1 User defined argument.
1774 * @param arg2 User defined argument.
1775 * @param arg3 User defined argument.
1776 *
1777 * @return New session on success or NULL on error.
1778 *
1779 */
1780async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
1781 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1782{
1783 if (exch == NULL) {
1784 errno = ENOENT;
1785 return NULL;
1786 }
1787
1788 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1789 if (sess == NULL) {
1790 errno = ENOMEM;
1791 return NULL;
1792 }
1793
1794 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1795 0);
1796
1797 if (phone < 0) {
1798 errno = phone;
1799 free(sess);
1800 return NULL;
1801 }
1802
1803 sess->mgmt = mgmt;
1804 sess->phone = phone;
1805 sess->arg1 = arg1;
1806 sess->arg2 = arg2;
1807 sess->arg3 = arg3;
1808
1809 fibril_mutex_initialize(&sess->remote_state_mtx);
1810 sess->remote_state_data = NULL;
1811
1812 list_initialize(&sess->exch_list);
1813 fibril_mutex_initialize(&sess->mutex);
1814 atomic_set(&sess->refcnt, 0);
1815
1816 return sess;
1817}
1818
1819/** Set arguments for new connections.
1820 *
1821 * FIXME This is an ugly hack to work around the problem that parallel
1822 * exchanges are implemented using parallel connections. When we create
1823 * a callback session, the framework does not know arguments for the new
1824 * connections.
1825 *
1826 * The proper solution seems to be to implement parallel exchanges using
1827 * tagging.
1828 */
1829void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
1830 sysarg_t arg3)
1831{
1832 sess->arg1 = arg1;
1833 sess->arg2 = arg2;
1834 sess->arg3 = arg3;
1835}
1836
1837/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1838 *
1839 * Ask through phone for a new connection to some service and block until
1840 * success.
1841 *
1842 * @param mgmt Exchange management style.
1843 * @param exch Exchange for sending the message.
1844 * @param arg1 User defined argument.
1845 * @param arg2 User defined argument.
1846 * @param arg3 User defined argument.
1847 *
1848 * @return New session on success or NULL on error.
1849 *
1850 */
1851async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
1852 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1853{
1854 if (exch == NULL) {
1855 errno = ENOENT;
1856 return NULL;
1857 }
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 = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1866 IPC_FLAG_BLOCKING);
1867
1868 if (phone < 0) {
1869 errno = phone;
1870 free(sess);
1871 return NULL;
1872 }
1873
1874 sess->mgmt = mgmt;
1875 sess->phone = phone;
1876 sess->arg1 = arg1;
1877 sess->arg2 = arg2;
1878 sess->arg3 = arg3;
1879
1880 fibril_mutex_initialize(&sess->remote_state_mtx);
1881 sess->remote_state_data = NULL;
1882
1883 list_initialize(&sess->exch_list);
1884 fibril_mutex_initialize(&sess->mutex);
1885 atomic_set(&sess->refcnt, 0);
1886
1887 return sess;
1888}
1889
1890/** Connect to a task specified by id.
1891 *
1892 */
1893async_sess_t *async_connect_kbox(task_id_t id)
1894{
1895 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1896 if (sess == NULL) {
1897 errno = ENOMEM;
1898 return NULL;
1899 }
1900
1901 int phone = ipc_connect_kbox(id);
1902 if (phone < 0) {
1903 errno = phone;
1904 free(sess);
1905 return NULL;
1906 }
1907
1908 sess->mgmt = EXCHANGE_ATOMIC;
1909 sess->phone = phone;
1910 sess->arg1 = 0;
1911 sess->arg2 = 0;
1912 sess->arg3 = 0;
1913
1914 fibril_mutex_initialize(&sess->remote_state_mtx);
1915 sess->remote_state_data = NULL;
1916
1917 list_initialize(&sess->exch_list);
1918 fibril_mutex_initialize(&sess->mutex);
1919 atomic_set(&sess->refcnt, 0);
1920
1921 return sess;
1922}
1923
1924static int async_hangup_internal(int phone)
1925{
1926 return ipc_hangup(phone);
1927}
1928
1929/** Wrapper for ipc_hangup.
1930 *
1931 * @param sess Session to hung up.
1932 *
1933 * @return Zero on success or a negative error code.
1934 *
1935 */
1936int async_hangup(async_sess_t *sess)
1937{
1938 async_exch_t *exch;
1939
1940 assert(sess);
1941
1942 if (atomic_get(&sess->refcnt) > 0)
1943 return EBUSY;
1944
1945 fibril_mutex_lock(&async_sess_mutex);
1946
1947 int rc = async_hangup_internal(sess->phone);
1948
1949 while (!list_empty(&sess->exch_list)) {
1950 exch = (async_exch_t *)
1951 list_get_instance(list_first(&sess->exch_list),
1952 async_exch_t, sess_link);
1953
1954 list_remove(&exch->sess_link);
1955 list_remove(&exch->global_link);
1956 async_hangup_internal(exch->phone);
1957 free(exch);
1958 }
1959
1960 free(sess);
1961
1962 fibril_mutex_unlock(&async_sess_mutex);
1963
1964 return rc;
1965}
1966
1967/** Interrupt one thread of this task from waiting for IPC. */
1968void async_poke(void)
1969{
1970 ipc_poke();
1971}
1972
1973/** Start new exchange in a session.
1974 *
1975 * @param session Session.
1976 *
1977 * @return New exchange or NULL on error.
1978 *
1979 */
1980async_exch_t *async_exchange_begin(async_sess_t *sess)
1981{
1982 if (sess == NULL)
1983 return NULL;
1984
1985 async_exch_t *exch;
1986
1987 fibril_mutex_lock(&async_sess_mutex);
1988
1989 if (!list_empty(&sess->exch_list)) {
1990 /*
1991 * There are inactive exchanges in the session.
1992 */
1993 exch = (async_exch_t *)
1994 list_get_instance(list_first(&sess->exch_list),
1995 async_exch_t, sess_link);
1996
1997 list_remove(&exch->sess_link);
1998 list_remove(&exch->global_link);
1999 } else {
2000 /*
2001 * There are no available exchanges in the session.
2002 */
2003
2004 if ((sess->mgmt == EXCHANGE_ATOMIC) ||
2005 (sess->mgmt == EXCHANGE_SERIALIZE)) {
2006 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2007 if (exch != NULL) {
2008 link_initialize(&exch->sess_link);
2009 link_initialize(&exch->global_link);
2010 exch->sess = sess;
2011 exch->phone = sess->phone;
2012 }
2013 } else { /* EXCHANGE_PARALLEL */
2014 /*
2015 * Make a one-time attempt to connect a new data phone.
2016 */
2017
2018 int phone;
2019
2020retry:
2021 phone = async_connect_me_to_internal(sess->phone, sess->arg1,
2022 sess->arg2, sess->arg3, 0);
2023 if (phone >= 0) {
2024 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2025 if (exch != NULL) {
2026 link_initialize(&exch->sess_link);
2027 link_initialize(&exch->global_link);
2028 exch->sess = sess;
2029 exch->phone = phone;
2030 } else
2031 async_hangup_internal(phone);
2032 } else if (!list_empty(&inactive_exch_list)) {
2033 /*
2034 * We did not manage to connect a new phone. But we
2035 * can try to close some of the currently inactive
2036 * connections in other sessions and try again.
2037 */
2038 exch = (async_exch_t *)
2039 list_get_instance(list_first(&inactive_exch_list),
2040 async_exch_t, global_link);
2041
2042 list_remove(&exch->sess_link);
2043 list_remove(&exch->global_link);
2044 async_hangup_internal(exch->phone);
2045 free(exch);
2046 goto retry;
2047 } else {
2048 /*
2049 * Wait for a phone to become available.
2050 */
2051 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
2052 goto retry;
2053 }
2054 }
2055 }
2056
2057 fibril_mutex_unlock(&async_sess_mutex);
2058
2059 if (exch != NULL) {
2060 atomic_inc(&sess->refcnt);
2061
2062 if (sess->mgmt == EXCHANGE_SERIALIZE)
2063 fibril_mutex_lock(&sess->mutex);
2064 }
2065
2066 return exch;
2067}
2068
2069/** Finish an exchange.
2070 *
2071 * @param exch Exchange to finish.
2072 *
2073 */
2074void async_exchange_end(async_exch_t *exch)
2075{
2076 if (exch == NULL)
2077 return;
2078
2079 async_sess_t *sess = exch->sess;
2080
2081 atomic_dec(&sess->refcnt);
2082
2083 if (sess->mgmt == EXCHANGE_SERIALIZE)
2084 fibril_mutex_unlock(&sess->mutex);
2085
2086 fibril_mutex_lock(&async_sess_mutex);
2087
2088 list_append(&exch->sess_link, &sess->exch_list);
2089 list_append(&exch->global_link, &inactive_exch_list);
2090 fibril_condvar_signal(&avail_phone_cv);
2091
2092 fibril_mutex_unlock(&async_sess_mutex);
2093}
2094
2095/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
2096 *
2097 * @param exch Exchange for sending the message.
2098 * @param size Size of the destination address space area.
2099 * @param arg User defined argument.
2100 * @param flags Storage for the received flags. Can be NULL.
2101 * @param dst Destination address space area base. Cannot be NULL.
2102 *
2103 * @return Zero on success or a negative error code from errno.h.
2104 *
2105 */
2106int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
2107 unsigned int *flags, void **dst)
2108{
2109 if (exch == NULL)
2110 return ENOENT;
2111
2112 sysarg_t _flags = 0;
2113 sysarg_t _dst = (sysarg_t) -1;
2114 int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
2115 arg, NULL, &_flags, NULL, &_dst);
2116
2117 if (flags)
2118 *flags = (unsigned int) _flags;
2119
2120 *dst = (void *) _dst;
2121 return res;
2122}
2123
2124/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
2125 *
2126 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
2127 * calls so that the user doesn't have to remember the meaning of each IPC
2128 * argument.
2129 *
2130 * So far, this wrapper is to be used from within a connection fibril.
2131 *
2132 * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
2133 * @param size Destination address space area size.
2134 *
2135 * @return True on success, false on failure.
2136 *
2137 */
2138bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
2139{
2140 assert(callid);
2141 assert(size);
2142
2143 ipc_call_t data;
2144 *callid = async_get_call(&data);
2145
2146 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
2147 return false;
2148
2149 *size = (size_t) IPC_GET_ARG1(data);
2150 return true;
2151}
2152
2153/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
2154 *
2155 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
2156 * calls so that the user doesn't have to remember the meaning of each IPC
2157 * argument.
2158 *
2159 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2160 * @param src Source address space base.
2161 * @param flags Flags to be used for sharing. Bits can be only cleared.
2162 *
2163 * @return Zero on success or a value from @ref errno.h on failure.
2164 *
2165 */
2166int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
2167{
2168 return ipc_share_in_finalize(callid, src, flags);
2169}
2170
2171/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
2172 *
2173 * @param exch Exchange for sending the message.
2174 * @param src Source address space area base address.
2175 * @param flags Flags to be used for sharing. Bits can be only cleared.
2176 *
2177 * @return Zero on success or a negative error code from errno.h.
2178 *
2179 */
2180int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
2181{
2182 if (exch == NULL)
2183 return ENOENT;
2184
2185 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
2186 (sysarg_t) flags);
2187}
2188
2189/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
2190 *
2191 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
2192 * calls so that the user doesn't have to remember the meaning of each IPC
2193 * argument.
2194 *
2195 * So far, this wrapper is to be used from within a connection fibril.
2196 *
2197 * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
2198 * @param size Storage for the source address space area size.
2199 * @param flags Storage for the sharing flags.
2200 *
2201 * @return True on success, false on failure.
2202 *
2203 */
2204bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
2205{
2206 assert(callid);
2207 assert(size);
2208 assert(flags);
2209
2210 ipc_call_t data;
2211 *callid = async_get_call(&data);
2212
2213 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
2214 return false;
2215
2216 *size = (size_t) IPC_GET_ARG2(data);
2217 *flags = (unsigned int) IPC_GET_ARG3(data);
2218 return true;
2219}
2220
2221/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2222 *
2223 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2224 * calls so that the user doesn't have to remember the meaning of each IPC
2225 * argument.
2226 *
2227 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2228 * @param dst Destination address space area base address.
2229 *
2230 * @return Zero on success or a value from @ref errno.h on failure.
2231 *
2232 */
2233int async_share_out_finalize(ipc_callid_t callid, void **dst)
2234{
2235 return ipc_share_out_finalize(callid, dst);
2236}
2237
2238/** Start IPC_M_DATA_READ using the async framework.
2239 *
2240 * @param exch Exchange for sending the message.
2241 * @param dst Address of the beginning of the destination buffer.
2242 * @param size Size of the destination buffer (in bytes).
2243 * @param dataptr Storage of call data (arg 2 holds actual data size).
2244 *
2245 * @return Hash of the sent message or 0 on error.
2246 *
2247 */
2248aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2249 ipc_call_t *dataptr)
2250{
2251 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2252 (sysarg_t) size, dataptr);
2253}
2254
2255/** Wrapper for IPC_M_DATA_READ calls using the async framework.
2256 *
2257 * @param exch Exchange for sending the message.
2258 * @param dst Address of the beginning of the destination buffer.
2259 * @param size Size of the destination buffer.
2260 *
2261 * @return Zero on success or a negative error code from errno.h.
2262 *
2263 */
2264int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
2265{
2266 if (exch == NULL)
2267 return ENOENT;
2268
2269 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2270 (sysarg_t) size);
2271}
2272
2273/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2274 *
2275 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2276 * calls so that the user doesn't have to remember the meaning of each IPC
2277 * argument.
2278 *
2279 * So far, this wrapper is to be used from within a connection fibril.
2280 *
2281 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2282 * @param size Storage for the maximum size. Can be NULL.
2283 *
2284 * @return True on success, false on failure.
2285 *
2286 */
2287bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
2288{
2289 assert(callid);
2290
2291 ipc_call_t data;
2292 *callid = async_get_call(&data);
2293
2294 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
2295 return false;
2296
2297 if (size)
2298 *size = (size_t) IPC_GET_ARG2(data);
2299
2300 return true;
2301}
2302
2303/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2304 *
2305 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2306 * calls so that the user doesn't have to remember the meaning of each IPC
2307 * argument.
2308 *
2309 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2310 * @param src Source address for the IPC_M_DATA_READ call.
2311 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2312 * the maximum size announced by the sender.
2313 *
2314 * @return Zero on success or a value from @ref errno.h on failure.
2315 *
2316 */
2317int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
2318{
2319 return ipc_data_read_finalize(callid, src, size);
2320}
2321
2322/** Wrapper for forwarding any read request
2323 *
2324 */
2325int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2326 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2327 ipc_call_t *dataptr)
2328{
2329 if (exch == NULL)
2330 return ENOENT;
2331
2332 ipc_callid_t callid;
2333 if (!async_data_read_receive(&callid, NULL)) {
2334 ipc_answer_0(callid, EINVAL);
2335 return EINVAL;
2336 }
2337
2338 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2339 dataptr);
2340 if (msg == 0) {
2341 ipc_answer_0(callid, EINVAL);
2342 return EINVAL;
2343 }
2344
2345 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
2346 IPC_FF_ROUTE_FROM_ME);
2347 if (retval != EOK) {
2348 async_forget(msg);
2349 ipc_answer_0(callid, retval);
2350 return retval;
2351 }
2352
2353 sysarg_t rc;
2354 async_wait_for(msg, &rc);
2355
2356 return (int) rc;
2357}
2358
2359/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
2360 *
2361 * @param exch Exchange for sending the message.
2362 * @param src Address of the beginning of the source buffer.
2363 * @param size Size of the source buffer.
2364 *
2365 * @return Zero on success or a negative error code from errno.h.
2366 *
2367 */
2368int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
2369{
2370 if (exch == NULL)
2371 return ENOENT;
2372
2373 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2374 (sysarg_t) size);
2375}
2376
2377/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2378 *
2379 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2380 * calls so that the user doesn't have to remember the meaning of each IPC
2381 * argument.
2382 *
2383 * So far, this wrapper is to be used from within a connection fibril.
2384 *
2385 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2386 * @param size Storage for the suggested size. May be NULL.
2387 *
2388 * @return True on success, false on failure.
2389 *
2390 */
2391bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
2392{
2393 assert(callid);
2394
2395 ipc_call_t data;
2396 *callid = async_get_call(&data);
2397
2398 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
2399 return false;
2400
2401 if (size)
2402 *size = (size_t) IPC_GET_ARG2(data);
2403
2404 return true;
2405}
2406
2407/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2408 *
2409 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2410 * calls so that the user doesn't have to remember the meaning of each IPC
2411 * argument.
2412 *
2413 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2414 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2415 * @param size Final size for the IPC_M_DATA_WRITE call.
2416 *
2417 * @return Zero on success or a value from @ref errno.h on failure.
2418 *
2419 */
2420int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
2421{
2422 return ipc_data_write_finalize(callid, dst, size);
2423}
2424
2425/** Wrapper for receiving binary data or strings
2426 *
2427 * This wrapper only makes it more comfortable to use async_data_write_*
2428 * functions to receive binary data or strings.
2429 *
2430 * @param data Pointer to data pointer (which should be later disposed
2431 * by free()). If the operation fails, the pointer is not
2432 * touched.
2433 * @param nullterm If true then the received data is always zero terminated.
2434 * This also causes to allocate one extra byte beyond the
2435 * raw transmitted data.
2436 * @param min_size Minimum size (in bytes) of the data to receive.
2437 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2438 * no limit.
2439 * @param granulariy If non-zero then the size of the received data has to
2440 * be divisible by this value.
2441 * @param received If not NULL, the size of the received data is stored here.
2442 *
2443 * @return Zero on success or a value from @ref errno.h on failure.
2444 *
2445 */
2446int async_data_write_accept(void **data, const bool nullterm,
2447 const size_t min_size, const size_t max_size, const size_t granularity,
2448 size_t *received)
2449{
2450 assert(data);
2451
2452 ipc_callid_t callid;
2453 size_t size;
2454 if (!async_data_write_receive(&callid, &size)) {
2455 ipc_answer_0(callid, EINVAL);
2456 return EINVAL;
2457 }
2458
2459 if (size < min_size) {
2460 ipc_answer_0(callid, EINVAL);
2461 return EINVAL;
2462 }
2463
2464 if ((max_size > 0) && (size > max_size)) {
2465 ipc_answer_0(callid, EINVAL);
2466 return EINVAL;
2467 }
2468
2469 if ((granularity > 0) && ((size % granularity) != 0)) {
2470 ipc_answer_0(callid, EINVAL);
2471 return EINVAL;
2472 }
2473
2474 void *_data;
2475
2476 if (nullterm)
2477 _data = malloc(size + 1);
2478 else
2479 _data = malloc(size);
2480
2481 if (_data == NULL) {
2482 ipc_answer_0(callid, ENOMEM);
2483 return ENOMEM;
2484 }
2485
2486 int rc = async_data_write_finalize(callid, _data, size);
2487 if (rc != EOK) {
2488 free(_data);
2489 return rc;
2490 }
2491
2492 if (nullterm)
2493 ((char *) _data)[size] = 0;
2494
2495 *data = _data;
2496 if (received != NULL)
2497 *received = size;
2498
2499 return EOK;
2500}
2501
2502/** Wrapper for voiding any data that is about to be received
2503 *
2504 * This wrapper can be used to void any pending data
2505 *
2506 * @param retval Error value from @ref errno.h to be returned to the caller.
2507 *
2508 */
2509void async_data_write_void(sysarg_t retval)
2510{
2511 ipc_callid_t callid;
2512 async_data_write_receive(&callid, NULL);
2513 ipc_answer_0(callid, retval);
2514}
2515
2516/** Wrapper for forwarding any data that is about to be received
2517 *
2518 */
2519int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
2520 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2521 ipc_call_t *dataptr)
2522{
2523 if (exch == NULL)
2524 return ENOENT;
2525
2526 ipc_callid_t callid;
2527 if (!async_data_write_receive(&callid, NULL)) {
2528 ipc_answer_0(callid, EINVAL);
2529 return EINVAL;
2530 }
2531
2532 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2533 dataptr);
2534 if (msg == 0) {
2535 ipc_answer_0(callid, EINVAL);
2536 return EINVAL;
2537 }
2538
2539 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
2540 IPC_FF_ROUTE_FROM_ME);
2541 if (retval != EOK) {
2542 async_forget(msg);
2543 ipc_answer_0(callid, retval);
2544 return retval;
2545 }
2546
2547 sysarg_t rc;
2548 async_wait_for(msg, &rc);
2549
2550 return (int) rc;
2551}
2552
2553/** Wrapper for sending an exchange over different exchange for cloning
2554 *
2555 * @param exch Exchange to be used for sending.
2556 * @param clone_exch Exchange to be cloned.
2557 *
2558 */
2559int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
2560{
2561 return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
2562}
2563
2564/** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
2565 *
2566 * If the current call is IPC_M_CONNECTION_CLONE then a new
2567 * async session is created for the accepted phone.
2568 *
2569 * @param mgmt Exchange management style.
2570 *
2571 * @return New async session or NULL on failure.
2572 *
2573 */
2574async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
2575{
2576 /* Accept the phone */
2577 ipc_call_t call;
2578 ipc_callid_t callid = async_get_call(&call);
2579 int phone = (int) IPC_GET_ARG1(call);
2580
2581 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
2582 (phone < 0)) {
2583 async_answer_0(callid, EINVAL);
2584 return NULL;
2585 }
2586
2587 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2588 if (sess == NULL) {
2589 async_answer_0(callid, ENOMEM);
2590 return NULL;
2591 }
2592
2593 sess->mgmt = mgmt;
2594 sess->phone = phone;
2595 sess->arg1 = 0;
2596 sess->arg2 = 0;
2597 sess->arg3 = 0;
2598
2599 fibril_mutex_initialize(&sess->remote_state_mtx);
2600 sess->remote_state_data = NULL;
2601
2602 list_initialize(&sess->exch_list);
2603 fibril_mutex_initialize(&sess->mutex);
2604 atomic_set(&sess->refcnt, 0);
2605
2606 /* Acknowledge the cloned phone */
2607 async_answer_0(callid, EOK);
2608
2609 return sess;
2610}
2611
2612/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2613 *
2614 * If the current call is IPC_M_CONNECT_TO_ME then a new
2615 * async session is created for the accepted phone.
2616 *
2617 * @param mgmt Exchange management style.
2618 *
2619 * @return New async session.
2620 * @return NULL on failure.
2621 *
2622 */
2623async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
2624{
2625 /* Accept the phone */
2626 ipc_call_t call;
2627 ipc_callid_t callid = async_get_call(&call);
2628 int phone = (int) IPC_GET_ARG5(call);
2629
2630 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
2631 (phone < 0)) {
2632 async_answer_0(callid, EINVAL);
2633 return NULL;
2634 }
2635
2636 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2637 if (sess == NULL) {
2638 async_answer_0(callid, ENOMEM);
2639 return NULL;
2640 }
2641
2642 sess->mgmt = mgmt;
2643 sess->phone = phone;
2644 sess->arg1 = 0;
2645 sess->arg2 = 0;
2646 sess->arg3 = 0;
2647
2648 fibril_mutex_initialize(&sess->remote_state_mtx);
2649 sess->remote_state_data = NULL;
2650
2651 list_initialize(&sess->exch_list);
2652 fibril_mutex_initialize(&sess->mutex);
2653 atomic_set(&sess->refcnt, 0);
2654
2655 /* Acknowledge the connected phone */
2656 async_answer_0(callid, EOK);
2657
2658 return sess;
2659}
2660
2661/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2662 *
2663 * If the call is IPC_M_CONNECT_TO_ME then a new
2664 * async session is created. However, the phone is
2665 * not accepted automatically.
2666 *
2667 * @param mgmt Exchange management style.
2668 * @param call Call data.
2669 *
2670 * @return New async session.
2671 * @return NULL on failure.
2672 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
2673 *
2674 */
2675async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
2676{
2677 int phone = (int) IPC_GET_ARG5(*call);
2678
2679 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
2680 (phone < 0))
2681 return NULL;
2682
2683 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2684 if (sess == NULL)
2685 return NULL;
2686
2687 sess->mgmt = mgmt;
2688 sess->phone = phone;
2689 sess->arg1 = 0;
2690 sess->arg2 = 0;
2691 sess->arg3 = 0;
2692
2693 fibril_mutex_initialize(&sess->remote_state_mtx);
2694 sess->remote_state_data = NULL;
2695
2696 list_initialize(&sess->exch_list);
2697 fibril_mutex_initialize(&sess->mutex);
2698 atomic_set(&sess->refcnt, 0);
2699
2700 return sess;
2701}
2702
2703int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
2704 sysarg_t arg3, async_exch_t *other_exch)
2705{
2706 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
2707 arg1, arg2, arg3, 0, other_exch->phone);
2708}
2709
2710bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
2711 sysarg_t *arg2, sysarg_t *arg3)
2712{
2713 assert(callid);
2714
2715 ipc_call_t call;
2716 *callid = async_get_call(&call);
2717
2718 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
2719 return false;
2720
2721 if (arg1)
2722 *arg1 = IPC_GET_ARG1(call);
2723 if (arg2)
2724 *arg2 = IPC_GET_ARG2(call);
2725 if (arg3)
2726 *arg3 = IPC_GET_ARG3(call);
2727
2728 return true;
2729}
2730
2731int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
2732{
2733 return ipc_answer_1(callid, EOK, other_exch->phone);
2734}
2735
2736/** Lock and get session remote state
2737 *
2738 * Lock and get the local replica of the remote state
2739 * in stateful sessions. The call should be paired
2740 * with async_remote_state_release*().
2741 *
2742 * @param[in] sess Stateful session.
2743 *
2744 * @return Local replica of the remote state.
2745 *
2746 */
2747void *async_remote_state_acquire(async_sess_t *sess)
2748{
2749 fibril_mutex_lock(&sess->remote_state_mtx);
2750 return sess->remote_state_data;
2751}
2752
2753/** Update the session remote state
2754 *
2755 * Update the local replica of the remote state
2756 * in stateful sessions. The remote state must
2757 * be already locked.
2758 *
2759 * @param[in] sess Stateful session.
2760 * @param[in] state New local replica of the remote state.
2761 *
2762 */
2763void async_remote_state_update(async_sess_t *sess, void *state)
2764{
2765 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2766 sess->remote_state_data = state;
2767}
2768
2769/** Release the session remote state
2770 *
2771 * Unlock the local replica of the remote state
2772 * in stateful sessions.
2773 *
2774 * @param[in] sess Stateful session.
2775 *
2776 */
2777void async_remote_state_release(async_sess_t *sess)
2778{
2779 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2780
2781 fibril_mutex_unlock(&sess->remote_state_mtx);
2782}
2783
2784/** Release the session remote state and end an exchange
2785 *
2786 * Unlock the local replica of the remote state
2787 * in stateful sessions. This is convenience function
2788 * which gets the session pointer from the exchange
2789 * and also ends the exchange.
2790 *
2791 * @param[in] exch Stateful session's exchange.
2792 *
2793 */
2794void async_remote_state_release_exchange(async_exch_t *exch)
2795{
2796 if (exch == NULL)
2797 return;
2798
2799 async_sess_t *sess = exch->sess;
2800 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2801
2802 async_exchange_end(exch);
2803 fibril_mutex_unlock(&sess->remote_state_mtx);
2804}
2805
2806/** @}
2807 */
Note: See TracBrowser for help on using the repository browser.