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