source: mainline/uspace/lib/libc/generic/async.c@ 4f5edcf6

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

Prepare awaiter_t for use outside of async.c.

  • Property mode set to 100644
File size: 27.1 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 ipcarg_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 awaiter_t wdata;
137
138 /** Hash table link. */
139 link_t link;
140
141 /** Incoming phone hash. */
142 ipcarg_t in_phone_hash;
143
144 /** Messages that should be delivered to this fibril. */
145 link_t msg_queue;
146
147 /** Identification of the opening call. */
148 ipc_callid_t callid;
149 /** Call data of the opening call. */
150 ipc_call_t call;
151
152 /** Identification of the closing call. */
153 ipc_callid_t close_callid;
154
155 /** Fibril function that will be used to handle the connection. */
156 void (*cfibril)(ipc_callid_t, ipc_call_t *);
157} connection_t;
158
159/** Identifier of the incoming connection handled by the current fibril. */
160fibril_local connection_t *FIBRIL_connection;
161
162static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
163static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
164
165/**
166 * Pointer to a fibril function that will be used to handle connections.
167 */
168static async_client_conn_t client_connection = default_client_connection;
169
170/**
171 * Pointer to a fibril function that will be used to handle interrupt
172 * notifications.
173 */
174static async_client_conn_t interrupt_received = default_interrupt_received;
175
176static hash_table_t conn_hash_table;
177static LIST_INITIALIZE(timeout_list);
178
179#define CONN_HASH_TABLE_CHAINS 32
180
181/** Compute hash into the connection hash table based on the source phone hash.
182 *
183 * @param key Pointer to source phone hash.
184 *
185 * @return Index into the connection hash table.
186 *
187 */
188static hash_index_t conn_hash(unsigned long *key)
189{
190 assert(key);
191 return (((*key) >> 4) % CONN_HASH_TABLE_CHAINS);
192}
193
194/** Compare hash table item with a key.
195 *
196 * @param key Array containing the source phone hash as the only item.
197 * @param keys Expected 1 but ignored.
198 * @param item Connection hash table item.
199 *
200 * @return True on match, false otherwise.
201 *
202 */
203static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
204{
205 connection_t *hs = hash_table_get_instance(item, connection_t, link);
206 return (key[0] == hs->in_phone_hash);
207}
208
209/** Connection hash table removal callback function.
210 *
211 * This function is called whenever a connection is removed from the connection
212 * hash table.
213 *
214 * @param item Connection hash table item being removed.
215 *
216 */
217static void conn_remove(link_t *item)
218{
219 free(hash_table_get_instance(item, connection_t, link));
220}
221
222
223/** Operations for the connection hash table. */
224static hash_table_operations_t conn_hash_table_ops = {
225 .hash = conn_hash,
226 .compare = conn_compare,
227 .remove_callback = conn_remove
228};
229
230/** Sort in current fibril's timeout request.
231 *
232 * @param wd Wait data of the current fibril.
233 *
234 */
235static void insert_timeout(awaiter_t *wd)
236{
237 wd->timedout = false;
238 wd->inlist = true;
239
240 link_t *tmp = timeout_list.next;
241 while (tmp != &timeout_list) {
242 awaiter_t *cur = list_get_instance(tmp, awaiter_t, link);
243
244 if (tv_gteq(&cur->expires, &wd->expires))
245 break;
246
247 tmp = tmp->next;
248 }
249
250 list_append(&wd->link, tmp);
251}
252
253/** Try to route a call to an appropriate connection fibril.
254 *
255 * If the proper connection fibril is found, a message with the call is added to
256 * its message queue. If the fibril was not active, it is activated and all
257 * timeouts are unregistered.
258 *
259 * @param callid Hash of the incoming call.
260 * @param call Data of the incoming call.
261 *
262 * @return False if the call doesn't match any connection.
263 * True if the call was passed to the respective connection fibril.
264 *
265 */
266static bool route_call(ipc_callid_t callid, ipc_call_t *call)
267{
268 futex_down(&async_futex);
269
270 unsigned long key = call->in_phone_hash;
271 link_t *hlp = hash_table_find(&conn_hash_table, &key);
272
273 if (!hlp) {
274 futex_up(&async_futex);
275 return false;
276 }
277
278 connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
279
280 msg_t *msg = malloc(sizeof(*msg));
281 if (!msg) {
282 futex_up(&async_futex);
283 return false;
284 }
285
286 msg->callid = callid;
287 msg->call = *call;
288 list_append(&msg->link, &conn->msg_queue);
289
290 if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP)
291 conn->close_callid = callid;
292
293 /* If the connection fibril is waiting for an event, activate it */
294 if (!conn->wdata.active) {
295
296 /* If in timeout list, remove it */
297 if (conn->wdata.inlist) {
298 conn->wdata.inlist = false;
299 list_remove(&conn->wdata.link);
300 }
301
302 conn->wdata.active = true;
303 fibril_add_ready(conn->wdata.fid);
304 }
305
306 futex_up(&async_futex);
307 return true;
308}
309
310/** Notification fibril.
311 *
312 * When a notification arrives, a fibril with this implementing function is
313 * created. It calls interrupt_received() and does the final cleanup.
314 *
315 * @param arg Message structure pointer.
316 *
317 * @return Always zero.
318 *
319 */
320static int notification_fibril(void *arg)
321{
322 msg_t *msg = (msg_t *) arg;
323 interrupt_received(msg->callid, &msg->call);
324
325 free(msg);
326 return 0;
327}
328
329/** Process interrupt notification.
330 *
331 * A new fibril is created which would process the notification.
332 *
333 * @param callid Hash of the incoming call.
334 * @param call Data of the incoming call.
335 *
336 * @return False if an error occured.
337 * True if the call was passed to the notification fibril.
338 *
339 */
340static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
341{
342 futex_down(&async_futex);
343
344 msg_t *msg = malloc(sizeof(*msg));
345 if (!msg) {
346 futex_up(&async_futex);
347 return false;
348 }
349
350 msg->callid = callid;
351 msg->call = *call;
352
353 fid_t fid = fibril_create(notification_fibril, msg);
354 fibril_add_ready(fid);
355
356 futex_up(&async_futex);
357 return true;
358}
359
360/** Return new incoming message for the current (fibril-local) connection.
361 *
362 * @param call Storage where the incoming call data will be stored.
363 * @param usecs Timeout in microseconds. Zero denotes no timeout.
364 *
365 * @return If no timeout was specified, then a hash of the
366 * incoming call is returned. If a timeout is specified,
367 * then a hash of the incoming call is returned unless
368 * the timeout expires prior to receiving a message. In
369 * that case zero is returned.
370 *
371 */
372ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
373{
374 assert(FIBRIL_connection);
375
376 /* Why doing this?
377 * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
378 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
379 * I would never expect to find so many errors in
380 * a compiler.
381 */
382 connection_t *conn = FIBRIL_connection;
383
384 futex_down(&async_futex);
385
386 if (usecs) {
387 gettimeofday(&conn->wdata.expires, NULL);
388 tv_add(&conn->wdata.expires, usecs);
389 } else
390 conn->wdata.inlist = false;
391
392 /* If nothing in queue, wait until something arrives */
393 while (list_empty(&conn->msg_queue)) {
394 if (usecs)
395 insert_timeout(&conn->wdata);
396
397 conn->wdata.active = false;
398
399 /*
400 * Note: the current fibril will be rescheduled either due to a
401 * timeout or due to an arriving message destined to it. In the
402 * former case, handle_expired_timeouts() and, in the latter
403 * case, route_call() will perform the wakeup.
404 */
405 fibril_switch(FIBRIL_TO_MANAGER);
406
407 /*
408 * Futex is up after getting back from async_manager.
409 * Get it again.
410 */
411 futex_down(&async_futex);
412 if ((usecs) && (conn->wdata.timedout)
413 && (list_empty(&conn->msg_queue))) {
414 /* If we timed out -> exit */
415 futex_up(&async_futex);
416 return 0;
417 }
418 }
419
420 msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
421 list_remove(&msg->link);
422
423 ipc_callid_t callid = msg->callid;
424 *call = msg->call;
425 free(msg);
426
427 futex_up(&async_futex);
428 return callid;
429}
430
431/** Default fibril function that gets called to handle new connection.
432 *
433 * This function is defined as a weak symbol - to be redefined in user code.
434 *
435 * @param callid Hash of the incoming call.
436 * @param call Data of the incoming call.
437 *
438 */
439static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
440{
441 ipc_answer_0(callid, ENOENT);
442}
443
444/** Default fibril function that gets called to handle interrupt notifications.
445 *
446 * This function is defined as a weak symbol - to be redefined in user code.
447 *
448 * @param callid Hash of the incoming call.
449 * @param call Data of the incoming call.
450 *
451 */
452static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
453{
454}
455
456/** Wrapper for client connection fibril.
457 *
458 * When a new connection arrives, a fibril with this implementing function is
459 * created. It calls client_connection() and does the final cleanup.
460 *
461 * @param arg Connection structure pointer.
462 *
463 * @return Always zero.
464 *
465 */
466static int connection_fibril(void *arg)
467{
468 /*
469 * Setup fibril-local connection pointer and call client_connection().
470 *
471 */
472 FIBRIL_connection = (connection_t *) arg;
473 FIBRIL_connection->cfibril(FIBRIL_connection->callid,
474 &FIBRIL_connection->call);
475
476 /* Remove myself from the connection hash table */
477 futex_down(&async_futex);
478 unsigned long key = FIBRIL_connection->in_phone_hash;
479 hash_table_remove(&conn_hash_table, &key, 1);
480 futex_up(&async_futex);
481
482 /* Answer all remaining messages with EHANGUP */
483 while (!list_empty(&FIBRIL_connection->msg_queue)) {
484 msg_t *msg;
485
486 msg = list_get_instance(FIBRIL_connection->msg_queue.next,
487 msg_t, link);
488 list_remove(&msg->link);
489 ipc_answer_0(msg->callid, EHANGUP);
490 free(msg);
491 }
492
493 if (FIBRIL_connection->close_callid)
494 ipc_answer_0(FIBRIL_connection->close_callid, EOK);
495
496 return 0;
497}
498
499/** Create a new fibril for a new connection.
500 *
501 * Create new fibril for connection, fill in connection structures and inserts
502 * it into the hash table, so that later we can easily do routing of messages to
503 * particular fibrils.
504 *
505 * @param in_phone_hash Identification of the incoming connection.
506 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
507 * If callid is zero, the connection was opened by
508 * accepting the IPC_M_CONNECT_TO_ME call and this function
509 * is called directly by the server.
510 * @param call Call data of the opening call.
511 * @param cfibril Fibril function that should be called upon opening the
512 * connection.
513 *
514 * @return New fibril id or NULL on failure.
515 *
516 */
517fid_t async_new_connection(ipcarg_t in_phone_hash, ipc_callid_t callid,
518 ipc_call_t *call, void (*cfibril)(ipc_callid_t, ipc_call_t *))
519{
520 connection_t *conn = malloc(sizeof(*conn));
521 if (!conn) {
522 if (callid)
523 ipc_answer_0(callid, ENOMEM);
524 return NULL;
525 }
526
527 conn->in_phone_hash = in_phone_hash;
528 list_initialize(&conn->msg_queue);
529 conn->callid = callid;
530 conn->close_callid = false;
531
532 if (call)
533 conn->call = *call;
534
535 /* We will activate the fibril ASAP */
536 conn->wdata.active = true;
537 conn->cfibril = cfibril;
538 conn->wdata.fid = fibril_create(connection_fibril, conn);
539
540 if (!conn->wdata.fid) {
541 free(conn);
542 if (callid)
543 ipc_answer_0(callid, ENOMEM);
544 return NULL;
545 }
546
547 /* Add connection to the connection hash table */
548 unsigned long key = conn->in_phone_hash;
549
550 futex_down(&async_futex);
551 hash_table_insert(&conn_hash_table, &key, &conn->link);
552 futex_up(&async_futex);
553
554 fibril_add_ready(conn->wdata.fid);
555
556 return conn->wdata.fid;
557}
558
559/** Handle a call that was received.
560 *
561 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
562 * Otherwise the call is routed to its connection fibril.
563 *
564 * @param callid Hash of the incoming call.
565 * @param call Data of the incoming call.
566 *
567 */
568static void handle_call(ipc_callid_t callid, ipc_call_t *call)
569{
570 /* Unrouted call - do some default behaviour */
571 if ((callid & IPC_CALLID_NOTIFICATION)) {
572 process_notification(callid, call);
573 goto out;
574 }
575
576 switch (IPC_GET_METHOD(*call)) {
577 case IPC_M_CONNECT_ME:
578 case IPC_M_CONNECT_ME_TO:
579 /* Open new connection with fibril etc. */
580 async_new_connection(IPC_GET_ARG5(*call), callid, call,
581 client_connection);
582 goto out;
583 }
584
585 /* Try to route the call through the connection hash table */
586 if (route_call(callid, call))
587 goto out;
588
589 /* Unknown call from unknown phone - hang it up */
590 ipc_answer_0(callid, EHANGUP);
591 return;
592
593out:
594 ;
595}
596
597/** Fire all timeouts that expired. */
598static void handle_expired_timeouts(void)
599{
600 struct timeval tv;
601 gettimeofday(&tv, NULL);
602
603 futex_down(&async_futex);
604
605 link_t *cur = timeout_list.next;
606 while (cur != &timeout_list) {
607 awaiter_t *waiter = list_get_instance(cur, awaiter_t, link);
608
609 if (tv_gt(&waiter->expires, &tv))
610 break;
611
612 cur = cur->next;
613
614 list_remove(&waiter->link);
615 waiter->inlist = false;
616 waiter->timedout = true;
617
618 /*
619 * Redundant condition?
620 * The fibril should not be active when it gets here.
621 */
622 if (!waiter->active) {
623 waiter->active = true;
624 fibril_add_ready(waiter->fid);
625 }
626 }
627
628 futex_up(&async_futex);
629}
630
631/** Endless loop dispatching incoming calls and answers.
632 *
633 * @return Never returns.
634 *
635 */
636static int async_manager_worker(void)
637{
638 while (true) {
639 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
640 futex_up(&async_futex);
641 /*
642 * async_futex is always held when entering a manager
643 * fibril.
644 */
645 continue;
646 }
647
648 futex_down(&async_futex);
649
650 suseconds_t timeout;
651 if (!list_empty(&timeout_list)) {
652 awaiter_t *waiter = list_get_instance(timeout_list.next,
653 awaiter_t, link);
654
655 struct timeval tv;
656 gettimeofday(&tv, NULL);
657
658 if (tv_gteq(&tv, &waiter->expires)) {
659 futex_up(&async_futex);
660 handle_expired_timeouts();
661 continue;
662 } else
663 timeout = tv_sub(&waiter->expires, &tv);
664 } else
665 timeout = SYNCH_NO_TIMEOUT;
666
667 futex_up(&async_futex);
668
669 atomic_inc(&threads_in_ipc_wait);
670
671 ipc_call_t call;
672 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
673 SYNCH_FLAGS_NONE);
674
675 atomic_dec(&threads_in_ipc_wait);
676
677 if (!callid) {
678 handle_expired_timeouts();
679 continue;
680 }
681
682 if (callid & IPC_CALLID_ANSWERED)
683 continue;
684
685 handle_call(callid, &call);
686 }
687
688 return 0;
689}
690
691/** Function to start async_manager as a standalone fibril.
692 *
693 * When more kernel threads are used, one async manager should exist per thread.
694 *
695 * @param arg Unused.
696 * @return Never returns.
697 *
698 */
699static int async_manager_fibril(void *arg)
700{
701 futex_up(&async_futex);
702
703 /*
704 * async_futex is always locked when entering manager
705 */
706 async_manager_worker();
707
708 return 0;
709}
710
711/** Add one manager to manager list. */
712void async_create_manager(void)
713{
714 fid_t fid = fibril_create(async_manager_fibril, NULL);
715 fibril_add_manager(fid);
716}
717
718/** Remove one manager from manager list */
719void async_destroy_manager(void)
720{
721 fibril_remove_manager();
722}
723
724/** Initialize the async framework.
725 *
726 * @return Zero on success or an error code.
727 */
728int __async_init(void)
729{
730 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
731 &conn_hash_table_ops)) {
732 printf("%s: cannot create hash table\n", "async");
733 return ENOMEM;
734 }
735
736 return 0;
737}
738
739/** Reply received callback.
740 *
741 * This function is called whenever a reply for an asynchronous message sent out
742 * by the asynchronous framework is received.
743 *
744 * Notify the fibril which is waiting for this message that it has arrived.
745 *
746 * @param arg Pointer to the asynchronous message record.
747 * @param retval Value returned in the answer.
748 * @param data Call data of the answer.
749 */
750static void reply_received(void *arg, int retval, ipc_call_t *data)
751{
752 futex_down(&async_futex);
753
754 amsg_t *msg = (amsg_t *) arg;
755 msg->retval = retval;
756
757 /* Copy data after futex_down, just in case the call was detached */
758 if ((msg->dataptr) && (data))
759 *msg->dataptr = *data;
760
761 write_barrier();
762
763 /* Remove message from timeout list */
764 if (msg->wdata.inlist)
765 list_remove(&msg->wdata.link);
766
767 msg->done = true;
768 if (!msg->wdata.active) {
769 msg->wdata.active = true;
770 fibril_add_ready(msg->wdata.fid);
771 }
772
773 futex_up(&async_futex);
774}
775
776/** Send message and return id of the sent message.
777 *
778 * The return value can be used as input for async_wait() to wait for
779 * completion.
780 *
781 * @param phoneid Handle of the phone that will be used for the send.
782 * @param method Service-defined method.
783 * @param arg1 Service-defined payload argument.
784 * @param arg2 Service-defined payload argument.
785 * @param arg3 Service-defined payload argument.
786 * @param arg4 Service-defined payload argument.
787 * @param dataptr If non-NULL, storage where the reply data will be
788 * stored.
789 *
790 * @return Hash of the sent message or 0 on error.
791 *
792 */
793aid_t async_send_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
794 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
795{
796 amsg_t *msg = malloc(sizeof(*msg));
797
798 if (!msg)
799 return 0;
800
801 msg->done = false;
802 msg->dataptr = dataptr;
803
804 msg->wdata.inlist = false;
805 /* We may sleep in the next method, but it will use its own mechanism */
806 msg->wdata.active = true;
807
808 ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
809 reply_received, true);
810
811 return (aid_t) msg;
812}
813
814/** Send message and return id of the sent message
815 *
816 * The return value can be used as input for async_wait() to wait for
817 * completion.
818 *
819 * @param phoneid Handle of the phone that will be used for the send.
820 * @param method Service-defined method.
821 * @param arg1 Service-defined payload argument.
822 * @param arg2 Service-defined payload argument.
823 * @param arg3 Service-defined payload argument.
824 * @param arg4 Service-defined payload argument.
825 * @param arg5 Service-defined payload argument.
826 * @param dataptr If non-NULL, storage where the reply data will be
827 * stored.
828 *
829 * @return Hash of the sent message or 0 on error.
830 *
831 */
832aid_t async_send_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
833 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5,
834 ipc_call_t *dataptr)
835{
836 amsg_t *msg = malloc(sizeof(*msg));
837
838 if (!msg)
839 return 0;
840
841 msg->done = false;
842 msg->dataptr = dataptr;
843
844 msg->wdata.inlist = false;
845 /* We may sleep in next method, but it will use its own mechanism */
846 msg->wdata.active = true;
847
848 ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
849 reply_received, true);
850
851 return (aid_t) msg;
852}
853
854/** Wait for a message sent by the async framework.
855 *
856 * @param amsgid Hash of the message to wait for.
857 * @param retval Pointer to storage where the retval of the answer will
858 * be stored.
859 *
860 */
861void async_wait_for(aid_t amsgid, ipcarg_t *retval)
862{
863 amsg_t *msg = (amsg_t *) amsgid;
864
865 futex_down(&async_futex);
866 if (msg->done) {
867 futex_up(&async_futex);
868 goto done;
869 }
870
871 msg->wdata.fid = fibril_get_id();
872 msg->wdata.active = false;
873 msg->wdata.inlist = false;
874
875 /* Leave the async_futex locked when entering this function */
876 fibril_switch(FIBRIL_TO_MANAGER);
877
878 /* Futex is up automatically after fibril_switch */
879
880done:
881 if (retval)
882 *retval = msg->retval;
883
884 free(msg);
885}
886
887/** Wait for a message sent by the async framework, timeout variant.
888 *
889 * @param amsgid Hash of the message to wait for.
890 * @param retval Pointer to storage where the retval of the answer will
891 * be stored.
892 * @param timeout Timeout in microseconds.
893 *
894 * @return Zero on success, ETIMEOUT if the timeout has expired.
895 *
896 */
897int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
898{
899 amsg_t *msg = (amsg_t *) amsgid;
900
901 /* TODO: Let it go through the event read at least once */
902 if (timeout < 0)
903 return ETIMEOUT;
904
905 futex_down(&async_futex);
906 if (msg->done) {
907 futex_up(&async_futex);
908 goto done;
909 }
910
911 gettimeofday(&msg->wdata.expires, NULL);
912 tv_add(&msg->wdata.expires, timeout);
913
914 msg->wdata.fid = fibril_get_id();
915 msg->wdata.active = false;
916 insert_timeout(&msg->wdata);
917
918 /* Leave the async_futex locked when entering this function */
919 fibril_switch(FIBRIL_TO_MANAGER);
920
921 /* Futex is up automatically after fibril_switch */
922
923 if (!msg->done)
924 return ETIMEOUT;
925
926done:
927 if (retval)
928 *retval = msg->retval;
929
930 free(msg);
931
932 return 0;
933}
934
935/** Wait for specified time.
936 *
937 * The current fibril is suspended but the thread continues to execute.
938 *
939 * @param timeout Duration of the wait in microseconds.
940 *
941 */
942void async_usleep(suseconds_t timeout)
943{
944 amsg_t *msg = malloc(sizeof(*msg));
945
946 if (!msg)
947 return;
948
949 msg->wdata.fid = fibril_get_id();
950 msg->wdata.active = false;
951
952 gettimeofday(&msg->wdata.expires, NULL);
953 tv_add(&msg->wdata.expires, timeout);
954
955 futex_down(&async_futex);
956
957 insert_timeout(&msg->wdata);
958
959 /* Leave the async_futex locked when entering this function */
960 fibril_switch(FIBRIL_TO_MANAGER);
961
962 /* Futex is up automatically after fibril_switch() */
963
964 free(msg);
965}
966
967/** Setter for client_connection function pointer.
968 *
969 * @param conn Function that will implement a new connection fibril.
970 *
971 */
972void async_set_client_connection(async_client_conn_t conn)
973{
974 client_connection = conn;
975}
976
977/** Setter for interrupt_received function pointer.
978 *
979 * @param intr Function that will implement a new interrupt
980 * notification fibril.
981 */
982void async_set_interrupt_received(async_client_conn_t intr)
983{
984 interrupt_received = intr;
985}
986
987/** Pseudo-synchronous message sending - fast version.
988 *
989 * Send message asynchronously and return only after the reply arrives.
990 *
991 * This function can only transfer 4 register payload arguments. For
992 * transferring more arguments, see the slower async_req_slow().
993 *
994 * @param phoneid Hash of the phone through which to make the call.
995 * @param method Method of the call.
996 * @param arg1 Service-defined payload argument.
997 * @param arg2 Service-defined payload argument.
998 * @param arg3 Service-defined payload argument.
999 * @param arg4 Service-defined payload argument.
1000 * @param r1 If non-NULL, storage for the 1st reply argument.
1001 * @param r2 If non-NULL, storage for the 2nd reply argument.
1002 * @param r3 If non-NULL, storage for the 3rd reply argument.
1003 * @param r4 If non-NULL, storage for the 4th reply argument.
1004 * @param r5 If non-NULL, storage for the 5th reply argument.
1005 *
1006 * @return Return code of the reply or a negative error code.
1007 *
1008 */
1009ipcarg_t async_req_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
1010 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t *r1, ipcarg_t *r2,
1011 ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
1012{
1013 ipc_call_t result;
1014 aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1015 &result);
1016
1017 ipcarg_t rc;
1018 async_wait_for(eid, &rc);
1019
1020 if (r1)
1021 *r1 = IPC_GET_ARG1(result);
1022
1023 if (r2)
1024 *r2 = IPC_GET_ARG2(result);
1025
1026 if (r3)
1027 *r3 = IPC_GET_ARG3(result);
1028
1029 if (r4)
1030 *r4 = IPC_GET_ARG4(result);
1031
1032 if (r5)
1033 *r5 = IPC_GET_ARG5(result);
1034
1035 return rc;
1036}
1037
1038/** Pseudo-synchronous message sending - slow version.
1039 *
1040 * Send message asynchronously and return only after the reply arrives.
1041 *
1042 * @param phoneid Hash of the phone through which to make the call.
1043 * @param method Method of the call.
1044 * @param arg1 Service-defined payload argument.
1045 * @param arg2 Service-defined payload argument.
1046 * @param arg3 Service-defined payload argument.
1047 * @param arg4 Service-defined payload argument.
1048 * @param arg5 Service-defined payload argument.
1049 * @param r1 If non-NULL, storage for the 1st reply argument.
1050 * @param r2 If non-NULL, storage for the 2nd reply argument.
1051 * @param r3 If non-NULL, storage for the 3rd reply argument.
1052 * @param r4 If non-NULL, storage for the 4th reply argument.
1053 * @param r5 If non-NULL, storage for the 5th reply argument.
1054 *
1055 * @return Return code of the reply or a negative error code.
1056 *
1057 */
1058ipcarg_t async_req_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
1059 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *r1,
1060 ipcarg_t *r2, ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
1061{
1062 ipc_call_t result;
1063 aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1064 &result);
1065
1066 ipcarg_t rc;
1067 async_wait_for(eid, &rc);
1068
1069 if (r1)
1070 *r1 = IPC_GET_ARG1(result);
1071
1072 if (r2)
1073 *r2 = IPC_GET_ARG2(result);
1074
1075 if (r3)
1076 *r3 = IPC_GET_ARG3(result);
1077
1078 if (r4)
1079 *r4 = IPC_GET_ARG4(result);
1080
1081 if (r5)
1082 *r5 = IPC_GET_ARG5(result);
1083
1084 return rc;
1085}
1086
1087/** @}
1088 */
Note: See TracBrowser for help on using the repository browser.