source: mainline/uspace/lib/c/generic/async.c@ 97b199b1

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

Use NULL instead of 0 as a hash_table_ops_t member initializer.

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