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

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

Fibril synchronization needs to have a means to interrupt
idle threads from waiting for IPC and get them to execute
awakened fibrils.

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