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

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

Rename insert_timeout() to async_insert_timeout() and make it a private global
interface.

  • Property mode set to 100644
File size: 27.4 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 */
235void async_insert_timeout(awaiter_t *wd)
236{
237 wd->to_event.occurred = false;
238 wd->to_event.inlist = true;
239
240 link_t *tmp = timeout_list.next;
241 while (tmp != &timeout_list) {
242 awaiter_t *cur;
243
244 cur = list_get_instance(tmp, awaiter_t, to_event.link);
245 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
246 break;
247 tmp = tmp->next;
248 }
249
250 list_append(&wd->to_event.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.to_event.inlist) {
298 conn->wdata.to_event.inlist = false;
299 list_remove(&conn->wdata.to_event.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.to_event.expires, NULL);
388 tv_add(&conn->wdata.to_event.expires, usecs);
389 } else
390 conn->wdata.to_event.inlist = false;
391
392 /* If nothing in queue, wait until something arrives */
393 while (list_empty(&conn->msg_queue)) {
394 if (usecs)
395 async_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.to_event.occurred)
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;
608
609 waiter = list_get_instance(cur, awaiter_t, to_event.link);
610 if (tv_gt(&waiter->to_event.expires, &tv))
611 break;
612
613 cur = cur->next;
614
615 list_remove(&waiter->to_event.link);
616 waiter->to_event.inlist = false;
617 waiter->to_event.occurred = true;
618
619 /*
620 * Redundant condition?
621 * The fibril should not be active when it gets here.
622 */
623 if (!waiter->active) {
624 waiter->active = true;
625 fibril_add_ready(waiter->fid);
626 }
627 }
628
629 futex_up(&async_futex);
630}
631
632/** Endless loop dispatching incoming calls and answers.
633 *
634 * @return Never returns.
635 *
636 */
637static int async_manager_worker(void)
638{
639 while (true) {
640 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
641 futex_up(&async_futex);
642 /*
643 * async_futex is always held when entering a manager
644 * fibril.
645 */
646 continue;
647 }
648
649 futex_down(&async_futex);
650
651 suseconds_t timeout;
652 if (!list_empty(&timeout_list)) {
653 awaiter_t *waiter = list_get_instance(timeout_list.next,
654 awaiter_t, to_event.link);
655
656 struct timeval tv;
657 gettimeofday(&tv, NULL);
658
659 if (tv_gteq(&tv, &waiter->to_event.expires)) {
660 futex_up(&async_futex);
661 handle_expired_timeouts();
662 continue;
663 } else
664 timeout = tv_sub(&waiter->to_event.expires,
665 &tv);
666 } else
667 timeout = SYNCH_NO_TIMEOUT;
668
669 futex_up(&async_futex);
670
671 atomic_inc(&threads_in_ipc_wait);
672
673 ipc_call_t call;
674 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
675 SYNCH_FLAGS_NONE);
676
677 atomic_dec(&threads_in_ipc_wait);
678
679 if (!callid) {
680 handle_expired_timeouts();
681 continue;
682 }
683
684 if (callid & IPC_CALLID_ANSWERED)
685 continue;
686
687 handle_call(callid, &call);
688 }
689
690 return 0;
691}
692
693/** Function to start async_manager as a standalone fibril.
694 *
695 * When more kernel threads are used, one async manager should exist per thread.
696 *
697 * @param arg Unused.
698 * @return Never returns.
699 *
700 */
701static int async_manager_fibril(void *arg)
702{
703 futex_up(&async_futex);
704
705 /*
706 * async_futex is always locked when entering manager
707 */
708 async_manager_worker();
709
710 return 0;
711}
712
713/** Add one manager to manager list. */
714void async_create_manager(void)
715{
716 fid_t fid = fibril_create(async_manager_fibril, NULL);
717 fibril_add_manager(fid);
718}
719
720/** Remove one manager from manager list */
721void async_destroy_manager(void)
722{
723 fibril_remove_manager();
724}
725
726/** Initialize the async framework.
727 *
728 * @return Zero on success or an error code.
729 */
730int __async_init(void)
731{
732 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
733 &conn_hash_table_ops)) {
734 printf("%s: cannot create hash table\n", "async");
735 return ENOMEM;
736 }
737
738 return 0;
739}
740
741/** Reply received callback.
742 *
743 * This function is called whenever a reply for an asynchronous message sent out
744 * by the asynchronous framework is received.
745 *
746 * Notify the fibril which is waiting for this message that it has arrived.
747 *
748 * @param arg Pointer to the asynchronous message record.
749 * @param retval Value returned in the answer.
750 * @param data Call data of the answer.
751 */
752static void reply_received(void *arg, int retval, ipc_call_t *data)
753{
754 futex_down(&async_futex);
755
756 amsg_t *msg = (amsg_t *) arg;
757 msg->retval = retval;
758
759 /* Copy data after futex_down, just in case the call was detached */
760 if ((msg->dataptr) && (data))
761 *msg->dataptr = *data;
762
763 write_barrier();
764
765 /* Remove message from timeout list */
766 if (msg->wdata.to_event.inlist)
767 list_remove(&msg->wdata.to_event.link);
768
769 msg->done = true;
770 if (!msg->wdata.active) {
771 msg->wdata.active = true;
772 fibril_add_ready(msg->wdata.fid);
773 }
774
775 futex_up(&async_futex);
776}
777
778/** Send message and return id of the sent message.
779 *
780 * The return value can be used as input for async_wait() to wait for
781 * completion.
782 *
783 * @param phoneid Handle of the phone that will be used for the send.
784 * @param method Service-defined method.
785 * @param arg1 Service-defined payload argument.
786 * @param arg2 Service-defined payload argument.
787 * @param arg3 Service-defined payload argument.
788 * @param arg4 Service-defined payload argument.
789 * @param dataptr If non-NULL, storage where the reply data will be
790 * stored.
791 *
792 * @return Hash of the sent message or 0 on error.
793 *
794 */
795aid_t async_send_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
796 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
797{
798 amsg_t *msg = malloc(sizeof(*msg));
799
800 if (!msg)
801 return 0;
802
803 msg->done = false;
804 msg->dataptr = dataptr;
805
806 msg->wdata.to_event.inlist = false;
807 /* We may sleep in the next method, but it will use its own mechanism */
808 msg->wdata.active = true;
809
810 ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
811 reply_received, true);
812
813 return (aid_t) msg;
814}
815
816/** Send message and return id of the sent message
817 *
818 * The return value can be used as input for async_wait() to wait for
819 * completion.
820 *
821 * @param phoneid Handle of the phone that will be used for the send.
822 * @param method Service-defined method.
823 * @param arg1 Service-defined payload argument.
824 * @param arg2 Service-defined payload argument.
825 * @param arg3 Service-defined payload argument.
826 * @param arg4 Service-defined payload argument.
827 * @param arg5 Service-defined payload argument.
828 * @param dataptr If non-NULL, storage where the reply data will be
829 * stored.
830 *
831 * @return Hash of the sent message or 0 on error.
832 *
833 */
834aid_t async_send_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
835 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5,
836 ipc_call_t *dataptr)
837{
838 amsg_t *msg = malloc(sizeof(*msg));
839
840 if (!msg)
841 return 0;
842
843 msg->done = false;
844 msg->dataptr = dataptr;
845
846 msg->wdata.to_event.inlist = false;
847 /* We may sleep in next method, but it will use its own mechanism */
848 msg->wdata.active = true;
849
850 ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
851 reply_received, true);
852
853 return (aid_t) msg;
854}
855
856/** Wait for a message sent by the async framework.
857 *
858 * @param amsgid Hash of the message to wait for.
859 * @param retval Pointer to storage where the retval of the answer will
860 * be stored.
861 *
862 */
863void async_wait_for(aid_t amsgid, ipcarg_t *retval)
864{
865 amsg_t *msg = (amsg_t *) amsgid;
866
867 futex_down(&async_futex);
868 if (msg->done) {
869 futex_up(&async_futex);
870 goto done;
871 }
872
873 msg->wdata.fid = fibril_get_id();
874 msg->wdata.active = false;
875 msg->wdata.to_event.inlist = false;
876
877 /* Leave the async_futex locked when entering this function */
878 fibril_switch(FIBRIL_TO_MANAGER);
879
880 /* Futex is up automatically after fibril_switch */
881
882done:
883 if (retval)
884 *retval = msg->retval;
885
886 free(msg);
887}
888
889/** Wait for a message sent by the async framework, timeout variant.
890 *
891 * @param amsgid Hash of the message to wait for.
892 * @param retval Pointer to storage where the retval of the answer will
893 * be stored.
894 * @param timeout Timeout in microseconds.
895 *
896 * @return Zero on success, ETIMEOUT if the timeout has expired.
897 *
898 */
899int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
900{
901 amsg_t *msg = (amsg_t *) amsgid;
902
903 /* TODO: Let it go through the event read at least once */
904 if (timeout < 0)
905 return ETIMEOUT;
906
907 futex_down(&async_futex);
908 if (msg->done) {
909 futex_up(&async_futex);
910 goto done;
911 }
912
913 gettimeofday(&msg->wdata.to_event.expires, NULL);
914 tv_add(&msg->wdata.to_event.expires, timeout);
915
916 msg->wdata.fid = fibril_get_id();
917 msg->wdata.active = false;
918 async_insert_timeout(&msg->wdata);
919
920 /* Leave the async_futex locked when entering this function */
921 fibril_switch(FIBRIL_TO_MANAGER);
922
923 /* Futex is up automatically after fibril_switch */
924
925 if (!msg->done)
926 return ETIMEOUT;
927
928done:
929 if (retval)
930 *retval = msg->retval;
931
932 free(msg);
933
934 return 0;
935}
936
937/** Wait for specified time.
938 *
939 * The current fibril is suspended but the thread continues to execute.
940 *
941 * @param timeout Duration of the wait in microseconds.
942 *
943 */
944void async_usleep(suseconds_t timeout)
945{
946 amsg_t *msg = malloc(sizeof(*msg));
947
948 if (!msg)
949 return;
950
951 msg->wdata.fid = fibril_get_id();
952 msg->wdata.active = false;
953
954 gettimeofday(&msg->wdata.to_event.expires, NULL);
955 tv_add(&msg->wdata.to_event.expires, timeout);
956
957 futex_down(&async_futex);
958
959 async_insert_timeout(&msg->wdata);
960
961 /* Leave the async_futex locked when entering this function */
962 fibril_switch(FIBRIL_TO_MANAGER);
963
964 /* Futex is up automatically after fibril_switch() */
965
966 free(msg);
967}
968
969/** Setter for client_connection function pointer.
970 *
971 * @param conn Function that will implement a new connection fibril.
972 *
973 */
974void async_set_client_connection(async_client_conn_t conn)
975{
976 client_connection = conn;
977}
978
979/** Setter for interrupt_received function pointer.
980 *
981 * @param intr Function that will implement a new interrupt
982 * notification fibril.
983 */
984void async_set_interrupt_received(async_client_conn_t intr)
985{
986 interrupt_received = intr;
987}
988
989/** Pseudo-synchronous message sending - fast version.
990 *
991 * Send message asynchronously and return only after the reply arrives.
992 *
993 * This function can only transfer 4 register payload arguments. For
994 * transferring more arguments, see the slower async_req_slow().
995 *
996 * @param phoneid Hash of the phone through which to make the call.
997 * @param method Method of the call.
998 * @param arg1 Service-defined payload argument.
999 * @param arg2 Service-defined payload argument.
1000 * @param arg3 Service-defined payload argument.
1001 * @param arg4 Service-defined payload argument.
1002 * @param r1 If non-NULL, storage for the 1st reply argument.
1003 * @param r2 If non-NULL, storage for the 2nd reply argument.
1004 * @param r3 If non-NULL, storage for the 3rd reply argument.
1005 * @param r4 If non-NULL, storage for the 4th reply argument.
1006 * @param r5 If non-NULL, storage for the 5th reply argument.
1007 *
1008 * @return Return code of the reply or a negative error code.
1009 *
1010 */
1011ipcarg_t async_req_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
1012 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t *r1, ipcarg_t *r2,
1013 ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
1014{
1015 ipc_call_t result;
1016 aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1017 &result);
1018
1019 ipcarg_t rc;
1020 async_wait_for(eid, &rc);
1021
1022 if (r1)
1023 *r1 = IPC_GET_ARG1(result);
1024
1025 if (r2)
1026 *r2 = IPC_GET_ARG2(result);
1027
1028 if (r3)
1029 *r3 = IPC_GET_ARG3(result);
1030
1031 if (r4)
1032 *r4 = IPC_GET_ARG4(result);
1033
1034 if (r5)
1035 *r5 = IPC_GET_ARG5(result);
1036
1037 return rc;
1038}
1039
1040/** Pseudo-synchronous message sending - slow version.
1041 *
1042 * Send message asynchronously and return only after the reply arrives.
1043 *
1044 * @param phoneid Hash of the phone through which to make the call.
1045 * @param method Method of the call.
1046 * @param arg1 Service-defined payload argument.
1047 * @param arg2 Service-defined payload argument.
1048 * @param arg3 Service-defined payload argument.
1049 * @param arg4 Service-defined payload argument.
1050 * @param arg5 Service-defined payload argument.
1051 * @param r1 If non-NULL, storage for the 1st reply argument.
1052 * @param r2 If non-NULL, storage for the 2nd reply argument.
1053 * @param r3 If non-NULL, storage for the 3rd reply argument.
1054 * @param r4 If non-NULL, storage for the 4th reply argument.
1055 * @param r5 If non-NULL, storage for the 5th reply argument.
1056 *
1057 * @return Return code of the reply or a negative error code.
1058 *
1059 */
1060ipcarg_t async_req_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
1061 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *r1,
1062 ipcarg_t *r2, ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
1063{
1064 ipc_call_t result;
1065 aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1066 &result);
1067
1068 ipcarg_t rc;
1069 async_wait_for(eid, &rc);
1070
1071 if (r1)
1072 *r1 = IPC_GET_ARG1(result);
1073
1074 if (r2)
1075 *r2 = IPC_GET_ARG2(result);
1076
1077 if (r3)
1078 *r3 = IPC_GET_ARG3(result);
1079
1080 if (r4)
1081 *r4 = IPC_GET_ARG4(result);
1082
1083 if (r5)
1084 *r5 = IPC_GET_ARG5(result);
1085
1086 return rc;
1087}
1088
1089/** @}
1090 */
Note: See TracBrowser for help on using the repository browser.