source: mainline/uspace/lib/c/generic/async.c@ 64d2b10

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

libc: do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 47.5 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 synchronization problems.
44 *
45 * Default semantics:
46 * - async_send_*(): Send asynchronously. If the kernel refuses to send
47 * more messages, [ try to get responses from kernel, if
48 * nothing found, might try synchronous ]
49 *
50 * Example of use (pseudo C):
51 *
52 * 1) Multithreaded client application
53 *
54 * fibril_create(fibril1, ...);
55 * fibril_create(fibril2, ...);
56 * ...
57 *
58 * int fibril1(void *arg)
59 * {
60 * conn = async_connect_me_to();
61 * c1 = async_send(conn);
62 * c2 = async_send(conn);
63 * async_wait_for(c1);
64 * async_wait_for(c2);
65 * ...
66 * }
67 *
68 *
69 * 2) Multithreaded server application
70 *
71 * main()
72 * {
73 * async_manager();
74 * }
75 *
76 * my_client_connection(icallid, *icall)
77 * {
78 * if (want_refuse) {
79 * async_answer_0(icallid, ELIMIT);
80 * return;
81 * }
82 * async_answer_0(icallid, EOK);
83 *
84 * callid = async_get_call(&call);
85 * somehow_handle_the_call(callid, call);
86 * async_answer_2(callid, 1, 2, 3);
87 *
88 * callid = async_get_call(&call);
89 * ...
90 * }
91 *
92 */
93
94#define LIBC_ASYNC_C_
95#include <ipc/ipc.h>
96#include <async.h>
97#undef LIBC_ASYNC_C_
98
99#include <futex.h>
100#include <fibril.h>
101#include <stdio.h>
102#include <adt/hash_table.h>
103#include <adt/list.h>
104#include <assert.h>
105#include <errno.h>
106#include <sys/time.h>
107#include <arch/barrier.h>
108#include <bool.h>
109#include "private/async.h"
110
111atomic_t async_futex = FUTEX_INITIALIZER;
112
113/** Number of threads waiting for IPC in the kernel. */
114atomic_t threads_in_ipc_wait = { 0 };
115
116typedef struct {
117 awaiter_t wdata;
118
119 /** If reply was received. */
120 bool done;
121
122 /** Pointer to where the answer data is stored. */
123 ipc_call_t *dataptr;
124
125 sysarg_t retval;
126} amsg_t;
127
128/**
129 * Structures of this type are used to group information about a call and a
130 * message queue link.
131 */
132typedef struct {
133 link_t link;
134 ipc_callid_t callid;
135 ipc_call_t call;
136} msg_t;
137
138typedef struct {
139 sysarg_t in_task_hash;
140 link_t link;
141 int refcnt;
142 void *data;
143} client_t;
144
145typedef struct {
146 awaiter_t wdata;
147
148 /** Hash table link. */
149 link_t link;
150
151 /** Incoming client task hash. */
152 sysarg_t in_task_hash;
153 /** Incoming phone hash. */
154 sysarg_t in_phone_hash;
155
156 /** Link to the client tracking structure. */
157 client_t *client;
158
159 /** Messages that should be delivered to this fibril. */
160 link_t msg_queue;
161
162 /** Identification of the opening call. */
163 ipc_callid_t callid;
164 /** Call data of the opening call. */
165 ipc_call_t call;
166
167 /** Identification of the closing call. */
168 ipc_callid_t close_callid;
169
170 /** Fibril function that will be used to handle the connection. */
171 void (*cfibril)(ipc_callid_t, ipc_call_t *);
172} connection_t;
173
174/** Identifier of the incoming connection handled by the current fibril. */
175fibril_local connection_t *FIBRIL_connection;
176
177static void *default_client_data_constructor(void)
178{
179 return NULL;
180}
181
182static void default_client_data_destructor(void *data)
183{
184}
185
186static async_client_data_ctor_t async_client_data_create =
187 default_client_data_constructor;
188static async_client_data_dtor_t async_client_data_destroy =
189 default_client_data_destructor;
190
191void async_set_client_data_constructor(async_client_data_ctor_t ctor)
192{
193 async_client_data_create = ctor;
194}
195
196void async_set_client_data_destructor(async_client_data_dtor_t dtor)
197{
198 async_client_data_destroy = dtor;
199}
200
201void *async_client_data_get(void)
202{
203 assert(FIBRIL_connection);
204
205 return FIBRIL_connection->client->data;
206}
207
208static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
209static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
210
211/**
212 * Pointer to a fibril function that will be used to handle connections.
213 */
214static async_client_conn_t client_connection = default_client_connection;
215
216/**
217 * Pointer to a fibril function that will be used to handle interrupt
218 * notifications.
219 */
220static async_client_conn_t interrupt_received = default_interrupt_received;
221
222static hash_table_t client_hash_table;
223static hash_table_t conn_hash_table;
224static LIST_INITIALIZE(timeout_list);
225
226#define CLIENT_HASH_TABLE_BUCKETS 32
227#define CONN_HASH_TABLE_BUCKETS 32
228
229static hash_index_t client_hash(unsigned long *key)
230{
231 assert(key);
232 return (((*key) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
233}
234
235static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
236{
237 client_t *cl = hash_table_get_instance(item, client_t, link);
238 return (key[0] == cl->in_task_hash);
239}
240
241static void client_remove(link_t *item)
242{
243}
244
245/** Operations for the client hash table. */
246static hash_table_operations_t client_hash_table_ops = {
247 .hash = client_hash,
248 .compare = client_compare,
249 .remove_callback = client_remove
250};
251
252/** Compute hash into the connection hash table based on the source phone hash.
253 *
254 * @param key Pointer to source phone hash.
255 *
256 * @return Index into the connection hash table.
257 *
258 */
259static hash_index_t conn_hash(unsigned long *key)
260{
261 assert(key);
262 return (((*key) >> 4) % CONN_HASH_TABLE_BUCKETS);
263}
264
265/** Compare hash table item with a key.
266 *
267 * @param key Array containing the source phone hash as the only item.
268 * @param keys Expected 1 but ignored.
269 * @param item Connection hash table item.
270 *
271 * @return True on match, false otherwise.
272 *
273 */
274static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
275{
276 connection_t *hs = hash_table_get_instance(item, connection_t, link);
277 return (key[0] == hs->in_phone_hash);
278}
279
280/** Connection hash table removal callback function.
281 *
282 * This function is called whenever a connection is removed from the connection
283 * hash table.
284 *
285 * @param item Connection hash table item being removed.
286 *
287 */
288static void conn_remove(link_t *item)
289{
290 free(hash_table_get_instance(item, connection_t, link));
291}
292
293
294/** Operations for the connection hash table. */
295static hash_table_operations_t conn_hash_table_ops = {
296 .hash = conn_hash,
297 .compare = conn_compare,
298 .remove_callback = conn_remove
299};
300
301/** Sort in current fibril's timeout request.
302 *
303 * @param wd Wait data of the current fibril.
304 *
305 */
306void async_insert_timeout(awaiter_t *wd)
307{
308 wd->to_event.occurred = false;
309 wd->to_event.inlist = true;
310
311 link_t *tmp = timeout_list.next;
312 while (tmp != &timeout_list) {
313 awaiter_t *cur;
314
315 cur = list_get_instance(tmp, awaiter_t, to_event.link);
316 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
317 break;
318 tmp = tmp->next;
319 }
320
321 list_append(&wd->to_event.link, tmp);
322}
323
324/** Try to route a call to an appropriate connection fibril.
325 *
326 * If the proper connection fibril is found, a message with the call is added to
327 * its message queue. If the fibril was not active, it is activated and all
328 * timeouts are unregistered.
329 *
330 * @param callid Hash of the incoming call.
331 * @param call Data of the incoming call.
332 *
333 * @return False if the call doesn't match any connection.
334 * True if the call was passed to the respective connection fibril.
335 *
336 */
337static bool route_call(ipc_callid_t callid, ipc_call_t *call)
338{
339 futex_down(&async_futex);
340
341 unsigned long key = call->in_phone_hash;
342 link_t *hlp = hash_table_find(&conn_hash_table, &key);
343
344 if (!hlp) {
345 futex_up(&async_futex);
346 return false;
347 }
348
349 connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
350
351 msg_t *msg = malloc(sizeof(*msg));
352 if (!msg) {
353 futex_up(&async_futex);
354 return false;
355 }
356
357 msg->callid = callid;
358 msg->call = *call;
359 list_append(&msg->link, &conn->msg_queue);
360
361 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
362 conn->close_callid = callid;
363
364 /* If the connection fibril is waiting for an event, activate it */
365 if (!conn->wdata.active) {
366
367 /* If in timeout list, remove it */
368 if (conn->wdata.to_event.inlist) {
369 conn->wdata.to_event.inlist = false;
370 list_remove(&conn->wdata.to_event.link);
371 }
372
373 conn->wdata.active = true;
374 fibril_add_ready(conn->wdata.fid);
375 }
376
377 futex_up(&async_futex);
378 return true;
379}
380
381/** Notification fibril.
382 *
383 * When a notification arrives, a fibril with this implementing function is
384 * created. It calls interrupt_received() and does the final cleanup.
385 *
386 * @param arg Message structure pointer.
387 *
388 * @return Always zero.
389 *
390 */
391static int notification_fibril(void *arg)
392{
393 msg_t *msg = (msg_t *) arg;
394 interrupt_received(msg->callid, &msg->call);
395
396 free(msg);
397 return 0;
398}
399
400/** Process interrupt notification.
401 *
402 * A new fibril is created which would process the notification.
403 *
404 * @param callid Hash of the incoming call.
405 * @param call Data of the incoming call.
406 *
407 * @return False if an error occured.
408 * True if the call was passed to the notification fibril.
409 *
410 */
411static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
412{
413 futex_down(&async_futex);
414
415 msg_t *msg = malloc(sizeof(*msg));
416 if (!msg) {
417 futex_up(&async_futex);
418 return false;
419 }
420
421 msg->callid = callid;
422 msg->call = *call;
423
424 fid_t fid = fibril_create(notification_fibril, msg);
425 fibril_add_ready(fid);
426
427 futex_up(&async_futex);
428 return true;
429}
430
431/** Return new incoming message for the current (fibril-local) connection.
432 *
433 * @param call Storage where the incoming call data will be stored.
434 * @param usecs Timeout in microseconds. Zero denotes no timeout.
435 *
436 * @return If no timeout was specified, then a hash of the
437 * incoming call is returned. If a timeout is specified,
438 * then a hash of the incoming call is returned unless
439 * the timeout expires prior to receiving a message. In
440 * that case zero is returned.
441 *
442 */
443ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
444{
445 assert(FIBRIL_connection);
446
447 /* Why doing this?
448 * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
449 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
450 * I would never expect to find so many errors in
451 * a compiler.
452 */
453 connection_t *conn = FIBRIL_connection;
454
455 futex_down(&async_futex);
456
457 if (usecs) {
458 gettimeofday(&conn->wdata.to_event.expires, NULL);
459 tv_add(&conn->wdata.to_event.expires, usecs);
460 } else
461 conn->wdata.to_event.inlist = false;
462
463 /* If nothing in queue, wait until something arrives */
464 while (list_empty(&conn->msg_queue)) {
465 if (conn->close_callid) {
466 /*
467 * Handle the case when the connection was already
468 * closed by the client but the server did not notice
469 * the first IPC_M_PHONE_HUNGUP call and continues to
470 * call async_get_call_timeout(). Repeat
471 * IPC_M_PHONE_HUNGUP until the caller notices.
472 */
473 memset(call, 0, sizeof(ipc_call_t));
474 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
475 futex_up(&async_futex);
476 return conn->close_callid;
477 }
478
479 if (usecs)
480 async_insert_timeout(&conn->wdata);
481
482 conn->wdata.active = false;
483
484 /*
485 * Note: the current fibril will be rescheduled either due to a
486 * timeout or due to an arriving message destined to it. In the
487 * former case, handle_expired_timeouts() and, in the latter
488 * case, route_call() will perform the wakeup.
489 */
490 fibril_switch(FIBRIL_TO_MANAGER);
491
492 /*
493 * Futex is up after getting back from async_manager.
494 * Get it again.
495 */
496 futex_down(&async_futex);
497 if ((usecs) && (conn->wdata.to_event.occurred)
498 && (list_empty(&conn->msg_queue))) {
499 /* If we timed out -> exit */
500 futex_up(&async_futex);
501 return 0;
502 }
503 }
504
505 msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
506 list_remove(&msg->link);
507
508 ipc_callid_t callid = msg->callid;
509 *call = msg->call;
510 free(msg);
511
512 futex_up(&async_futex);
513 return callid;
514}
515
516/** Default fibril function that gets called to handle new connection.
517 *
518 * This function is defined as a weak symbol - to be redefined in user code.
519 *
520 * @param callid Hash of the incoming call.
521 * @param call Data of the incoming call.
522 *
523 */
524static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
525{
526 ipc_answer_0(callid, ENOENT);
527}
528
529/** Default fibril function that gets called to handle interrupt notifications.
530 *
531 * This function is defined as a weak symbol - to be redefined in user code.
532 *
533 * @param callid Hash of the incoming call.
534 * @param call Data of the incoming call.
535 *
536 */
537static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
538{
539}
540
541/** Wrapper for client connection fibril.
542 *
543 * When a new connection arrives, a fibril with this implementing function is
544 * created. It calls client_connection() and does the final cleanup.
545 *
546 * @param arg Connection structure pointer.
547 *
548 * @return Always zero.
549 *
550 */
551static int connection_fibril(void *arg)
552{
553 unsigned long key;
554 client_t *cl;
555 link_t *lnk;
556 bool destroy = false;
557
558 /*
559 * Setup fibril-local connection pointer.
560 */
561 FIBRIL_connection = (connection_t *) arg;
562
563 /*
564 * Add our reference for the current connection in the client task
565 * tracking structure. If this is the first reference, create and
566 * hash in a new tracking structure.
567 */
568 futex_down(&async_futex);
569 key = FIBRIL_connection->in_task_hash;
570 lnk = hash_table_find(&client_hash_table, &key);
571 if (lnk) {
572 cl = hash_table_get_instance(lnk, client_t, link);
573 cl->refcnt++;
574 } else {
575 cl = malloc(sizeof(client_t));
576 if (!cl) {
577 ipc_answer_0(FIBRIL_connection->callid, ENOMEM);
578 futex_up(&async_futex);
579 return 0;
580 }
581 cl->in_task_hash = FIBRIL_connection->in_task_hash;
582 async_serialize_start();
583 cl->data = async_client_data_create();
584 async_serialize_end();
585 cl->refcnt = 1;
586 hash_table_insert(&client_hash_table, &key, &cl->link);
587 }
588 futex_up(&async_futex);
589
590 FIBRIL_connection->client = cl;
591
592 /*
593 * Call the connection handler function.
594 */
595 FIBRIL_connection->cfibril(FIBRIL_connection->callid,
596 &FIBRIL_connection->call);
597
598 /*
599 * Remove the reference for this client task connection.
600 */
601 futex_down(&async_futex);
602 if (--cl->refcnt == 0) {
603 hash_table_remove(&client_hash_table, &key, 1);
604 destroy = true;
605 }
606 futex_up(&async_futex);
607
608 if (destroy) {
609 if (cl->data)
610 async_client_data_destroy(cl->data);
611 free(cl);
612 }
613
614 /*
615 * Remove myself from the connection hash table.
616 */
617 futex_down(&async_futex);
618 key = FIBRIL_connection->in_phone_hash;
619 hash_table_remove(&conn_hash_table, &key, 1);
620 futex_up(&async_futex);
621
622 /*
623 * Answer all remaining messages with EHANGUP.
624 */
625 while (!list_empty(&FIBRIL_connection->msg_queue)) {
626 msg_t *msg;
627
628 msg = list_get_instance(FIBRIL_connection->msg_queue.next,
629 msg_t, link);
630 list_remove(&msg->link);
631 ipc_answer_0(msg->callid, EHANGUP);
632 free(msg);
633 }
634
635 /*
636 * If the connection was hung-up, answer the last call,
637 * i.e. IPC_M_PHONE_HUNGUP.
638 */
639 if (FIBRIL_connection->close_callid)
640 ipc_answer_0(FIBRIL_connection->close_callid, EOK);
641
642 return 0;
643}
644
645/** Create a new fibril for a new connection.
646 *
647 * Create new fibril for connection, fill in connection structures and inserts
648 * it into the hash table, so that later we can easily do routing of messages to
649 * particular fibrils.
650 *
651 * @param in_task_hash Identification of the incoming connection.
652 * @param in_phone_hash Identification of the incoming connection.
653 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
654 * If callid is zero, the connection was opened by
655 * accepting the IPC_M_CONNECT_TO_ME call and this function
656 * is called directly by the server.
657 * @param call Call data of the opening call.
658 * @param cfibril Fibril function that should be called upon opening the
659 * connection.
660 *
661 * @return New fibril id or NULL on failure.
662 *
663 */
664fid_t async_new_connection(sysarg_t in_task_hash, sysarg_t in_phone_hash,
665 ipc_callid_t callid, ipc_call_t *call,
666 void (*cfibril)(ipc_callid_t, ipc_call_t *))
667{
668 connection_t *conn = malloc(sizeof(*conn));
669 if (!conn) {
670 if (callid)
671 ipc_answer_0(callid, ENOMEM);
672 return (uintptr_t) NULL;
673 }
674
675 conn->in_task_hash = in_task_hash;
676 conn->in_phone_hash = in_phone_hash;
677 list_initialize(&conn->msg_queue);
678 conn->callid = callid;
679 conn->close_callid = 0;
680
681 if (call)
682 conn->call = *call;
683
684 /* We will activate the fibril ASAP */
685 conn->wdata.active = true;
686 conn->cfibril = cfibril;
687 conn->wdata.fid = fibril_create(connection_fibril, conn);
688
689 if (!conn->wdata.fid) {
690 free(conn);
691 if (callid)
692 ipc_answer_0(callid, ENOMEM);
693 return (uintptr_t) NULL;
694 }
695
696 /* Add connection to the connection hash table */
697 unsigned long key = conn->in_phone_hash;
698
699 futex_down(&async_futex);
700 hash_table_insert(&conn_hash_table, &key, &conn->link);
701 futex_up(&async_futex);
702
703 fibril_add_ready(conn->wdata.fid);
704
705 return conn->wdata.fid;
706}
707
708/** Handle a call that was received.
709 *
710 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
711 * Otherwise the call is routed to its connection fibril.
712 *
713 * @param callid Hash of the incoming call.
714 * @param call Data of the incoming call.
715 *
716 */
717static void handle_call(ipc_callid_t callid, ipc_call_t *call)
718{
719 /* Unrouted call - do some default behaviour */
720 if ((callid & IPC_CALLID_NOTIFICATION)) {
721 process_notification(callid, call);
722 goto out;
723 }
724
725 switch (IPC_GET_IMETHOD(*call)) {
726 case IPC_M_CONNECT_ME:
727 case IPC_M_CONNECT_ME_TO:
728 /* Open new connection with fibril etc. */
729 async_new_connection(call->in_task_hash, IPC_GET_ARG5(*call),
730 callid, call, client_connection);
731 goto out;
732 }
733
734 /* Try to route the call through the connection hash table */
735 if (route_call(callid, call))
736 goto out;
737
738 /* Unknown call from unknown phone - hang it up */
739 ipc_answer_0(callid, EHANGUP);
740 return;
741
742out:
743 ;
744}
745
746/** Fire all timeouts that expired. */
747static void handle_expired_timeouts(void)
748{
749 struct timeval tv;
750 gettimeofday(&tv, NULL);
751
752 futex_down(&async_futex);
753
754 link_t *cur = timeout_list.next;
755 while (cur != &timeout_list) {
756 awaiter_t *waiter;
757
758 waiter = list_get_instance(cur, awaiter_t, to_event.link);
759 if (tv_gt(&waiter->to_event.expires, &tv))
760 break;
761
762 cur = cur->next;
763
764 list_remove(&waiter->to_event.link);
765 waiter->to_event.inlist = false;
766 waiter->to_event.occurred = true;
767
768 /*
769 * Redundant condition?
770 * The fibril should not be active when it gets here.
771 */
772 if (!waiter->active) {
773 waiter->active = true;
774 fibril_add_ready(waiter->fid);
775 }
776 }
777
778 futex_up(&async_futex);
779}
780
781/** Endless loop dispatching incoming calls and answers.
782 *
783 * @return Never returns.
784 *
785 */
786static int async_manager_worker(void)
787{
788 while (true) {
789 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
790 futex_up(&async_futex);
791 /*
792 * async_futex is always held when entering a manager
793 * fibril.
794 */
795 continue;
796 }
797
798 futex_down(&async_futex);
799
800 suseconds_t timeout;
801 if (!list_empty(&timeout_list)) {
802 awaiter_t *waiter = list_get_instance(timeout_list.next,
803 awaiter_t, to_event.link);
804
805 struct timeval tv;
806 gettimeofday(&tv, NULL);
807
808 if (tv_gteq(&tv, &waiter->to_event.expires)) {
809 futex_up(&async_futex);
810 handle_expired_timeouts();
811 continue;
812 } else
813 timeout = tv_sub(&waiter->to_event.expires,
814 &tv);
815 } else
816 timeout = SYNCH_NO_TIMEOUT;
817
818 futex_up(&async_futex);
819
820 atomic_inc(&threads_in_ipc_wait);
821
822 ipc_call_t call;
823 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
824 SYNCH_FLAGS_NONE);
825
826 atomic_dec(&threads_in_ipc_wait);
827
828 if (!callid) {
829 handle_expired_timeouts();
830 continue;
831 }
832
833 if (callid & IPC_CALLID_ANSWERED)
834 continue;
835
836 handle_call(callid, &call);
837 }
838
839 return 0;
840}
841
842/** Function to start async_manager as a standalone fibril.
843 *
844 * When more kernel threads are used, one async manager should exist per thread.
845 *
846 * @param arg Unused.
847 * @return Never returns.
848 *
849 */
850static int async_manager_fibril(void *arg)
851{
852 futex_up(&async_futex);
853
854 /*
855 * async_futex is always locked when entering manager
856 */
857 async_manager_worker();
858
859 return 0;
860}
861
862/** Add one manager to manager list. */
863void async_create_manager(void)
864{
865 fid_t fid = fibril_create(async_manager_fibril, NULL);
866 fibril_add_manager(fid);
867}
868
869/** Remove one manager from manager list */
870void async_destroy_manager(void)
871{
872 fibril_remove_manager();
873}
874
875/** Initialize the async framework.
876 *
877 * @return Zero on success or an error code.
878 */
879int __async_init(void)
880{
881 if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS, 1,
882 &client_hash_table_ops) || !hash_table_create(&conn_hash_table,
883 CONN_HASH_TABLE_BUCKETS, 1, &conn_hash_table_ops)) {
884 return ENOMEM;
885 }
886
887 _async_sess_init();
888
889 return 0;
890}
891
892/** Reply received callback.
893 *
894 * This function is called whenever a reply for an asynchronous message sent out
895 * by the asynchronous framework is received.
896 *
897 * Notify the fibril which is waiting for this message that it has arrived.
898 *
899 * @param arg Pointer to the asynchronous message record.
900 * @param retval Value returned in the answer.
901 * @param data Call data of the answer.
902 */
903static void reply_received(void *arg, int retval, ipc_call_t *data)
904{
905 futex_down(&async_futex);
906
907 amsg_t *msg = (amsg_t *) arg;
908 msg->retval = retval;
909
910 /* Copy data after futex_down, just in case the call was detached */
911 if ((msg->dataptr) && (data))
912 *msg->dataptr = *data;
913
914 write_barrier();
915
916 /* Remove message from timeout list */
917 if (msg->wdata.to_event.inlist)
918 list_remove(&msg->wdata.to_event.link);
919
920 msg->done = true;
921 if (!msg->wdata.active) {
922 msg->wdata.active = true;
923 fibril_add_ready(msg->wdata.fid);
924 }
925
926 futex_up(&async_futex);
927}
928
929/** Send message and return id of the sent message.
930 *
931 * The return value can be used as input for async_wait() to wait for
932 * completion.
933 *
934 * @param phoneid Handle of the phone that will be used for the send.
935 * @param method Service-defined method.
936 * @param arg1 Service-defined payload argument.
937 * @param arg2 Service-defined payload argument.
938 * @param arg3 Service-defined payload argument.
939 * @param arg4 Service-defined payload argument.
940 * @param dataptr If non-NULL, storage where the reply data will be
941 * stored.
942 *
943 * @return Hash of the sent message or 0 on error.
944 *
945 */
946aid_t async_send_fast(int phoneid, sysarg_t method, sysarg_t arg1,
947 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
948{
949 amsg_t *msg = malloc(sizeof(*msg));
950
951 if (!msg)
952 return 0;
953
954 msg->done = false;
955 msg->dataptr = dataptr;
956
957 msg->wdata.to_event.inlist = false;
958 /* We may sleep in the next method, but it will use its own mechanism */
959 msg->wdata.active = true;
960
961 ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
962 reply_received, true);
963
964 return (aid_t) msg;
965}
966
967/** Send message and return id of the sent message
968 *
969 * The return value can be used as input for async_wait() to wait for
970 * completion.
971 *
972 * @param phoneid Handle of the phone that will be used for the send.
973 * @param method Service-defined method.
974 * @param arg1 Service-defined payload argument.
975 * @param arg2 Service-defined payload argument.
976 * @param arg3 Service-defined payload argument.
977 * @param arg4 Service-defined payload argument.
978 * @param arg5 Service-defined payload argument.
979 * @param dataptr If non-NULL, storage where the reply data will be
980 * stored.
981 *
982 * @return Hash of the sent message or 0 on error.
983 *
984 */
985aid_t async_send_slow(int phoneid, sysarg_t method, sysarg_t arg1,
986 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
987 ipc_call_t *dataptr)
988{
989 amsg_t *msg = malloc(sizeof(*msg));
990
991 if (!msg)
992 return 0;
993
994 msg->done = false;
995 msg->dataptr = dataptr;
996
997 msg->wdata.to_event.inlist = false;
998 /* We may sleep in next method, but it will use its own mechanism */
999 msg->wdata.active = true;
1000
1001 ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
1002 reply_received, true);
1003
1004 return (aid_t) msg;
1005}
1006
1007/** Wait for a message sent by the async framework.
1008 *
1009 * @param amsgid Hash of the message to wait for.
1010 * @param retval Pointer to storage where the retval of the answer will
1011 * be stored.
1012 *
1013 */
1014void async_wait_for(aid_t amsgid, sysarg_t *retval)
1015{
1016 amsg_t *msg = (amsg_t *) amsgid;
1017
1018 futex_down(&async_futex);
1019 if (msg->done) {
1020 futex_up(&async_futex);
1021 goto done;
1022 }
1023
1024 msg->wdata.fid = fibril_get_id();
1025 msg->wdata.active = false;
1026 msg->wdata.to_event.inlist = false;
1027
1028 /* Leave the async_futex locked when entering this function */
1029 fibril_switch(FIBRIL_TO_MANAGER);
1030
1031 /* Futex is up automatically after fibril_switch */
1032
1033done:
1034 if (retval)
1035 *retval = msg->retval;
1036
1037 free(msg);
1038}
1039
1040/** Wait for a message sent by the async framework, timeout variant.
1041 *
1042 * @param amsgid Hash of the message to wait for.
1043 * @param retval Pointer to storage where the retval of the answer will
1044 * be stored.
1045 * @param timeout Timeout in microseconds.
1046 *
1047 * @return Zero on success, ETIMEOUT if the timeout has expired.
1048 *
1049 */
1050int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
1051{
1052 amsg_t *msg = (amsg_t *) amsgid;
1053
1054 /* TODO: Let it go through the event read at least once */
1055 if (timeout < 0)
1056 return ETIMEOUT;
1057
1058 futex_down(&async_futex);
1059 if (msg->done) {
1060 futex_up(&async_futex);
1061 goto done;
1062 }
1063
1064 gettimeofday(&msg->wdata.to_event.expires, NULL);
1065 tv_add(&msg->wdata.to_event.expires, timeout);
1066
1067 msg->wdata.fid = fibril_get_id();
1068 msg->wdata.active = false;
1069 async_insert_timeout(&msg->wdata);
1070
1071 /* Leave the async_futex locked when entering this function */
1072 fibril_switch(FIBRIL_TO_MANAGER);
1073
1074 /* Futex is up automatically after fibril_switch */
1075
1076 if (!msg->done)
1077 return ETIMEOUT;
1078
1079done:
1080 if (retval)
1081 *retval = msg->retval;
1082
1083 free(msg);
1084
1085 return 0;
1086}
1087
1088/** Wait for specified time.
1089 *
1090 * The current fibril is suspended but the thread continues to execute.
1091 *
1092 * @param timeout Duration of the wait in microseconds.
1093 *
1094 */
1095void async_usleep(suseconds_t timeout)
1096{
1097 amsg_t *msg = malloc(sizeof(*msg));
1098
1099 if (!msg)
1100 return;
1101
1102 msg->wdata.fid = fibril_get_id();
1103 msg->wdata.active = false;
1104
1105 gettimeofday(&msg->wdata.to_event.expires, NULL);
1106 tv_add(&msg->wdata.to_event.expires, timeout);
1107
1108 futex_down(&async_futex);
1109
1110 async_insert_timeout(&msg->wdata);
1111
1112 /* Leave the async_futex locked when entering this function */
1113 fibril_switch(FIBRIL_TO_MANAGER);
1114
1115 /* Futex is up automatically after fibril_switch() */
1116
1117 free(msg);
1118}
1119
1120/** Setter for client_connection function pointer.
1121 *
1122 * @param conn Function that will implement a new connection fibril.
1123 *
1124 */
1125void async_set_client_connection(async_client_conn_t conn)
1126{
1127 client_connection = conn;
1128}
1129
1130/** Setter for interrupt_received function pointer.
1131 *
1132 * @param intr Function that will implement a new interrupt
1133 * notification fibril.
1134 */
1135void async_set_interrupt_received(async_client_conn_t intr)
1136{
1137 interrupt_received = intr;
1138}
1139
1140/** Pseudo-synchronous message sending - fast version.
1141 *
1142 * Send message asynchronously and return only after the reply arrives.
1143 *
1144 * This function can only transfer 4 register payload arguments. For
1145 * transferring more arguments, see the slower async_req_slow().
1146 *
1147 * @param phoneid Hash of the phone through which to make the call.
1148 * @param method Method of the call.
1149 * @param arg1 Service-defined payload argument.
1150 * @param arg2 Service-defined payload argument.
1151 * @param arg3 Service-defined payload argument.
1152 * @param arg4 Service-defined payload argument.
1153 * @param r1 If non-NULL, storage for the 1st reply argument.
1154 * @param r2 If non-NULL, storage for the 2nd reply argument.
1155 * @param r3 If non-NULL, storage for the 3rd reply argument.
1156 * @param r4 If non-NULL, storage for the 4th reply argument.
1157 * @param r5 If non-NULL, storage for the 5th reply argument.
1158 *
1159 * @return Return code of the reply or a negative error code.
1160 *
1161 */
1162sysarg_t async_req_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1163 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1164 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1165{
1166 ipc_call_t result;
1167 aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1168 &result);
1169
1170 sysarg_t rc;
1171 async_wait_for(eid, &rc);
1172
1173 if (r1)
1174 *r1 = IPC_GET_ARG1(result);
1175
1176 if (r2)
1177 *r2 = IPC_GET_ARG2(result);
1178
1179 if (r3)
1180 *r3 = IPC_GET_ARG3(result);
1181
1182 if (r4)
1183 *r4 = IPC_GET_ARG4(result);
1184
1185 if (r5)
1186 *r5 = IPC_GET_ARG5(result);
1187
1188 return rc;
1189}
1190
1191/** Pseudo-synchronous message sending - slow version.
1192 *
1193 * Send message asynchronously and return only after the reply arrives.
1194 *
1195 * @param phoneid Hash of the phone through which to make the call.
1196 * @param method Method of the call.
1197 * @param arg1 Service-defined payload argument.
1198 * @param arg2 Service-defined payload argument.
1199 * @param arg3 Service-defined payload argument.
1200 * @param arg4 Service-defined payload argument.
1201 * @param arg5 Service-defined payload argument.
1202 * @param r1 If non-NULL, storage for the 1st reply argument.
1203 * @param r2 If non-NULL, storage for the 2nd reply argument.
1204 * @param r3 If non-NULL, storage for the 3rd reply argument.
1205 * @param r4 If non-NULL, storage for the 4th reply argument.
1206 * @param r5 If non-NULL, storage for the 5th reply argument.
1207 *
1208 * @return Return code of the reply or a negative error code.
1209 *
1210 */
1211sysarg_t async_req_slow(int phoneid, sysarg_t method, sysarg_t arg1,
1212 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1213 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1214{
1215 ipc_call_t result;
1216 aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1217 &result);
1218
1219 sysarg_t rc;
1220 async_wait_for(eid, &rc);
1221
1222 if (r1)
1223 *r1 = IPC_GET_ARG1(result);
1224
1225 if (r2)
1226 *r2 = IPC_GET_ARG2(result);
1227
1228 if (r3)
1229 *r3 = IPC_GET_ARG3(result);
1230
1231 if (r4)
1232 *r4 = IPC_GET_ARG4(result);
1233
1234 if (r5)
1235 *r5 = IPC_GET_ARG5(result);
1236
1237 return rc;
1238}
1239
1240void async_msg_0(int phone, sysarg_t imethod)
1241{
1242 ipc_call_async_0(phone, imethod, NULL, NULL, true);
1243}
1244
1245void async_msg_1(int phone, sysarg_t imethod, sysarg_t arg1)
1246{
1247 ipc_call_async_1(phone, imethod, arg1, NULL, NULL, true);
1248}
1249
1250void async_msg_2(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2)
1251{
1252 ipc_call_async_2(phone, imethod, arg1, arg2, NULL, NULL, true);
1253}
1254
1255void async_msg_3(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
1256 sysarg_t arg3)
1257{
1258 ipc_call_async_3(phone, imethod, arg1, arg2, arg3, NULL, NULL, true);
1259}
1260
1261void async_msg_4(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
1262 sysarg_t arg3, sysarg_t arg4)
1263{
1264 ipc_call_async_4(phone, imethod, arg1, arg2, arg3, arg4, NULL, NULL,
1265 true);
1266}
1267
1268void async_msg_5(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
1269 sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1270{
1271 ipc_call_async_5(phone, imethod, arg1, arg2, arg3, arg4, arg5, NULL,
1272 NULL, true);
1273}
1274
1275sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
1276{
1277 return ipc_answer_0(callid, retval);
1278}
1279
1280sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
1281{
1282 return ipc_answer_1(callid, retval, arg1);
1283}
1284
1285sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1286 sysarg_t arg2)
1287{
1288 return ipc_answer_2(callid, retval, arg1, arg2);
1289}
1290
1291sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1292 sysarg_t arg2, sysarg_t arg3)
1293{
1294 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
1295}
1296
1297sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1298 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1299{
1300 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
1301}
1302
1303sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1304 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1305{
1306 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
1307}
1308
1309int async_forward_fast(ipc_callid_t callid, int phoneid, int imethod,
1310 sysarg_t arg1, sysarg_t arg2, int mode)
1311{
1312 return ipc_forward_fast(callid, phoneid, imethod, arg1, arg2, mode);
1313}
1314
1315int async_forward_slow(ipc_callid_t callid, int phoneid, int imethod,
1316 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
1317 int mode)
1318{
1319 return ipc_forward_slow(callid, phoneid, imethod, arg1, arg2, arg3, arg4,
1320 arg5, mode);
1321}
1322
1323/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1324 *
1325 * Ask through phone for a new connection to some service.
1326 *
1327 * @param phone Phone handle used for contacting the other side.
1328 * @param arg1 User defined argument.
1329 * @param arg2 User defined argument.
1330 * @param arg3 User defined argument.
1331 * @param client_receiver Connection handing routine.
1332 *
1333 * @return New phone handle on success or a negative error code.
1334 *
1335 */
1336int async_connect_to_me(int phone, sysarg_t arg1, sysarg_t arg2,
1337 sysarg_t arg3, async_client_conn_t client_receiver)
1338{
1339 sysarg_t task_hash;
1340 sysarg_t phone_hash;
1341 int rc = async_req_3_5(phone, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1342 NULL, NULL, NULL, &task_hash, &phone_hash);
1343 if (rc != EOK)
1344 return rc;
1345
1346 if (client_receiver != NULL)
1347 async_new_connection(task_hash, phone_hash, 0, NULL,
1348 client_receiver);
1349
1350 return EOK;
1351}
1352
1353/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1354 *
1355 * Ask through phone for a new connection to some service.
1356 *
1357 * @param phone Phone handle used for contacting the other side.
1358 * @param arg1 User defined argument.
1359 * @param arg2 User defined argument.
1360 * @param arg3 User defined argument.
1361 *
1362 * @return New phone handle on success or a negative error code.
1363 *
1364 */
1365int async_connect_me_to(int phone, sysarg_t arg1, sysarg_t arg2,
1366 sysarg_t arg3)
1367{
1368 sysarg_t newphid;
1369 int rc = async_req_3_5(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
1370 NULL, NULL, NULL, NULL, &newphid);
1371
1372 if (rc != EOK)
1373 return rc;
1374
1375 return newphid;
1376}
1377
1378/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1379 *
1380 * Ask through phone for a new connection to some service and block until
1381 * success.
1382 *
1383 * @param phoneid Phone handle used for contacting the other side.
1384 * @param arg1 User defined argument.
1385 * @param arg2 User defined argument.
1386 * @param arg3 User defined argument.
1387 *
1388 * @return New phone handle on success or a negative error code.
1389 *
1390 */
1391int async_connect_me_to_blocking(int phoneid, sysarg_t arg1, sysarg_t arg2,
1392 sysarg_t arg3)
1393{
1394 sysarg_t newphid;
1395 int rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
1396 IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
1397
1398 if (rc != EOK)
1399 return rc;
1400
1401 return newphid;
1402}
1403
1404/** Connect to a task specified by id.
1405 *
1406 */
1407int async_connect_kbox(task_id_t id)
1408{
1409 return ipc_connect_kbox(id);
1410}
1411
1412/** Wrapper for ipc_hangup.
1413 *
1414 * @param phone Phone handle to hung up.
1415 *
1416 * @return Zero on success or a negative error code.
1417 *
1418 */
1419int async_hangup(int phone)
1420{
1421 return ipc_hangup(phone);
1422}
1423
1424/** Interrupt one thread of this task from waiting for IPC. */
1425void async_poke(void)
1426{
1427 ipc_poke();
1428}
1429
1430/** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
1431 *
1432 * @param phoneid Phone that will be used to contact the receiving side.
1433 * @param dst Destination address space area base.
1434 * @param size Size of the destination address space area.
1435 * @param arg User defined argument.
1436 * @param flags Storage where the received flags will be stored. Can be
1437 * NULL.
1438 *
1439 * @return Zero on success or a negative error code from errno.h.
1440 */
1441int async_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
1442 int *flags)
1443{
1444 int res;
1445 sysarg_t tmp_flags;
1446 res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
1447 (sysarg_t) size, arg, NULL, &tmp_flags);
1448 if (flags)
1449 *flags = tmp_flags;
1450 return res;
1451}
1452
1453/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1454 *
1455 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN calls
1456 * so that the user doesn't have to remember the meaning of each IPC argument.
1457 *
1458 * So far, this wrapper is to be used from within a connection fibril.
1459 *
1460 * @param callid Storage where the hash of the IPC_M_SHARE_IN call will
1461 * be stored.
1462 * @param size Destination address space area size.
1463 *
1464 * @return Non-zero on success, zero on failure.
1465 */
1466int async_share_in_receive(ipc_callid_t *callid, size_t *size)
1467{
1468 ipc_call_t data;
1469
1470 assert(callid);
1471 assert(size);
1472
1473 *callid = async_get_call(&data);
1474 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
1475 return 0;
1476 *size = (size_t) IPC_GET_ARG2(data);
1477 return 1;
1478}
1479
1480/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1481 *
1482 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1483 * so that the user doesn't have to remember the meaning of each IPC argument.
1484 *
1485 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1486 * @param src Source address space base.
1487 * @param flags Flags to be used for sharing. Bits can be only cleared.
1488 *
1489 * @return Zero on success or a value from @ref errno.h on failure.
1490 */
1491int async_share_in_finalize(ipc_callid_t callid, void *src, int flags)
1492{
1493 return ipc_share_in_finalize(callid, src, flags);
1494}
1495
1496/** Wrapper for making IPC_M_SHARE_OUT calls using the async framework.
1497 *
1498 * @param phoneid Phone that will be used to contact the receiving side.
1499 * @param src Source address space area base address.
1500 * @param flags Flags to be used for sharing. Bits can be only cleared.
1501 *
1502 * @return Zero on success or a negative error code from errno.h.
1503 */
1504int async_share_out_start(int phoneid, void *src, int flags)
1505{
1506 return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
1507 (sysarg_t) flags);
1508}
1509
1510/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1511 *
1512 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT calls
1513 * so that the user doesn't have to remember the meaning of each IPC argument.
1514 *
1515 * So far, this wrapper is to be used from within a connection fibril.
1516 *
1517 * @param callid Storage where the hash of the IPC_M_SHARE_OUT call will
1518 * be stored.
1519 * @param size Storage where the source address space area size will be
1520 * stored.
1521 * @param flags Storage where the sharing flags will be stored.
1522 *
1523 * @return Non-zero on success, zero on failure.
1524 */
1525int async_share_out_receive(ipc_callid_t *callid, size_t *size, int *flags)
1526{
1527 ipc_call_t data;
1528
1529 assert(callid);
1530 assert(size);
1531 assert(flags);
1532
1533 *callid = async_get_call(&data);
1534 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
1535 return 0;
1536 *size = (size_t) IPC_GET_ARG2(data);
1537 *flags = (int) IPC_GET_ARG3(data);
1538 return 1;
1539}
1540
1541/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
1542 *
1543 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT calls
1544 * so that the user doesn't have to remember the meaning of each IPC argument.
1545 *
1546 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1547 * @param dst Destination address space area base address.
1548 *
1549 * @return Zero on success or a value from @ref errno.h on failure.
1550 */
1551int async_share_out_finalize(ipc_callid_t callid, void *dst)
1552{
1553 return ipc_share_out_finalize(callid, dst);
1554}
1555
1556
1557/** Wrapper for making IPC_M_DATA_READ calls using the async framework.
1558 *
1559 * @param phoneid Phone that will be used to contact the receiving side.
1560 * @param dst Address of the beginning of the destination buffer.
1561 * @param size Size of the destination buffer.
1562 *
1563 * @return Zero on success or a negative error code from errno.h.
1564 */
1565int async_data_read_start(int phoneid, void *dst, size_t size)
1566{
1567 return async_req_2_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
1568 (sysarg_t) size);
1569}
1570
1571/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
1572 *
1573 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ calls
1574 * so that the user doesn't have to remember the meaning of each IPC argument.
1575 *
1576 * So far, this wrapper is to be used from within a connection fibril.
1577 *
1578 * @param callid Storage where the hash of the IPC_M_DATA_READ call will
1579 * be stored.
1580 * @param size Storage where the maximum size will be stored. Can be
1581 * NULL.
1582 *
1583 * @return Non-zero on success, zero on failure.
1584 */
1585int async_data_read_receive(ipc_callid_t *callid, size_t *size)
1586{
1587 ipc_call_t data;
1588
1589 assert(callid);
1590
1591 *callid = async_get_call(&data);
1592 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
1593 return 0;
1594 if (size)
1595 *size = (size_t) IPC_GET_ARG2(data);
1596 return 1;
1597}
1598
1599/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
1600 *
1601 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1602 * so that the user doesn't have to remember the meaning of each IPC argument.
1603 *
1604 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1605 * @param src Source address for the IPC_M_DATA_READ call.
1606 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
1607 * the maximum size announced by the sender.
1608 *
1609 * @return Zero on success or a value from @ref errno.h on failure.
1610 */
1611int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
1612{
1613 return ipc_data_read_finalize(callid, src, size);
1614}
1615
1616/** Wrapper for forwarding any read request
1617 *
1618 */
1619int async_data_read_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1620 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1621{
1622 ipc_callid_t callid;
1623 if (!async_data_read_receive(&callid, NULL)) {
1624 ipc_answer_0(callid, EINVAL);
1625 return EINVAL;
1626 }
1627
1628 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1629 dataptr);
1630 if (msg == 0) {
1631 ipc_answer_0(callid, EINVAL);
1632 return EINVAL;
1633 }
1634
1635 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1636 IPC_FF_ROUTE_FROM_ME);
1637 if (retval != EOK) {
1638 async_wait_for(msg, NULL);
1639 ipc_answer_0(callid, retval);
1640 return retval;
1641 }
1642
1643 sysarg_t rc;
1644 async_wait_for(msg, &rc);
1645
1646 return (int) rc;
1647}
1648
1649/** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
1650 *
1651 * @param phoneid Phone that will be used to contact the receiving side.
1652 * @param src Address of the beginning of the source buffer.
1653 * @param size Size of the source buffer.
1654 *
1655 * @return Zero on success or a negative error code from errno.h.
1656 *
1657 */
1658int async_data_write_start(int phoneid, const void *src, size_t size)
1659{
1660 return async_req_2_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
1661 (sysarg_t) size);
1662}
1663
1664/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
1665 *
1666 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE calls
1667 * so that the user doesn't have to remember the meaning of each IPC argument.
1668 *
1669 * So far, this wrapper is to be used from within a connection fibril.
1670 *
1671 * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
1672 * be stored.
1673 * @param size Storage where the suggested size will be stored. May be
1674 * NULL
1675 *
1676 * @return Non-zero on success, zero on failure.
1677 *
1678 */
1679int async_data_write_receive(ipc_callid_t *callid, size_t *size)
1680{
1681 ipc_call_t data;
1682
1683 assert(callid);
1684
1685 *callid = async_get_call(&data);
1686 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
1687 return 0;
1688
1689 if (size)
1690 *size = (size_t) IPC_GET_ARG2(data);
1691
1692 return 1;
1693}
1694
1695/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
1696 *
1697 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
1698 * so that the user doesn't have to remember the meaning of each IPC argument.
1699 *
1700 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1701 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
1702 * @param size Final size for the IPC_M_DATA_WRITE call.
1703 *
1704 * @return Zero on success or a value from @ref errno.h on failure.
1705 *
1706 */
1707int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
1708{
1709 return ipc_data_write_finalize(callid, dst, size);
1710}
1711
1712/** Wrapper for receiving binary data or strings
1713 *
1714 * This wrapper only makes it more comfortable to use async_data_write_*
1715 * functions to receive binary data or strings.
1716 *
1717 * @param data Pointer to data pointer (which should be later disposed
1718 * by free()). If the operation fails, the pointer is not
1719 * touched.
1720 * @param nullterm If true then the received data is always zero terminated.
1721 * This also causes to allocate one extra byte beyond the
1722 * raw transmitted data.
1723 * @param min_size Minimum size (in bytes) of the data to receive.
1724 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
1725 * no limit.
1726 * @param granulariy If non-zero then the size of the received data has to
1727 * be divisible by this value.
1728 * @param received If not NULL, the size of the received data is stored here.
1729 *
1730 * @return Zero on success or a value from @ref errno.h on failure.
1731 *
1732 */
1733int async_data_write_accept(void **data, const bool nullterm,
1734 const size_t min_size, const size_t max_size, const size_t granularity,
1735 size_t *received)
1736{
1737 ipc_callid_t callid;
1738 size_t size;
1739 if (!async_data_write_receive(&callid, &size)) {
1740 ipc_answer_0(callid, EINVAL);
1741 return EINVAL;
1742 }
1743
1744 if (size < min_size) {
1745 ipc_answer_0(callid, EINVAL);
1746 return EINVAL;
1747 }
1748
1749 if ((max_size > 0) && (size > max_size)) {
1750 ipc_answer_0(callid, EINVAL);
1751 return EINVAL;
1752 }
1753
1754 if ((granularity > 0) && ((size % granularity) != 0)) {
1755 ipc_answer_0(callid, EINVAL);
1756 return EINVAL;
1757 }
1758
1759 void *_data;
1760
1761 if (nullterm)
1762 _data = malloc(size + 1);
1763 else
1764 _data = malloc(size);
1765
1766 if (_data == NULL) {
1767 ipc_answer_0(callid, ENOMEM);
1768 return ENOMEM;
1769 }
1770
1771 int rc = async_data_write_finalize(callid, _data, size);
1772 if (rc != EOK) {
1773 free(_data);
1774 return rc;
1775 }
1776
1777 if (nullterm)
1778 ((char *) _data)[size] = 0;
1779
1780 *data = _data;
1781 if (received != NULL)
1782 *received = size;
1783
1784 return EOK;
1785}
1786
1787/** Wrapper for voiding any data that is about to be received
1788 *
1789 * This wrapper can be used to void any pending data
1790 *
1791 * @param retval Error value from @ref errno.h to be returned to the caller.
1792 *
1793 */
1794void async_data_write_void(const int retval)
1795{
1796 ipc_callid_t callid;
1797 async_data_write_receive(&callid, NULL);
1798 ipc_answer_0(callid, retval);
1799}
1800
1801/** Wrapper for forwarding any data that is about to be received
1802 *
1803 */
1804int async_data_write_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1805 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1806{
1807 ipc_callid_t callid;
1808 if (!async_data_write_receive(&callid, NULL)) {
1809 ipc_answer_0(callid, EINVAL);
1810 return EINVAL;
1811 }
1812
1813 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1814 dataptr);
1815 if (msg == 0) {
1816 ipc_answer_0(callid, EINVAL);
1817 return EINVAL;
1818 }
1819
1820 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1821 IPC_FF_ROUTE_FROM_ME);
1822 if (retval != EOK) {
1823 async_wait_for(msg, NULL);
1824 ipc_answer_0(callid, retval);
1825 return retval;
1826 }
1827
1828 sysarg_t rc;
1829 async_wait_for(msg, &rc);
1830
1831 return (int) rc;
1832}
1833
1834/** @}
1835 */
Note: See TracBrowser for help on using the repository browser.