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

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

remove the obsolete async API

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