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