source: mainline/uspace/libc/generic/async.c@ d7baee6

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

Textual changes.

  • Property mode set to 100644
File size: 20.2 KB
RevLine 
[06502f7d]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.
[b2951e2]27 */
28
[a46da63]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
32/** @file
[450cd3a]33 */
[06502f7d]34
[80649a91]35/**
36 * Asynchronous library
37 *
38 * The aim of this library is facilitating writing programs utilizing
[14de0dd8]39 * the asynchronous nature of HelenOS IPC, yet using a normal way
[80649a91]40 * of programming.
41 *
42 * You should be able to write very simple multithreaded programs,
43 * the async framework will automatically take care of most synchronization
44 * problems.
45 *
46 * Default semantics:
47 * - send() - send asynchronously. If the kernel refuses to send more
48 * messages, [ try to get responses from kernel, if nothing
49 * found, might try synchronous ]
50 *
51 * Example of use:
52 *
53 * 1) Multithreaded client application
54 * create_thread(thread1);
55 * create_thread(thread2);
56 * ...
57 *
58 * thread1() {
59 * conn = ipc_connect_me_to();
60 * c1 = send(conn);
61 * c2 = send(conn);
62 * wait_for(c1);
63 * wait_for(c2);
64 * }
65 *
66 *
67 * 2) Multithreaded server application
68 * main() {
[53ca318]69 * async_manager();
[80649a91]70 * }
71 *
72 *
[53ca318]73 * client_connection(icallid, *icall) {
74 * if (want_refuse) {
75 * ipc_answer_fast(icallid, ELIMIT, 0, 0);
76 * return;
77 * }
78 * ipc_answer_fast(icallid, 0, 0, 0);
[80649a91]79 *
[53ca318]80 * callid = async_get_call(&call);
81 * handle(callid, call);
[14de0dd8]82 * ipc_answer_fast(callid, 1, 2, 3);
[53ca318]83 *
84 * callid = async_get_call(&call);
[80649a91]85 * ....
86 * }
[a2cd194]87 *
[8bc924e]88 * TODO: Detaching/joining dead psthreads?
[80649a91]89 */
90#include <futex.h>
91#include <async.h>
92#include <psthread.h>
93#include <stdio.h>
94#include <libadt/hash_table.h>
95#include <libadt/list.h>
96#include <ipc/ipc.h>
97#include <assert.h>
98#include <errno.h>
[c042bdd]99#include <time.h>
100#include <arch/barrier.h>
[80649a91]101
[fc42b28]102atomic_t async_futex = FUTEX_INITIALIZER;
[80649a91]103static hash_table_t conn_hash_table;
[c042bdd]104static LIST_INITIALIZE(timeout_list);
[80649a91]105
[01ff41c]106typedef struct {
[14de0dd8]107 struct timeval expires; /**< Expiration time for waiting thread */
108 int inlist; /**< If true, this struct is in timeout list */
[49d072e]109 link_t link;
110
[14de0dd8]111 pstid_t ptid; /**< Thread waiting for this message */
112 int active; /**< If this thread is currently active */
113 int timedout; /**< If true, we timed out */
[49d072e]114} awaiter_t;
115
116typedef struct {
117 awaiter_t wdata;
118
[14de0dd8]119 int done; /**< If reply was received */
120 ipc_call_t *dataptr; /**< Pointer where the answer data
121 * is stored */
[01ff41c]122 ipcarg_t retval;
123} amsg_t;
124
[80649a91]125typedef struct {
126 link_t link;
127 ipc_callid_t callid;
128 ipc_call_t call;
129} msg_t;
130
131typedef struct {
[49d072e]132 awaiter_t wdata;
133
[14de0dd8]134 link_t link; /**< Hash table link */
135 ipcarg_t in_phone_hash; /**< Incoming phone hash. */
136 link_t msg_queue; /**< Messages that should be delivered to this thread */
[80649a91]137 /* Structures for connection opening packet */
138 ipc_callid_t callid;
139 ipc_call_t call;
[14de0dd8]140 ipc_callid_t close_callid; /* Identification of closing packet */
[53ca318]141 void (*cthread)(ipc_callid_t,ipc_call_t *);
[80649a91]142} connection_t;
143
[085bd54]144/** Identifier of incoming connection handled by current thread */
[80649a91]145__thread connection_t *PS_connection;
[085bd54]146/** If true, it is forbidden to use async_req functions and
147 * all preemption is disabled */
148__thread int in_interrupt_handler;
[80649a91]149
[da0c91e7]150static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
[51dbadf3]151static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
[da0c91e7]152static async_client_conn_t client_connection = default_client_connection;
[51dbadf3]153static async_client_conn_t interrupt_received = default_interrupt_received;
[da0c91e7]154
[c042bdd]155/** Add microseconds to give timeval */
156static void tv_add(struct timeval *tv, suseconds_t usecs)
157{
158 tv->tv_sec += usecs / 1000000;
159 tv->tv_usec += usecs % 1000000;
160 if (tv->tv_usec > 1000000) {
161 tv->tv_sec++;
162 tv->tv_usec -= 1000000;
163 }
164}
165
166/** Subtract 2 timevals, return microseconds difference */
167static suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
168{
169 suseconds_t result;
170
171 result = tv1->tv_usec - tv2->tv_usec;
172 result += (tv1->tv_sec - tv2->tv_sec) * 1000000;
173
174 return result;
175}
176
177/** Compare timeval
178 *
179 * @return 1 if tv1 > tv2, otherwise 0
180 */
181static int tv_gt(struct timeval *tv1, struct timeval *tv2)
182{
183 if (tv1->tv_sec > tv2->tv_sec)
184 return 1;
185 if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec)
186 return 1;
187 return 0;
188}
[c0e674a]189static int tv_gteq(struct timeval *tv1, struct timeval *tv2)
190{
191 if (tv1->tv_sec > tv2->tv_sec)
192 return 1;
193 if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec >= tv2->tv_usec)
194 return 1;
195 return 0;
196}
[c042bdd]197
[80649a91]198/* Hash table functions */
[a2cd194]199#define CONN_HASH_TABLE_CHAINS 32
[80649a91]200
201static hash_index_t conn_hash(unsigned long *key)
[450cd3a]202{
[80649a91]203 assert(key);
[a2cd194]204 return ((*key) >> 4) % CONN_HASH_TABLE_CHAINS;
[450cd3a]205}
[06502f7d]206
[80649a91]207static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
[450cd3a]208{
[80649a91]209 connection_t *hs;
210
211 hs = hash_table_get_instance(item, connection_t, link);
212
213 return key[0] == hs->in_phone_hash;
[450cd3a]214}
[06502f7d]215
[80649a91]216static void conn_remove(link_t *item)
[450cd3a]217{
[80649a91]218 free(hash_table_get_instance(item, connection_t, link));
[450cd3a]219}
220
[80649a91]221
222/** Operations for NS hash table. */
223static hash_table_operations_t conn_hash_table_ops = {
224 .hash = conn_hash,
225 .compare = conn_compare,
226 .remove_callback = conn_remove
227};
228
[49d072e]229/** Insert sort timeout msg into timeouts list
230 *
231 */
232static void insert_timeout(awaiter_t *wd)
233{
234 link_t *tmp;
235 awaiter_t *cur;
236
237 wd->timedout = 0;
[085bd54]238 wd->inlist = 1;
[49d072e]239
240 tmp = timeout_list.next;
241 while (tmp != &timeout_list) {
242 cur = list_get_instance(tmp, awaiter_t, link);
243 if (tv_gteq(&cur->expires, &wd->expires))
244 break;
245 tmp = tmp->next;
246 }
247 list_append(&wd->link, tmp);
248}
249
[01ff41c]250/*************************************************/
251
[80649a91]252/** Try to route a call to an appropriate connection thread
253 *
254 */
255static int route_call(ipc_callid_t callid, ipc_call_t *call)
[450cd3a]256{
[80649a91]257 connection_t *conn;
258 msg_t *msg;
259 link_t *hlp;
260 unsigned long key;
261
[01ff41c]262 futex_down(&async_futex);
[80649a91]263
264 key = call->in_phone_hash;
265 hlp = hash_table_find(&conn_hash_table, &key);
266 if (!hlp) {
[01ff41c]267 futex_up(&async_futex);
[80649a91]268 return 0;
[450cd3a]269 }
[80649a91]270 conn = hash_table_get_instance(hlp, connection_t, link);
271
272 msg = malloc(sizeof(*msg));
273 msg->callid = callid;
274 msg->call = *call;
275 list_append(&msg->link, &conn->msg_queue);
[41269bd]276
277 if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP)
278 conn->close_callid = callid;
[80649a91]279
[49d072e]280 /* If the call is waiting for event, run it */
281 if (!conn->wdata.active) {
282 /* If in timeout list, remove it */
283 if (conn->wdata.inlist) {
284 conn->wdata.inlist = 0;
285 list_remove(&conn->wdata.link);
286 }
287 conn->wdata.active = 1;
288 psthread_add_ready(conn->wdata.ptid);
[80649a91]289 }
290
[01ff41c]291 futex_up(&async_futex);
[80649a91]292
293 return 1;
294}
295
[a2cd194]296/** Return new incoming message for current(thread-local) connection */
[49d072e]297ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
[80649a91]298{
299 msg_t *msg;
300 ipc_callid_t callid;
[6c46350]301 connection_t *conn;
[80649a91]302
[c0e674a]303 assert(PS_connection);
[6c46350]304 /* GCC 4.1.0 coughs on PS_connection-> dereference,
305 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
306 * I would never expect to find so many errors in
307 * compiler *($&$(*&$
308 */
309 conn = PS_connection;
[c0e674a]310
[01ff41c]311 futex_down(&async_futex);
[80649a91]312
[49d072e]313 if (usecs) {
[6c46350]314 gettimeofday(&conn->wdata.expires, NULL);
315 tv_add(&conn->wdata.expires, usecs);
[49d072e]316 } else {
[6c46350]317 conn->wdata.inlist = 0;
[49d072e]318 }
[80649a91]319 /* If nothing in queue, wait until something appears */
[6c46350]320 while (list_empty(&conn->msg_queue)) {
[085bd54]321 if (usecs)
[6c46350]322 insert_timeout(&conn->wdata);
[085bd54]323
[6c46350]324 conn->wdata.active = 0;
[80649a91]325 psthread_schedule_next_adv(PS_TO_MANAGER);
[49d072e]326 /* Futex is up after getting back from async_manager
327 * get it again */
328 futex_down(&async_futex);
[6c46350]329 if (usecs && conn->wdata.timedout && \
330 list_empty(&conn->msg_queue)) {
[49d072e]331 /* If we timed out-> exit */
332 futex_up(&async_futex);
333 return 0;
334 }
[450cd3a]335 }
336
[6c46350]337 msg = list_get_instance(conn->msg_queue.next, msg_t, link);
[80649a91]338 list_remove(&msg->link);
339 callid = msg->callid;
340 *call = msg->call;
341 free(msg);
342
[01ff41c]343 futex_up(&async_futex);
[80649a91]344 return callid;
345}
346
[a2cd194]347/** Thread function that gets created on new connection
348 *
349 * This function is defined as a weak symbol - to be redefined in
350 * user code.
351 */
[da0c91e7]352static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
[80649a91]353{
[a2cd194]354 ipc_answer_fast(callid, ENOENT, 0, 0);
[80649a91]355}
[51dbadf3]356static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
[44c6d88d]357{
358}
359
[a2cd194]360/** Wrapper for client connection thread
361 *
362 * When new connection arrives, thread with this function is created.
363 * It calls client_connection and does final cleanup.
364 *
[b2951e2]365 * @param arg Connection structure pointer
[a2cd194]366 */
[80649a91]367static int connection_thread(void *arg)
368{
[a2cd194]369 unsigned long key;
370 msg_t *msg;
[41269bd]371 int close_answered = 0;
[a2cd194]372
[80649a91]373 /* Setup thread local connection pointer */
374 PS_connection = (connection_t *)arg;
[1c20e22]375 PS_connection->cthread(PS_connection->callid, &PS_connection->call);
[a46da63]376
[a2cd194]377 /* Remove myself from connection hash table */
[01ff41c]378 futex_down(&async_futex);
[1c20e22]379 key = PS_connection->in_phone_hash;
[a2cd194]380 hash_table_remove(&conn_hash_table, &key, 1);
[01ff41c]381 futex_up(&async_futex);
[a46da63]382
[a2cd194]383 /* Answer all remaining messages with ehangup */
[1c20e22]384 while (!list_empty(&PS_connection->msg_queue)) {
385 msg = list_get_instance(PS_connection->msg_queue.next, msg_t, link);
[a2cd194]386 list_remove(&msg->link);
[41269bd]387 if (msg->callid == PS_connection->close_callid)
388 close_answered = 1;
[a2cd194]389 ipc_answer_fast(msg->callid, EHANGUP, 0, 0);
390 free(msg);
391 }
[41269bd]392 if (PS_connection->close_callid)
393 ipc_answer_fast(PS_connection->close_callid, 0, 0, 0);
[a46da63]394
395 return 0;
[80649a91]396}
397
398/** Create new thread for a new connection
399 *
400 * Creates new thread for connection, fills in connection
401 * structures and inserts it into the hash table, so that
402 * later we can easily do routing of messages to particular
403 * threads.
[53ca318]404 *
[44c6d88d]405 * @param in_phone_hash Identification of the incoming connection
[53ca318]406 * @param callid Callid of the IPC_M_CONNECT_ME_TO packet
407 * @param call Call data of the opening packet
408 * @param cthread Thread function that should be called upon
409 * opening the connection
410 * @return New thread id
[80649a91]411 */
[a46da63]412pstid_t async_new_connection(ipcarg_t in_phone_hash,ipc_callid_t callid, ipc_call_t *call, void (*cthread)(ipc_callid_t, ipc_call_t *))
[80649a91]413{
414 connection_t *conn;
415 unsigned long key;
416
417 conn = malloc(sizeof(*conn));
418 if (!conn) {
419 ipc_answer_fast(callid, ENOMEM, 0, 0);
[53ca318]420 return NULL;
[80649a91]421 }
[44c6d88d]422 conn->in_phone_hash = in_phone_hash;
[80649a91]423 list_initialize(&conn->msg_queue);
424 conn->callid = callid;
[41269bd]425 conn->close_callid = 0;
[eaf34f7]426 if (call)
427 conn->call = *call;
[49d072e]428 conn->wdata.active = 1; /* We will activate it asap */
[53ca318]429 conn->cthread = cthread;
[49d072e]430
431 conn->wdata.ptid = psthread_create(connection_thread, conn);
432 if (!conn->wdata.ptid) {
[80649a91]433 free(conn);
434 ipc_answer_fast(callid, ENOMEM, 0, 0);
[53ca318]435 return NULL;
[80649a91]436 }
[49d072e]437 /* Add connection to hash table */
[80649a91]438 key = conn->in_phone_hash;
[01ff41c]439 futex_down(&async_futex);
[80649a91]440 hash_table_insert(&conn_hash_table, &key, &conn->link);
[01ff41c]441 futex_up(&async_futex);
[80649a91]442
[49d072e]443 psthread_add_ready(conn->wdata.ptid);
[53ca318]444
[49d072e]445 return conn->wdata.ptid;
[80649a91]446}
447
[01ff41c]448/** Handle call that was received */
[80649a91]449static void handle_call(ipc_callid_t callid, ipc_call_t *call)
450{
[44c6d88d]451 /* Unrouted call - do some default behaviour */
[15039b67]452 if ((callid & IPC_CALLID_NOTIFICATION)) {
[085bd54]453 in_interrupt_handler = 1;
[51dbadf3]454 (*interrupt_received)(callid,call);
[085bd54]455 in_interrupt_handler = 0;
[44c6d88d]456 return;
[15039b67]457 }
458
459 switch (IPC_GET_METHOD(*call)) {
[80649a91]460 case IPC_M_CONNECT_ME_TO:
461 /* Open new connection with thread etc. */
[44c6d88d]462 async_new_connection(IPC_GET_ARG3(*call), callid, call, client_connection);
463 return;
[80649a91]464 }
[44c6d88d]465
466 /* Try to route call through connection tables */
467 if (route_call(callid, call))
468 return;
469
470 /* Unknown call from unknown phone - hang it up */
471 ipc_answer_fast(callid, EHANGUP, 0, 0);
[450cd3a]472}
473
[6c46350]474/** Fire all timeouts that expired
475 *
476 */
[c042bdd]477static void handle_expired_timeouts(void)
478{
479 struct timeval tv;
[49d072e]480 awaiter_t *waiter;
[c042bdd]481 link_t *cur;
482
483 gettimeofday(&tv,NULL);
484 futex_down(&async_futex);
485
486 cur = timeout_list.next;
487 while (cur != &timeout_list) {
[49d072e]488 waiter = list_get_instance(cur,awaiter_t,link);
489 if (tv_gt(&waiter->expires, &tv))
[c042bdd]490 break;
491 cur = cur->next;
[49d072e]492 list_remove(&waiter->link);
493 waiter->inlist = 0;
494 waiter->timedout = 1;
[c042bdd]495 /* Redundant condition? The thread should not
496 * be active when it gets here.
497 */
[49d072e]498 if (!waiter->active) {
499 waiter->active = 1;
500 psthread_add_ready(waiter->ptid);
[c042bdd]501 }
502 }
503
504 futex_up(&async_futex);
505}
506
[80649a91]507/** Endless loop dispatching incoming calls and answers */
[085bd54]508static int async_manager_worker(void)
[80649a91]509{
510 ipc_call_t call;
511 ipc_callid_t callid;
[0b99e40]512 int timeout;
[49d072e]513 awaiter_t *waiter;
[c042bdd]514 struct timeval tv;
[80649a91]515
516 while (1) {
517 if (psthread_schedule_next_adv(PS_FROM_MANAGER)) {
[a46da63]518 futex_up(&async_futex);
519 /* async_futex is always held
520 * when entering manager thread
521 */
[80649a91]522 continue;
523 }
[c042bdd]524 futex_down(&async_futex);
525 if (!list_empty(&timeout_list)) {
[49d072e]526 waiter = list_get_instance(timeout_list.next,awaiter_t,link);
[c042bdd]527 gettimeofday(&tv,NULL);
[49d072e]528 if (tv_gteq(&tv, &waiter->expires)) {
[6c46350]529 futex_up(&async_futex);
[c042bdd]530 handle_expired_timeouts();
531 continue;
532 } else
[49d072e]533 timeout = tv_sub(&waiter->expires, &tv);
[c042bdd]534 } else
[0b99e40]535 timeout = SYNCH_NO_TIMEOUT;
[c042bdd]536 futex_up(&async_futex);
537
[2d22049]538 callid = ipc_wait_cycle(&call, timeout, SYNCH_FLAGS_NONE);
[0b99e40]539
540 if (!callid) {
[c042bdd]541 handle_expired_timeouts();
[0b99e40]542 continue;
543 }
[80649a91]544
[085bd54]545 if (callid & IPC_CALLID_ANSWERED) {
[80649a91]546 continue;
[085bd54]547 }
[01ff41c]548
[80649a91]549 handle_call(callid, &call);
550 }
[a46da63]551
552 return 0;
[80649a91]553}
554
[a2cd194]555/** Function to start async_manager as a standalone thread
556 *
557 * When more kernel threads are used, one async manager should
558 * exist per thread. The particular implementation may change,
559 * currently one async_manager is started automatically per kernel
560 * thread except main thread.
561 */
[80649a91]562static int async_manager_thread(void *arg)
563{
[a46da63]564 futex_up(&async_futex);
565 /* async_futex is always locked when entering
566 * manager */
[085bd54]567 async_manager_worker();
[a46da63]568
569 return 0;
[80649a91]570}
[450cd3a]571
[80649a91]572/** Add one manager to manager list */
573void async_create_manager(void)
[450cd3a]574{
[80649a91]575 pstid_t ptid;
576
577 ptid = psthread_create(async_manager_thread, NULL);
578 psthread_add_manager(ptid);
579}
580
581/** Remove one manager from manager list */
582void async_destroy_manager(void)
583{
584 psthread_remove_manager();
585}
586
587/** Initialize internal structures needed for async manager */
588int _async_init(void)
589{
[a2cd194]590 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1, &conn_hash_table_ops)) {
[80649a91]591 printf("%s: cannot create hash table\n", "async");
592 return ENOMEM;
593 }
594
[a46da63]595 return 0;
[450cd3a]596}
[01ff41c]597
598/** IPC handler for messages in async framework
599 *
600 * Notify thread that is waiting for this message, that it arrived
601 */
602static void reply_received(void *private, int retval,
603 ipc_call_t *data)
604{
605 amsg_t *msg = (amsg_t *) private;
606
607 msg->retval = retval;
608
609 futex_down(&async_futex);
610 /* Copy data after futex_down, just in case the
611 * call was detached
612 */
613 if (msg->dataptr)
614 *msg->dataptr = *data;
[0b99e40]615
[c042bdd]616 write_barrier();
617 /* Remove message from timeout list */
[49d072e]618 if (msg->wdata.inlist)
619 list_remove(&msg->wdata.link);
[01ff41c]620 msg->done = 1;
[49d072e]621 if (! msg->wdata.active) {
622 msg->wdata.active = 1;
623 psthread_add_ready(msg->wdata.ptid);
[01ff41c]624 }
625 futex_up(&async_futex);
626}
627
628/** Send message and return id of the sent message
629 *
630 * The return value can be used as input for async_wait() to wait
631 * for completion.
632 */
633aid_t async_send_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
634 ipc_call_t *dataptr)
635{
636 amsg_t *msg;
637
[085bd54]638 if (in_interrupt_handler) {
[2b017ba]639 printf("Cannot send asynchronous request in interrupt handler.\n");
[085bd54]640 _exit(1);
641 }
642
[01ff41c]643 msg = malloc(sizeof(*msg));
644 msg->done = 0;
645 msg->dataptr = dataptr;
[49d072e]646
647 msg->wdata.active = 1; /* We may sleep in next method, but it
648 * will use it's own mechanism */
[b1f51f0]649 ipc_call_async_2(phoneid,method,arg1,arg2,msg,reply_received,1);
[01ff41c]650
651 return (aid_t) msg;
652}
653
[90f5d64]654/** Send message and return id of the sent message
655 *
656 * The return value can be used as input for async_wait() to wait
657 * for completion.
658 */
659aid_t async_send_3(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
660 ipcarg_t arg3, ipc_call_t *dataptr)
661{
662 amsg_t *msg;
663
[085bd54]664 if (in_interrupt_handler) {
[2b017ba]665 printf("Cannot send asynchronous request in interrupt handler.\n");
[085bd54]666 _exit(1);
667 }
668
[90f5d64]669 msg = malloc(sizeof(*msg));
670 msg->done = 0;
671 msg->dataptr = dataptr;
672
673 msg->wdata.active = 1; /* We may sleep in next method, but it
674 * will use it's own mechanism */
675 ipc_call_async_3(phoneid,method,arg1,arg2,arg3, msg,reply_received,1);
676
677 return (aid_t) msg;
678}
679
[01ff41c]680/** Wait for a message sent by async framework
681 *
682 * @param amsgid Message ID to wait for
683 * @param retval Pointer to variable where will be stored retval
684 * of the answered message. If NULL, it is ignored.
685 *
686 */
687void async_wait_for(aid_t amsgid, ipcarg_t *retval)
688{
689 amsg_t *msg = (amsg_t *) amsgid;
690
691 futex_down(&async_futex);
692 if (msg->done) {
693 futex_up(&async_futex);
694 goto done;
695 }
696
[49d072e]697 msg->wdata.ptid = psthread_get_id();
698 msg->wdata.active = 0;
699 msg->wdata.inlist = 0;
[01ff41c]700 /* Leave locked async_futex when entering this function */
701 psthread_schedule_next_adv(PS_TO_MANAGER);
702 /* futex is up automatically after psthread_schedule_next...*/
703done:
704 if (retval)
705 *retval = msg->retval;
706 free(msg);
707}
[0b99e40]708
[c042bdd]709/** Wait for a message sent by async framework with timeout
710 *
711 * @param amsgid Message ID to wait for
712 * @param retval Pointer to variable where will be stored retval
713 * of the answered message. If NULL, it is ignored.
714 * @param timeout Timeout in usecs
715 * @return 0 on success, ETIMEOUT if timeout expired
716 *
717 */
718int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
719{
720 amsg_t *msg = (amsg_t *) amsgid;
[0b99e40]721
[86029498]722 /* TODO: Let it go through the event read at least once */
723 if (timeout < 0)
724 return ETIMEOUT;
725
[c042bdd]726 futex_down(&async_futex);
727 if (msg->done) {
728 futex_up(&async_futex);
729 goto done;
730 }
[0b99e40]731
[49d072e]732 gettimeofday(&msg->wdata.expires, NULL);
733 tv_add(&msg->wdata.expires, timeout);
734
735 msg->wdata.ptid = psthread_get_id();
736 msg->wdata.active = 0;
737 insert_timeout(&msg->wdata);
[0b99e40]738
[c042bdd]739 /* Leave locked async_futex when entering this function */
740 psthread_schedule_next_adv(PS_TO_MANAGER);
741 /* futex is up automatically after psthread_schedule_next...*/
[0b99e40]742
[c042bdd]743 if (!msg->done)
744 return ETIMEOUT;
[0b99e40]745
[c042bdd]746done:
747 if (retval)
748 *retval = msg->retval;
749 free(msg);
750
751 return 0;
752}
[0b99e40]753
[44c6d88d]754/** Wait specified time, but in the meantime handle incoming events
755 *
756 * @param timeout Time in microseconds to wait
757 */
758void async_usleep(suseconds_t timeout)
759{
760 amsg_t *msg;
761
[085bd54]762 if (in_interrupt_handler) {
763 printf("Cannot call async_usleep in interrupt handler.\n");
764 _exit(1);
765 }
766
[44c6d88d]767 msg = malloc(sizeof(*msg));
768 if (!msg)
769 return;
770
[49d072e]771 msg->wdata.ptid = psthread_get_id();
772 msg->wdata.active = 0;
[44c6d88d]773
[49d072e]774 gettimeofday(&msg->wdata.expires, NULL);
775 tv_add(&msg->wdata.expires, timeout);
[44c6d88d]776
777 futex_down(&async_futex);
[49d072e]778 insert_timeout(&msg->wdata);
[44c6d88d]779 /* Leave locked async_futex when entering this function */
780 psthread_schedule_next_adv(PS_TO_MANAGER);
781 /* futex is up automatically after psthread_schedule_next...*/
782 free(msg);
783}
[da0c91e7]784
785/** Set function that is called, IPC_M_CONNECT_ME_TO is received
786 *
787 * @param conn Function that will form new psthread.
788 */
789void async_set_client_connection(async_client_conn_t conn)
790{
791 client_connection = conn;
792}
[51dbadf3]793void async_set_interrupt_received(async_client_conn_t conn)
794{
795 interrupt_received = conn;
796}
[085bd54]797
798/* Primitive functions for simple communication */
799void async_msg_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
800 ipcarg_t arg2, ipcarg_t arg3)
801{
802 ipc_call_async_3(phoneid, method, arg1, arg2, arg3, NULL, NULL, !in_interrupt_handler);
803}
804
805void async_msg_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2)
806{
807 ipc_call_async_2(phoneid, method, arg1, arg2, NULL, NULL, !in_interrupt_handler);
808}
[b2951e2]809
[a46da63]810/** @}
[b2951e2]811 */
Note: See TracBrowser for help on using the repository browser.