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

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

Track and reference count connections from the same client task.

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