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

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

Add IPC_M_CONNECTION_CLONE and IPC_M_CONNECT_ME.

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