source: mainline/uspace/lib/c/generic/async.c@ 8526e585

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

Reorder call to the client data destructor so that it can be more
flexible.

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