[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 | */
|
---|
[b6ee5b1] | 235 | void async_insert_timeout(awaiter_t *wd)
|
---|
[49d072e] | 236 | {
|
---|
[f53cc81] | 237 | wd->to_event.occurred = false;
|
---|
| 238 | wd->to_event.inlist = true;
|
---|
[c07544d3] | 239 |
|
---|
| 240 | link_t *tmp = timeout_list.next;
|
---|
[49d072e] | 241 | while (tmp != &timeout_list) {
|
---|
[f53cc81] | 242 | awaiter_t *cur;
|
---|
[c07544d3] | 243 |
|
---|
[f53cc81] | 244 | cur = list_get_instance(tmp, awaiter_t, to_event.link);
|
---|
| 245 | if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
|
---|
[49d072e] | 246 | break;
|
---|
| 247 | tmp = tmp->next;
|
---|
| 248 | }
|
---|
[c07544d3] | 249 |
|
---|
[f53cc81] | 250 | list_append(&wd->to_event.link, tmp);
|
---|
[49d072e] | 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 */
|
---|
[f53cc81] | 297 | if (conn->wdata.to_event.inlist) {
|
---|
| 298 | conn->wdata.to_event.inlist = false;
|
---|
| 299 | list_remove(&conn->wdata.to_event.link);
|
---|
[49d072e] | 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) {
|
---|
[f53cc81] | 387 | gettimeofday(&conn->wdata.to_event.expires, NULL);
|
---|
| 388 | tv_add(&conn->wdata.to_event.expires, usecs);
|
---|
[c07544d3] | 389 | } else
|
---|
[f53cc81] | 390 | conn->wdata.to_event.inlist = false;
|
---|
[c07544d3] | 391 |
|
---|
[e70bfa5] | 392 | /* If nothing in queue, wait until something arrives */
|
---|
[6c46350] | 393 | while (list_empty(&conn->msg_queue)) {
|
---|
[8c8f8d6] | 394 | if (conn->close_callid) {
|
---|
| 395 | /*
|
---|
| 396 | * Handle the case when the connection was already
|
---|
| 397 | * closed by the client but the server did not notice
|
---|
| 398 | * the first IPC_M_PHONE_HUNGUP call and continues to
|
---|
| 399 | * call async_get_call_timeout(). Repeat
|
---|
| 400 | * IPC_M_PHONE_HUNGUP until the caller notices.
|
---|
| 401 | */
|
---|
| 402 | memset(call, 0, sizeof(ipc_call_t));
|
---|
| 403 | IPC_SET_METHOD(*call, IPC_M_PHONE_HUNGUP);
|
---|
| 404 | futex_up(&async_futex);
|
---|
| 405 | return conn->close_callid;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[085bd54] | 408 | if (usecs)
|
---|
[b6ee5b1] | 409 | async_insert_timeout(&conn->wdata);
|
---|
[c07544d3] | 410 |
|
---|
| 411 | conn->wdata.active = false;
|
---|
| 412 |
|
---|
[c7509e5] | 413 | /*
|
---|
| 414 | * Note: the current fibril will be rescheduled either due to a
|
---|
| 415 | * timeout or due to an arriving message destined to it. In the
|
---|
| 416 | * former case, handle_expired_timeouts() and, in the latter
|
---|
| 417 | * case, route_call() will perform the wakeup.
|
---|
| 418 | */
|
---|
[116d3f6f] | 419 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
[c07544d3] | 420 |
|
---|
[e70bfa5] | 421 | /*
|
---|
[c07544d3] | 422 | * Futex is up after getting back from async_manager.
|
---|
| 423 | * Get it again.
|
---|
[c7509e5] | 424 | */
|
---|
[49d072e] | 425 | futex_down(&async_futex);
|
---|
[f53cc81] | 426 | if ((usecs) && (conn->wdata.to_event.occurred)
|
---|
[c07544d3] | 427 | && (list_empty(&conn->msg_queue))) {
|
---|
[e70bfa5] | 428 | /* If we timed out -> exit */
|
---|
[49d072e] | 429 | futex_up(&async_futex);
|
---|
| 430 | return 0;
|
---|
| 431 | }
|
---|
[450cd3a] | 432 | }
|
---|
| 433 |
|
---|
[c07544d3] | 434 | msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
|
---|
[80649a91] | 435 | list_remove(&msg->link);
|
---|
[c07544d3] | 436 |
|
---|
| 437 | ipc_callid_t callid = msg->callid;
|
---|
[80649a91] | 438 | *call = msg->call;
|
---|
| 439 | free(msg);
|
---|
| 440 |
|
---|
[01ff41c] | 441 | futex_up(&async_futex);
|
---|
[80649a91] | 442 | return callid;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[36c9234] | 445 | /** Default fibril function that gets called to handle new connection.
|
---|
[a2cd194] | 446 | *
|
---|
[e70bfa5] | 447 | * This function is defined as a weak symbol - to be redefined in user code.
|
---|
[36c9234] | 448 | *
|
---|
[c07544d3] | 449 | * @param callid Hash of the incoming call.
|
---|
| 450 | * @param call Data of the incoming call.
|
---|
| 451 | *
|
---|
[a2cd194] | 452 | */
|
---|
[da0c91e7] | 453 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
|
---|
[80649a91] | 454 | {
|
---|
[b74959bd] | 455 | ipc_answer_0(callid, ENOENT);
|
---|
[80649a91] | 456 | }
|
---|
[36c9234] | 457 |
|
---|
| 458 | /** Default fibril function that gets called to handle interrupt notifications.
|
---|
| 459 | *
|
---|
[c07544d3] | 460 | * This function is defined as a weak symbol - to be redefined in user code.
|
---|
| 461 | *
|
---|
| 462 | * @param callid Hash of the incoming call.
|
---|
| 463 | * @param call Data of the incoming call.
|
---|
| 464 | *
|
---|
[36c9234] | 465 | */
|
---|
[51dbadf3] | 466 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
|
---|
[44c6d88d] | 467 | {
|
---|
| 468 | }
|
---|
| 469 |
|
---|
[f2f0392] | 470 | /** Wrapper for client connection fibril.
|
---|
| 471 | *
|
---|
[36c9234] | 472 | * When a new connection arrives, a fibril with this implementing function is
|
---|
[f2f0392] | 473 | * created. It calls client_connection() and does the final cleanup.
|
---|
[a2cd194] | 474 | *
|
---|
[c07544d3] | 475 | * @param arg Connection structure pointer.
|
---|
| 476 | *
|
---|
| 477 | * @return Always zero.
|
---|
[a2cd194] | 478 | *
|
---|
| 479 | */
|
---|
[c07544d3] | 480 | static int connection_fibril(void *arg)
|
---|
[80649a91] | 481 | {
|
---|
[c07544d3] | 482 | /*
|
---|
| 483 | * Setup fibril-local connection pointer and call client_connection().
|
---|
| 484 | *
|
---|
| 485 | */
|
---|
[bc1f1c2] | 486 | FIBRIL_connection = (connection_t *) arg;
|
---|
| 487 | FIBRIL_connection->cfibril(FIBRIL_connection->callid,
|
---|
| 488 | &FIBRIL_connection->call);
|
---|
[a46da63] | 489 |
|
---|
[36c9234] | 490 | /* Remove myself from the connection hash table */
|
---|
[01ff41c] | 491 | futex_down(&async_futex);
|
---|
[c07544d3] | 492 | unsigned long key = FIBRIL_connection->in_phone_hash;
|
---|
[a2cd194] | 493 | hash_table_remove(&conn_hash_table, &key, 1);
|
---|
[01ff41c] | 494 | futex_up(&async_futex);
|
---|
[a46da63] | 495 |
|
---|
[36c9234] | 496 | /* Answer all remaining messages with EHANGUP */
|
---|
[bc1f1c2] | 497 | while (!list_empty(&FIBRIL_connection->msg_queue)) {
|
---|
[cc27c8c5] | 498 | msg_t *msg;
|
---|
[c07544d3] | 499 |
|
---|
[cc27c8c5] | 500 | msg = list_get_instance(FIBRIL_connection->msg_queue.next,
|
---|
| 501 | msg_t, link);
|
---|
[a2cd194] | 502 | list_remove(&msg->link);
|
---|
[b74959bd] | 503 | ipc_answer_0(msg->callid, EHANGUP);
|
---|
[a2cd194] | 504 | free(msg);
|
---|
| 505 | }
|
---|
[c07544d3] | 506 |
|
---|
[bc1f1c2] | 507 | if (FIBRIL_connection->close_callid)
|
---|
[b74959bd] | 508 | ipc_answer_0(FIBRIL_connection->close_callid, EOK);
|
---|
[a46da63] | 509 |
|
---|
| 510 | return 0;
|
---|
[80649a91] | 511 | }
|
---|
| 512 |
|
---|
[f2f0392] | 513 | /** Create a new fibril for a new connection.
|
---|
[80649a91] | 514 | *
|
---|
[c07544d3] | 515 | * Create new fibril for connection, fill in connection structures and inserts
|
---|
[f2f0392] | 516 | * it into the hash table, so that later we can easily do routing of messages to
|
---|
| 517 | * particular fibrils.
|
---|
[53ca318] | 518 | *
|
---|
[c07544d3] | 519 | * @param in_phone_hash Identification of the incoming connection.
|
---|
| 520 | * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
|
---|
| 521 | * If callid is zero, the connection was opened by
|
---|
| 522 | * accepting the IPC_M_CONNECT_TO_ME call and this function
|
---|
| 523 | * is called directly by the server.
|
---|
| 524 | * @param call Call data of the opening call.
|
---|
| 525 | * @param cfibril Fibril function that should be called upon opening the
|
---|
| 526 | * connection.
|
---|
| 527 | *
|
---|
| 528 | * @return New fibril id or NULL on failure.
|
---|
[36c9234] | 529 | *
|
---|
[80649a91] | 530 | */
|
---|
[bc1f1c2] | 531 | fid_t async_new_connection(ipcarg_t in_phone_hash, ipc_callid_t callid,
|
---|
| 532 | ipc_call_t *call, void (*cfibril)(ipc_callid_t, ipc_call_t *))
|
---|
[80649a91] | 533 | {
|
---|
[c07544d3] | 534 | connection_t *conn = malloc(sizeof(*conn));
|
---|
[80649a91] | 535 | if (!conn) {
|
---|
[6675c70] | 536 | if (callid)
|
---|
[b74959bd] | 537 | ipc_answer_0(callid, ENOMEM);
|
---|
[53ca318] | 538 | return NULL;
|
---|
[80649a91] | 539 | }
|
---|
[c07544d3] | 540 |
|
---|
[44c6d88d] | 541 | conn->in_phone_hash = in_phone_hash;
|
---|
[80649a91] | 542 | list_initialize(&conn->msg_queue);
|
---|
| 543 | conn->callid = callid;
|
---|
[c4702804] | 544 | conn->close_callid = 0;
|
---|
[c07544d3] | 545 |
|
---|
[eaf34f7] | 546 | if (call)
|
---|
| 547 | conn->call = *call;
|
---|
[6b21292] | 548 |
|
---|
[c07544d3] | 549 | /* We will activate the fibril ASAP */
|
---|
| 550 | conn->wdata.active = true;
|
---|
| 551 | conn->cfibril = cfibril;
|
---|
[bc1f1c2] | 552 | conn->wdata.fid = fibril_create(connection_fibril, conn);
|
---|
[c07544d3] | 553 |
|
---|
[bc1f1c2] | 554 | if (!conn->wdata.fid) {
|
---|
[80649a91] | 555 | free(conn);
|
---|
[6675c70] | 556 | if (callid)
|
---|
[b74959bd] | 557 | ipc_answer_0(callid, ENOMEM);
|
---|
[53ca318] | 558 | return NULL;
|
---|
[80649a91] | 559 | }
|
---|
[6b21292] | 560 |
|
---|
[36c9234] | 561 | /* Add connection to the connection hash table */
|
---|
[9db9b10] | 562 | unsigned long key = conn->in_phone_hash;
|
---|
[c07544d3] | 563 |
|
---|
[01ff41c] | 564 | futex_down(&async_futex);
|
---|
[80649a91] | 565 | hash_table_insert(&conn_hash_table, &key, &conn->link);
|
---|
[01ff41c] | 566 | futex_up(&async_futex);
|
---|
[6b21292] | 567 |
|
---|
[bc1f1c2] | 568 | fibril_add_ready(conn->wdata.fid);
|
---|
[6b21292] | 569 |
|
---|
[bc1f1c2] | 570 | return conn->wdata.fid;
|
---|
[80649a91] | 571 | }
|
---|
| 572 |
|
---|
[36c9234] | 573 | /** Handle a call that was received.
|
---|
| 574 | *
|
---|
| 575 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
---|
| 576 | * Otherwise the call is routed to its connection fibril.
|
---|
| 577 | *
|
---|
[c07544d3] | 578 | * @param callid Hash of the incoming call.
|
---|
| 579 | * @param call Data of the incoming call.
|
---|
[6b21292] | 580 | *
|
---|
[36c9234] | 581 | */
|
---|
[80649a91] | 582 | static void handle_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 583 | {
|
---|
[44c6d88d] | 584 | /* Unrouted call - do some default behaviour */
|
---|
[15039b67] | 585 | if ((callid & IPC_CALLID_NOTIFICATION)) {
|
---|
[c07544d3] | 586 | process_notification(callid, call);
|
---|
[9db9b10] | 587 | goto out;
|
---|
[6b21292] | 588 | }
|
---|
| 589 |
|
---|
[15039b67] | 590 | switch (IPC_GET_METHOD(*call)) {
|
---|
[2c0e5d2] | 591 | case IPC_M_CONNECT_ME:
|
---|
[80649a91] | 592 | case IPC_M_CONNECT_ME_TO:
|
---|
[f2f0392] | 593 | /* Open new connection with fibril etc. */
|
---|
[b61d47d] | 594 | async_new_connection(IPC_GET_ARG5(*call), callid, call,
|
---|
[bc1f1c2] | 595 | client_connection);
|
---|
[9db9b10] | 596 | goto out;
|
---|
[80649a91] | 597 | }
|
---|
[6b21292] | 598 |
|
---|
[36c9234] | 599 | /* Try to route the call through the connection hash table */
|
---|
[44c6d88d] | 600 | if (route_call(callid, call))
|
---|
[9db9b10] | 601 | goto out;
|
---|
[6b21292] | 602 |
|
---|
[44c6d88d] | 603 | /* Unknown call from unknown phone - hang it up */
|
---|
[b74959bd] | 604 | ipc_answer_0(callid, EHANGUP);
|
---|
[9db9b10] | 605 | return;
|
---|
| 606 |
|
---|
| 607 | out:
|
---|
[953769f] | 608 | ;
|
---|
[450cd3a] | 609 | }
|
---|
| 610 |
|
---|
[f2f0392] | 611 | /** Fire all timeouts that expired. */
|
---|
[c042bdd] | 612 | static void handle_expired_timeouts(void)
|
---|
| 613 | {
|
---|
| 614 | struct timeval tv;
|
---|
[36c9234] | 615 | gettimeofday(&tv, NULL);
|
---|
[c07544d3] | 616 |
|
---|
[c042bdd] | 617 | futex_down(&async_futex);
|
---|
[c07544d3] | 618 |
|
---|
| 619 | link_t *cur = timeout_list.next;
|
---|
[c042bdd] | 620 | while (cur != &timeout_list) {
|
---|
[f53cc81] | 621 | awaiter_t *waiter;
|
---|
[c07544d3] | 622 |
|
---|
[f53cc81] | 623 | waiter = list_get_instance(cur, awaiter_t, to_event.link);
|
---|
| 624 | if (tv_gt(&waiter->to_event.expires, &tv))
|
---|
[c042bdd] | 625 | break;
|
---|
[f53cc81] | 626 |
|
---|
[c042bdd] | 627 | cur = cur->next;
|
---|
[f53cc81] | 628 |
|
---|
| 629 | list_remove(&waiter->to_event.link);
|
---|
| 630 | waiter->to_event.inlist = false;
|
---|
| 631 | waiter->to_event.occurred = true;
|
---|
[c07544d3] | 632 |
|
---|
[36c9234] | 633 | /*
|
---|
[c07544d3] | 634 | * Redundant condition?
|
---|
| 635 | * The fibril should not be active when it gets here.
|
---|
[c042bdd] | 636 | */
|
---|
[49d072e] | 637 | if (!waiter->active) {
|
---|
[c07544d3] | 638 | waiter->active = true;
|
---|
[bc1f1c2] | 639 | fibril_add_ready(waiter->fid);
|
---|
[c042bdd] | 640 | }
|
---|
| 641 | }
|
---|
[c07544d3] | 642 |
|
---|
[c042bdd] | 643 | futex_up(&async_futex);
|
---|
| 644 | }
|
---|
| 645 |
|
---|
[36c9234] | 646 | /** Endless loop dispatching incoming calls and answers.
|
---|
| 647 | *
|
---|
[c07544d3] | 648 | * @return Never returns.
|
---|
| 649 | *
|
---|
[36c9234] | 650 | */
|
---|
[085bd54] | 651 | static int async_manager_worker(void)
|
---|
[80649a91] | 652 | {
|
---|
[c07544d3] | 653 | while (true) {
|
---|
[116d3f6f] | 654 | if (fibril_switch(FIBRIL_FROM_MANAGER)) {
|
---|
[a46da63] | 655 | futex_up(&async_futex);
|
---|
[36c9234] | 656 | /*
|
---|
| 657 | * async_futex is always held when entering a manager
|
---|
| 658 | * fibril.
|
---|
[a46da63] | 659 | */
|
---|
[80649a91] | 660 | continue;
|
---|
| 661 | }
|
---|
[c07544d3] | 662 |
|
---|
[c042bdd] | 663 | futex_down(&async_futex);
|
---|
[c07544d3] | 664 |
|
---|
| 665 | suseconds_t timeout;
|
---|
[c042bdd] | 666 | if (!list_empty(&timeout_list)) {
|
---|
[cc27c8c5] | 667 | awaiter_t *waiter = list_get_instance(timeout_list.next,
|
---|
[f53cc81] | 668 | awaiter_t, to_event.link);
|
---|
[c07544d3] | 669 |
|
---|
| 670 | struct timeval tv;
|
---|
[bc1f1c2] | 671 | gettimeofday(&tv, NULL);
|
---|
[c07544d3] | 672 |
|
---|
[f53cc81] | 673 | if (tv_gteq(&tv, &waiter->to_event.expires)) {
|
---|
[6c46350] | 674 | futex_up(&async_futex);
|
---|
[c042bdd] | 675 | handle_expired_timeouts();
|
---|
| 676 | continue;
|
---|
| 677 | } else
|
---|
[f53cc81] | 678 | timeout = tv_sub(&waiter->to_event.expires,
|
---|
| 679 | &tv);
|
---|
[c042bdd] | 680 | } else
|
---|
[0b99e40] | 681 | timeout = SYNCH_NO_TIMEOUT;
|
---|
[c07544d3] | 682 |
|
---|
[c042bdd] | 683 | futex_up(&async_futex);
|
---|
[8619f25] | 684 |
|
---|
| 685 | atomic_inc(&threads_in_ipc_wait);
|
---|
[c07544d3] | 686 |
|
---|
| 687 | ipc_call_t call;
|
---|
[cc27c8c5] | 688 | ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
|
---|
| 689 | SYNCH_FLAGS_NONE);
|
---|
[c07544d3] | 690 |
|
---|
[8619f25] | 691 | atomic_dec(&threads_in_ipc_wait);
|
---|
| 692 |
|
---|
[0b99e40] | 693 | if (!callid) {
|
---|
[c042bdd] | 694 | handle_expired_timeouts();
|
---|
[0b99e40] | 695 | continue;
|
---|
| 696 | }
|
---|
[c07544d3] | 697 |
|
---|
| 698 | if (callid & IPC_CALLID_ANSWERED)
|
---|
[80649a91] | 699 | continue;
|
---|
[c07544d3] | 700 |
|
---|
[80649a91] | 701 | handle_call(callid, &call);
|
---|
| 702 | }
|
---|
[a46da63] | 703 |
|
---|
| 704 | return 0;
|
---|
[80649a91] | 705 | }
|
---|
| 706 |
|
---|
[36c9234] | 707 | /** Function to start async_manager as a standalone fibril.
|
---|
[c07544d3] | 708 | *
|
---|
[36c9234] | 709 | * When more kernel threads are used, one async manager should exist per thread.
|
---|
| 710 | *
|
---|
[c07544d3] | 711 | * @param arg Unused.
|
---|
| 712 | * @return Never returns.
|
---|
[36c9234] | 713 | *
|
---|
[a2cd194] | 714 | */
|
---|
[9591265] | 715 | static int async_manager_fibril(void *arg)
|
---|
[80649a91] | 716 | {
|
---|
[a46da63] | 717 | futex_up(&async_futex);
|
---|
[c07544d3] | 718 |
|
---|
[36c9234] | 719 | /*
|
---|
| 720 | * async_futex is always locked when entering manager
|
---|
| 721 | */
|
---|
[085bd54] | 722 | async_manager_worker();
|
---|
[a46da63] | 723 |
|
---|
| 724 | return 0;
|
---|
[80649a91] | 725 | }
|
---|
[450cd3a] | 726 |
|
---|
[36c9234] | 727 | /** Add one manager to manager list. */
|
---|
[80649a91] | 728 | void async_create_manager(void)
|
---|
[450cd3a] | 729 | {
|
---|
[c07544d3] | 730 | fid_t fid = fibril_create(async_manager_fibril, NULL);
|
---|
[bc1f1c2] | 731 | fibril_add_manager(fid);
|
---|
[80649a91] | 732 | }
|
---|
| 733 |
|
---|
| 734 | /** Remove one manager from manager list */
|
---|
| 735 | void async_destroy_manager(void)
|
---|
| 736 | {
|
---|
[bc1f1c2] | 737 | fibril_remove_manager();
|
---|
[80649a91] | 738 | }
|
---|
| 739 |
|
---|
[36c9234] | 740 | /** Initialize the async framework.
|
---|
| 741 | *
|
---|
[c07544d3] | 742 | * @return Zero on success or an error code.
|
---|
[36c9234] | 743 | */
|
---|
[db24058] | 744 | int __async_init(void)
|
---|
[80649a91] | 745 | {
|
---|
[bc1f1c2] | 746 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
|
---|
| 747 | &conn_hash_table_ops)) {
|
---|
[80649a91] | 748 | printf("%s: cannot create hash table\n", "async");
|
---|
| 749 | return ENOMEM;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
[a46da63] | 752 | return 0;
|
---|
[450cd3a] | 753 | }
|
---|
[01ff41c] | 754 |
|
---|
[36c9234] | 755 | /** Reply received callback.
|
---|
[01ff41c] | 756 | *
|
---|
[36c9234] | 757 | * This function is called whenever a reply for an asynchronous message sent out
|
---|
| 758 | * by the asynchronous framework is received.
|
---|
| 759 | *
|
---|
| 760 | * Notify the fibril which is waiting for this message that it has arrived.
|
---|
| 761 | *
|
---|
[c07544d3] | 762 | * @param arg Pointer to the asynchronous message record.
|
---|
| 763 | * @param retval Value returned in the answer.
|
---|
| 764 | * @param data Call data of the answer.
|
---|
[01ff41c] | 765 | */
|
---|
[c07544d3] | 766 | static void reply_received(void *arg, int retval, ipc_call_t *data)
|
---|
[01ff41c] | 767 | {
|
---|
[9db9b10] | 768 | futex_down(&async_futex);
|
---|
| 769 |
|
---|
[c07544d3] | 770 | amsg_t *msg = (amsg_t *) arg;
|
---|
[01ff41c] | 771 | msg->retval = retval;
|
---|
[c07544d3] | 772 |
|
---|
[36c9234] | 773 | /* Copy data after futex_down, just in case the call was detached */
|
---|
[9db9b10] | 774 | if ((msg->dataptr) && (data))
|
---|
[c07544d3] | 775 | *msg->dataptr = *data;
|
---|
| 776 |
|
---|
[c042bdd] | 777 | write_barrier();
|
---|
[c07544d3] | 778 |
|
---|
[c042bdd] | 779 | /* Remove message from timeout list */
|
---|
[f53cc81] | 780 | if (msg->wdata.to_event.inlist)
|
---|
| 781 | list_remove(&msg->wdata.to_event.link);
|
---|
[c07544d3] | 782 |
|
---|
| 783 | msg->done = true;
|
---|
[36c9234] | 784 | if (!msg->wdata.active) {
|
---|
[c07544d3] | 785 | msg->wdata.active = true;
|
---|
[bc1f1c2] | 786 | fibril_add_ready(msg->wdata.fid);
|
---|
[01ff41c] | 787 | }
|
---|
[c07544d3] | 788 |
|
---|
[01ff41c] | 789 | futex_up(&async_futex);
|
---|
| 790 | }
|
---|
| 791 |
|
---|
[36c9234] | 792 | /** Send message and return id of the sent message.
|
---|
| 793 | *
|
---|
| 794 | * The return value can be used as input for async_wait() to wait for
|
---|
| 795 | * completion.
|
---|
[01ff41c] | 796 | *
|
---|
[c07544d3] | 797 | * @param phoneid Handle of the phone that will be used for the send.
|
---|
| 798 | * @param method Service-defined method.
|
---|
| 799 | * @param arg1 Service-defined payload argument.
|
---|
| 800 | * @param arg2 Service-defined payload argument.
|
---|
| 801 | * @param arg3 Service-defined payload argument.
|
---|
| 802 | * @param arg4 Service-defined payload argument.
|
---|
| 803 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
| 804 | * stored.
|
---|
| 805 | *
|
---|
| 806 | * @return Hash of the sent message or 0 on error.
|
---|
[36c9234] | 807 | *
|
---|
[01ff41c] | 808 | */
|
---|
[0cc4313] | 809 | aid_t async_send_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
| 810 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
|
---|
[01ff41c] | 811 | {
|
---|
[c07544d3] | 812 | amsg_t *msg = malloc(sizeof(*msg));
|
---|
| 813 |
|
---|
| 814 | if (!msg)
|
---|
| 815 | return 0;
|
---|
[6b21292] | 816 |
|
---|
[c07544d3] | 817 | msg->done = false;
|
---|
[01ff41c] | 818 | msg->dataptr = dataptr;
|
---|
[6b21292] | 819 |
|
---|
[f53cc81] | 820 | msg->wdata.to_event.inlist = false;
|
---|
[36c9234] | 821 | /* We may sleep in the next method, but it will use its own mechanism */
|
---|
[c07544d3] | 822 | msg->wdata.active = true;
|
---|
| 823 |
|
---|
[0cc4313] | 824 | ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
|
---|
[c07544d3] | 825 | reply_received, true);
|
---|
[6b21292] | 826 |
|
---|
[01ff41c] | 827 | return (aid_t) msg;
|
---|
| 828 | }
|
---|
| 829 |
|
---|
[90f5d64] | 830 | /** Send message and return id of the sent message
|
---|
| 831 | *
|
---|
[36c9234] | 832 | * The return value can be used as input for async_wait() to wait for
|
---|
| 833 | * completion.
|
---|
| 834 | *
|
---|
[c07544d3] | 835 | * @param phoneid Handle of the phone that will be used for the send.
|
---|
| 836 | * @param method Service-defined method.
|
---|
| 837 | * @param arg1 Service-defined payload argument.
|
---|
| 838 | * @param arg2 Service-defined payload argument.
|
---|
| 839 | * @param arg3 Service-defined payload argument.
|
---|
| 840 | * @param arg4 Service-defined payload argument.
|
---|
| 841 | * @param arg5 Service-defined payload argument.
|
---|
| 842 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
| 843 | * stored.
|
---|
| 844 | *
|
---|
| 845 | * @return Hash of the sent message or 0 on error.
|
---|
[36c9234] | 846 | *
|
---|
[90f5d64] | 847 | */
|
---|
[0cc4313] | 848 | aid_t async_send_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
| 849 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5,
|
---|
| 850 | ipc_call_t *dataptr)
|
---|
[90f5d64] | 851 | {
|
---|
[c07544d3] | 852 | amsg_t *msg = malloc(sizeof(*msg));
|
---|
[6b21292] | 853 |
|
---|
[c07544d3] | 854 | if (!msg)
|
---|
| 855 | return 0;
|
---|
| 856 |
|
---|
| 857 | msg->done = false;
|
---|
[90f5d64] | 858 | msg->dataptr = dataptr;
|
---|
[6b21292] | 859 |
|
---|
[f53cc81] | 860 | msg->wdata.to_event.inlist = false;
|
---|
[36c9234] | 861 | /* We may sleep in next method, but it will use its own mechanism */
|
---|
[c07544d3] | 862 | msg->wdata.active = true;
|
---|
[6b21292] | 863 |
|
---|
[0cc4313] | 864 | ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
|
---|
[c07544d3] | 865 | reply_received, true);
|
---|
[6b21292] | 866 |
|
---|
[90f5d64] | 867 | return (aid_t) msg;
|
---|
| 868 | }
|
---|
| 869 |
|
---|
[36c9234] | 870 | /** Wait for a message sent by the async framework.
|
---|
[01ff41c] | 871 | *
|
---|
[c07544d3] | 872 | * @param amsgid Hash of the message to wait for.
|
---|
| 873 | * @param retval Pointer to storage where the retval of the answer will
|
---|
| 874 | * be stored.
|
---|
| 875 | *
|
---|
[01ff41c] | 876 | */
|
---|
| 877 | void async_wait_for(aid_t amsgid, ipcarg_t *retval)
|
---|
| 878 | {
|
---|
| 879 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
[c07544d3] | 880 |
|
---|
[01ff41c] | 881 | futex_down(&async_futex);
|
---|
| 882 | if (msg->done) {
|
---|
| 883 | futex_up(&async_futex);
|
---|
| 884 | goto done;
|
---|
| 885 | }
|
---|
[c07544d3] | 886 |
|
---|
[bc1f1c2] | 887 | msg->wdata.fid = fibril_get_id();
|
---|
[c07544d3] | 888 | msg->wdata.active = false;
|
---|
[f53cc81] | 889 | msg->wdata.to_event.inlist = false;
|
---|
[c07544d3] | 890 |
|
---|
[36c9234] | 891 | /* Leave the async_futex locked when entering this function */
|
---|
[116d3f6f] | 892 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
[c07544d3] | 893 |
|
---|
| 894 | /* Futex is up automatically after fibril_switch */
|
---|
| 895 |
|
---|
[01ff41c] | 896 | done:
|
---|
| 897 | if (retval)
|
---|
| 898 | *retval = msg->retval;
|
---|
[c07544d3] | 899 |
|
---|
[01ff41c] | 900 | free(msg);
|
---|
| 901 | }
|
---|
[0b99e40] | 902 |
|
---|
[36c9234] | 903 | /** Wait for a message sent by the async framework, timeout variant.
|
---|
[c042bdd] | 904 | *
|
---|
[c07544d3] | 905 | * @param amsgid Hash of the message to wait for.
|
---|
| 906 | * @param retval Pointer to storage where the retval of the answer will
|
---|
| 907 | * be stored.
|
---|
| 908 | * @param timeout Timeout in microseconds.
|
---|
| 909 | *
|
---|
| 910 | * @return Zero on success, ETIMEOUT if the timeout has expired.
|
---|
[c042bdd] | 911 | *
|
---|
| 912 | */
|
---|
| 913 | int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
|
---|
| 914 | {
|
---|
| 915 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
[c07544d3] | 916 |
|
---|
[86029498] | 917 | /* TODO: Let it go through the event read at least once */
|
---|
| 918 | if (timeout < 0)
|
---|
| 919 | return ETIMEOUT;
|
---|
[c07544d3] | 920 |
|
---|
[c042bdd] | 921 | futex_down(&async_futex);
|
---|
| 922 | if (msg->done) {
|
---|
| 923 | futex_up(&async_futex);
|
---|
| 924 | goto done;
|
---|
| 925 | }
|
---|
[c07544d3] | 926 |
|
---|
[f53cc81] | 927 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
---|
| 928 | tv_add(&msg->wdata.to_event.expires, timeout);
|
---|
[c07544d3] | 929 |
|
---|
[bc1f1c2] | 930 | msg->wdata.fid = fibril_get_id();
|
---|
[c07544d3] | 931 | msg->wdata.active = false;
|
---|
[b6ee5b1] | 932 | async_insert_timeout(&msg->wdata);
|
---|
[c07544d3] | 933 |
|
---|
[36c9234] | 934 | /* Leave the async_futex locked when entering this function */
|
---|
[116d3f6f] | 935 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
[c07544d3] | 936 |
|
---|
| 937 | /* Futex is up automatically after fibril_switch */
|
---|
| 938 |
|
---|
[c042bdd] | 939 | if (!msg->done)
|
---|
| 940 | return ETIMEOUT;
|
---|
[c07544d3] | 941 |
|
---|
[c042bdd] | 942 | done:
|
---|
| 943 | if (retval)
|
---|
| 944 | *retval = msg->retval;
|
---|
[c07544d3] | 945 |
|
---|
[c042bdd] | 946 | free(msg);
|
---|
[c07544d3] | 947 |
|
---|
[c042bdd] | 948 | return 0;
|
---|
| 949 | }
|
---|
[0b99e40] | 950 |
|
---|
[36c9234] | 951 | /** Wait for specified time.
|
---|
[44c6d88d] | 952 | *
|
---|
[36c9234] | 953 | * The current fibril is suspended but the thread continues to execute.
|
---|
| 954 | *
|
---|
[c07544d3] | 955 | * @param timeout Duration of the wait in microseconds.
|
---|
| 956 | *
|
---|
[44c6d88d] | 957 | */
|
---|
| 958 | void async_usleep(suseconds_t timeout)
|
---|
| 959 | {
|
---|
[c07544d3] | 960 | amsg_t *msg = malloc(sizeof(*msg));
|
---|
[44c6d88d] | 961 |
|
---|
| 962 | if (!msg)
|
---|
| 963 | return;
|
---|
[6b21292] | 964 |
|
---|
[bc1f1c2] | 965 | msg->wdata.fid = fibril_get_id();
|
---|
[c07544d3] | 966 | msg->wdata.active = false;
|
---|
[6b21292] | 967 |
|
---|
[f53cc81] | 968 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
---|
| 969 | tv_add(&msg->wdata.to_event.expires, timeout);
|
---|
[6b21292] | 970 |
|
---|
[44c6d88d] | 971 | futex_down(&async_futex);
|
---|
[c07544d3] | 972 |
|
---|
[b6ee5b1] | 973 | async_insert_timeout(&msg->wdata);
|
---|
[c07544d3] | 974 |
|
---|
[36c9234] | 975 | /* Leave the async_futex locked when entering this function */
|
---|
[116d3f6f] | 976 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
[c07544d3] | 977 |
|
---|
| 978 | /* Futex is up automatically after fibril_switch() */
|
---|
| 979 |
|
---|
[44c6d88d] | 980 | free(msg);
|
---|
| 981 | }
|
---|
[da0c91e7] | 982 |
|
---|
[36c9234] | 983 | /** Setter for client_connection function pointer.
|
---|
[da0c91e7] | 984 | *
|
---|
[c07544d3] | 985 | * @param conn Function that will implement a new connection fibril.
|
---|
| 986 | *
|
---|
[da0c91e7] | 987 | */
|
---|
| 988 | void async_set_client_connection(async_client_conn_t conn)
|
---|
| 989 | {
|
---|
| 990 | client_connection = conn;
|
---|
| 991 | }
|
---|
[36c9234] | 992 |
|
---|
| 993 | /** Setter for interrupt_received function pointer.
|
---|
| 994 | *
|
---|
[c07544d3] | 995 | * @param intr Function that will implement a new interrupt
|
---|
| 996 | * notification fibril.
|
---|
[36c9234] | 997 | */
|
---|
[c07544d3] | 998 | void async_set_interrupt_received(async_client_conn_t intr)
|
---|
[51dbadf3] | 999 | {
|
---|
[c07544d3] | 1000 | interrupt_received = intr;
|
---|
[51dbadf3] | 1001 | }
|
---|
[085bd54] | 1002 |
|
---|
[0cc4313] | 1003 | /** Pseudo-synchronous message sending - fast version.
|
---|
| 1004 | *
|
---|
| 1005 | * Send message asynchronously and return only after the reply arrives.
|
---|
| 1006 | *
|
---|
| 1007 | * This function can only transfer 4 register payload arguments. For
|
---|
| 1008 | * transferring more arguments, see the slower async_req_slow().
|
---|
| 1009 | *
|
---|
[c07544d3] | 1010 | * @param phoneid Hash of the phone through which to make the call.
|
---|
| 1011 | * @param method Method of the call.
|
---|
| 1012 | * @param arg1 Service-defined payload argument.
|
---|
| 1013 | * @param arg2 Service-defined payload argument.
|
---|
| 1014 | * @param arg3 Service-defined payload argument.
|
---|
| 1015 | * @param arg4 Service-defined payload argument.
|
---|
| 1016 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
| 1017 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
| 1018 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
| 1019 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
| 1020 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
| 1021 | *
|
---|
| 1022 | * @return Return code of the reply or a negative error code.
|
---|
| 1023 | *
|
---|
[0cc4313] | 1024 | */
|
---|
| 1025 | ipcarg_t async_req_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
| 1026 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t *r1, ipcarg_t *r2,
|
---|
| 1027 | ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
|
---|
[085bd54] | 1028 | {
|
---|
[0cc4313] | 1029 | ipc_call_t result;
|
---|
| 1030 | aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
| 1031 | &result);
|
---|
[c07544d3] | 1032 |
|
---|
| 1033 | ipcarg_t rc;
|
---|
[0cc4313] | 1034 | async_wait_for(eid, &rc);
|
---|
[c07544d3] | 1035 |
|
---|
| 1036 | if (r1)
|
---|
[0cc4313] | 1037 | *r1 = IPC_GET_ARG1(result);
|
---|
[c07544d3] | 1038 |
|
---|
[0cc4313] | 1039 | if (r2)
|
---|
| 1040 | *r2 = IPC_GET_ARG2(result);
|
---|
[c07544d3] | 1041 |
|
---|
[0cc4313] | 1042 | if (r3)
|
---|
| 1043 | *r3 = IPC_GET_ARG3(result);
|
---|
[c07544d3] | 1044 |
|
---|
[0cc4313] | 1045 | if (r4)
|
---|
| 1046 | *r4 = IPC_GET_ARG4(result);
|
---|
[c07544d3] | 1047 |
|
---|
[0cc4313] | 1048 | if (r5)
|
---|
| 1049 | *r5 = IPC_GET_ARG5(result);
|
---|
[c07544d3] | 1050 |
|
---|
[0cc4313] | 1051 | return rc;
|
---|
[085bd54] | 1052 | }
|
---|
| 1053 |
|
---|
[0cc4313] | 1054 | /** Pseudo-synchronous message sending - slow version.
|
---|
| 1055 | *
|
---|
| 1056 | * Send message asynchronously and return only after the reply arrives.
|
---|
| 1057 | *
|
---|
[c07544d3] | 1058 | * @param phoneid Hash of the phone through which to make the call.
|
---|
| 1059 | * @param method Method of the call.
|
---|
| 1060 | * @param arg1 Service-defined payload argument.
|
---|
| 1061 | * @param arg2 Service-defined payload argument.
|
---|
| 1062 | * @param arg3 Service-defined payload argument.
|
---|
| 1063 | * @param arg4 Service-defined payload argument.
|
---|
| 1064 | * @param arg5 Service-defined payload argument.
|
---|
| 1065 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
| 1066 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
| 1067 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
| 1068 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
| 1069 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
| 1070 | *
|
---|
| 1071 | * @return Return code of the reply or a negative error code.
|
---|
| 1072 | *
|
---|
[0cc4313] | 1073 | */
|
---|
| 1074 | ipcarg_t async_req_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
| 1075 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *r1,
|
---|
| 1076 | ipcarg_t *r2, ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
|
---|
[085bd54] | 1077 | {
|
---|
[0cc4313] | 1078 | ipc_call_t result;
|
---|
| 1079 | aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
|
---|
| 1080 | &result);
|
---|
[c07544d3] | 1081 |
|
---|
| 1082 | ipcarg_t rc;
|
---|
[0cc4313] | 1083 | async_wait_for(eid, &rc);
|
---|
[c07544d3] | 1084 |
|
---|
| 1085 | if (r1)
|
---|
[0cc4313] | 1086 | *r1 = IPC_GET_ARG1(result);
|
---|
[c07544d3] | 1087 |
|
---|
[0cc4313] | 1088 | if (r2)
|
---|
| 1089 | *r2 = IPC_GET_ARG2(result);
|
---|
[c07544d3] | 1090 |
|
---|
[0cc4313] | 1091 | if (r3)
|
---|
| 1092 | *r3 = IPC_GET_ARG3(result);
|
---|
[c07544d3] | 1093 |
|
---|
[0cc4313] | 1094 | if (r4)
|
---|
| 1095 | *r4 = IPC_GET_ARG4(result);
|
---|
[c07544d3] | 1096 |
|
---|
[0cc4313] | 1097 | if (r5)
|
---|
| 1098 | *r5 = IPC_GET_ARG5(result);
|
---|
[c07544d3] | 1099 |
|
---|
[0cc4313] | 1100 | return rc;
|
---|
[085bd54] | 1101 | }
|
---|
[b2951e2] | 1102 |
|
---|
[f74392f] | 1103 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
| 1104 | *
|
---|
| 1105 | * Ask through phone for a new connection to some service.
|
---|
| 1106 | *
|
---|
| 1107 | * @param phoneid Phone handle used for contacting the other side.
|
---|
| 1108 | * @param arg1 User defined argument.
|
---|
| 1109 | * @param arg2 User defined argument.
|
---|
| 1110 | * @param arg3 User defined argument.
|
---|
| 1111 | *
|
---|
| 1112 | * @return New phone handle on success or a negative error code.
|
---|
| 1113 | */
|
---|
| 1114 | int
|
---|
| 1115 | async_connect_me_to(int phoneid, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3)
|
---|
| 1116 | {
|
---|
| 1117 | int rc;
|
---|
| 1118 | ipcarg_t newphid;
|
---|
| 1119 |
|
---|
| 1120 | rc = async_req_3_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, NULL,
|
---|
| 1121 | NULL, NULL, NULL, &newphid);
|
---|
| 1122 |
|
---|
| 1123 | if (rc != EOK)
|
---|
| 1124 | return rc;
|
---|
| 1125 |
|
---|
| 1126 | return newphid;
|
---|
| 1127 | }
|
---|
| 1128 |
|
---|
| 1129 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
| 1130 | *
|
---|
| 1131 | * Ask through phone for a new connection to some service and block until
|
---|
| 1132 | * success.
|
---|
| 1133 | *
|
---|
| 1134 | * @param phoneid Phone handle used for contacting the other side.
|
---|
| 1135 | * @param arg1 User defined argument.
|
---|
| 1136 | * @param arg2 User defined argument.
|
---|
| 1137 | * @param arg3 User defined argument.
|
---|
| 1138 | *
|
---|
| 1139 | * @return New phone handle on success or a negative error code.
|
---|
| 1140 | */
|
---|
| 1141 | int
|
---|
| 1142 | async_connect_me_to_blocking(int phoneid, ipcarg_t arg1, ipcarg_t arg2,
|
---|
| 1143 | ipcarg_t arg3)
|
---|
| 1144 | {
|
---|
| 1145 | int rc;
|
---|
| 1146 | ipcarg_t newphid;
|
---|
| 1147 |
|
---|
| 1148 | rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
|
---|
| 1149 | IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
|
---|
| 1150 |
|
---|
| 1151 | if (rc != EOK)
|
---|
| 1152 | return rc;
|
---|
| 1153 |
|
---|
| 1154 | return newphid;
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
[0da4e41] | 1157 | /** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
|
---|
| 1158 | *
|
---|
| 1159 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
| 1160 | * @param dst Destination address space area base.
|
---|
| 1161 | * @param size Size of the destination address space area.
|
---|
| 1162 | * @param arg User defined argument.
|
---|
| 1163 | * @param flags Storage where the received flags will be stored. Can be
|
---|
| 1164 | * NULL.
|
---|
| 1165 | *
|
---|
| 1166 | * @return Zero on success or a negative error code from errno.h.
|
---|
| 1167 | */
|
---|
| 1168 | int async_share_in_start(int phoneid, void *dst, size_t size, ipcarg_t arg,
|
---|
| 1169 | int *flags)
|
---|
| 1170 | {
|
---|
| 1171 | int res;
|
---|
| 1172 | sysarg_t tmp_flags;
|
---|
| 1173 | res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (ipcarg_t) dst,
|
---|
| 1174 | (ipcarg_t) size, arg, NULL, &tmp_flags);
|
---|
| 1175 | if (flags)
|
---|
| 1176 | *flags = tmp_flags;
|
---|
| 1177 | return res;
|
---|
| 1178 | }
|
---|
| 1179 |
|
---|
| 1180 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
---|
| 1181 | *
|
---|
| 1182 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN calls
|
---|
| 1183 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
| 1184 | *
|
---|
| 1185 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1186 | *
|
---|
| 1187 | * @param callid Storage where the hash of the IPC_M_SHARE_IN call will
|
---|
| 1188 | * be stored.
|
---|
| 1189 | * @param size Destination address space area size.
|
---|
| 1190 | *
|
---|
| 1191 | * @return Non-zero on success, zero on failure.
|
---|
| 1192 | */
|
---|
| 1193 | int async_share_in_receive(ipc_callid_t *callid, size_t *size)
|
---|
| 1194 | {
|
---|
| 1195 | ipc_call_t data;
|
---|
| 1196 |
|
---|
| 1197 | assert(callid);
|
---|
| 1198 | assert(size);
|
---|
| 1199 |
|
---|
| 1200 | *callid = async_get_call(&data);
|
---|
| 1201 | if (IPC_GET_METHOD(data) != IPC_M_SHARE_IN)
|
---|
| 1202 | return 0;
|
---|
| 1203 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
| 1204 | return 1;
|
---|
| 1205 | }
|
---|
| 1206 |
|
---|
| 1207 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
---|
| 1208 | *
|
---|
| 1209 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
|
---|
| 1210 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
| 1211 | *
|
---|
| 1212 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
| 1213 | * @param src Source address space base.
|
---|
| 1214 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
| 1215 | *
|
---|
| 1216 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
| 1217 | */
|
---|
| 1218 | int async_share_in_finalize(ipc_callid_t callid, void *src, int flags)
|
---|
| 1219 | {
|
---|
| 1220 | return ipc_share_in_finalize(callid, src, flags);
|
---|
| 1221 | }
|
---|
| 1222 |
|
---|
| 1223 | /** Wrapper for making IPC_M_SHARE_OUT calls using the async framework.
|
---|
| 1224 | *
|
---|
| 1225 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
| 1226 | * @param src Source address space area base address.
|
---|
| 1227 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
| 1228 | *
|
---|
| 1229 | * @return Zero on success or a negative error code from errno.h.
|
---|
| 1230 | */
|
---|
| 1231 | int async_share_out_start(int phoneid, void *src, int flags)
|
---|
| 1232 | {
|
---|
| 1233 | return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (ipcarg_t) src, 0,
|
---|
| 1234 | (ipcarg_t) flags);
|
---|
| 1235 | }
|
---|
| 1236 |
|
---|
| 1237 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
---|
| 1238 | *
|
---|
| 1239 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT calls
|
---|
| 1240 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
| 1241 | *
|
---|
| 1242 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1243 | *
|
---|
| 1244 | * @param callid Storage where the hash of the IPC_M_SHARE_OUT call will
|
---|
| 1245 | * be stored.
|
---|
| 1246 | * @param size Storage where the source address space area size will be
|
---|
| 1247 | * stored.
|
---|
| 1248 | * @param flags Storage where the sharing flags will be stored.
|
---|
| 1249 | *
|
---|
| 1250 | * @return Non-zero on success, zero on failure.
|
---|
| 1251 | */
|
---|
| 1252 | int async_share_out_receive(ipc_callid_t *callid, size_t *size, int *flags)
|
---|
| 1253 | {
|
---|
| 1254 | ipc_call_t data;
|
---|
| 1255 |
|
---|
| 1256 | assert(callid);
|
---|
| 1257 | assert(size);
|
---|
| 1258 | assert(flags);
|
---|
| 1259 |
|
---|
| 1260 | *callid = async_get_call(&data);
|
---|
| 1261 | if (IPC_GET_METHOD(data) != IPC_M_SHARE_OUT)
|
---|
| 1262 | return 0;
|
---|
| 1263 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
| 1264 | *flags = (int) IPC_GET_ARG3(data);
|
---|
| 1265 | return 1;
|
---|
| 1266 | }
|
---|
| 1267 |
|
---|
| 1268 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
---|
| 1269 | *
|
---|
| 1270 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT calls
|
---|
| 1271 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
| 1272 | *
|
---|
| 1273 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
| 1274 | * @param dst Destination address space area base address.
|
---|
| 1275 | *
|
---|
| 1276 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
| 1277 | */
|
---|
| 1278 | int async_share_out_finalize(ipc_callid_t callid, void *dst)
|
---|
| 1279 | {
|
---|
| 1280 | return ipc_share_out_finalize(callid, dst);
|
---|
| 1281 | }
|
---|
| 1282 |
|
---|
| 1283 |
|
---|
| 1284 | /** Wrapper for making IPC_M_DATA_READ calls using the async framework.
|
---|
| 1285 | *
|
---|
| 1286 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
| 1287 | * @param dst Address of the beginning of the destination buffer.
|
---|
| 1288 | * @param size Size of the destination buffer.
|
---|
| 1289 | *
|
---|
| 1290 | * @return Zero on success or a negative error code from errno.h.
|
---|
| 1291 | */
|
---|
| 1292 | int async_data_read_start(int phoneid, void *dst, size_t size)
|
---|
| 1293 | {
|
---|
| 1294 | return async_req_2_0(phoneid, IPC_M_DATA_READ, (ipcarg_t) dst,
|
---|
| 1295 | (ipcarg_t) size);
|
---|
| 1296 | }
|
---|
| 1297 |
|
---|
| 1298 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
---|
| 1299 | *
|
---|
| 1300 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ calls
|
---|
| 1301 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
| 1302 | *
|
---|
| 1303 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1304 | *
|
---|
| 1305 | * @param callid Storage where the hash of the IPC_M_DATA_READ call will
|
---|
| 1306 | * be stored.
|
---|
| 1307 | * @param size Storage where the maximum size will be stored. Can be
|
---|
| 1308 | * NULL.
|
---|
| 1309 | *
|
---|
| 1310 | * @return Non-zero on success, zero on failure.
|
---|
| 1311 | */
|
---|
| 1312 | int async_data_read_receive(ipc_callid_t *callid, size_t *size)
|
---|
| 1313 | {
|
---|
| 1314 | ipc_call_t data;
|
---|
| 1315 |
|
---|
| 1316 | assert(callid);
|
---|
| 1317 |
|
---|
| 1318 | *callid = async_get_call(&data);
|
---|
| 1319 | if (IPC_GET_METHOD(data) != IPC_M_DATA_READ)
|
---|
| 1320 | return 0;
|
---|
| 1321 | if (size)
|
---|
| 1322 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
| 1323 | return 1;
|
---|
| 1324 | }
|
---|
| 1325 |
|
---|
| 1326 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
---|
| 1327 | *
|
---|
| 1328 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
|
---|
| 1329 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
| 1330 | *
|
---|
| 1331 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
| 1332 | * @param src Source address for the IPC_M_DATA_READ call.
|
---|
| 1333 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
---|
| 1334 | * the maximum size announced by the sender.
|
---|
| 1335 | *
|
---|
| 1336 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
| 1337 | */
|
---|
| 1338 | int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
|
---|
| 1339 | {
|
---|
| 1340 | return ipc_data_read_finalize(callid, src, size);
|
---|
| 1341 | }
|
---|
| 1342 |
|
---|
[b4cbef1] | 1343 | /** Wrapper for forwarding any read request
|
---|
| 1344 | *
|
---|
| 1345 | *
|
---|
| 1346 | */
|
---|
| 1347 | int async_data_read_forward_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
| 1348 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
|
---|
| 1349 | {
|
---|
| 1350 | ipc_callid_t callid;
|
---|
| 1351 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
| 1352 | ipc_answer_0(callid, EINVAL);
|
---|
| 1353 | return EINVAL;
|
---|
| 1354 | }
|
---|
| 1355 |
|
---|
| 1356 | aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
| 1357 | dataptr);
|
---|
| 1358 | if (msg == 0) {
|
---|
| 1359 | ipc_answer_0(callid, EINVAL);
|
---|
| 1360 | return EINVAL;
|
---|
| 1361 | }
|
---|
| 1362 |
|
---|
| 1363 | int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
|
---|
| 1364 | IPC_FF_ROUTE_FROM_ME);
|
---|
| 1365 | if (retval != EOK) {
|
---|
[a281fc82] | 1366 | async_wait_for(msg, NULL);
|
---|
[b4cbef1] | 1367 | ipc_answer_0(callid, retval);
|
---|
| 1368 | return retval;
|
---|
| 1369 | }
|
---|
| 1370 |
|
---|
| 1371 | ipcarg_t rc;
|
---|
| 1372 | async_wait_for(msg, &rc);
|
---|
| 1373 |
|
---|
| 1374 | return (int) rc;
|
---|
| 1375 | }
|
---|
| 1376 |
|
---|
[0da4e41] | 1377 | /** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
|
---|
| 1378 | *
|
---|
[b4cbef1] | 1379 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
| 1380 | * @param src Address of the beginning of the source buffer.
|
---|
| 1381 | * @param size Size of the source buffer.
|
---|
| 1382 | *
|
---|
| 1383 | * @return Zero on success or a negative error code from errno.h.
|
---|
[0da4e41] | 1384 | *
|
---|
| 1385 | */
|
---|
| 1386 | int async_data_write_start(int phoneid, const void *src, size_t size)
|
---|
| 1387 | {
|
---|
| 1388 | return async_req_2_0(phoneid, IPC_M_DATA_WRITE, (ipcarg_t) src,
|
---|
| 1389 | (ipcarg_t) size);
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
| 1392 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
---|
| 1393 | *
|
---|
| 1394 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE calls
|
---|
| 1395 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
| 1396 | *
|
---|
| 1397 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1398 | *
|
---|
[b4cbef1] | 1399 | * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
|
---|
| 1400 | * be stored.
|
---|
| 1401 | * @param size Storage where the suggested size will be stored. May be
|
---|
| 1402 | * NULL
|
---|
| 1403 | *
|
---|
| 1404 | * @return Non-zero on success, zero on failure.
|
---|
[0da4e41] | 1405 | *
|
---|
| 1406 | */
|
---|
| 1407 | int async_data_write_receive(ipc_callid_t *callid, size_t *size)
|
---|
| 1408 | {
|
---|
| 1409 | ipc_call_t data;
|
---|
| 1410 |
|
---|
| 1411 | assert(callid);
|
---|
[b4cbef1] | 1412 |
|
---|
[0da4e41] | 1413 | *callid = async_get_call(&data);
|
---|
| 1414 | if (IPC_GET_METHOD(data) != IPC_M_DATA_WRITE)
|
---|
| 1415 | return 0;
|
---|
[b4cbef1] | 1416 |
|
---|
[0da4e41] | 1417 | if (size)
|
---|
| 1418 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
[b4cbef1] | 1419 |
|
---|
[0da4e41] | 1420 | return 1;
|
---|
| 1421 | }
|
---|
| 1422 |
|
---|
| 1423 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
---|
| 1424 | *
|
---|
| 1425 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
|
---|
| 1426 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
| 1427 | *
|
---|
[b4cbef1] | 1428 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
| 1429 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
---|
| 1430 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
---|
| 1431 | *
|
---|
| 1432 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1433 | *
|
---|
| 1434 | */
|
---|
| 1435 | int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
|
---|
| 1436 | {
|
---|
| 1437 | return ipc_data_write_finalize(callid, dst, size);
|
---|
| 1438 | }
|
---|
| 1439 |
|
---|
[eda925a] | 1440 | /** Wrapper for receiving binary data or strings
|
---|
[8aa42e3] | 1441 | *
|
---|
| 1442 | * This wrapper only makes it more comfortable to use async_data_write_*
|
---|
[eda925a] | 1443 | * functions to receive binary data or strings.
|
---|
[8aa42e3] | 1444 | *
|
---|
[472c09d] | 1445 | * @param data Pointer to data pointer (which should be later disposed
|
---|
| 1446 | * by free()). If the operation fails, the pointer is not
|
---|
| 1447 | * touched.
|
---|
[eda925a] | 1448 | * @param nullterm If true then the received data is always zero terminated.
|
---|
| 1449 | * This also causes to allocate one extra byte beyond the
|
---|
| 1450 | * raw transmitted data.
|
---|
[b4cbef1] | 1451 | * @param min_size Minimum size (in bytes) of the data to receive.
|
---|
[472c09d] | 1452 | * @param max_size Maximum size (in bytes) of the data to receive. 0 means
|
---|
| 1453 | * no limit.
|
---|
[eda925a] | 1454 | * @param granulariy If non-zero then the size of the received data has to
|
---|
[472c09d] | 1455 | * be divisible by this value.
|
---|
| 1456 | * @param received If not NULL, the size of the received data is stored here.
|
---|
[8aa42e3] | 1457 | *
|
---|
| 1458 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
| 1459 | *
|
---|
| 1460 | */
|
---|
[eda925a] | 1461 | int async_data_write_accept(void **data, const bool nullterm,
|
---|
| 1462 | const size_t min_size, const size_t max_size, const size_t granularity,
|
---|
| 1463 | size_t *received)
|
---|
[8aa42e3] | 1464 | {
|
---|
| 1465 | ipc_callid_t callid;
|
---|
| 1466 | size_t size;
|
---|
| 1467 | if (!async_data_write_receive(&callid, &size)) {
|
---|
| 1468 | ipc_answer_0(callid, EINVAL);
|
---|
| 1469 | return EINVAL;
|
---|
| 1470 | }
|
---|
| 1471 |
|
---|
[b4cbef1] | 1472 | if (size < min_size) {
|
---|
| 1473 | ipc_answer_0(callid, EINVAL);
|
---|
| 1474 | return EINVAL;
|
---|
| 1475 | }
|
---|
| 1476 |
|
---|
[8aa42e3] | 1477 | if ((max_size > 0) && (size > max_size)) {
|
---|
| 1478 | ipc_answer_0(callid, EINVAL);
|
---|
| 1479 | return EINVAL;
|
---|
| 1480 | }
|
---|
| 1481 |
|
---|
[472c09d] | 1482 | if ((granularity > 0) && ((size % granularity) != 0)) {
|
---|
| 1483 | ipc_answer_0(callid, EINVAL);
|
---|
| 1484 | return EINVAL;
|
---|
| 1485 | }
|
---|
| 1486 |
|
---|
[eda925a] | 1487 | void *_data;
|
---|
| 1488 |
|
---|
| 1489 | if (nullterm)
|
---|
| 1490 | _data = malloc(size + 1);
|
---|
| 1491 | else
|
---|
| 1492 | _data = malloc(size);
|
---|
| 1493 |
|
---|
[472c09d] | 1494 | if (_data == NULL) {
|
---|
[8aa42e3] | 1495 | ipc_answer_0(callid, ENOMEM);
|
---|
| 1496 | return ENOMEM;
|
---|
| 1497 | }
|
---|
| 1498 |
|
---|
[472c09d] | 1499 | int rc = async_data_write_finalize(callid, _data, size);
|
---|
[8aa42e3] | 1500 | if (rc != EOK) {
|
---|
[472c09d] | 1501 | free(_data);
|
---|
[8aa42e3] | 1502 | return rc;
|
---|
| 1503 | }
|
---|
| 1504 |
|
---|
[eda925a] | 1505 | if (nullterm)
|
---|
| 1506 | ((char *) _data)[size] = 0;
|
---|
[8aa42e3] | 1507 |
|
---|
[eda925a] | 1508 | *data = _data;
|
---|
[472c09d] | 1509 | if (received != NULL)
|
---|
| 1510 | *received = size;
|
---|
| 1511 |
|
---|
[8aa42e3] | 1512 | return EOK;
|
---|
| 1513 | }
|
---|
| 1514 |
|
---|
[b4cbef1] | 1515 | /** Wrapper for voiding any data that is about to be received
|
---|
| 1516 | *
|
---|
| 1517 | * This wrapper can be used to void any pending data
|
---|
| 1518 | *
|
---|
| 1519 | * @param retval Error value from @ref errno.h to be returned to the caller.
|
---|
| 1520 | *
|
---|
| 1521 | */
|
---|
[eda925a] | 1522 | void async_data_write_void(const int retval)
|
---|
[b4cbef1] | 1523 | {
|
---|
| 1524 | ipc_callid_t callid;
|
---|
| 1525 | async_data_write_receive(&callid, NULL);
|
---|
| 1526 | ipc_answer_0(callid, retval);
|
---|
| 1527 | }
|
---|
| 1528 |
|
---|
| 1529 | /** Wrapper for forwarding any data that is about to be received
|
---|
| 1530 | *
|
---|
| 1531 | *
|
---|
| 1532 | */
|
---|
[eda925a] | 1533 | int async_data_write_forward_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
[b4cbef1] | 1534 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
|
---|
| 1535 | {
|
---|
| 1536 | ipc_callid_t callid;
|
---|
| 1537 | if (!async_data_write_receive(&callid, NULL)) {
|
---|
| 1538 | ipc_answer_0(callid, EINVAL);
|
---|
| 1539 | return EINVAL;
|
---|
| 1540 | }
|
---|
| 1541 |
|
---|
| 1542 | aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
| 1543 | dataptr);
|
---|
| 1544 | if (msg == 0) {
|
---|
| 1545 | ipc_answer_0(callid, EINVAL);
|
---|
| 1546 | return EINVAL;
|
---|
| 1547 | }
|
---|
| 1548 |
|
---|
| 1549 | int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
|
---|
| 1550 | IPC_FF_ROUTE_FROM_ME);
|
---|
| 1551 | if (retval != EOK) {
|
---|
[a281fc82] | 1552 | async_wait_for(msg, NULL);
|
---|
[b4cbef1] | 1553 | ipc_answer_0(callid, retval);
|
---|
| 1554 | return retval;
|
---|
| 1555 | }
|
---|
| 1556 |
|
---|
| 1557 | ipcarg_t rc;
|
---|
| 1558 | async_wait_for(msg, &rc);
|
---|
| 1559 |
|
---|
| 1560 | return (int) rc;
|
---|
| 1561 | }
|
---|
| 1562 |
|
---|
[a46da63] | 1563 | /** @}
|
---|
[b2951e2] | 1564 | */
|
---|