source: mainline/uspace/lib/c/generic/async.c@ 8869f7b

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

async framework: add support for in-band IPC_M_CONNECT_TO_ME processing

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