source: mainline/uspace/lib/c/generic/async.c@ 37cf3792

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

improve run-time termination

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