[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>
|
---|
[c042bdd] | 102 | #include <time.h>
|
---|
| 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 |
|
---|
[c042bdd] | 164 | /** Add microseconds to give timeval */
|
---|
| 165 | static void tv_add(struct timeval *tv, suseconds_t usecs)
|
---|
| 166 | {
|
---|
| 167 | tv->tv_sec += usecs / 1000000;
|
---|
| 168 | tv->tv_usec += usecs % 1000000;
|
---|
| 169 | if (tv->tv_usec > 1000000) {
|
---|
| 170 | tv->tv_sec++;
|
---|
| 171 | tv->tv_usec -= 1000000;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /** Subtract 2 timevals, return microseconds difference */
|
---|
| 176 | static suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
|
---|
| 177 | {
|
---|
| 178 | suseconds_t result;
|
---|
| 179 |
|
---|
| 180 | result = tv1->tv_usec - tv2->tv_usec;
|
---|
| 181 | result += (tv1->tv_sec - tv2->tv_sec) * 1000000;
|
---|
| 182 |
|
---|
| 183 | return result;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /** Compare timeval
|
---|
| 187 | *
|
---|
| 188 | * @return 1 if tv1 > tv2, otherwise 0
|
---|
| 189 | */
|
---|
| 190 | static int tv_gt(struct timeval *tv1, struct timeval *tv2)
|
---|
| 191 | {
|
---|
| 192 | if (tv1->tv_sec > tv2->tv_sec)
|
---|
| 193 | return 1;
|
---|
| 194 | if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec)
|
---|
| 195 | return 1;
|
---|
| 196 | return 0;
|
---|
| 197 | }
|
---|
[c0e674a] | 198 | static int tv_gteq(struct timeval *tv1, struct timeval *tv2)
|
---|
| 199 | {
|
---|
| 200 | if (tv1->tv_sec > tv2->tv_sec)
|
---|
| 201 | return 1;
|
---|
| 202 | if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec >= tv2->tv_usec)
|
---|
| 203 | return 1;
|
---|
| 204 | return 0;
|
---|
| 205 | }
|
---|
[c042bdd] | 206 |
|
---|
[80649a91] | 207 | /* Hash table functions */
|
---|
[a2cd194] | 208 | #define CONN_HASH_TABLE_CHAINS 32
|
---|
[80649a91] | 209 |
|
---|
| 210 | static hash_index_t conn_hash(unsigned long *key)
|
---|
[450cd3a] | 211 | {
|
---|
[80649a91] | 212 | assert(key);
|
---|
[a2cd194] | 213 | return ((*key) >> 4) % CONN_HASH_TABLE_CHAINS;
|
---|
[450cd3a] | 214 | }
|
---|
[06502f7d] | 215 |
|
---|
[80649a91] | 216 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[450cd3a] | 217 | {
|
---|
[80649a91] | 218 | connection_t *hs;
|
---|
| 219 |
|
---|
| 220 | hs = hash_table_get_instance(item, connection_t, link);
|
---|
| 221 |
|
---|
| 222 | return key[0] == hs->in_phone_hash;
|
---|
[450cd3a] | 223 | }
|
---|
[06502f7d] | 224 |
|
---|
[80649a91] | 225 | static void conn_remove(link_t *item)
|
---|
[450cd3a] | 226 | {
|
---|
[80649a91] | 227 | free(hash_table_get_instance(item, connection_t, link));
|
---|
[450cd3a] | 228 | }
|
---|
| 229 |
|
---|
[80649a91] | 230 |
|
---|
| 231 | /** Operations for NS hash table. */
|
---|
| 232 | static hash_table_operations_t conn_hash_table_ops = {
|
---|
| 233 | .hash = conn_hash,
|
---|
| 234 | .compare = conn_compare,
|
---|
| 235 | .remove_callback = conn_remove
|
---|
| 236 | };
|
---|
| 237 |
|
---|
[49d072e] | 238 | /** Insert sort timeout msg into timeouts list
|
---|
| 239 | *
|
---|
| 240 | */
|
---|
| 241 | static void insert_timeout(awaiter_t *wd)
|
---|
| 242 | {
|
---|
| 243 | link_t *tmp;
|
---|
| 244 | awaiter_t *cur;
|
---|
| 245 |
|
---|
| 246 | wd->timedout = 0;
|
---|
[085bd54] | 247 | wd->inlist = 1;
|
---|
[49d072e] | 248 |
|
---|
| 249 | tmp = timeout_list.next;
|
---|
| 250 | while (tmp != &timeout_list) {
|
---|
| 251 | cur = list_get_instance(tmp, awaiter_t, link);
|
---|
| 252 | if (tv_gteq(&cur->expires, &wd->expires))
|
---|
| 253 | break;
|
---|
| 254 | tmp = tmp->next;
|
---|
| 255 | }
|
---|
| 256 | list_append(&wd->link, tmp);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[01ff41c] | 259 | /*************************************************/
|
---|
| 260 |
|
---|
[80649a91] | 261 | /** Try to route a call to an appropriate connection thread
|
---|
| 262 | *
|
---|
| 263 | */
|
---|
| 264 | static int route_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
[450cd3a] | 265 | {
|
---|
[80649a91] | 266 | connection_t *conn;
|
---|
| 267 | msg_t *msg;
|
---|
| 268 | link_t *hlp;
|
---|
| 269 | unsigned long key;
|
---|
| 270 |
|
---|
[01ff41c] | 271 | futex_down(&async_futex);
|
---|
[80649a91] | 272 |
|
---|
| 273 | key = call->in_phone_hash;
|
---|
| 274 | hlp = hash_table_find(&conn_hash_table, &key);
|
---|
| 275 | if (!hlp) {
|
---|
[01ff41c] | 276 | futex_up(&async_futex);
|
---|
[80649a91] | 277 | return 0;
|
---|
[450cd3a] | 278 | }
|
---|
[80649a91] | 279 | conn = hash_table_get_instance(hlp, connection_t, link);
|
---|
| 280 |
|
---|
| 281 | msg = malloc(sizeof(*msg));
|
---|
| 282 | msg->callid = callid;
|
---|
| 283 | msg->call = *call;
|
---|
| 284 | list_append(&msg->link, &conn->msg_queue);
|
---|
[41269bd] | 285 |
|
---|
| 286 | if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP)
|
---|
| 287 | conn->close_callid = callid;
|
---|
[80649a91] | 288 |
|
---|
[49d072e] | 289 | /* If the call is waiting for event, run it */
|
---|
| 290 | if (!conn->wdata.active) {
|
---|
| 291 | /* If in timeout list, remove it */
|
---|
| 292 | if (conn->wdata.inlist) {
|
---|
| 293 | conn->wdata.inlist = 0;
|
---|
| 294 | list_remove(&conn->wdata.link);
|
---|
| 295 | }
|
---|
| 296 | conn->wdata.active = 1;
|
---|
[bc1f1c2] | 297 | fibril_add_ready(conn->wdata.fid);
|
---|
[80649a91] | 298 | }
|
---|
| 299 |
|
---|
[01ff41c] | 300 | futex_up(&async_futex);
|
---|
[80649a91] | 301 |
|
---|
| 302 | return 1;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[a2cd194] | 305 | /** Return new incoming message for current(thread-local) connection */
|
---|
[49d072e] | 306 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
|
---|
[80649a91] | 307 | {
|
---|
| 308 | msg_t *msg;
|
---|
| 309 | ipc_callid_t callid;
|
---|
[6c46350] | 310 | connection_t *conn;
|
---|
[80649a91] | 311 |
|
---|
[bc1f1c2] | 312 | assert(FIBRIL_connection);
|
---|
| 313 | /* GCC 4.1.0 coughs on FIBRIL_connection-> dereference,
|
---|
[6c46350] | 314 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
|
---|
| 315 | * I would never expect to find so many errors in
|
---|
| 316 | * compiler *($&$(*&$
|
---|
| 317 | */
|
---|
[bc1f1c2] | 318 | conn = FIBRIL_connection;
|
---|
[c0e674a] | 319 |
|
---|
[01ff41c] | 320 | futex_down(&async_futex);
|
---|
[80649a91] | 321 |
|
---|
[49d072e] | 322 | if (usecs) {
|
---|
[6c46350] | 323 | gettimeofday(&conn->wdata.expires, NULL);
|
---|
| 324 | tv_add(&conn->wdata.expires, usecs);
|
---|
[49d072e] | 325 | } else {
|
---|
[6c46350] | 326 | conn->wdata.inlist = 0;
|
---|
[49d072e] | 327 | }
|
---|
[80649a91] | 328 | /* If nothing in queue, wait until something appears */
|
---|
[6c46350] | 329 | while (list_empty(&conn->msg_queue)) {
|
---|
[085bd54] | 330 | if (usecs)
|
---|
[6c46350] | 331 | insert_timeout(&conn->wdata);
|
---|
[085bd54] | 332 |
|
---|
[6c46350] | 333 | conn->wdata.active = 0;
|
---|
[bc1f1c2] | 334 | fibril_schedule_next_adv(FIBRIL_TO_MANAGER);
|
---|
[49d072e] | 335 | /* Futex is up after getting back from async_manager
|
---|
| 336 | * get it again */
|
---|
| 337 | futex_down(&async_futex);
|
---|
[bc1f1c2] | 338 | if (usecs && conn->wdata.timedout &&
|
---|
[6c46350] | 339 | list_empty(&conn->msg_queue)) {
|
---|
[49d072e] | 340 | /* If we timed out-> exit */
|
---|
| 341 | futex_up(&async_futex);
|
---|
| 342 | return 0;
|
---|
| 343 | }
|
---|
[450cd3a] | 344 | }
|
---|
| 345 |
|
---|
[6c46350] | 346 | msg = list_get_instance(conn->msg_queue.next, msg_t, link);
|
---|
[80649a91] | 347 | list_remove(&msg->link);
|
---|
| 348 | callid = msg->callid;
|
---|
| 349 | *call = msg->call;
|
---|
| 350 | free(msg);
|
---|
| 351 |
|
---|
[01ff41c] | 352 | futex_up(&async_futex);
|
---|
[80649a91] | 353 | return callid;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
[a2cd194] | 356 | /** Thread function that gets created on new connection
|
---|
| 357 | *
|
---|
| 358 | * This function is defined as a weak symbol - to be redefined in
|
---|
| 359 | * user code.
|
---|
| 360 | */
|
---|
[da0c91e7] | 361 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
|
---|
[80649a91] | 362 | {
|
---|
[a2cd194] | 363 | ipc_answer_fast(callid, ENOENT, 0, 0);
|
---|
[80649a91] | 364 | }
|
---|
[51dbadf3] | 365 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
|
---|
[44c6d88d] | 366 | {
|
---|
| 367 | }
|
---|
| 368 |
|
---|
[a2cd194] | 369 | /** Wrapper for client connection thread
|
---|
| 370 | *
|
---|
| 371 | * When new connection arrives, thread with this function is created.
|
---|
| 372 | * It calls client_connection and does final cleanup.
|
---|
| 373 | *
|
---|
[b2951e2] | 374 | * @param arg Connection structure pointer
|
---|
[a2cd194] | 375 | */
|
---|
[bc1f1c2] | 376 | static int connection_fibril(void *arg)
|
---|
[80649a91] | 377 | {
|
---|
[a2cd194] | 378 | unsigned long key;
|
---|
| 379 | msg_t *msg;
|
---|
[41269bd] | 380 | int close_answered = 0;
|
---|
[a2cd194] | 381 |
|
---|
[80649a91] | 382 | /* Setup thread local connection pointer */
|
---|
[bc1f1c2] | 383 | FIBRIL_connection = (connection_t *) arg;
|
---|
| 384 | FIBRIL_connection->cfibril(FIBRIL_connection->callid,
|
---|
| 385 | &FIBRIL_connection->call);
|
---|
[a46da63] | 386 |
|
---|
[a2cd194] | 387 | /* Remove myself from connection hash table */
|
---|
[01ff41c] | 388 | futex_down(&async_futex);
|
---|
[bc1f1c2] | 389 | key = FIBRIL_connection->in_phone_hash;
|
---|
[a2cd194] | 390 | hash_table_remove(&conn_hash_table, &key, 1);
|
---|
[01ff41c] | 391 | futex_up(&async_futex);
|
---|
[a46da63] | 392 |
|
---|
[a2cd194] | 393 | /* Answer all remaining messages with ehangup */
|
---|
[bc1f1c2] | 394 | while (!list_empty(&FIBRIL_connection->msg_queue)) {
|
---|
| 395 | msg = list_get_instance(FIBRIL_connection->msg_queue.next,
|
---|
| 396 | msg_t, link);
|
---|
[a2cd194] | 397 | list_remove(&msg->link);
|
---|
[bc1f1c2] | 398 | if (msg->callid == FIBRIL_connection->close_callid)
|
---|
[41269bd] | 399 | close_answered = 1;
|
---|
[a2cd194] | 400 | ipc_answer_fast(msg->callid, EHANGUP, 0, 0);
|
---|
| 401 | free(msg);
|
---|
| 402 | }
|
---|
[bc1f1c2] | 403 | if (FIBRIL_connection->close_callid)
|
---|
| 404 | ipc_answer_fast(FIBRIL_connection->close_callid, 0, 0, 0);
|
---|
[a46da63] | 405 |
|
---|
| 406 | return 0;
|
---|
[80649a91] | 407 | }
|
---|
| 408 |
|
---|
| 409 | /** Create new thread for a new connection
|
---|
| 410 | *
|
---|
| 411 | * Creates new thread for connection, fills in connection
|
---|
| 412 | * structures and inserts it into the hash table, so that
|
---|
| 413 | * later we can easily do routing of messages to particular
|
---|
| 414 | * threads.
|
---|
[53ca318] | 415 | *
|
---|
[44c6d88d] | 416 | * @param in_phone_hash Identification of the incoming connection
|
---|
[53ca318] | 417 | * @param callid Callid of the IPC_M_CONNECT_ME_TO packet
|
---|
| 418 | * @param call Call data of the opening packet
|
---|
[bc1f1c2] | 419 | * @param cfibril Fibril function that should be called upon
|
---|
[53ca318] | 420 | * opening the connection
|
---|
[bc1f1c2] | 421 | * @return New fibril id.
|
---|
[80649a91] | 422 | */
|
---|
[bc1f1c2] | 423 | fid_t async_new_connection(ipcarg_t in_phone_hash, ipc_callid_t callid,
|
---|
| 424 | ipc_call_t *call, void (*cfibril)(ipc_callid_t, ipc_call_t *))
|
---|
[80649a91] | 425 | {
|
---|
| 426 | connection_t *conn;
|
---|
| 427 | unsigned long key;
|
---|
| 428 |
|
---|
| 429 | conn = malloc(sizeof(*conn));
|
---|
| 430 | if (!conn) {
|
---|
| 431 | ipc_answer_fast(callid, ENOMEM, 0, 0);
|
---|
[53ca318] | 432 | return NULL;
|
---|
[80649a91] | 433 | }
|
---|
[44c6d88d] | 434 | conn->in_phone_hash = in_phone_hash;
|
---|
[80649a91] | 435 | list_initialize(&conn->msg_queue);
|
---|
| 436 | conn->callid = callid;
|
---|
[41269bd] | 437 | conn->close_callid = 0;
|
---|
[eaf34f7] | 438 | if (call)
|
---|
| 439 | conn->call = *call;
|
---|
[49d072e] | 440 | conn->wdata.active = 1; /* We will activate it asap */
|
---|
[bc1f1c2] | 441 | conn->cfibril = cfibril;
|
---|
[49d072e] | 442 |
|
---|
[bc1f1c2] | 443 | conn->wdata.fid = fibril_create(connection_fibril, conn);
|
---|
| 444 | if (!conn->wdata.fid) {
|
---|
[80649a91] | 445 | free(conn);
|
---|
| 446 | ipc_answer_fast(callid, ENOMEM, 0, 0);
|
---|
[53ca318] | 447 | return NULL;
|
---|
[80649a91] | 448 | }
|
---|
[49d072e] | 449 | /* Add connection to hash table */
|
---|
[80649a91] | 450 | key = conn->in_phone_hash;
|
---|
[01ff41c] | 451 | futex_down(&async_futex);
|
---|
[80649a91] | 452 | hash_table_insert(&conn_hash_table, &key, &conn->link);
|
---|
[01ff41c] | 453 | futex_up(&async_futex);
|
---|
[80649a91] | 454 |
|
---|
[bc1f1c2] | 455 | fibril_add_ready(conn->wdata.fid);
|
---|
[53ca318] | 456 |
|
---|
[bc1f1c2] | 457 | return conn->wdata.fid;
|
---|
[80649a91] | 458 | }
|
---|
| 459 |
|
---|
[01ff41c] | 460 | /** Handle call that was received */
|
---|
[80649a91] | 461 | static void handle_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 462 | {
|
---|
[44c6d88d] | 463 | /* Unrouted call - do some default behaviour */
|
---|
[15039b67] | 464 | if ((callid & IPC_CALLID_NOTIFICATION)) {
|
---|
[085bd54] | 465 | in_interrupt_handler = 1;
|
---|
[51dbadf3] | 466 | (*interrupt_received)(callid,call);
|
---|
[085bd54] | 467 | in_interrupt_handler = 0;
|
---|
[44c6d88d] | 468 | return;
|
---|
[15039b67] | 469 | }
|
---|
| 470 |
|
---|
| 471 | switch (IPC_GET_METHOD(*call)) {
|
---|
[80649a91] | 472 | case IPC_M_CONNECT_ME_TO:
|
---|
| 473 | /* Open new connection with thread etc. */
|
---|
[bc1f1c2] | 474 | async_new_connection(IPC_GET_ARG3(*call), callid, call,
|
---|
| 475 | client_connection);
|
---|
[44c6d88d] | 476 | return;
|
---|
[80649a91] | 477 | }
|
---|
[44c6d88d] | 478 |
|
---|
| 479 | /* Try to route call through connection tables */
|
---|
| 480 | if (route_call(callid, call))
|
---|
| 481 | return;
|
---|
| 482 |
|
---|
| 483 | /* Unknown call from unknown phone - hang it up */
|
---|
| 484 | ipc_answer_fast(callid, EHANGUP, 0, 0);
|
---|
[450cd3a] | 485 | }
|
---|
| 486 |
|
---|
[6c46350] | 487 | /** Fire all timeouts that expired
|
---|
| 488 | *
|
---|
| 489 | */
|
---|
[c042bdd] | 490 | static void handle_expired_timeouts(void)
|
---|
| 491 | {
|
---|
| 492 | struct timeval tv;
|
---|
[49d072e] | 493 | awaiter_t *waiter;
|
---|
[c042bdd] | 494 | link_t *cur;
|
---|
| 495 |
|
---|
| 496 | gettimeofday(&tv,NULL);
|
---|
| 497 | futex_down(&async_futex);
|
---|
| 498 |
|
---|
| 499 | cur = timeout_list.next;
|
---|
| 500 | while (cur != &timeout_list) {
|
---|
[bc1f1c2] | 501 | waiter = list_get_instance(cur, awaiter_t, link);
|
---|
[49d072e] | 502 | if (tv_gt(&waiter->expires, &tv))
|
---|
[c042bdd] | 503 | break;
|
---|
| 504 | cur = cur->next;
|
---|
[49d072e] | 505 | list_remove(&waiter->link);
|
---|
| 506 | waiter->inlist = 0;
|
---|
| 507 | waiter->timedout = 1;
|
---|
[c042bdd] | 508 | /* Redundant condition? The thread should not
|
---|
| 509 | * be active when it gets here.
|
---|
| 510 | */
|
---|
[49d072e] | 511 | if (!waiter->active) {
|
---|
| 512 | waiter->active = 1;
|
---|
[bc1f1c2] | 513 | fibril_add_ready(waiter->fid);
|
---|
[c042bdd] | 514 | }
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | futex_up(&async_futex);
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[80649a91] | 520 | /** Endless loop dispatching incoming calls and answers */
|
---|
[085bd54] | 521 | static int async_manager_worker(void)
|
---|
[80649a91] | 522 | {
|
---|
| 523 | ipc_call_t call;
|
---|
| 524 | ipc_callid_t callid;
|
---|
[0b99e40] | 525 | int timeout;
|
---|
[49d072e] | 526 | awaiter_t *waiter;
|
---|
[c042bdd] | 527 | struct timeval tv;
|
---|
[80649a91] | 528 |
|
---|
| 529 | while (1) {
|
---|
[bc1f1c2] | 530 | if (fibril_schedule_next_adv(FIBRIL_FROM_MANAGER)) {
|
---|
[a46da63] | 531 | futex_up(&async_futex);
|
---|
| 532 | /* async_futex is always held
|
---|
| 533 | * when entering manager thread
|
---|
| 534 | */
|
---|
[80649a91] | 535 | continue;
|
---|
| 536 | }
|
---|
[c042bdd] | 537 | futex_down(&async_futex);
|
---|
| 538 | if (!list_empty(&timeout_list)) {
|
---|
[bc1f1c2] | 539 | waiter = list_get_instance(timeout_list.next, awaiter_t,
|
---|
| 540 | link);
|
---|
| 541 | gettimeofday(&tv, NULL);
|
---|
[49d072e] | 542 | if (tv_gteq(&tv, &waiter->expires)) {
|
---|
[6c46350] | 543 | futex_up(&async_futex);
|
---|
[c042bdd] | 544 | handle_expired_timeouts();
|
---|
| 545 | continue;
|
---|
| 546 | } else
|
---|
[49d072e] | 547 | timeout = tv_sub(&waiter->expires, &tv);
|
---|
[c042bdd] | 548 | } else
|
---|
[0b99e40] | 549 | timeout = SYNCH_NO_TIMEOUT;
|
---|
[c042bdd] | 550 | futex_up(&async_futex);
|
---|
| 551 |
|
---|
[2d22049] | 552 | callid = ipc_wait_cycle(&call, timeout, SYNCH_FLAGS_NONE);
|
---|
[0b99e40] | 553 |
|
---|
| 554 | if (!callid) {
|
---|
[c042bdd] | 555 | handle_expired_timeouts();
|
---|
[0b99e40] | 556 | continue;
|
---|
| 557 | }
|
---|
[80649a91] | 558 |
|
---|
[085bd54] | 559 | if (callid & IPC_CALLID_ANSWERED) {
|
---|
[80649a91] | 560 | continue;
|
---|
[085bd54] | 561 | }
|
---|
[01ff41c] | 562 |
|
---|
[80649a91] | 563 | handle_call(callid, &call);
|
---|
| 564 | }
|
---|
[a46da63] | 565 |
|
---|
| 566 | return 0;
|
---|
[80649a91] | 567 | }
|
---|
| 568 |
|
---|
[a2cd194] | 569 | /** Function to start async_manager as a standalone thread
|
---|
| 570 | *
|
---|
| 571 | * When more kernel threads are used, one async manager should
|
---|
| 572 | * exist per thread. The particular implementation may change,
|
---|
| 573 | * currently one async_manager is started automatically per kernel
|
---|
[9591265] | 574 | * thread except the main thread.
|
---|
[a2cd194] | 575 | */
|
---|
[9591265] | 576 | static int async_manager_fibril(void *arg)
|
---|
[80649a91] | 577 | {
|
---|
[a46da63] | 578 | futex_up(&async_futex);
|
---|
| 579 | /* async_futex is always locked when entering
|
---|
| 580 | * manager */
|
---|
[085bd54] | 581 | async_manager_worker();
|
---|
[a46da63] | 582 |
|
---|
| 583 | return 0;
|
---|
[80649a91] | 584 | }
|
---|
[450cd3a] | 585 |
|
---|
[80649a91] | 586 | /** Add one manager to manager list */
|
---|
| 587 | void async_create_manager(void)
|
---|
[450cd3a] | 588 | {
|
---|
[bc1f1c2] | 589 | fid_t fid;
|
---|
[80649a91] | 590 |
|
---|
[9591265] | 591 | fid = fibril_create(async_manager_fibril, NULL);
|
---|
[bc1f1c2] | 592 | fibril_add_manager(fid);
|
---|
[80649a91] | 593 | }
|
---|
| 594 |
|
---|
| 595 | /** Remove one manager from manager list */
|
---|
| 596 | void async_destroy_manager(void)
|
---|
| 597 | {
|
---|
[bc1f1c2] | 598 | fibril_remove_manager();
|
---|
[80649a91] | 599 | }
|
---|
| 600 |
|
---|
| 601 | /** Initialize internal structures needed for async manager */
|
---|
| 602 | int _async_init(void)
|
---|
| 603 | {
|
---|
[bc1f1c2] | 604 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
|
---|
| 605 | &conn_hash_table_ops)) {
|
---|
[80649a91] | 606 | printf("%s: cannot create hash table\n", "async");
|
---|
| 607 | return ENOMEM;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[a46da63] | 610 | return 0;
|
---|
[450cd3a] | 611 | }
|
---|
[01ff41c] | 612 |
|
---|
| 613 | /** IPC handler for messages in async framework
|
---|
| 614 | *
|
---|
[bc1f1c2] | 615 | * Notify the fibril which is waiting for this message, that it arrived
|
---|
[01ff41c] | 616 | */
|
---|
| 617 | static void reply_received(void *private, int retval,
|
---|
| 618 | ipc_call_t *data)
|
---|
| 619 | {
|
---|
| 620 | amsg_t *msg = (amsg_t *) private;
|
---|
| 621 |
|
---|
| 622 | msg->retval = retval;
|
---|
| 623 |
|
---|
| 624 | futex_down(&async_futex);
|
---|
| 625 | /* Copy data after futex_down, just in case the
|
---|
| 626 | * call was detached
|
---|
| 627 | */
|
---|
| 628 | if (msg->dataptr)
|
---|
| 629 | *msg->dataptr = *data;
|
---|
[0b99e40] | 630 |
|
---|
[c042bdd] | 631 | write_barrier();
|
---|
| 632 | /* Remove message from timeout list */
|
---|
[49d072e] | 633 | if (msg->wdata.inlist)
|
---|
| 634 | list_remove(&msg->wdata.link);
|
---|
[01ff41c] | 635 | msg->done = 1;
|
---|
[49d072e] | 636 | if (! msg->wdata.active) {
|
---|
| 637 | msg->wdata.active = 1;
|
---|
[bc1f1c2] | 638 | fibril_add_ready(msg->wdata.fid);
|
---|
[01ff41c] | 639 | }
|
---|
| 640 | futex_up(&async_futex);
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | /** Send message and return id of the sent message
|
---|
| 644 | *
|
---|
| 645 | * The return value can be used as input for async_wait() to wait
|
---|
| 646 | * for completion.
|
---|
| 647 | */
|
---|
| 648 | aid_t async_send_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
|
---|
| 649 | ipc_call_t *dataptr)
|
---|
| 650 | {
|
---|
| 651 | amsg_t *msg;
|
---|
| 652 |
|
---|
[085bd54] | 653 | if (in_interrupt_handler) {
|
---|
[bc1f1c2] | 654 | printf("Cannot send asynchronous request in interrupt "
|
---|
| 655 | "handler.\n");
|
---|
[085bd54] | 656 | _exit(1);
|
---|
| 657 | }
|
---|
| 658 |
|
---|
[01ff41c] | 659 | msg = malloc(sizeof(*msg));
|
---|
| 660 | msg->done = 0;
|
---|
| 661 | msg->dataptr = dataptr;
|
---|
[49d072e] | 662 |
|
---|
| 663 | msg->wdata.active = 1; /* We may sleep in next method, but it
|
---|
| 664 | * will use it's own mechanism */
|
---|
[bc1f1c2] | 665 | ipc_call_async_2(phoneid, method, arg1, arg2, msg, reply_received, 1);
|
---|
[01ff41c] | 666 |
|
---|
| 667 | return (aid_t) msg;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
[90f5d64] | 670 | /** Send message and return id of the sent message
|
---|
| 671 | *
|
---|
| 672 | * The return value can be used as input for async_wait() to wait
|
---|
| 673 | * for completion.
|
---|
| 674 | */
|
---|
| 675 | aid_t async_send_3(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2,
|
---|
| 676 | ipcarg_t arg3, ipc_call_t *dataptr)
|
---|
| 677 | {
|
---|
| 678 | amsg_t *msg;
|
---|
| 679 |
|
---|
[085bd54] | 680 | if (in_interrupt_handler) {
|
---|
[2b017ba] | 681 | printf("Cannot send asynchronous request in interrupt handler.\n");
|
---|
[085bd54] | 682 | _exit(1);
|
---|
| 683 | }
|
---|
| 684 |
|
---|
[90f5d64] | 685 | msg = malloc(sizeof(*msg));
|
---|
| 686 | msg->done = 0;
|
---|
| 687 | msg->dataptr = dataptr;
|
---|
| 688 |
|
---|
| 689 | msg->wdata.active = 1; /* We may sleep in next method, but it
|
---|
| 690 | * will use it's own mechanism */
|
---|
[bc1f1c2] | 691 | ipc_call_async_3(phoneid, method, arg1, arg2, arg3, msg, reply_received,
|
---|
| 692 | 1);
|
---|
[90f5d64] | 693 |
|
---|
| 694 | return (aid_t) msg;
|
---|
| 695 | }
|
---|
| 696 |
|
---|
[01ff41c] | 697 | /** Wait for a message sent by async framework
|
---|
| 698 | *
|
---|
| 699 | * @param amsgid Message ID to wait for
|
---|
| 700 | * @param retval Pointer to variable where will be stored retval
|
---|
| 701 | * of the answered message. If NULL, it is ignored.
|
---|
| 702 | *
|
---|
| 703 | */
|
---|
| 704 | void async_wait_for(aid_t amsgid, ipcarg_t *retval)
|
---|
| 705 | {
|
---|
| 706 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
| 707 |
|
---|
| 708 | futex_down(&async_futex);
|
---|
| 709 | if (msg->done) {
|
---|
| 710 | futex_up(&async_futex);
|
---|
| 711 | goto done;
|
---|
| 712 | }
|
---|
| 713 |
|
---|
[bc1f1c2] | 714 | msg->wdata.fid = fibril_get_id();
|
---|
[49d072e] | 715 | msg->wdata.active = 0;
|
---|
| 716 | msg->wdata.inlist = 0;
|
---|
[01ff41c] | 717 | /* Leave locked async_futex when entering this function */
|
---|
[bc1f1c2] | 718 | fibril_schedule_next_adv(FIBRIL_TO_MANAGER);
|
---|
| 719 | /* futex is up automatically after fibril_schedule_next...*/
|
---|
[01ff41c] | 720 | done:
|
---|
| 721 | if (retval)
|
---|
| 722 | *retval = msg->retval;
|
---|
| 723 | free(msg);
|
---|
| 724 | }
|
---|
[0b99e40] | 725 |
|
---|
[c042bdd] | 726 | /** Wait for a message sent by async framework with timeout
|
---|
| 727 | *
|
---|
| 728 | * @param amsgid Message ID to wait for
|
---|
| 729 | * @param retval Pointer to variable where will be stored retval
|
---|
| 730 | * of the answered message. If NULL, it is ignored.
|
---|
| 731 | * @param timeout Timeout in usecs
|
---|
| 732 | * @return 0 on success, ETIMEOUT if timeout expired
|
---|
| 733 | *
|
---|
| 734 | */
|
---|
| 735 | int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
|
---|
| 736 | {
|
---|
| 737 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
[0b99e40] | 738 |
|
---|
[86029498] | 739 | /* TODO: Let it go through the event read at least once */
|
---|
| 740 | if (timeout < 0)
|
---|
| 741 | return ETIMEOUT;
|
---|
| 742 |
|
---|
[c042bdd] | 743 | futex_down(&async_futex);
|
---|
| 744 | if (msg->done) {
|
---|
| 745 | futex_up(&async_futex);
|
---|
| 746 | goto done;
|
---|
| 747 | }
|
---|
[0b99e40] | 748 |
|
---|
[49d072e] | 749 | gettimeofday(&msg->wdata.expires, NULL);
|
---|
| 750 | tv_add(&msg->wdata.expires, timeout);
|
---|
| 751 |
|
---|
[bc1f1c2] | 752 | msg->wdata.fid = fibril_get_id();
|
---|
[49d072e] | 753 | msg->wdata.active = 0;
|
---|
| 754 | insert_timeout(&msg->wdata);
|
---|
[0b99e40] | 755 |
|
---|
[c042bdd] | 756 | /* Leave locked async_futex when entering this function */
|
---|
[bc1f1c2] | 757 | fibril_schedule_next_adv(FIBRIL_TO_MANAGER);
|
---|
| 758 | /* futex is up automatically after fibril_schedule_next...*/
|
---|
[0b99e40] | 759 |
|
---|
[c042bdd] | 760 | if (!msg->done)
|
---|
| 761 | return ETIMEOUT;
|
---|
[0b99e40] | 762 |
|
---|
[c042bdd] | 763 | done:
|
---|
| 764 | if (retval)
|
---|
| 765 | *retval = msg->retval;
|
---|
| 766 | free(msg);
|
---|
| 767 |
|
---|
| 768 | return 0;
|
---|
| 769 | }
|
---|
[0b99e40] | 770 |
|
---|
[44c6d88d] | 771 | /** Wait specified time, but in the meantime handle incoming events
|
---|
| 772 | *
|
---|
| 773 | * @param timeout Time in microseconds to wait
|
---|
| 774 | */
|
---|
| 775 | void async_usleep(suseconds_t timeout)
|
---|
| 776 | {
|
---|
| 777 | amsg_t *msg;
|
---|
| 778 |
|
---|
[085bd54] | 779 | if (in_interrupt_handler) {
|
---|
| 780 | printf("Cannot call async_usleep in interrupt handler.\n");
|
---|
| 781 | _exit(1);
|
---|
| 782 | }
|
---|
| 783 |
|
---|
[44c6d88d] | 784 | msg = malloc(sizeof(*msg));
|
---|
| 785 | if (!msg)
|
---|
| 786 | return;
|
---|
| 787 |
|
---|
[bc1f1c2] | 788 | msg->wdata.fid = fibril_get_id();
|
---|
[49d072e] | 789 | msg->wdata.active = 0;
|
---|
[44c6d88d] | 790 |
|
---|
[49d072e] | 791 | gettimeofday(&msg->wdata.expires, NULL);
|
---|
| 792 | tv_add(&msg->wdata.expires, timeout);
|
---|
[44c6d88d] | 793 |
|
---|
| 794 | futex_down(&async_futex);
|
---|
[49d072e] | 795 | insert_timeout(&msg->wdata);
|
---|
[44c6d88d] | 796 | /* Leave locked async_futex when entering this function */
|
---|
[bc1f1c2] | 797 | fibril_schedule_next_adv(FIBRIL_TO_MANAGER);
|
---|
| 798 | /* futex is up automatically after fibril_schedule_next...*/
|
---|
[44c6d88d] | 799 | free(msg);
|
---|
| 800 | }
|
---|
[da0c91e7] | 801 |
|
---|
| 802 | /** Set function that is called, IPC_M_CONNECT_ME_TO is received
|
---|
| 803 | *
|
---|
| 804 | * @param conn Function that will form new psthread.
|
---|
| 805 | */
|
---|
| 806 | void async_set_client_connection(async_client_conn_t conn)
|
---|
| 807 | {
|
---|
| 808 | client_connection = conn;
|
---|
| 809 | }
|
---|
[51dbadf3] | 810 | void async_set_interrupt_received(async_client_conn_t conn)
|
---|
| 811 | {
|
---|
| 812 | interrupt_received = conn;
|
---|
| 813 | }
|
---|
[085bd54] | 814 |
|
---|
| 815 | /* Primitive functions for simple communication */
|
---|
| 816 | void async_msg_3(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
| 817 | ipcarg_t arg2, ipcarg_t arg3)
|
---|
| 818 | {
|
---|
[bc1f1c2] | 819 | ipc_call_async_3(phoneid, method, arg1, arg2, arg3, NULL, NULL,
|
---|
| 820 | !in_interrupt_handler);
|
---|
[085bd54] | 821 | }
|
---|
| 822 |
|
---|
| 823 | void async_msg_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2)
|
---|
| 824 | {
|
---|
[bc1f1c2] | 825 | ipc_call_async_2(phoneid, method, arg1, arg2, NULL, NULL,
|
---|
| 826 | !in_interrupt_handler);
|
---|
[085bd54] | 827 | }
|
---|
[b2951e2] | 828 |
|
---|
[a46da63] | 829 | /** @}
|
---|
[b2951e2] | 830 | */
|
---|