source: mainline/uspace/lib/c/generic/async.c@ 46eec3b

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

Introduce a callback mechanism to create and destroy client data area.

  • 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 void *unref_client_data = NULL;
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 unref_client_data = cl->data;
590 free(cl);
591 }
592 futex_up(&async_futex);
593
594 if (unref_client_data)
595 async_client_data_destroy(unref_client_data);
596
597 /*
598 * Remove myself from the connection hash table.
599 */
600 futex_down(&async_futex);
601 key = FIBRIL_connection->in_phone_hash;
602 hash_table_remove(&conn_hash_table, &key, 1);
603 futex_up(&async_futex);
604
605 /*
606 * Answer all remaining messages with EHANGUP.
607 */
608 while (!list_empty(&FIBRIL_connection->msg_queue)) {
609 msg_t *msg;
610
611 msg = list_get_instance(FIBRIL_connection->msg_queue.next,
612 msg_t, link);
613 list_remove(&msg->link);
614 ipc_answer_0(msg->callid, EHANGUP);
615 free(msg);
616 }
617
618 /*
619 * If the connection was hung-up, answer the last call,
620 * i.e. IPC_M_PHONE_HUNGUP.
621 */
622 if (FIBRIL_connection->close_callid)
623 ipc_answer_0(FIBRIL_connection->close_callid, EOK);
624
625 return 0;
626}
627
628/** Create a new fibril for a new connection.
629 *
630 * Create new fibril for connection, fill in connection structures and inserts
631 * it into the hash table, so that later we can easily do routing of messages to
632 * particular fibrils.
633 *
634 * @param in_task_hash Identification of the incoming connection.
635 * @param in_phone_hash Identification of the incoming connection.
636 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
637 * If callid is zero, the connection was opened by
638 * accepting the IPC_M_CONNECT_TO_ME call and this function
639 * is called directly by the server.
640 * @param call Call data of the opening call.
641 * @param cfibril Fibril function that should be called upon opening the
642 * connection.
643 *
644 * @return New fibril id or NULL on failure.
645 *
646 */
647fid_t async_new_connection(sysarg_t in_task_hash, sysarg_t in_phone_hash,
648 ipc_callid_t callid, ipc_call_t *call,
649 void (*cfibril)(ipc_callid_t, ipc_call_t *))
650{
651 connection_t *conn = malloc(sizeof(*conn));
652 if (!conn) {
653 if (callid)
654 ipc_answer_0(callid, ENOMEM);
655 return (uintptr_t) NULL;
656 }
657
658 conn->in_task_hash = in_task_hash;
659 conn->in_phone_hash = in_phone_hash;
660 list_initialize(&conn->msg_queue);
661 conn->callid = callid;
662 conn->close_callid = 0;
663
664 if (call)
665 conn->call = *call;
666
667 /* We will activate the fibril ASAP */
668 conn->wdata.active = true;
669 conn->cfibril = cfibril;
670 conn->wdata.fid = fibril_create(connection_fibril, conn);
671
672 if (!conn->wdata.fid) {
673 free(conn);
674 if (callid)
675 ipc_answer_0(callid, ENOMEM);
676 return (uintptr_t) NULL;
677 }
678
679 /* Add connection to the connection hash table */
680 unsigned long key = conn->in_phone_hash;
681
682 futex_down(&async_futex);
683 hash_table_insert(&conn_hash_table, &key, &conn->link);
684 futex_up(&async_futex);
685
686 fibril_add_ready(conn->wdata.fid);
687
688 return conn->wdata.fid;
689}
690
691/** Handle a call that was received.
692 *
693 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
694 * Otherwise the call is routed to its connection fibril.
695 *
696 * @param callid Hash of the incoming call.
697 * @param call Data of the incoming call.
698 *
699 */
700static void handle_call(ipc_callid_t callid, ipc_call_t *call)
701{
702 /* Unrouted call - do some default behaviour */
703 if ((callid & IPC_CALLID_NOTIFICATION)) {
704 process_notification(callid, call);
705 goto out;
706 }
707
708 switch (IPC_GET_IMETHOD(*call)) {
709 case IPC_M_CONNECT_ME:
710 case IPC_M_CONNECT_ME_TO:
711 /* Open new connection with fibril etc. */
712 async_new_connection(call->in_task_hash, IPC_GET_ARG5(*call),
713 callid, call, client_connection);
714 goto out;
715 }
716
717 /* Try to route the call through the connection hash table */
718 if (route_call(callid, call))
719 goto out;
720
721 /* Unknown call from unknown phone - hang it up */
722 ipc_answer_0(callid, EHANGUP);
723 return;
724
725out:
726 ;
727}
728
729/** Fire all timeouts that expired. */
730static void handle_expired_timeouts(void)
731{
732 struct timeval tv;
733 gettimeofday(&tv, NULL);
734
735 futex_down(&async_futex);
736
737 link_t *cur = timeout_list.next;
738 while (cur != &timeout_list) {
739 awaiter_t *waiter;
740
741 waiter = list_get_instance(cur, awaiter_t, to_event.link);
742 if (tv_gt(&waiter->to_event.expires, &tv))
743 break;
744
745 cur = cur->next;
746
747 list_remove(&waiter->to_event.link);
748 waiter->to_event.inlist = false;
749 waiter->to_event.occurred = true;
750
751 /*
752 * Redundant condition?
753 * The fibril should not be active when it gets here.
754 */
755 if (!waiter->active) {
756 waiter->active = true;
757 fibril_add_ready(waiter->fid);
758 }
759 }
760
761 futex_up(&async_futex);
762}
763
764/** Endless loop dispatching incoming calls and answers.
765 *
766 * @return Never returns.
767 *
768 */
769static int async_manager_worker(void)
770{
771 while (true) {
772 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
773 futex_up(&async_futex);
774 /*
775 * async_futex is always held when entering a manager
776 * fibril.
777 */
778 continue;
779 }
780
781 futex_down(&async_futex);
782
783 suseconds_t timeout;
784 if (!list_empty(&timeout_list)) {
785 awaiter_t *waiter = list_get_instance(timeout_list.next,
786 awaiter_t, to_event.link);
787
788 struct timeval tv;
789 gettimeofday(&tv, NULL);
790
791 if (tv_gteq(&tv, &waiter->to_event.expires)) {
792 futex_up(&async_futex);
793 handle_expired_timeouts();
794 continue;
795 } else
796 timeout = tv_sub(&waiter->to_event.expires,
797 &tv);
798 } else
799 timeout = SYNCH_NO_TIMEOUT;
800
801 futex_up(&async_futex);
802
803 atomic_inc(&threads_in_ipc_wait);
804
805 ipc_call_t call;
806 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
807 SYNCH_FLAGS_NONE);
808
809 atomic_dec(&threads_in_ipc_wait);
810
811 if (!callid) {
812 handle_expired_timeouts();
813 continue;
814 }
815
816 if (callid & IPC_CALLID_ANSWERED)
817 continue;
818
819 handle_call(callid, &call);
820 }
821
822 return 0;
823}
824
825/** Function to start async_manager as a standalone fibril.
826 *
827 * When more kernel threads are used, one async manager should exist per thread.
828 *
829 * @param arg Unused.
830 * @return Never returns.
831 *
832 */
833static int async_manager_fibril(void *arg)
834{
835 futex_up(&async_futex);
836
837 /*
838 * async_futex is always locked when entering manager
839 */
840 async_manager_worker();
841
842 return 0;
843}
844
845/** Add one manager to manager list. */
846void async_create_manager(void)
847{
848 fid_t fid = fibril_create(async_manager_fibril, NULL);
849 fibril_add_manager(fid);
850}
851
852/** Remove one manager from manager list */
853void async_destroy_manager(void)
854{
855 fibril_remove_manager();
856}
857
858/** Initialize the async framework.
859 *
860 * @return Zero on success or an error code.
861 */
862int __async_init(void)
863{
864 if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS, 1,
865 &client_hash_table_ops) || !hash_table_create(&conn_hash_table,
866 CONN_HASH_TABLE_BUCKETS, 1, &conn_hash_table_ops)) {
867 return ENOMEM;
868 }
869
870 _async_sess_init();
871
872 return 0;
873}
874
875/** Reply received callback.
876 *
877 * This function is called whenever a reply for an asynchronous message sent out
878 * by the asynchronous framework is received.
879 *
880 * Notify the fibril which is waiting for this message that it has arrived.
881 *
882 * @param arg Pointer to the asynchronous message record.
883 * @param retval Value returned in the answer.
884 * @param data Call data of the answer.
885 */
886static void reply_received(void *arg, int retval, ipc_call_t *data)
887{
888 futex_down(&async_futex);
889
890 amsg_t *msg = (amsg_t *) arg;
891 msg->retval = retval;
892
893 /* Copy data after futex_down, just in case the call was detached */
894 if ((msg->dataptr) && (data))
895 *msg->dataptr = *data;
896
897 write_barrier();
898
899 /* Remove message from timeout list */
900 if (msg->wdata.to_event.inlist)
901 list_remove(&msg->wdata.to_event.link);
902
903 msg->done = true;
904 if (!msg->wdata.active) {
905 msg->wdata.active = true;
906 fibril_add_ready(msg->wdata.fid);
907 }
908
909 futex_up(&async_futex);
910}
911
912/** Send message and return id of the sent message.
913 *
914 * The return value can be used as input for async_wait() to wait for
915 * completion.
916 *
917 * @param phoneid Handle of the phone that will be used for the send.
918 * @param method Service-defined method.
919 * @param arg1 Service-defined payload argument.
920 * @param arg2 Service-defined payload argument.
921 * @param arg3 Service-defined payload argument.
922 * @param arg4 Service-defined payload argument.
923 * @param dataptr If non-NULL, storage where the reply data will be
924 * stored.
925 *
926 * @return Hash of the sent message or 0 on error.
927 *
928 */
929aid_t async_send_fast(int phoneid, sysarg_t method, sysarg_t arg1,
930 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
931{
932 amsg_t *msg = malloc(sizeof(*msg));
933
934 if (!msg)
935 return 0;
936
937 msg->done = false;
938 msg->dataptr = dataptr;
939
940 msg->wdata.to_event.inlist = false;
941 /* We may sleep in the next method, but it will use its own mechanism */
942 msg->wdata.active = true;
943
944 ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
945 reply_received, true);
946
947 return (aid_t) msg;
948}
949
950/** Send message and return id of the sent message
951 *
952 * The return value can be used as input for async_wait() to wait for
953 * completion.
954 *
955 * @param phoneid Handle of the phone that will be used for the send.
956 * @param method Service-defined method.
957 * @param arg1 Service-defined payload argument.
958 * @param arg2 Service-defined payload argument.
959 * @param arg3 Service-defined payload argument.
960 * @param arg4 Service-defined payload argument.
961 * @param arg5 Service-defined payload argument.
962 * @param dataptr If non-NULL, storage where the reply data will be
963 * stored.
964 *
965 * @return Hash of the sent message or 0 on error.
966 *
967 */
968aid_t async_send_slow(int phoneid, sysarg_t method, sysarg_t arg1,
969 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
970 ipc_call_t *dataptr)
971{
972 amsg_t *msg = malloc(sizeof(*msg));
973
974 if (!msg)
975 return 0;
976
977 msg->done = false;
978 msg->dataptr = dataptr;
979
980 msg->wdata.to_event.inlist = false;
981 /* We may sleep in next method, but it will use its own mechanism */
982 msg->wdata.active = true;
983
984 ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
985 reply_received, true);
986
987 return (aid_t) msg;
988}
989
990/** Wait for a message sent by the async framework.
991 *
992 * @param amsgid Hash of the message to wait for.
993 * @param retval Pointer to storage where the retval of the answer will
994 * be stored.
995 *
996 */
997void async_wait_for(aid_t amsgid, sysarg_t *retval)
998{
999 amsg_t *msg = (amsg_t *) amsgid;
1000
1001 futex_down(&async_futex);
1002 if (msg->done) {
1003 futex_up(&async_futex);
1004 goto done;
1005 }
1006
1007 msg->wdata.fid = fibril_get_id();
1008 msg->wdata.active = false;
1009 msg->wdata.to_event.inlist = false;
1010
1011 /* Leave the async_futex locked when entering this function */
1012 fibril_switch(FIBRIL_TO_MANAGER);
1013
1014 /* Futex is up automatically after fibril_switch */
1015
1016done:
1017 if (retval)
1018 *retval = msg->retval;
1019
1020 free(msg);
1021}
1022
1023/** Wait for a message sent by the async framework, timeout variant.
1024 *
1025 * @param amsgid Hash of the message to wait for.
1026 * @param retval Pointer to storage where the retval of the answer will
1027 * be stored.
1028 * @param timeout Timeout in microseconds.
1029 *
1030 * @return Zero on success, ETIMEOUT if the timeout has expired.
1031 *
1032 */
1033int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
1034{
1035 amsg_t *msg = (amsg_t *) amsgid;
1036
1037 /* TODO: Let it go through the event read at least once */
1038 if (timeout < 0)
1039 return ETIMEOUT;
1040
1041 futex_down(&async_futex);
1042 if (msg->done) {
1043 futex_up(&async_futex);
1044 goto done;
1045 }
1046
1047 gettimeofday(&msg->wdata.to_event.expires, NULL);
1048 tv_add(&msg->wdata.to_event.expires, timeout);
1049
1050 msg->wdata.fid = fibril_get_id();
1051 msg->wdata.active = false;
1052 async_insert_timeout(&msg->wdata);
1053
1054 /* Leave the async_futex locked when entering this function */
1055 fibril_switch(FIBRIL_TO_MANAGER);
1056
1057 /* Futex is up automatically after fibril_switch */
1058
1059 if (!msg->done)
1060 return ETIMEOUT;
1061
1062done:
1063 if (retval)
1064 *retval = msg->retval;
1065
1066 free(msg);
1067
1068 return 0;
1069}
1070
1071/** Wait for specified time.
1072 *
1073 * The current fibril is suspended but the thread continues to execute.
1074 *
1075 * @param timeout Duration of the wait in microseconds.
1076 *
1077 */
1078void async_usleep(suseconds_t timeout)
1079{
1080 amsg_t *msg = malloc(sizeof(*msg));
1081
1082 if (!msg)
1083 return;
1084
1085 msg->wdata.fid = fibril_get_id();
1086 msg->wdata.active = false;
1087
1088 gettimeofday(&msg->wdata.to_event.expires, NULL);
1089 tv_add(&msg->wdata.to_event.expires, timeout);
1090
1091 futex_down(&async_futex);
1092
1093 async_insert_timeout(&msg->wdata);
1094
1095 /* Leave the async_futex locked when entering this function */
1096 fibril_switch(FIBRIL_TO_MANAGER);
1097
1098 /* Futex is up automatically after fibril_switch() */
1099
1100 free(msg);
1101}
1102
1103/** Setter for client_connection function pointer.
1104 *
1105 * @param conn Function that will implement a new connection fibril.
1106 *
1107 */
1108void async_set_client_connection(async_client_conn_t conn)
1109{
1110 client_connection = conn;
1111}
1112
1113/** Setter for interrupt_received function pointer.
1114 *
1115 * @param intr Function that will implement a new interrupt
1116 * notification fibril.
1117 */
1118void async_set_interrupt_received(async_client_conn_t intr)
1119{
1120 interrupt_received = intr;
1121}
1122
1123/** Pseudo-synchronous message sending - fast version.
1124 *
1125 * Send message asynchronously and return only after the reply arrives.
1126 *
1127 * This function can only transfer 4 register payload arguments. For
1128 * transferring more arguments, see the slower async_req_slow().
1129 *
1130 * @param phoneid Hash of the phone through which to make the call.
1131 * @param method Method of the call.
1132 * @param arg1 Service-defined payload argument.
1133 * @param arg2 Service-defined payload argument.
1134 * @param arg3 Service-defined payload argument.
1135 * @param arg4 Service-defined payload argument.
1136 * @param r1 If non-NULL, storage for the 1st reply argument.
1137 * @param r2 If non-NULL, storage for the 2nd reply argument.
1138 * @param r3 If non-NULL, storage for the 3rd reply argument.
1139 * @param r4 If non-NULL, storage for the 4th reply argument.
1140 * @param r5 If non-NULL, storage for the 5th reply argument.
1141 *
1142 * @return Return code of the reply or a negative error code.
1143 *
1144 */
1145sysarg_t async_req_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1146 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1147 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1148{
1149 ipc_call_t result;
1150 aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1151 &result);
1152
1153 sysarg_t rc;
1154 async_wait_for(eid, &rc);
1155
1156 if (r1)
1157 *r1 = IPC_GET_ARG1(result);
1158
1159 if (r2)
1160 *r2 = IPC_GET_ARG2(result);
1161
1162 if (r3)
1163 *r3 = IPC_GET_ARG3(result);
1164
1165 if (r4)
1166 *r4 = IPC_GET_ARG4(result);
1167
1168 if (r5)
1169 *r5 = IPC_GET_ARG5(result);
1170
1171 return rc;
1172}
1173
1174/** Pseudo-synchronous message sending - slow version.
1175 *
1176 * Send message asynchronously and return only after the reply arrives.
1177 *
1178 * @param phoneid Hash of the phone through which to make the call.
1179 * @param method Method of the call.
1180 * @param arg1 Service-defined payload argument.
1181 * @param arg2 Service-defined payload argument.
1182 * @param arg3 Service-defined payload argument.
1183 * @param arg4 Service-defined payload argument.
1184 * @param arg5 Service-defined payload argument.
1185 * @param r1 If non-NULL, storage for the 1st reply argument.
1186 * @param r2 If non-NULL, storage for the 2nd reply argument.
1187 * @param r3 If non-NULL, storage for the 3rd reply argument.
1188 * @param r4 If non-NULL, storage for the 4th reply argument.
1189 * @param r5 If non-NULL, storage for the 5th reply argument.
1190 *
1191 * @return Return code of the reply or a negative error code.
1192 *
1193 */
1194sysarg_t async_req_slow(int phoneid, sysarg_t method, sysarg_t arg1,
1195 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1196 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1197{
1198 ipc_call_t result;
1199 aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1200 &result);
1201
1202 sysarg_t rc;
1203 async_wait_for(eid, &rc);
1204
1205 if (r1)
1206 *r1 = IPC_GET_ARG1(result);
1207
1208 if (r2)
1209 *r2 = IPC_GET_ARG2(result);
1210
1211 if (r3)
1212 *r3 = IPC_GET_ARG3(result);
1213
1214 if (r4)
1215 *r4 = IPC_GET_ARG4(result);
1216
1217 if (r5)
1218 *r5 = IPC_GET_ARG5(result);
1219
1220 return rc;
1221}
1222
1223/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1224 *
1225 * Ask through phone for a new connection to some service.
1226 *
1227 * @param phoneid Phone handle used for contacting the other side.
1228 * @param arg1 User defined argument.
1229 * @param arg2 User defined argument.
1230 * @param arg3 User defined argument.
1231 *
1232 * @return New phone handle on success or a negative error code.
1233 */
1234int
1235async_connect_me_to(int phoneid, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1236{
1237 int rc;
1238 sysarg_t newphid;
1239
1240 rc = async_req_3_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, NULL,
1241 NULL, NULL, NULL, &newphid);
1242
1243 if (rc != EOK)
1244 return rc;
1245
1246 return newphid;
1247}
1248
1249/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1250 *
1251 * Ask through phone for a new connection to some service and block until
1252 * success.
1253 *
1254 * @param phoneid Phone handle used for contacting the other side.
1255 * @param arg1 User defined argument.
1256 * @param arg2 User defined argument.
1257 * @param arg3 User defined argument.
1258 *
1259 * @return New phone handle on success or a negative error code.
1260 */
1261int
1262async_connect_me_to_blocking(int phoneid, sysarg_t arg1, sysarg_t arg2,
1263 sysarg_t arg3)
1264{
1265 int rc;
1266 sysarg_t newphid;
1267
1268 rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
1269 IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
1270
1271 if (rc != EOK)
1272 return rc;
1273
1274 return newphid;
1275}
1276
1277/** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
1278 *
1279 * @param phoneid Phone that will be used to contact the receiving side.
1280 * @param dst Destination address space area base.
1281 * @param size Size of the destination address space area.
1282 * @param arg User defined argument.
1283 * @param flags Storage where the received flags will be stored. Can be
1284 * NULL.
1285 *
1286 * @return Zero on success or a negative error code from errno.h.
1287 */
1288int async_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
1289 int *flags)
1290{
1291 int res;
1292 sysarg_t tmp_flags;
1293 res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
1294 (sysarg_t) size, arg, NULL, &tmp_flags);
1295 if (flags)
1296 *flags = tmp_flags;
1297 return res;
1298}
1299
1300/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1301 *
1302 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN calls
1303 * so that the user doesn't have to remember the meaning of each IPC argument.
1304 *
1305 * So far, this wrapper is to be used from within a connection fibril.
1306 *
1307 * @param callid Storage where the hash of the IPC_M_SHARE_IN call will
1308 * be stored.
1309 * @param size Destination address space area size.
1310 *
1311 * @return Non-zero on success, zero on failure.
1312 */
1313int async_share_in_receive(ipc_callid_t *callid, size_t *size)
1314{
1315 ipc_call_t data;
1316
1317 assert(callid);
1318 assert(size);
1319
1320 *callid = async_get_call(&data);
1321 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
1322 return 0;
1323 *size = (size_t) IPC_GET_ARG2(data);
1324 return 1;
1325}
1326
1327/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1328 *
1329 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1330 * so that the user doesn't have to remember the meaning of each IPC argument.
1331 *
1332 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1333 * @param src Source address space base.
1334 * @param flags Flags to be used for sharing. Bits can be only cleared.
1335 *
1336 * @return Zero on success or a value from @ref errno.h on failure.
1337 */
1338int async_share_in_finalize(ipc_callid_t callid, void *src, int flags)
1339{
1340 return ipc_share_in_finalize(callid, src, flags);
1341}
1342
1343/** Wrapper for making IPC_M_SHARE_OUT calls using the async framework.
1344 *
1345 * @param phoneid Phone that will be used to contact the receiving side.
1346 * @param src Source address space area base address.
1347 * @param flags Flags to be used for sharing. Bits can be only cleared.
1348 *
1349 * @return Zero on success or a negative error code from errno.h.
1350 */
1351int async_share_out_start(int phoneid, void *src, int flags)
1352{
1353 return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
1354 (sysarg_t) flags);
1355}
1356
1357/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1358 *
1359 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT calls
1360 * so that the user doesn't have to remember the meaning of each IPC argument.
1361 *
1362 * So far, this wrapper is to be used from within a connection fibril.
1363 *
1364 * @param callid Storage where the hash of the IPC_M_SHARE_OUT call will
1365 * be stored.
1366 * @param size Storage where the source address space area size will be
1367 * stored.
1368 * @param flags Storage where the sharing flags will be stored.
1369 *
1370 * @return Non-zero on success, zero on failure.
1371 */
1372int async_share_out_receive(ipc_callid_t *callid, size_t *size, int *flags)
1373{
1374 ipc_call_t data;
1375
1376 assert(callid);
1377 assert(size);
1378 assert(flags);
1379
1380 *callid = async_get_call(&data);
1381 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
1382 return 0;
1383 *size = (size_t) IPC_GET_ARG2(data);
1384 *flags = (int) IPC_GET_ARG3(data);
1385 return 1;
1386}
1387
1388/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
1389 *
1390 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT calls
1391 * so that the user doesn't have to remember the meaning of each IPC argument.
1392 *
1393 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1394 * @param dst Destination address space area base address.
1395 *
1396 * @return Zero on success or a value from @ref errno.h on failure.
1397 */
1398int async_share_out_finalize(ipc_callid_t callid, void *dst)
1399{
1400 return ipc_share_out_finalize(callid, dst);
1401}
1402
1403
1404/** Wrapper for making IPC_M_DATA_READ calls using the async framework.
1405 *
1406 * @param phoneid Phone that will be used to contact the receiving side.
1407 * @param dst Address of the beginning of the destination buffer.
1408 * @param size Size of the destination buffer.
1409 *
1410 * @return Zero on success or a negative error code from errno.h.
1411 */
1412int async_data_read_start(int phoneid, void *dst, size_t size)
1413{
1414 return async_req_2_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
1415 (sysarg_t) size);
1416}
1417
1418/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
1419 *
1420 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ calls
1421 * so that the user doesn't have to remember the meaning of each IPC argument.
1422 *
1423 * So far, this wrapper is to be used from within a connection fibril.
1424 *
1425 * @param callid Storage where the hash of the IPC_M_DATA_READ call will
1426 * be stored.
1427 * @param size Storage where the maximum size will be stored. Can be
1428 * NULL.
1429 *
1430 * @return Non-zero on success, zero on failure.
1431 */
1432int async_data_read_receive(ipc_callid_t *callid, size_t *size)
1433{
1434 ipc_call_t data;
1435
1436 assert(callid);
1437
1438 *callid = async_get_call(&data);
1439 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
1440 return 0;
1441 if (size)
1442 *size = (size_t) IPC_GET_ARG2(data);
1443 return 1;
1444}
1445
1446/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
1447 *
1448 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1449 * so that the user doesn't have to remember the meaning of each IPC argument.
1450 *
1451 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1452 * @param src Source address for the IPC_M_DATA_READ call.
1453 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
1454 * the maximum size announced by the sender.
1455 *
1456 * @return Zero on success or a value from @ref errno.h on failure.
1457 */
1458int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
1459{
1460 return ipc_data_read_finalize(callid, src, size);
1461}
1462
1463/** Wrapper for forwarding any read request
1464 *
1465 *
1466 */
1467int async_data_read_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1468 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1469{
1470 ipc_callid_t callid;
1471 if (!async_data_read_receive(&callid, NULL)) {
1472 ipc_answer_0(callid, EINVAL);
1473 return EINVAL;
1474 }
1475
1476 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1477 dataptr);
1478 if (msg == 0) {
1479 ipc_answer_0(callid, EINVAL);
1480 return EINVAL;
1481 }
1482
1483 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1484 IPC_FF_ROUTE_FROM_ME);
1485 if (retval != EOK) {
1486 async_wait_for(msg, NULL);
1487 ipc_answer_0(callid, retval);
1488 return retval;
1489 }
1490
1491 sysarg_t rc;
1492 async_wait_for(msg, &rc);
1493
1494 return (int) rc;
1495}
1496
1497/** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
1498 *
1499 * @param phoneid Phone that will be used to contact the receiving side.
1500 * @param src Address of the beginning of the source buffer.
1501 * @param size Size of the source buffer.
1502 *
1503 * @return Zero on success or a negative error code from errno.h.
1504 *
1505 */
1506int async_data_write_start(int phoneid, const void *src, size_t size)
1507{
1508 return async_req_2_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
1509 (sysarg_t) size);
1510}
1511
1512/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
1513 *
1514 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE calls
1515 * so that the user doesn't have to remember the meaning of each IPC argument.
1516 *
1517 * So far, this wrapper is to be used from within a connection fibril.
1518 *
1519 * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
1520 * be stored.
1521 * @param size Storage where the suggested size will be stored. May be
1522 * NULL
1523 *
1524 * @return Non-zero on success, zero on failure.
1525 *
1526 */
1527int async_data_write_receive(ipc_callid_t *callid, size_t *size)
1528{
1529 ipc_call_t data;
1530
1531 assert(callid);
1532
1533 *callid = async_get_call(&data);
1534 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
1535 return 0;
1536
1537 if (size)
1538 *size = (size_t) IPC_GET_ARG2(data);
1539
1540 return 1;
1541}
1542
1543/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
1544 *
1545 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
1546 * so that the user doesn't have to remember the meaning of each IPC argument.
1547 *
1548 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1549 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
1550 * @param size Final size for the IPC_M_DATA_WRITE call.
1551 *
1552 * @return Zero on success or a value from @ref errno.h on failure.
1553 *
1554 */
1555int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
1556{
1557 return ipc_data_write_finalize(callid, dst, size);
1558}
1559
1560/** Wrapper for receiving binary data or strings
1561 *
1562 * This wrapper only makes it more comfortable to use async_data_write_*
1563 * functions to receive binary data or strings.
1564 *
1565 * @param data Pointer to data pointer (which should be later disposed
1566 * by free()). If the operation fails, the pointer is not
1567 * touched.
1568 * @param nullterm If true then the received data is always zero terminated.
1569 * This also causes to allocate one extra byte beyond the
1570 * raw transmitted data.
1571 * @param min_size Minimum size (in bytes) of the data to receive.
1572 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
1573 * no limit.
1574 * @param granulariy If non-zero then the size of the received data has to
1575 * be divisible by this value.
1576 * @param received If not NULL, the size of the received data is stored here.
1577 *
1578 * @return Zero on success or a value from @ref errno.h on failure.
1579 *
1580 */
1581int async_data_write_accept(void **data, const bool nullterm,
1582 const size_t min_size, const size_t max_size, const size_t granularity,
1583 size_t *received)
1584{
1585 ipc_callid_t callid;
1586 size_t size;
1587 if (!async_data_write_receive(&callid, &size)) {
1588 ipc_answer_0(callid, EINVAL);
1589 return EINVAL;
1590 }
1591
1592 if (size < min_size) {
1593 ipc_answer_0(callid, EINVAL);
1594 return EINVAL;
1595 }
1596
1597 if ((max_size > 0) && (size > max_size)) {
1598 ipc_answer_0(callid, EINVAL);
1599 return EINVAL;
1600 }
1601
1602 if ((granularity > 0) && ((size % granularity) != 0)) {
1603 ipc_answer_0(callid, EINVAL);
1604 return EINVAL;
1605 }
1606
1607 void *_data;
1608
1609 if (nullterm)
1610 _data = malloc(size + 1);
1611 else
1612 _data = malloc(size);
1613
1614 if (_data == NULL) {
1615 ipc_answer_0(callid, ENOMEM);
1616 return ENOMEM;
1617 }
1618
1619 int rc = async_data_write_finalize(callid, _data, size);
1620 if (rc != EOK) {
1621 free(_data);
1622 return rc;
1623 }
1624
1625 if (nullterm)
1626 ((char *) _data)[size] = 0;
1627
1628 *data = _data;
1629 if (received != NULL)
1630 *received = size;
1631
1632 return EOK;
1633}
1634
1635/** Wrapper for voiding any data that is about to be received
1636 *
1637 * This wrapper can be used to void any pending data
1638 *
1639 * @param retval Error value from @ref errno.h to be returned to the caller.
1640 *
1641 */
1642void async_data_write_void(const int retval)
1643{
1644 ipc_callid_t callid;
1645 async_data_write_receive(&callid, NULL);
1646 ipc_answer_0(callid, retval);
1647}
1648
1649/** Wrapper for forwarding any data that is about to be received
1650 *
1651 *
1652 */
1653int async_data_write_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1654 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1655{
1656 ipc_callid_t callid;
1657 if (!async_data_write_receive(&callid, NULL)) {
1658 ipc_answer_0(callid, EINVAL);
1659 return EINVAL;
1660 }
1661
1662 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1663 dataptr);
1664 if (msg == 0) {
1665 ipc_answer_0(callid, EINVAL);
1666 return EINVAL;
1667 }
1668
1669 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1670 IPC_FF_ROUTE_FROM_ME);
1671 if (retval != EOK) {
1672 async_wait_for(msg, NULL);
1673 ipc_answer_0(callid, retval);
1674 return retval;
1675 }
1676
1677 sysarg_t rc;
1678 async_wait_for(msg, &rc);
1679
1680 return (int) rc;
1681}
1682
1683/** @}
1684 */
Note: See TracBrowser for help on using the repository browser.