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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b72efe8 was b72efe8, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Separate list_t typedef from link_t (user-space part).

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