source: mainline/uspace/lib/c/generic/async.c@ 57dea62

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

coding style (no change in functionality)

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