source: mainline/uspace/lib/c/generic/async.c@ a1347a7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a1347a7 was f302586, checked in by Martin Decky <martin@…>, 13 years ago

make sure the client_connection is explicitly set at most once
it is not a mutable variable, it is a weak symbol

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