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