source: mainline/uspace/lib/c/generic/async.c@ 48bd6f4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 48bd6f4 was 192565b, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

Merge mainline changes.

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