source: mainline/uspace/lib/c/generic/async.c@ 2654afb

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2654afb was 7f9d97f3, checked in by Jakub Jermar <jakub@…>, 10 years ago

Make gettimeofday() return the actual microseconds.

Enhance struct tm to also have a field to hold microseconds and make
sure that this information propagates from the RTC driver.

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