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

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

Improve confusing comment.
handle_call() is a name of an async framework function, which is meant to be used
in a different way.

  • Property mode set to 100644
File size: 27.5 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);
[0772aff]85 * somehow_handle_the_call(callid, call);
[c07544d3]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>
[d9c8c81]98#include <adt/hash_table.h>
99#include <adt/list.h>
[80649a91]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
[8619f25]109/** Number of threads waiting for IPC in the kernel. */
110atomic_t threads_in_ipc_wait = { 0 };
111
[e70bfa5]112/** Structures of this type represent a waiting fibril. */
[01ff41c]113typedef struct {
[e70bfa5]114 /** Expiration time. */
[c07544d3]115 struct timeval expires;
116
[bc1f1c2]117 /** If true, this struct is in the timeout list. */
[c07544d3]118 bool inlist;
119
[e70bfa5]120 /** Timeout list link. */
[49d072e]121 link_t link;
[c07544d3]122
[36c9234]123 /** Identification of and link to the waiting fibril. */
[bc1f1c2]124 fid_t fid;
[c07544d3]125
[e70bfa5]126 /** If true, this fibril is currently active. */
[c07544d3]127 bool active;
128
[e70bfa5]129 /** If true, we have timed out. */
[c07544d3]130 bool timedout;
[49d072e]131} awaiter_t;
132
133typedef struct {
134 awaiter_t wdata;
[e70bfa5]135
136 /** If reply was received. */
[c07544d3]137 bool done;
138
[e70bfa5]139 /** Pointer to where the answer data is stored. */
[c07544d3]140 ipc_call_t *dataptr;
141
[01ff41c]142 ipcarg_t retval;
143} amsg_t;
144
[36c9234]145/**
146 * Structures of this type are used to group information about a call and a
147 * message queue link.
148 */
[80649a91]149typedef struct {
150 link_t link;
151 ipc_callid_t callid;
152 ipc_call_t call;
153} msg_t;
154
155typedef struct {
[49d072e]156 awaiter_t wdata;
[c07544d3]157
[e70bfa5]158 /** Hash table link. */
159 link_t link;
[c07544d3]160
[e70bfa5]161 /** Incoming phone hash. */
[c07544d3]162 ipcarg_t in_phone_hash;
163
[e70bfa5]164 /** Messages that should be delivered to this fibril. */
[c07544d3]165 link_t msg_queue;
166
[e70bfa5]167 /** Identification of the opening call. */
[80649a91]168 ipc_callid_t callid;
[e70bfa5]169 /** Call data of the opening call. */
[80649a91]170 ipc_call_t call;
[c07544d3]171
[e70bfa5]172 /** Identification of the closing call. */
173 ipc_callid_t close_callid;
[c07544d3]174
[e70bfa5]175 /** Fibril function that will be used to handle the connection. */
[bc1f1c2]176 void (*cfibril)(ipc_callid_t, ipc_call_t *);
[80649a91]177} connection_t;
178
[bc1f1c2]179/** Identifier of the incoming connection handled by the current fibril. */
[26360f7]180fibril_local connection_t *FIBRIL_connection;
[e70bfa5]181
[da0c91e7]182static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
[51dbadf3]183static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
[36c9234]184
185/**
186 * Pointer to a fibril function that will be used to handle connections.
187 */
[da0c91e7]188static async_client_conn_t client_connection = default_client_connection;
[c07544d3]189
[36c9234]190/**
191 * Pointer to a fibril function that will be used to handle interrupt
192 * notifications.
193 */
[51dbadf3]194static async_client_conn_t interrupt_received = default_interrupt_received;
[da0c91e7]195
[c07544d3]196static hash_table_t conn_hash_table;
197static LIST_INITIALIZE(timeout_list);
198
199#define CONN_HASH_TABLE_CHAINS 32
[80649a91]200
[e70bfa5]201/** Compute hash into the connection hash table based on the source phone hash.
202 *
[c07544d3]203 * @param key Pointer to source phone hash.
204 *
205 * @return Index into the connection hash table.
[e70bfa5]206 *
207 */
[80649a91]208static hash_index_t conn_hash(unsigned long *key)
[450cd3a]209{
[80649a91]210 assert(key);
[c07544d3]211 return (((*key) >> 4) % CONN_HASH_TABLE_CHAINS);
[450cd3a]212}
[06502f7d]213
[e70bfa5]214/** Compare hash table item with a key.
215 *
[c07544d3]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.
[e70bfa5]221 *
222 */
[80649a91]223static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
[450cd3a]224{
[c07544d3]225 connection_t *hs = hash_table_get_instance(item, connection_t, link);
226 return (key[0] == hs->in_phone_hash);
[450cd3a]227}
[06502f7d]228
[e70bfa5]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 *
[c07544d3]234 * @param item Connection hash table item being removed.
235 *
[e70bfa5]236 */
[80649a91]237static void conn_remove(link_t *item)
[450cd3a]238{
[80649a91]239 free(hash_table_get_instance(item, connection_t, link));
[450cd3a]240}
241
[80649a91]242
[e70bfa5]243/** Operations for the connection hash table. */
[80649a91]244static hash_table_operations_t conn_hash_table_ops = {
245 .hash = conn_hash,
246 .compare = conn_compare,
247 .remove_callback = conn_remove
248};
249
[e70bfa5]250/** Sort in current fibril's timeout request.
[49d072e]251 *
[c07544d3]252 * @param wd Wait data of the current fibril.
253 *
[49d072e]254 */
255static void insert_timeout(awaiter_t *wd)
256{
[c07544d3]257 wd->timedout = false;
258 wd->inlist = true;
259
260 link_t *tmp = timeout_list.next;
[49d072e]261 while (tmp != &timeout_list) {
[c07544d3]262 awaiter_t *cur = list_get_instance(tmp, awaiter_t, link);
263
[49d072e]264 if (tv_gteq(&cur->expires, &wd->expires))
265 break;
[c07544d3]266
[49d072e]267 tmp = tmp->next;
268 }
[c07544d3]269
[49d072e]270 list_append(&wd->link, tmp);
271}
272
[e70bfa5]273/** Try to route a call to an appropriate connection fibril.
[80649a91]274 *
[36c9234]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 *
[c07544d3]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.
[36c9234]284 *
[80649a91]285 */
[c07544d3]286static bool route_call(ipc_callid_t callid, ipc_call_t *call)
[450cd3a]287{
[01ff41c]288 futex_down(&async_futex);
[c07544d3]289
290 unsigned long key = call->in_phone_hash;
291 link_t *hlp = hash_table_find(&conn_hash_table, &key);
292
[80649a91]293 if (!hlp) {
[01ff41c]294 futex_up(&async_futex);
[c07544d3]295 return false;
[450cd3a]296 }
[c07544d3]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
[80649a91]306 msg->callid = callid;
307 msg->call = *call;
308 list_append(&msg->link, &conn->msg_queue);
[c07544d3]309
[41269bd]310 if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP)
311 conn->close_callid = callid;
[80649a91]312
[36c9234]313 /* If the connection fibril is waiting for an event, activate it */
[49d072e]314 if (!conn->wdata.active) {
[c07544d3]315
[49d072e]316 /* If in timeout list, remove it */
317 if (conn->wdata.inlist) {
[c07544d3]318 conn->wdata.inlist = false;
[49d072e]319 list_remove(&conn->wdata.link);
320 }
[c07544d3]321
322 conn->wdata.active = true;
[bc1f1c2]323 fibril_add_ready(conn->wdata.fid);
[80649a91]324 }
[c07544d3]325
[01ff41c]326 futex_up(&async_futex);
[c07544d3]327 return true;
328}
[80649a91]329
[c07544d3]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;
[80649a91]378}
379
[e70bfa5]380/** Return new incoming message for the current (fibril-local) connection.
381 *
[c07544d3]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.
[e70bfa5]390 *
391 */
[49d072e]392ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
[80649a91]393{
[bc1f1c2]394 assert(FIBRIL_connection);
[c07544d3]395
396 /* Why doing this?
397 * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
[6c46350]398 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
[c07544d3]399 * I would never expect to find so many errors in
400 * a compiler.
[6c46350]401 */
[c07544d3]402 connection_t *conn = FIBRIL_connection;
403
[01ff41c]404 futex_down(&async_futex);
[c07544d3]405
[49d072e]406 if (usecs) {
[6c46350]407 gettimeofday(&conn->wdata.expires, NULL);
408 tv_add(&conn->wdata.expires, usecs);
[c07544d3]409 } else
410 conn->wdata.inlist = false;
411
[e70bfa5]412 /* If nothing in queue, wait until something arrives */
[6c46350]413 while (list_empty(&conn->msg_queue)) {
[085bd54]414 if (usecs)
[6c46350]415 insert_timeout(&conn->wdata);
[c07544d3]416
417 conn->wdata.active = false;
418
[c7509e5]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 */
[116d3f6f]425 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]426
[e70bfa5]427 /*
[c07544d3]428 * Futex is up after getting back from async_manager.
429 * Get it again.
[c7509e5]430 */
[49d072e]431 futex_down(&async_futex);
[c07544d3]432 if ((usecs) && (conn->wdata.timedout)
433 && (list_empty(&conn->msg_queue))) {
[e70bfa5]434 /* If we timed out -> exit */
[49d072e]435 futex_up(&async_futex);
436 return 0;
437 }
[450cd3a]438 }
439
[c07544d3]440 msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
[80649a91]441 list_remove(&msg->link);
[c07544d3]442
443 ipc_callid_t callid = msg->callid;
[80649a91]444 *call = msg->call;
445 free(msg);
446
[01ff41c]447 futex_up(&async_futex);
[80649a91]448 return callid;
449}
450
[36c9234]451/** Default fibril function that gets called to handle new connection.
[a2cd194]452 *
[e70bfa5]453 * This function is defined as a weak symbol - to be redefined in user code.
[36c9234]454 *
[c07544d3]455 * @param callid Hash of the incoming call.
456 * @param call Data of the incoming call.
457 *
[a2cd194]458 */
[da0c91e7]459static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
[80649a91]460{
[b74959bd]461 ipc_answer_0(callid, ENOENT);
[80649a91]462}
[36c9234]463
464/** Default fibril function that gets called to handle interrupt notifications.
465 *
[c07544d3]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 *
[36c9234]471 */
[51dbadf3]472static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
[44c6d88d]473{
474}
475
[f2f0392]476/** Wrapper for client connection fibril.
477 *
[36c9234]478 * When a new connection arrives, a fibril with this implementing function is
[f2f0392]479 * created. It calls client_connection() and does the final cleanup.
[a2cd194]480 *
[c07544d3]481 * @param arg Connection structure pointer.
482 *
483 * @return Always zero.
[a2cd194]484 *
485 */
[c07544d3]486static int connection_fibril(void *arg)
[80649a91]487{
[c07544d3]488 /*
489 * Setup fibril-local connection pointer and call client_connection().
490 *
491 */
[bc1f1c2]492 FIBRIL_connection = (connection_t *) arg;
493 FIBRIL_connection->cfibril(FIBRIL_connection->callid,
494 &FIBRIL_connection->call);
[a46da63]495
[36c9234]496 /* Remove myself from the connection hash table */
[01ff41c]497 futex_down(&async_futex);
[c07544d3]498 unsigned long key = FIBRIL_connection->in_phone_hash;
[a2cd194]499 hash_table_remove(&conn_hash_table, &key, 1);
[01ff41c]500 futex_up(&async_futex);
[a46da63]501
[36c9234]502 /* Answer all remaining messages with EHANGUP */
[bc1f1c2]503 while (!list_empty(&FIBRIL_connection->msg_queue)) {
[cc27c8c5]504 msg_t *msg;
[c07544d3]505
[cc27c8c5]506 msg = list_get_instance(FIBRIL_connection->msg_queue.next,
507 msg_t, link);
[a2cd194]508 list_remove(&msg->link);
[b74959bd]509 ipc_answer_0(msg->callid, EHANGUP);
[a2cd194]510 free(msg);
511 }
[c07544d3]512
[bc1f1c2]513 if (FIBRIL_connection->close_callid)
[b74959bd]514 ipc_answer_0(FIBRIL_connection->close_callid, EOK);
[a46da63]515
516 return 0;
[80649a91]517}
518
[f2f0392]519/** Create a new fibril for a new connection.
[80649a91]520 *
[c07544d3]521 * Create new fibril for connection, fill in connection structures and inserts
[f2f0392]522 * it into the hash table, so that later we can easily do routing of messages to
523 * particular fibrils.
[53ca318]524 *
[c07544d3]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.
[36c9234]535 *
[80649a91]536 */
[bc1f1c2]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 *))
[80649a91]539{
[c07544d3]540 connection_t *conn = malloc(sizeof(*conn));
[80649a91]541 if (!conn) {
[6675c70]542 if (callid)
[b74959bd]543 ipc_answer_0(callid, ENOMEM);
[53ca318]544 return NULL;
[80649a91]545 }
[c07544d3]546
[44c6d88d]547 conn->in_phone_hash = in_phone_hash;
[80649a91]548 list_initialize(&conn->msg_queue);
549 conn->callid = callid;
[c07544d3]550 conn->close_callid = false;
551
[eaf34f7]552 if (call)
553 conn->call = *call;
[6b21292]554
[c07544d3]555 /* We will activate the fibril ASAP */
556 conn->wdata.active = true;
557 conn->cfibril = cfibril;
[bc1f1c2]558 conn->wdata.fid = fibril_create(connection_fibril, conn);
[c07544d3]559
[bc1f1c2]560 if (!conn->wdata.fid) {
[80649a91]561 free(conn);
[6675c70]562 if (callid)
[b74959bd]563 ipc_answer_0(callid, ENOMEM);
[53ca318]564 return NULL;
[80649a91]565 }
[6b21292]566
[36c9234]567 /* Add connection to the connection hash table */
[9db9b10]568 unsigned long key = conn->in_phone_hash;
[c07544d3]569
[01ff41c]570 futex_down(&async_futex);
[80649a91]571 hash_table_insert(&conn_hash_table, &key, &conn->link);
[01ff41c]572 futex_up(&async_futex);
[6b21292]573
[bc1f1c2]574 fibril_add_ready(conn->wdata.fid);
[6b21292]575
[bc1f1c2]576 return conn->wdata.fid;
[80649a91]577}
578
[36c9234]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 *
[c07544d3]584 * @param callid Hash of the incoming call.
585 * @param call Data of the incoming call.
[6b21292]586 *
[36c9234]587 */
[80649a91]588static void handle_call(ipc_callid_t callid, ipc_call_t *call)
589{
[44c6d88d]590 /* Unrouted call - do some default behaviour */
[15039b67]591 if ((callid & IPC_CALLID_NOTIFICATION)) {
[c07544d3]592 process_notification(callid, call);
[9db9b10]593 goto out;
[6b21292]594 }
595
[15039b67]596 switch (IPC_GET_METHOD(*call)) {
[2c0e5d2]597 case IPC_M_CONNECT_ME:
[80649a91]598 case IPC_M_CONNECT_ME_TO:
[f2f0392]599 /* Open new connection with fibril etc. */
[b61d47d]600 async_new_connection(IPC_GET_ARG5(*call), callid, call,
[bc1f1c2]601 client_connection);
[9db9b10]602 goto out;
[80649a91]603 }
[6b21292]604
[36c9234]605 /* Try to route the call through the connection hash table */
[44c6d88d]606 if (route_call(callid, call))
[9db9b10]607 goto out;
[6b21292]608
[44c6d88d]609 /* Unknown call from unknown phone - hang it up */
[b74959bd]610 ipc_answer_0(callid, EHANGUP);
[9db9b10]611 return;
612
613out:
[953769f]614 ;
[450cd3a]615}
616
[f2f0392]617/** Fire all timeouts that expired. */
[c042bdd]618static void handle_expired_timeouts(void)
619{
620 struct timeval tv;
[36c9234]621 gettimeofday(&tv, NULL);
[c07544d3]622
[c042bdd]623 futex_down(&async_futex);
[c07544d3]624
625 link_t *cur = timeout_list.next;
[c042bdd]626 while (cur != &timeout_list) {
[c07544d3]627 awaiter_t *waiter = list_get_instance(cur, awaiter_t, link);
628
[49d072e]629 if (tv_gt(&waiter->expires, &tv))
[c042bdd]630 break;
[c07544d3]631
[c042bdd]632 cur = cur->next;
[c07544d3]633
[49d072e]634 list_remove(&waiter->link);
[c07544d3]635 waiter->inlist = false;
636 waiter->timedout = true;
637
[36c9234]638 /*
[c07544d3]639 * Redundant condition?
640 * The fibril should not be active when it gets here.
[c042bdd]641 */
[49d072e]642 if (!waiter->active) {
[c07544d3]643 waiter->active = true;
[bc1f1c2]644 fibril_add_ready(waiter->fid);
[c042bdd]645 }
646 }
[c07544d3]647
[c042bdd]648 futex_up(&async_futex);
649}
650
[36c9234]651/** Endless loop dispatching incoming calls and answers.
652 *
[c07544d3]653 * @return Never returns.
654 *
[36c9234]655 */
[085bd54]656static int async_manager_worker(void)
[80649a91]657{
[c07544d3]658 while (true) {
[116d3f6f]659 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
[a46da63]660 futex_up(&async_futex);
[36c9234]661 /*
662 * async_futex is always held when entering a manager
663 * fibril.
[a46da63]664 */
[80649a91]665 continue;
666 }
[c07544d3]667
[c042bdd]668 futex_down(&async_futex);
[c07544d3]669
670 suseconds_t timeout;
[c042bdd]671 if (!list_empty(&timeout_list)) {
[cc27c8c5]672 awaiter_t *waiter = list_get_instance(timeout_list.next,
673 awaiter_t, link);
[c07544d3]674
675 struct timeval tv;
[bc1f1c2]676 gettimeofday(&tv, NULL);
[c07544d3]677
[49d072e]678 if (tv_gteq(&tv, &waiter->expires)) {
[6c46350]679 futex_up(&async_futex);
[c042bdd]680 handle_expired_timeouts();
681 continue;
682 } else
[49d072e]683 timeout = tv_sub(&waiter->expires, &tv);
[c042bdd]684 } else
[0b99e40]685 timeout = SYNCH_NO_TIMEOUT;
[c07544d3]686
[c042bdd]687 futex_up(&async_futex);
[8619f25]688
689 atomic_inc(&threads_in_ipc_wait);
[c07544d3]690
691 ipc_call_t call;
[cc27c8c5]692 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
693 SYNCH_FLAGS_NONE);
[c07544d3]694
[8619f25]695 atomic_dec(&threads_in_ipc_wait);
696
[0b99e40]697 if (!callid) {
[c042bdd]698 handle_expired_timeouts();
[0b99e40]699 continue;
700 }
[c07544d3]701
702 if (callid & IPC_CALLID_ANSWERED)
[80649a91]703 continue;
[c07544d3]704
[80649a91]705 handle_call(callid, &call);
706 }
[a46da63]707
708 return 0;
[80649a91]709}
710
[36c9234]711/** Function to start async_manager as a standalone fibril.
[c07544d3]712 *
[36c9234]713 * When more kernel threads are used, one async manager should exist per thread.
714 *
[c07544d3]715 * @param arg Unused.
716 * @return Never returns.
[36c9234]717 *
[a2cd194]718 */
[9591265]719static int async_manager_fibril(void *arg)
[80649a91]720{
[a46da63]721 futex_up(&async_futex);
[c07544d3]722
[36c9234]723 /*
724 * async_futex is always locked when entering manager
725 */
[085bd54]726 async_manager_worker();
[a46da63]727
728 return 0;
[80649a91]729}
[450cd3a]730
[36c9234]731/** Add one manager to manager list. */
[80649a91]732void async_create_manager(void)
[450cd3a]733{
[c07544d3]734 fid_t fid = fibril_create(async_manager_fibril, NULL);
[bc1f1c2]735 fibril_add_manager(fid);
[80649a91]736}
737
738/** Remove one manager from manager list */
739void async_destroy_manager(void)
740{
[bc1f1c2]741 fibril_remove_manager();
[80649a91]742}
743
[36c9234]744/** Initialize the async framework.
745 *
[c07544d3]746 * @return Zero on success or an error code.
[36c9234]747 */
[db24058]748int __async_init(void)
[80649a91]749{
[bc1f1c2]750 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
751 &conn_hash_table_ops)) {
[80649a91]752 printf("%s: cannot create hash table\n", "async");
753 return ENOMEM;
754 }
755
[a46da63]756 return 0;
[450cd3a]757}
[01ff41c]758
[36c9234]759/** Reply received callback.
[01ff41c]760 *
[36c9234]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 *
[c07544d3]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.
[01ff41c]769 */
[c07544d3]770static void reply_received(void *arg, int retval, ipc_call_t *data)
[01ff41c]771{
[9db9b10]772 futex_down(&async_futex);
773
[c07544d3]774 amsg_t *msg = (amsg_t *) arg;
[01ff41c]775 msg->retval = retval;
[c07544d3]776
[36c9234]777 /* Copy data after futex_down, just in case the call was detached */
[9db9b10]778 if ((msg->dataptr) && (data))
[c07544d3]779 *msg->dataptr = *data;
780
[c042bdd]781 write_barrier();
[c07544d3]782
[c042bdd]783 /* Remove message from timeout list */
[49d072e]784 if (msg->wdata.inlist)
785 list_remove(&msg->wdata.link);
[c07544d3]786
787 msg->done = true;
[36c9234]788 if (!msg->wdata.active) {
[c07544d3]789 msg->wdata.active = true;
[bc1f1c2]790 fibril_add_ready(msg->wdata.fid);
[01ff41c]791 }
[c07544d3]792
[01ff41c]793 futex_up(&async_futex);
794}
795
[36c9234]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.
[01ff41c]800 *
[c07544d3]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.
[36c9234]811 *
[01ff41c]812 */
[0cc4313]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)
[01ff41c]815{
[c07544d3]816 amsg_t *msg = malloc(sizeof(*msg));
817
818 if (!msg)
819 return 0;
[6b21292]820
[c07544d3]821 msg->done = false;
[01ff41c]822 msg->dataptr = dataptr;
[6b21292]823
[cc99bcd]824 msg->wdata.inlist = false;
[36c9234]825 /* We may sleep in the next method, but it will use its own mechanism */
[c07544d3]826 msg->wdata.active = true;
827
[0cc4313]828 ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
[c07544d3]829 reply_received, true);
[6b21292]830
[01ff41c]831 return (aid_t) msg;
832}
833
[90f5d64]834/** Send message and return id of the sent message
835 *
[36c9234]836 * The return value can be used as input for async_wait() to wait for
837 * completion.
838 *
[c07544d3]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.
[36c9234]850 *
[90f5d64]851 */
[0cc4313]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)
[90f5d64]855{
[c07544d3]856 amsg_t *msg = malloc(sizeof(*msg));
[6b21292]857
[c07544d3]858 if (!msg)
859 return 0;
860
861 msg->done = false;
[90f5d64]862 msg->dataptr = dataptr;
[6b21292]863
[cc99bcd]864 msg->wdata.inlist = false;
[36c9234]865 /* We may sleep in next method, but it will use its own mechanism */
[c07544d3]866 msg->wdata.active = true;
[6b21292]867
[0cc4313]868 ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
[c07544d3]869 reply_received, true);
[6b21292]870
[90f5d64]871 return (aid_t) msg;
872}
873
[36c9234]874/** Wait for a message sent by the async framework.
[01ff41c]875 *
[c07544d3]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 *
[01ff41c]880 */
881void async_wait_for(aid_t amsgid, ipcarg_t *retval)
882{
883 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]884
[01ff41c]885 futex_down(&async_futex);
886 if (msg->done) {
887 futex_up(&async_futex);
888 goto done;
889 }
[c07544d3]890
[bc1f1c2]891 msg->wdata.fid = fibril_get_id();
[c07544d3]892 msg->wdata.active = false;
893 msg->wdata.inlist = false;
894
[36c9234]895 /* Leave the async_futex locked when entering this function */
[116d3f6f]896 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]897
898 /* Futex is up automatically after fibril_switch */
899
[01ff41c]900done:
901 if (retval)
902 *retval = msg->retval;
[c07544d3]903
[01ff41c]904 free(msg);
905}
[0b99e40]906
[36c9234]907/** Wait for a message sent by the async framework, timeout variant.
[c042bdd]908 *
[c07544d3]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.
[c042bdd]915 *
916 */
917int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
918{
919 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]920
[86029498]921 /* TODO: Let it go through the event read at least once */
922 if (timeout < 0)
923 return ETIMEOUT;
[c07544d3]924
[c042bdd]925 futex_down(&async_futex);
926 if (msg->done) {
927 futex_up(&async_futex);
928 goto done;
929 }
[c07544d3]930
[49d072e]931 gettimeofday(&msg->wdata.expires, NULL);
932 tv_add(&msg->wdata.expires, timeout);
[c07544d3]933
[bc1f1c2]934 msg->wdata.fid = fibril_get_id();
[c07544d3]935 msg->wdata.active = false;
[49d072e]936 insert_timeout(&msg->wdata);
[c07544d3]937
[36c9234]938 /* Leave the async_futex locked when entering this function */
[116d3f6f]939 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]940
941 /* Futex is up automatically after fibril_switch */
942
[c042bdd]943 if (!msg->done)
944 return ETIMEOUT;
[c07544d3]945
[c042bdd]946done:
947 if (retval)
948 *retval = msg->retval;
[c07544d3]949
[c042bdd]950 free(msg);
[c07544d3]951
[c042bdd]952 return 0;
953}
[0b99e40]954
[36c9234]955/** Wait for specified time.
[44c6d88d]956 *
[36c9234]957 * The current fibril is suspended but the thread continues to execute.
958 *
[c07544d3]959 * @param timeout Duration of the wait in microseconds.
960 *
[44c6d88d]961 */
962void async_usleep(suseconds_t timeout)
963{
[c07544d3]964 amsg_t *msg = malloc(sizeof(*msg));
[44c6d88d]965
966 if (!msg)
967 return;
[6b21292]968
[bc1f1c2]969 msg->wdata.fid = fibril_get_id();
[c07544d3]970 msg->wdata.active = false;
[6b21292]971
[49d072e]972 gettimeofday(&msg->wdata.expires, NULL);
973 tv_add(&msg->wdata.expires, timeout);
[6b21292]974
[44c6d88d]975 futex_down(&async_futex);
[c07544d3]976
[49d072e]977 insert_timeout(&msg->wdata);
[c07544d3]978
[36c9234]979 /* Leave the async_futex locked when entering this function */
[116d3f6f]980 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]981
982 /* Futex is up automatically after fibril_switch() */
983
[44c6d88d]984 free(msg);
985}
[da0c91e7]986
[36c9234]987/** Setter for client_connection function pointer.
[da0c91e7]988 *
[c07544d3]989 * @param conn Function that will implement a new connection fibril.
990 *
[da0c91e7]991 */
992void async_set_client_connection(async_client_conn_t conn)
993{
994 client_connection = conn;
995}
[36c9234]996
997/** Setter for interrupt_received function pointer.
998 *
[c07544d3]999 * @param intr Function that will implement a new interrupt
1000 * notification fibril.
[36c9234]1001 */
[c07544d3]1002void async_set_interrupt_received(async_client_conn_t intr)
[51dbadf3]1003{
[c07544d3]1004 interrupt_received = intr;
[51dbadf3]1005}
[085bd54]1006
[0cc4313]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 *
[c07544d3]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 *
[0cc4313]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)
[085bd54]1032{
[0cc4313]1033 ipc_call_t result;
1034 aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1035 &result);
[c07544d3]1036
1037 ipcarg_t rc;
[0cc4313]1038 async_wait_for(eid, &rc);
[c07544d3]1039
1040 if (r1)
[0cc4313]1041 *r1 = IPC_GET_ARG1(result);
[c07544d3]1042
[0cc4313]1043 if (r2)
1044 *r2 = IPC_GET_ARG2(result);
[c07544d3]1045
[0cc4313]1046 if (r3)
1047 *r3 = IPC_GET_ARG3(result);
[c07544d3]1048
[0cc4313]1049 if (r4)
1050 *r4 = IPC_GET_ARG4(result);
[c07544d3]1051
[0cc4313]1052 if (r5)
1053 *r5 = IPC_GET_ARG5(result);
[c07544d3]1054
[0cc4313]1055 return rc;
[085bd54]1056}
1057
[0cc4313]1058/** Pseudo-synchronous message sending - slow version.
1059 *
1060 * Send message asynchronously and return only after the reply arrives.
1061 *
[c07544d3]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 *
[0cc4313]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)
[085bd54]1081{
[0cc4313]1082 ipc_call_t result;
1083 aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1084 &result);
[c07544d3]1085
1086 ipcarg_t rc;
[0cc4313]1087 async_wait_for(eid, &rc);
[c07544d3]1088
1089 if (r1)
[0cc4313]1090 *r1 = IPC_GET_ARG1(result);
[c07544d3]1091
[0cc4313]1092 if (r2)
1093 *r2 = IPC_GET_ARG2(result);
[c07544d3]1094
[0cc4313]1095 if (r3)
1096 *r3 = IPC_GET_ARG3(result);
[c07544d3]1097
[0cc4313]1098 if (r4)
1099 *r4 = IPC_GET_ARG4(result);
[c07544d3]1100
[0cc4313]1101 if (r5)
1102 *r5 = IPC_GET_ARG5(result);
[c07544d3]1103
[0cc4313]1104 return rc;
[085bd54]1105}
[b2951e2]1106
[a46da63]1107/** @}
[b2951e2]1108 */
Note: See TracBrowser for help on using the repository browser.