| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
|---|
| 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.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Asynchronous library
|
|---|
| 37 | *
|
|---|
| 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.
|
|---|
| 41 | *
|
|---|
| 42 | * You should be able to write very simple multithreaded programs, the async
|
|---|
| 43 | * framework will automatically take care of most synchronization problems.
|
|---|
| 44 | *
|
|---|
| 45 | * Default semantics:
|
|---|
| 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 ]
|
|---|
| 49 | *
|
|---|
| 50 | * Example of use (pseudo C):
|
|---|
| 51 | *
|
|---|
| 52 | * 1) Multithreaded client application
|
|---|
| 53 | *
|
|---|
| 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 | * }
|
|---|
| 67 | *
|
|---|
| 68 | *
|
|---|
| 69 | * 2) Multithreaded server application
|
|---|
| 70 | *
|
|---|
| 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);
|
|---|
| 83 | *
|
|---|
| 84 | * callid = async_get_call(&call);
|
|---|
| 85 | * somehow_handle_the_call(callid, call);
|
|---|
| 86 | * ipc_answer_2(callid, 1, 2, 3);
|
|---|
| 87 | *
|
|---|
| 88 | * callid = async_get_call(&call);
|
|---|
| 89 | * ...
|
|---|
| 90 | * }
|
|---|
| 91 | *
|
|---|
| 92 | */
|
|---|
| 93 |
|
|---|
| 94 | #include <futex.h>
|
|---|
| 95 | #include <async.h>
|
|---|
| 96 | #include <async_priv.h>
|
|---|
| 97 | #include <fibril.h>
|
|---|
| 98 | #include <stdio.h>
|
|---|
| 99 | #include <adt/hash_table.h>
|
|---|
| 100 | #include <adt/list.h>
|
|---|
| 101 | #include <ipc/ipc.h>
|
|---|
| 102 | #include <assert.h>
|
|---|
| 103 | #include <errno.h>
|
|---|
| 104 | #include <sys/time.h>
|
|---|
| 105 | #include <arch/barrier.h>
|
|---|
| 106 | #include <bool.h>
|
|---|
| 107 |
|
|---|
| 108 | atomic_t async_futex = FUTEX_INITIALIZER;
|
|---|
| 109 |
|
|---|
| 110 | /** Number of threads waiting for IPC in the kernel. */
|
|---|
| 111 | atomic_t threads_in_ipc_wait = { 0 };
|
|---|
| 112 |
|
|---|
| 113 | typedef struct {
|
|---|
| 114 | awaiter_t wdata;
|
|---|
| 115 |
|
|---|
| 116 | /** If reply was received. */
|
|---|
| 117 | bool done;
|
|---|
| 118 |
|
|---|
| 119 | /** Pointer to where the answer data is stored. */
|
|---|
| 120 | ipc_call_t *dataptr;
|
|---|
| 121 |
|
|---|
| 122 | sysarg_t retval;
|
|---|
| 123 | } amsg_t;
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Structures of this type are used to group information about a call and a
|
|---|
| 127 | * message queue link.
|
|---|
| 128 | */
|
|---|
| 129 | typedef struct {
|
|---|
| 130 | link_t link;
|
|---|
| 131 | ipc_callid_t callid;
|
|---|
| 132 | ipc_call_t call;
|
|---|
| 133 | } msg_t;
|
|---|
| 134 |
|
|---|
| 135 | typedef struct {
|
|---|
| 136 | sysarg_t in_task_hash;
|
|---|
| 137 | link_t link;
|
|---|
| 138 | int refcnt;
|
|---|
| 139 | void *data;
|
|---|
| 140 | } client_t;
|
|---|
| 141 |
|
|---|
| 142 | typedef struct {
|
|---|
| 143 | awaiter_t wdata;
|
|---|
| 144 |
|
|---|
| 145 | /** Hash table link. */
|
|---|
| 146 | link_t link;
|
|---|
| 147 |
|
|---|
| 148 | /** Incoming client task hash. */
|
|---|
| 149 | sysarg_t in_task_hash;
|
|---|
| 150 | /** Incoming phone hash. */
|
|---|
| 151 | sysarg_t in_phone_hash;
|
|---|
| 152 |
|
|---|
| 153 | /** Link to the client tracking structure. */
|
|---|
| 154 | client_t *client;
|
|---|
| 155 |
|
|---|
| 156 | /** Messages that should be delivered to this fibril. */
|
|---|
| 157 | link_t msg_queue;
|
|---|
| 158 |
|
|---|
| 159 | /** Identification of the opening call. */
|
|---|
| 160 | ipc_callid_t callid;
|
|---|
| 161 | /** Call data of the opening call. */
|
|---|
| 162 | ipc_call_t call;
|
|---|
| 163 |
|
|---|
| 164 | /** Identification of the closing call. */
|
|---|
| 165 | ipc_callid_t close_callid;
|
|---|
| 166 |
|
|---|
| 167 | /** Fibril function that will be used to handle the connection. */
|
|---|
| 168 | void (*cfibril)(ipc_callid_t, ipc_call_t *);
|
|---|
| 169 | } connection_t;
|
|---|
| 170 |
|
|---|
| 171 | /** Identifier of the incoming connection handled by the current fibril. */
|
|---|
| 172 | fibril_local connection_t *FIBRIL_connection;
|
|---|
| 173 |
|
|---|
| 174 | static void *default_client_data_constructor(void)
|
|---|
| 175 | {
|
|---|
| 176 | return NULL;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | static void default_client_data_destructor(void *data)
|
|---|
| 180 | {
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | static async_client_data_ctor_t async_client_data_create =
|
|---|
| 184 | default_client_data_constructor;
|
|---|
| 185 | static async_client_data_dtor_t async_client_data_destroy =
|
|---|
| 186 | default_client_data_destructor;
|
|---|
| 187 |
|
|---|
| 188 | void async_set_client_data_constructor(async_client_data_ctor_t ctor)
|
|---|
| 189 | {
|
|---|
| 190 | async_client_data_create = ctor;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | void async_set_client_data_destructor(async_client_data_dtor_t dtor)
|
|---|
| 194 | {
|
|---|
| 195 | async_client_data_destroy = dtor;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | void *async_client_data_get(void)
|
|---|
| 199 | {
|
|---|
| 200 | assert(FIBRIL_connection);
|
|---|
| 201 |
|
|---|
| 202 | return FIBRIL_connection->client->data;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
|
|---|
| 206 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
|
|---|
| 207 |
|
|---|
| 208 | /**
|
|---|
| 209 | * Pointer to a fibril function that will be used to handle connections.
|
|---|
| 210 | */
|
|---|
| 211 | static async_client_conn_t client_connection = default_client_connection;
|
|---|
| 212 |
|
|---|
| 213 | /**
|
|---|
| 214 | * Pointer to a fibril function that will be used to handle interrupt
|
|---|
| 215 | * notifications.
|
|---|
| 216 | */
|
|---|
| 217 | static async_client_conn_t interrupt_received = default_interrupt_received;
|
|---|
| 218 |
|
|---|
| 219 | static hash_table_t client_hash_table;
|
|---|
| 220 | static hash_table_t conn_hash_table;
|
|---|
| 221 | static LIST_INITIALIZE(timeout_list);
|
|---|
| 222 |
|
|---|
| 223 | #define CLIENT_HASH_TABLE_BUCKETS 32
|
|---|
| 224 | #define CONN_HASH_TABLE_BUCKETS 32
|
|---|
| 225 |
|
|---|
| 226 | static hash_index_t client_hash(unsigned long *key)
|
|---|
| 227 | {
|
|---|
| 228 | assert(key);
|
|---|
| 229 | return (((*key) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
|---|
| 233 | {
|
|---|
| 234 | client_t *cl = hash_table_get_instance(item, client_t, link);
|
|---|
| 235 | return (key[0] == cl->in_task_hash);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | static void client_remove(link_t *item)
|
|---|
| 239 | {
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /** Operations for the client hash table. */
|
|---|
| 243 | static hash_table_operations_t client_hash_table_ops = {
|
|---|
| 244 | .hash = client_hash,
|
|---|
| 245 | .compare = client_compare,
|
|---|
| 246 | .remove_callback = client_remove
|
|---|
| 247 | };
|
|---|
| 248 |
|
|---|
| 249 | /** Compute hash into the connection hash table based on the source phone hash.
|
|---|
| 250 | *
|
|---|
| 251 | * @param key Pointer to source phone hash.
|
|---|
| 252 | *
|
|---|
| 253 | * @return Index into the connection hash table.
|
|---|
| 254 | *
|
|---|
| 255 | */
|
|---|
| 256 | static hash_index_t conn_hash(unsigned long *key)
|
|---|
| 257 | {
|
|---|
| 258 | assert(key);
|
|---|
| 259 | return (((*key) >> 4) % CONN_HASH_TABLE_BUCKETS);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /** Compare hash table item with a key.
|
|---|
| 263 | *
|
|---|
| 264 | * @param key Array containing the source phone hash as the only item.
|
|---|
| 265 | * @param keys Expected 1 but ignored.
|
|---|
| 266 | * @param item Connection hash table item.
|
|---|
| 267 | *
|
|---|
| 268 | * @return True on match, false otherwise.
|
|---|
| 269 | *
|
|---|
| 270 | */
|
|---|
| 271 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
|---|
| 272 | {
|
|---|
| 273 | connection_t *hs = hash_table_get_instance(item, connection_t, link);
|
|---|
| 274 | return (key[0] == hs->in_phone_hash);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | /** Connection hash table removal callback function.
|
|---|
| 278 | *
|
|---|
| 279 | * This function is called whenever a connection is removed from the connection
|
|---|
| 280 | * hash table.
|
|---|
| 281 | *
|
|---|
| 282 | * @param item Connection hash table item being removed.
|
|---|
| 283 | *
|
|---|
| 284 | */
|
|---|
| 285 | static void conn_remove(link_t *item)
|
|---|
| 286 | {
|
|---|
| 287 | free(hash_table_get_instance(item, connection_t, link));
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 | /** Operations for the connection hash table. */
|
|---|
| 292 | static hash_table_operations_t conn_hash_table_ops = {
|
|---|
| 293 | .hash = conn_hash,
|
|---|
| 294 | .compare = conn_compare,
|
|---|
| 295 | .remove_callback = conn_remove
|
|---|
| 296 | };
|
|---|
| 297 |
|
|---|
| 298 | /** Sort in current fibril's timeout request.
|
|---|
| 299 | *
|
|---|
| 300 | * @param wd Wait data of the current fibril.
|
|---|
| 301 | *
|
|---|
| 302 | */
|
|---|
| 303 | void async_insert_timeout(awaiter_t *wd)
|
|---|
| 304 | {
|
|---|
| 305 | wd->to_event.occurred = false;
|
|---|
| 306 | wd->to_event.inlist = true;
|
|---|
| 307 |
|
|---|
| 308 | link_t *tmp = timeout_list.next;
|
|---|
| 309 | while (tmp != &timeout_list) {
|
|---|
| 310 | awaiter_t *cur;
|
|---|
| 311 |
|
|---|
| 312 | cur = list_get_instance(tmp, awaiter_t, to_event.link);
|
|---|
| 313 | if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
|
|---|
| 314 | break;
|
|---|
| 315 | tmp = tmp->next;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | list_append(&wd->to_event.link, tmp);
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | /** Try to route a call to an appropriate connection fibril.
|
|---|
| 322 | *
|
|---|
| 323 | * If the proper connection fibril is found, a message with the call is added to
|
|---|
| 324 | * its message queue. If the fibril was not active, it is activated and all
|
|---|
| 325 | * timeouts are unregistered.
|
|---|
| 326 | *
|
|---|
| 327 | * @param callid Hash of the incoming call.
|
|---|
| 328 | * @param call Data of the incoming call.
|
|---|
| 329 | *
|
|---|
| 330 | * @return False if the call doesn't match any connection.
|
|---|
| 331 | * True if the call was passed to the respective connection fibril.
|
|---|
| 332 | *
|
|---|
| 333 | */
|
|---|
| 334 | static bool route_call(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 335 | {
|
|---|
| 336 | futex_down(&async_futex);
|
|---|
| 337 |
|
|---|
| 338 | unsigned long key = call->in_phone_hash;
|
|---|
| 339 | link_t *hlp = hash_table_find(&conn_hash_table, &key);
|
|---|
| 340 |
|
|---|
| 341 | if (!hlp) {
|
|---|
| 342 | futex_up(&async_futex);
|
|---|
| 343 | return false;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
|
|---|
| 347 |
|
|---|
| 348 | msg_t *msg = malloc(sizeof(*msg));
|
|---|
| 349 | if (!msg) {
|
|---|
| 350 | futex_up(&async_futex);
|
|---|
| 351 | return false;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | msg->callid = callid;
|
|---|
| 355 | msg->call = *call;
|
|---|
| 356 | list_append(&msg->link, &conn->msg_queue);
|
|---|
| 357 |
|
|---|
| 358 | if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
|
|---|
| 359 | conn->close_callid = callid;
|
|---|
| 360 |
|
|---|
| 361 | /* If the connection fibril is waiting for an event, activate it */
|
|---|
| 362 | if (!conn->wdata.active) {
|
|---|
| 363 |
|
|---|
| 364 | /* If in timeout list, remove it */
|
|---|
| 365 | if (conn->wdata.to_event.inlist) {
|
|---|
| 366 | conn->wdata.to_event.inlist = false;
|
|---|
| 367 | list_remove(&conn->wdata.to_event.link);
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | conn->wdata.active = true;
|
|---|
| 371 | fibril_add_ready(conn->wdata.fid);
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | futex_up(&async_futex);
|
|---|
| 375 | return true;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | /** Notification fibril.
|
|---|
| 379 | *
|
|---|
| 380 | * When a notification arrives, a fibril with this implementing function is
|
|---|
| 381 | * created. It calls interrupt_received() and does the final cleanup.
|
|---|
| 382 | *
|
|---|
| 383 | * @param arg Message structure pointer.
|
|---|
| 384 | *
|
|---|
| 385 | * @return Always zero.
|
|---|
| 386 | *
|
|---|
| 387 | */
|
|---|
| 388 | static int notification_fibril(void *arg)
|
|---|
| 389 | {
|
|---|
| 390 | msg_t *msg = (msg_t *) arg;
|
|---|
| 391 | interrupt_received(msg->callid, &msg->call);
|
|---|
| 392 |
|
|---|
| 393 | free(msg);
|
|---|
| 394 | return 0;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | /** Process interrupt notification.
|
|---|
| 398 | *
|
|---|
| 399 | * A new fibril is created which would process the notification.
|
|---|
| 400 | *
|
|---|
| 401 | * @param callid Hash of the incoming call.
|
|---|
| 402 | * @param call Data of the incoming call.
|
|---|
| 403 | *
|
|---|
| 404 | * @return False if an error occured.
|
|---|
| 405 | * True if the call was passed to the notification fibril.
|
|---|
| 406 | *
|
|---|
| 407 | */
|
|---|
| 408 | static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 409 | {
|
|---|
| 410 | futex_down(&async_futex);
|
|---|
| 411 |
|
|---|
| 412 | msg_t *msg = malloc(sizeof(*msg));
|
|---|
| 413 | if (!msg) {
|
|---|
| 414 | futex_up(&async_futex);
|
|---|
| 415 | return false;
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | msg->callid = callid;
|
|---|
| 419 | msg->call = *call;
|
|---|
| 420 |
|
|---|
| 421 | fid_t fid = fibril_create(notification_fibril, msg);
|
|---|
| 422 | fibril_add_ready(fid);
|
|---|
| 423 |
|
|---|
| 424 | futex_up(&async_futex);
|
|---|
| 425 | return true;
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | /** Return new incoming message for the current (fibril-local) connection.
|
|---|
| 429 | *
|
|---|
| 430 | * @param call Storage where the incoming call data will be stored.
|
|---|
| 431 | * @param usecs Timeout in microseconds. Zero denotes no timeout.
|
|---|
| 432 | *
|
|---|
| 433 | * @return If no timeout was specified, then a hash of the
|
|---|
| 434 | * incoming call is returned. If a timeout is specified,
|
|---|
| 435 | * then a hash of the incoming call is returned unless
|
|---|
| 436 | * the timeout expires prior to receiving a message. In
|
|---|
| 437 | * that case zero is returned.
|
|---|
| 438 | *
|
|---|
| 439 | */
|
|---|
| 440 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
|
|---|
| 441 | {
|
|---|
| 442 | assert(FIBRIL_connection);
|
|---|
| 443 |
|
|---|
| 444 | /* Why doing this?
|
|---|
| 445 | * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
|
|---|
| 446 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
|
|---|
| 447 | * I would never expect to find so many errors in
|
|---|
| 448 | * a compiler.
|
|---|
| 449 | */
|
|---|
| 450 | connection_t *conn = FIBRIL_connection;
|
|---|
| 451 |
|
|---|
| 452 | futex_down(&async_futex);
|
|---|
| 453 |
|
|---|
| 454 | if (usecs) {
|
|---|
| 455 | gettimeofday(&conn->wdata.to_event.expires, NULL);
|
|---|
| 456 | tv_add(&conn->wdata.to_event.expires, usecs);
|
|---|
| 457 | } else
|
|---|
| 458 | conn->wdata.to_event.inlist = false;
|
|---|
| 459 |
|
|---|
| 460 | /* If nothing in queue, wait until something arrives */
|
|---|
| 461 | while (list_empty(&conn->msg_queue)) {
|
|---|
| 462 | if (conn->close_callid) {
|
|---|
| 463 | /*
|
|---|
| 464 | * Handle the case when the connection was already
|
|---|
| 465 | * closed by the client but the server did not notice
|
|---|
| 466 | * the first IPC_M_PHONE_HUNGUP call and continues to
|
|---|
| 467 | * call async_get_call_timeout(). Repeat
|
|---|
| 468 | * IPC_M_PHONE_HUNGUP until the caller notices.
|
|---|
| 469 | */
|
|---|
| 470 | memset(call, 0, sizeof(ipc_call_t));
|
|---|
| 471 | IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
|
|---|
| 472 | futex_up(&async_futex);
|
|---|
| 473 | return conn->close_callid;
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | if (usecs)
|
|---|
| 477 | async_insert_timeout(&conn->wdata);
|
|---|
| 478 |
|
|---|
| 479 | conn->wdata.active = false;
|
|---|
| 480 |
|
|---|
| 481 | /*
|
|---|
| 482 | * Note: the current fibril will be rescheduled either due to a
|
|---|
| 483 | * timeout or due to an arriving message destined to it. In the
|
|---|
| 484 | * former case, handle_expired_timeouts() and, in the latter
|
|---|
| 485 | * case, route_call() will perform the wakeup.
|
|---|
| 486 | */
|
|---|
| 487 | fibril_switch(FIBRIL_TO_MANAGER);
|
|---|
| 488 |
|
|---|
| 489 | /*
|
|---|
| 490 | * Futex is up after getting back from async_manager.
|
|---|
| 491 | * Get it again.
|
|---|
| 492 | */
|
|---|
| 493 | futex_down(&async_futex);
|
|---|
| 494 | if ((usecs) && (conn->wdata.to_event.occurred)
|
|---|
| 495 | && (list_empty(&conn->msg_queue))) {
|
|---|
| 496 | /* If we timed out -> exit */
|
|---|
| 497 | futex_up(&async_futex);
|
|---|
| 498 | return 0;
|
|---|
| 499 | }
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
|
|---|
| 503 | list_remove(&msg->link);
|
|---|
| 504 |
|
|---|
| 505 | ipc_callid_t callid = msg->callid;
|
|---|
| 506 | *call = msg->call;
|
|---|
| 507 | free(msg);
|
|---|
| 508 |
|
|---|
| 509 | futex_up(&async_futex);
|
|---|
| 510 | return callid;
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | /** Default fibril function that gets called to handle new connection.
|
|---|
| 514 | *
|
|---|
| 515 | * This function is defined as a weak symbol - to be redefined in user code.
|
|---|
| 516 | *
|
|---|
| 517 | * @param callid Hash of the incoming call.
|
|---|
| 518 | * @param call Data of the incoming call.
|
|---|
| 519 | *
|
|---|
| 520 | */
|
|---|
| 521 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 522 | {
|
|---|
| 523 | ipc_answer_0(callid, ENOENT);
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | /** Default fibril function that gets called to handle interrupt notifications.
|
|---|
| 527 | *
|
|---|
| 528 | * This function is defined as a weak symbol - to be redefined in user code.
|
|---|
| 529 | *
|
|---|
| 530 | * @param callid Hash of the incoming call.
|
|---|
| 531 | * @param call Data of the incoming call.
|
|---|
| 532 | *
|
|---|
| 533 | */
|
|---|
| 534 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 535 | {
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | /** Wrapper for client connection fibril.
|
|---|
| 539 | *
|
|---|
| 540 | * When a new connection arrives, a fibril with this implementing function is
|
|---|
| 541 | * created. It calls client_connection() and does the final cleanup.
|
|---|
| 542 | *
|
|---|
| 543 | * @param arg Connection structure pointer.
|
|---|
| 544 | *
|
|---|
| 545 | * @return Always zero.
|
|---|
| 546 | *
|
|---|
| 547 | */
|
|---|
| 548 | static int connection_fibril(void *arg)
|
|---|
| 549 | {
|
|---|
| 550 | unsigned long key;
|
|---|
| 551 | client_t *cl;
|
|---|
| 552 | link_t *lnk;
|
|---|
| 553 | bool destroy = false;
|
|---|
| 554 |
|
|---|
| 555 | /*
|
|---|
| 556 | * Setup fibril-local connection pointer.
|
|---|
| 557 | */
|
|---|
| 558 | FIBRIL_connection = (connection_t *) arg;
|
|---|
| 559 |
|
|---|
| 560 | /*
|
|---|
| 561 | * Add our reference for the current connection in the client task
|
|---|
| 562 | * tracking structure. If this is the first reference, create and
|
|---|
| 563 | * hash in a new tracking structure.
|
|---|
| 564 | */
|
|---|
| 565 | futex_down(&async_futex);
|
|---|
| 566 | key = FIBRIL_connection->in_task_hash;
|
|---|
| 567 | lnk = hash_table_find(&client_hash_table, &key);
|
|---|
| 568 | if (lnk) {
|
|---|
| 569 | cl = hash_table_get_instance(lnk, client_t, link);
|
|---|
| 570 | cl->refcnt++;
|
|---|
| 571 | } else {
|
|---|
| 572 | cl = malloc(sizeof(client_t));
|
|---|
| 573 | if (!cl) {
|
|---|
| 574 | ipc_answer_0(FIBRIL_connection->callid, ENOMEM);
|
|---|
| 575 | futex_up(&async_futex);
|
|---|
| 576 | return 0;
|
|---|
| 577 | }
|
|---|
| 578 | cl->in_task_hash = FIBRIL_connection->in_task_hash;
|
|---|
| 579 | async_serialize_start();
|
|---|
| 580 | cl->data = async_client_data_create();
|
|---|
| 581 | async_serialize_end();
|
|---|
| 582 | cl->refcnt = 1;
|
|---|
| 583 | hash_table_insert(&client_hash_table, &key, &cl->link);
|
|---|
| 584 | }
|
|---|
| 585 | futex_up(&async_futex);
|
|---|
| 586 |
|
|---|
| 587 | FIBRIL_connection->client = cl;
|
|---|
| 588 |
|
|---|
| 589 | /*
|
|---|
| 590 | * Call the connection handler function.
|
|---|
| 591 | */
|
|---|
| 592 | FIBRIL_connection->cfibril(FIBRIL_connection->callid,
|
|---|
| 593 | &FIBRIL_connection->call);
|
|---|
| 594 |
|
|---|
| 595 | /*
|
|---|
| 596 | * Remove the reference for this client task connection.
|
|---|
| 597 | */
|
|---|
| 598 | futex_down(&async_futex);
|
|---|
| 599 | if (--cl->refcnt == 0) {
|
|---|
| 600 | hash_table_remove(&client_hash_table, &key, 1);
|
|---|
| 601 | destroy = true;
|
|---|
| 602 | }
|
|---|
| 603 | futex_up(&async_futex);
|
|---|
| 604 |
|
|---|
| 605 | if (destroy) {
|
|---|
| 606 | if (cl->data)
|
|---|
| 607 | async_client_data_destroy(cl->data);
|
|---|
| 608 | free(cl);
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | /*
|
|---|
| 612 | * Remove myself from the connection hash table.
|
|---|
| 613 | */
|
|---|
| 614 | futex_down(&async_futex);
|
|---|
| 615 | key = FIBRIL_connection->in_phone_hash;
|
|---|
| 616 | hash_table_remove(&conn_hash_table, &key, 1);
|
|---|
| 617 | futex_up(&async_futex);
|
|---|
| 618 |
|
|---|
| 619 | /*
|
|---|
| 620 | * Answer all remaining messages with EHANGUP.
|
|---|
| 621 | */
|
|---|
| 622 | while (!list_empty(&FIBRIL_connection->msg_queue)) {
|
|---|
| 623 | msg_t *msg;
|
|---|
| 624 |
|
|---|
| 625 | msg = list_get_instance(FIBRIL_connection->msg_queue.next,
|
|---|
| 626 | msg_t, link);
|
|---|
| 627 | list_remove(&msg->link);
|
|---|
| 628 | ipc_answer_0(msg->callid, EHANGUP);
|
|---|
| 629 | free(msg);
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | /*
|
|---|
| 633 | * If the connection was hung-up, answer the last call,
|
|---|
| 634 | * i.e. IPC_M_PHONE_HUNGUP.
|
|---|
| 635 | */
|
|---|
| 636 | if (FIBRIL_connection->close_callid)
|
|---|
| 637 | ipc_answer_0(FIBRIL_connection->close_callid, EOK);
|
|---|
| 638 |
|
|---|
| 639 | return 0;
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | /** Create a new fibril for a new connection.
|
|---|
| 643 | *
|
|---|
| 644 | * Create new fibril for connection, fill in connection structures and inserts
|
|---|
| 645 | * it into the hash table, so that later we can easily do routing of messages to
|
|---|
| 646 | * particular fibrils.
|
|---|
| 647 | *
|
|---|
| 648 | * @param in_task_hash Identification of the incoming connection.
|
|---|
| 649 | * @param in_phone_hash Identification of the incoming connection.
|
|---|
| 650 | * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
|
|---|
| 651 | * If callid is zero, the connection was opened by
|
|---|
| 652 | * accepting the IPC_M_CONNECT_TO_ME call and this function
|
|---|
| 653 | * is called directly by the server.
|
|---|
| 654 | * @param call Call data of the opening call.
|
|---|
| 655 | * @param cfibril Fibril function that should be called upon opening the
|
|---|
| 656 | * connection.
|
|---|
| 657 | *
|
|---|
| 658 | * @return New fibril id or NULL on failure.
|
|---|
| 659 | *
|
|---|
| 660 | */
|
|---|
| 661 | fid_t async_new_connection(sysarg_t in_task_hash, sysarg_t in_phone_hash,
|
|---|
| 662 | ipc_callid_t callid, ipc_call_t *call,
|
|---|
| 663 | void (*cfibril)(ipc_callid_t, ipc_call_t *))
|
|---|
| 664 | {
|
|---|
| 665 | connection_t *conn = malloc(sizeof(*conn));
|
|---|
| 666 | if (!conn) {
|
|---|
| 667 | if (callid)
|
|---|
| 668 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 669 | return (uintptr_t) NULL;
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | conn->in_task_hash = in_task_hash;
|
|---|
| 673 | conn->in_phone_hash = in_phone_hash;
|
|---|
| 674 | list_initialize(&conn->msg_queue);
|
|---|
| 675 | conn->callid = callid;
|
|---|
| 676 | conn->close_callid = 0;
|
|---|
| 677 |
|
|---|
| 678 | if (call)
|
|---|
| 679 | conn->call = *call;
|
|---|
| 680 |
|
|---|
| 681 | /* We will activate the fibril ASAP */
|
|---|
| 682 | conn->wdata.active = true;
|
|---|
| 683 | conn->cfibril = cfibril;
|
|---|
| 684 | conn->wdata.fid = fibril_create(connection_fibril, conn);
|
|---|
| 685 |
|
|---|
| 686 | if (!conn->wdata.fid) {
|
|---|
| 687 | free(conn);
|
|---|
| 688 | if (callid)
|
|---|
| 689 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 690 | return (uintptr_t) NULL;
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | /* Add connection to the connection hash table */
|
|---|
| 694 | unsigned long key = conn->in_phone_hash;
|
|---|
| 695 |
|
|---|
| 696 | futex_down(&async_futex);
|
|---|
| 697 | hash_table_insert(&conn_hash_table, &key, &conn->link);
|
|---|
| 698 | futex_up(&async_futex);
|
|---|
| 699 |
|
|---|
| 700 | fibril_add_ready(conn->wdata.fid);
|
|---|
| 701 |
|
|---|
| 702 | return conn->wdata.fid;
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | /** Handle a call that was received.
|
|---|
| 706 | *
|
|---|
| 707 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
|---|
| 708 | * Otherwise the call is routed to its connection fibril.
|
|---|
| 709 | *
|
|---|
| 710 | * @param callid Hash of the incoming call.
|
|---|
| 711 | * @param call Data of the incoming call.
|
|---|
| 712 | *
|
|---|
| 713 | */
|
|---|
| 714 | static void handle_call(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 715 | {
|
|---|
| 716 | /* Unrouted call - do some default behaviour */
|
|---|
| 717 | if ((callid & IPC_CALLID_NOTIFICATION)) {
|
|---|
| 718 | process_notification(callid, call);
|
|---|
| 719 | goto out;
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | switch (IPC_GET_IMETHOD(*call)) {
|
|---|
| 723 | case IPC_M_CONNECT_ME:
|
|---|
| 724 | case IPC_M_CONNECT_ME_TO:
|
|---|
| 725 | /* Open new connection with fibril etc. */
|
|---|
| 726 | async_new_connection(call->in_task_hash, IPC_GET_ARG5(*call),
|
|---|
| 727 | callid, call, client_connection);
|
|---|
| 728 | goto out;
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | /* Try to route the call through the connection hash table */
|
|---|
| 732 | if (route_call(callid, call))
|
|---|
| 733 | goto out;
|
|---|
| 734 |
|
|---|
| 735 | /* Unknown call from unknown phone - hang it up */
|
|---|
| 736 | ipc_answer_0(callid, EHANGUP);
|
|---|
| 737 | return;
|
|---|
| 738 |
|
|---|
| 739 | out:
|
|---|
| 740 | ;
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | /** Fire all timeouts that expired. */
|
|---|
| 744 | static void handle_expired_timeouts(void)
|
|---|
| 745 | {
|
|---|
| 746 | struct timeval tv;
|
|---|
| 747 | gettimeofday(&tv, NULL);
|
|---|
| 748 |
|
|---|
| 749 | futex_down(&async_futex);
|
|---|
| 750 |
|
|---|
| 751 | link_t *cur = timeout_list.next;
|
|---|
| 752 | while (cur != &timeout_list) {
|
|---|
| 753 | awaiter_t *waiter;
|
|---|
| 754 |
|
|---|
| 755 | waiter = list_get_instance(cur, awaiter_t, to_event.link);
|
|---|
| 756 | if (tv_gt(&waiter->to_event.expires, &tv))
|
|---|
| 757 | break;
|
|---|
| 758 |
|
|---|
| 759 | cur = cur->next;
|
|---|
| 760 |
|
|---|
| 761 | list_remove(&waiter->to_event.link);
|
|---|
| 762 | waiter->to_event.inlist = false;
|
|---|
| 763 | waiter->to_event.occurred = true;
|
|---|
| 764 |
|
|---|
| 765 | /*
|
|---|
| 766 | * Redundant condition?
|
|---|
| 767 | * The fibril should not be active when it gets here.
|
|---|
| 768 | */
|
|---|
| 769 | if (!waiter->active) {
|
|---|
| 770 | waiter->active = true;
|
|---|
| 771 | fibril_add_ready(waiter->fid);
|
|---|
| 772 | }
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | futex_up(&async_futex);
|
|---|
| 776 | }
|
|---|
| 777 |
|
|---|
| 778 | /** Endless loop dispatching incoming calls and answers.
|
|---|
| 779 | *
|
|---|
| 780 | * @return Never returns.
|
|---|
| 781 | *
|
|---|
| 782 | */
|
|---|
| 783 | static int async_manager_worker(void)
|
|---|
| 784 | {
|
|---|
| 785 | while (true) {
|
|---|
| 786 | if (fibril_switch(FIBRIL_FROM_MANAGER)) {
|
|---|
| 787 | futex_up(&async_futex);
|
|---|
| 788 | /*
|
|---|
| 789 | * async_futex is always held when entering a manager
|
|---|
| 790 | * fibril.
|
|---|
| 791 | */
|
|---|
| 792 | continue;
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | futex_down(&async_futex);
|
|---|
| 796 |
|
|---|
| 797 | suseconds_t timeout;
|
|---|
| 798 | if (!list_empty(&timeout_list)) {
|
|---|
| 799 | awaiter_t *waiter = list_get_instance(timeout_list.next,
|
|---|
| 800 | awaiter_t, to_event.link);
|
|---|
| 801 |
|
|---|
| 802 | struct timeval tv;
|
|---|
| 803 | gettimeofday(&tv, NULL);
|
|---|
| 804 |
|
|---|
| 805 | if (tv_gteq(&tv, &waiter->to_event.expires)) {
|
|---|
| 806 | futex_up(&async_futex);
|
|---|
| 807 | handle_expired_timeouts();
|
|---|
| 808 | continue;
|
|---|
| 809 | } else
|
|---|
| 810 | timeout = tv_sub(&waiter->to_event.expires,
|
|---|
| 811 | &tv);
|
|---|
| 812 | } else
|
|---|
| 813 | timeout = SYNCH_NO_TIMEOUT;
|
|---|
| 814 |
|
|---|
| 815 | futex_up(&async_futex);
|
|---|
| 816 |
|
|---|
| 817 | atomic_inc(&threads_in_ipc_wait);
|
|---|
| 818 |
|
|---|
| 819 | ipc_call_t call;
|
|---|
| 820 | ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
|
|---|
| 821 | SYNCH_FLAGS_NONE);
|
|---|
| 822 |
|
|---|
| 823 | atomic_dec(&threads_in_ipc_wait);
|
|---|
| 824 |
|
|---|
| 825 | if (!callid) {
|
|---|
| 826 | handle_expired_timeouts();
|
|---|
| 827 | continue;
|
|---|
| 828 | }
|
|---|
| 829 |
|
|---|
| 830 | if (callid & IPC_CALLID_ANSWERED)
|
|---|
| 831 | continue;
|
|---|
| 832 |
|
|---|
| 833 | handle_call(callid, &call);
|
|---|
| 834 | }
|
|---|
| 835 |
|
|---|
| 836 | return 0;
|
|---|
| 837 | }
|
|---|
| 838 |
|
|---|
| 839 | /** Function to start async_manager as a standalone fibril.
|
|---|
| 840 | *
|
|---|
| 841 | * When more kernel threads are used, one async manager should exist per thread.
|
|---|
| 842 | *
|
|---|
| 843 | * @param arg Unused.
|
|---|
| 844 | * @return Never returns.
|
|---|
| 845 | *
|
|---|
| 846 | */
|
|---|
| 847 | static int async_manager_fibril(void *arg)
|
|---|
| 848 | {
|
|---|
| 849 | futex_up(&async_futex);
|
|---|
| 850 |
|
|---|
| 851 | /*
|
|---|
| 852 | * async_futex is always locked when entering manager
|
|---|
| 853 | */
|
|---|
| 854 | async_manager_worker();
|
|---|
| 855 |
|
|---|
| 856 | return 0;
|
|---|
| 857 | }
|
|---|
| 858 |
|
|---|
| 859 | /** Add one manager to manager list. */
|
|---|
| 860 | void async_create_manager(void)
|
|---|
| 861 | {
|
|---|
| 862 | fid_t fid = fibril_create(async_manager_fibril, NULL);
|
|---|
| 863 | fibril_add_manager(fid);
|
|---|
| 864 | }
|
|---|
| 865 |
|
|---|
| 866 | /** Remove one manager from manager list */
|
|---|
| 867 | void async_destroy_manager(void)
|
|---|
| 868 | {
|
|---|
| 869 | fibril_remove_manager();
|
|---|
| 870 | }
|
|---|
| 871 |
|
|---|
| 872 | /** Initialize the async framework.
|
|---|
| 873 | *
|
|---|
| 874 | * @return Zero on success or an error code.
|
|---|
| 875 | */
|
|---|
| 876 | int __async_init(void)
|
|---|
| 877 | {
|
|---|
| 878 | if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS, 1,
|
|---|
| 879 | &client_hash_table_ops) || !hash_table_create(&conn_hash_table,
|
|---|
| 880 | CONN_HASH_TABLE_BUCKETS, 1, &conn_hash_table_ops)) {
|
|---|
| 881 | return ENOMEM;
|
|---|
| 882 | }
|
|---|
| 883 |
|
|---|
| 884 | _async_sess_init();
|
|---|
| 885 |
|
|---|
| 886 | return 0;
|
|---|
| 887 | }
|
|---|
| 888 |
|
|---|
| 889 | /** Reply received callback.
|
|---|
| 890 | *
|
|---|
| 891 | * This function is called whenever a reply for an asynchronous message sent out
|
|---|
| 892 | * by the asynchronous framework is received.
|
|---|
| 893 | *
|
|---|
| 894 | * Notify the fibril which is waiting for this message that it has arrived.
|
|---|
| 895 | *
|
|---|
| 896 | * @param arg Pointer to the asynchronous message record.
|
|---|
| 897 | * @param retval Value returned in the answer.
|
|---|
| 898 | * @param data Call data of the answer.
|
|---|
| 899 | */
|
|---|
| 900 | static void reply_received(void *arg, int retval, ipc_call_t *data)
|
|---|
| 901 | {
|
|---|
| 902 | futex_down(&async_futex);
|
|---|
| 903 |
|
|---|
| 904 | amsg_t *msg = (amsg_t *) arg;
|
|---|
| 905 | msg->retval = retval;
|
|---|
| 906 |
|
|---|
| 907 | /* Copy data after futex_down, just in case the call was detached */
|
|---|
| 908 | if ((msg->dataptr) && (data))
|
|---|
| 909 | *msg->dataptr = *data;
|
|---|
| 910 |
|
|---|
| 911 | write_barrier();
|
|---|
| 912 |
|
|---|
| 913 | /* Remove message from timeout list */
|
|---|
| 914 | if (msg->wdata.to_event.inlist)
|
|---|
| 915 | list_remove(&msg->wdata.to_event.link);
|
|---|
| 916 |
|
|---|
| 917 | msg->done = true;
|
|---|
| 918 | if (!msg->wdata.active) {
|
|---|
| 919 | msg->wdata.active = true;
|
|---|
| 920 | fibril_add_ready(msg->wdata.fid);
|
|---|
| 921 | }
|
|---|
| 922 |
|
|---|
| 923 | futex_up(&async_futex);
|
|---|
| 924 | }
|
|---|
| 925 |
|
|---|
| 926 | /** Send message and return id of the sent message.
|
|---|
| 927 | *
|
|---|
| 928 | * The return value can be used as input for async_wait() to wait for
|
|---|
| 929 | * completion.
|
|---|
| 930 | *
|
|---|
| 931 | * @param phoneid Handle of the phone that will be used for the send.
|
|---|
| 932 | * @param method Service-defined method.
|
|---|
| 933 | * @param arg1 Service-defined payload argument.
|
|---|
| 934 | * @param arg2 Service-defined payload argument.
|
|---|
| 935 | * @param arg3 Service-defined payload argument.
|
|---|
| 936 | * @param arg4 Service-defined payload argument.
|
|---|
| 937 | * @param dataptr If non-NULL, storage where the reply data will be
|
|---|
| 938 | * stored.
|
|---|
| 939 | *
|
|---|
| 940 | * @return Hash of the sent message or 0 on error.
|
|---|
| 941 | *
|
|---|
| 942 | */
|
|---|
| 943 | aid_t async_send_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
|---|
| 944 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
|---|
| 945 | {
|
|---|
| 946 | amsg_t *msg = malloc(sizeof(*msg));
|
|---|
| 947 |
|
|---|
| 948 | if (!msg)
|
|---|
| 949 | return 0;
|
|---|
| 950 |
|
|---|
| 951 | msg->done = false;
|
|---|
| 952 | msg->dataptr = dataptr;
|
|---|
| 953 |
|
|---|
| 954 | msg->wdata.to_event.inlist = false;
|
|---|
| 955 | /* We may sleep in the next method, but it will use its own mechanism */
|
|---|
| 956 | msg->wdata.active = true;
|
|---|
| 957 |
|
|---|
| 958 | ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
|
|---|
| 959 | reply_received, true);
|
|---|
| 960 |
|
|---|
| 961 | return (aid_t) msg;
|
|---|
| 962 | }
|
|---|
| 963 |
|
|---|
| 964 | /** Send message and return id of the sent message
|
|---|
| 965 | *
|
|---|
| 966 | * The return value can be used as input for async_wait() to wait for
|
|---|
| 967 | * completion.
|
|---|
| 968 | *
|
|---|
| 969 | * @param phoneid Handle of the phone that will be used for the send.
|
|---|
| 970 | * @param method Service-defined method.
|
|---|
| 971 | * @param arg1 Service-defined payload argument.
|
|---|
| 972 | * @param arg2 Service-defined payload argument.
|
|---|
| 973 | * @param arg3 Service-defined payload argument.
|
|---|
| 974 | * @param arg4 Service-defined payload argument.
|
|---|
| 975 | * @param arg5 Service-defined payload argument.
|
|---|
| 976 | * @param dataptr If non-NULL, storage where the reply data will be
|
|---|
| 977 | * stored.
|
|---|
| 978 | *
|
|---|
| 979 | * @return Hash of the sent message or 0 on error.
|
|---|
| 980 | *
|
|---|
| 981 | */
|
|---|
| 982 | aid_t async_send_slow(int phoneid, sysarg_t method, sysarg_t arg1,
|
|---|
| 983 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
|---|
| 984 | ipc_call_t *dataptr)
|
|---|
| 985 | {
|
|---|
| 986 | amsg_t *msg = malloc(sizeof(*msg));
|
|---|
| 987 |
|
|---|
| 988 | if (!msg)
|
|---|
| 989 | return 0;
|
|---|
| 990 |
|
|---|
| 991 | msg->done = false;
|
|---|
| 992 | msg->dataptr = dataptr;
|
|---|
| 993 |
|
|---|
| 994 | msg->wdata.to_event.inlist = false;
|
|---|
| 995 | /* We may sleep in next method, but it will use its own mechanism */
|
|---|
| 996 | msg->wdata.active = true;
|
|---|
| 997 |
|
|---|
| 998 | ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
|
|---|
| 999 | reply_received, true);
|
|---|
| 1000 |
|
|---|
| 1001 | return (aid_t) msg;
|
|---|
| 1002 | }
|
|---|
| 1003 |
|
|---|
| 1004 | /** Wait for a message sent by the async framework.
|
|---|
| 1005 | *
|
|---|
| 1006 | * @param amsgid Hash of the message to wait for.
|
|---|
| 1007 | * @param retval Pointer to storage where the retval of the answer will
|
|---|
| 1008 | * be stored.
|
|---|
| 1009 | *
|
|---|
| 1010 | */
|
|---|
| 1011 | void async_wait_for(aid_t amsgid, sysarg_t *retval)
|
|---|
| 1012 | {
|
|---|
| 1013 | amsg_t *msg = (amsg_t *) amsgid;
|
|---|
| 1014 |
|
|---|
| 1015 | futex_down(&async_futex);
|
|---|
| 1016 | if (msg->done) {
|
|---|
| 1017 | futex_up(&async_futex);
|
|---|
| 1018 | goto done;
|
|---|
| 1019 | }
|
|---|
| 1020 |
|
|---|
| 1021 | msg->wdata.fid = fibril_get_id();
|
|---|
| 1022 | msg->wdata.active = false;
|
|---|
| 1023 | msg->wdata.to_event.inlist = false;
|
|---|
| 1024 |
|
|---|
| 1025 | /* Leave the async_futex locked when entering this function */
|
|---|
| 1026 | fibril_switch(FIBRIL_TO_MANAGER);
|
|---|
| 1027 |
|
|---|
| 1028 | /* Futex is up automatically after fibril_switch */
|
|---|
| 1029 |
|
|---|
| 1030 | done:
|
|---|
| 1031 | if (retval)
|
|---|
| 1032 | *retval = msg->retval;
|
|---|
| 1033 |
|
|---|
| 1034 | free(msg);
|
|---|
| 1035 | }
|
|---|
| 1036 |
|
|---|
| 1037 | /** Wait for a message sent by the async framework, timeout variant.
|
|---|
| 1038 | *
|
|---|
| 1039 | * @param amsgid Hash of the message to wait for.
|
|---|
| 1040 | * @param retval Pointer to storage where the retval of the answer will
|
|---|
| 1041 | * be stored.
|
|---|
| 1042 | * @param timeout Timeout in microseconds.
|
|---|
| 1043 | *
|
|---|
| 1044 | * @return Zero on success, ETIMEOUT if the timeout has expired.
|
|---|
| 1045 | *
|
|---|
| 1046 | */
|
|---|
| 1047 | int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
|
|---|
| 1048 | {
|
|---|
| 1049 | amsg_t *msg = (amsg_t *) amsgid;
|
|---|
| 1050 |
|
|---|
| 1051 | /* TODO: Let it go through the event read at least once */
|
|---|
| 1052 | if (timeout < 0)
|
|---|
| 1053 | return ETIMEOUT;
|
|---|
| 1054 |
|
|---|
| 1055 | futex_down(&async_futex);
|
|---|
| 1056 | if (msg->done) {
|
|---|
| 1057 | futex_up(&async_futex);
|
|---|
| 1058 | goto done;
|
|---|
| 1059 | }
|
|---|
| 1060 |
|
|---|
| 1061 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
|---|
| 1062 | tv_add(&msg->wdata.to_event.expires, timeout);
|
|---|
| 1063 |
|
|---|
| 1064 | msg->wdata.fid = fibril_get_id();
|
|---|
| 1065 | msg->wdata.active = false;
|
|---|
| 1066 | async_insert_timeout(&msg->wdata);
|
|---|
| 1067 |
|
|---|
| 1068 | /* Leave the async_futex locked when entering this function */
|
|---|
| 1069 | fibril_switch(FIBRIL_TO_MANAGER);
|
|---|
| 1070 |
|
|---|
| 1071 | /* Futex is up automatically after fibril_switch */
|
|---|
| 1072 |
|
|---|
| 1073 | if (!msg->done)
|
|---|
| 1074 | return ETIMEOUT;
|
|---|
| 1075 |
|
|---|
| 1076 | done:
|
|---|
| 1077 | if (retval)
|
|---|
| 1078 | *retval = msg->retval;
|
|---|
| 1079 |
|
|---|
| 1080 | free(msg);
|
|---|
| 1081 |
|
|---|
| 1082 | return 0;
|
|---|
| 1083 | }
|
|---|
| 1084 |
|
|---|
| 1085 | /** Wait for specified time.
|
|---|
| 1086 | *
|
|---|
| 1087 | * The current fibril is suspended but the thread continues to execute.
|
|---|
| 1088 | *
|
|---|
| 1089 | * @param timeout Duration of the wait in microseconds.
|
|---|
| 1090 | *
|
|---|
| 1091 | */
|
|---|
| 1092 | void async_usleep(suseconds_t timeout)
|
|---|
| 1093 | {
|
|---|
| 1094 | amsg_t *msg = malloc(sizeof(*msg));
|
|---|
| 1095 |
|
|---|
| 1096 | if (!msg)
|
|---|
| 1097 | return;
|
|---|
| 1098 |
|
|---|
| 1099 | msg->wdata.fid = fibril_get_id();
|
|---|
| 1100 | msg->wdata.active = false;
|
|---|
| 1101 |
|
|---|
| 1102 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
|---|
| 1103 | tv_add(&msg->wdata.to_event.expires, timeout);
|
|---|
| 1104 |
|
|---|
| 1105 | futex_down(&async_futex);
|
|---|
| 1106 |
|
|---|
| 1107 | async_insert_timeout(&msg->wdata);
|
|---|
| 1108 |
|
|---|
| 1109 | /* Leave the async_futex locked when entering this function */
|
|---|
| 1110 | fibril_switch(FIBRIL_TO_MANAGER);
|
|---|
| 1111 |
|
|---|
| 1112 | /* Futex is up automatically after fibril_switch() */
|
|---|
| 1113 |
|
|---|
| 1114 | free(msg);
|
|---|
| 1115 | }
|
|---|
| 1116 |
|
|---|
| 1117 | /** Setter for client_connection function pointer.
|
|---|
| 1118 | *
|
|---|
| 1119 | * @param conn Function that will implement a new connection fibril.
|
|---|
| 1120 | *
|
|---|
| 1121 | */
|
|---|
| 1122 | void async_set_client_connection(async_client_conn_t conn)
|
|---|
| 1123 | {
|
|---|
| 1124 | client_connection = conn;
|
|---|
| 1125 | }
|
|---|
| 1126 |
|
|---|
| 1127 | /** Setter for interrupt_received function pointer.
|
|---|
| 1128 | *
|
|---|
| 1129 | * @param intr Function that will implement a new interrupt
|
|---|
| 1130 | * notification fibril.
|
|---|
| 1131 | */
|
|---|
| 1132 | void async_set_interrupt_received(async_client_conn_t intr)
|
|---|
| 1133 | {
|
|---|
| 1134 | interrupt_received = intr;
|
|---|
| 1135 | }
|
|---|
| 1136 |
|
|---|
| 1137 | /** Pseudo-synchronous message sending - fast version.
|
|---|
| 1138 | *
|
|---|
| 1139 | * Send message asynchronously and return only after the reply arrives.
|
|---|
| 1140 | *
|
|---|
| 1141 | * This function can only transfer 4 register payload arguments. For
|
|---|
| 1142 | * transferring more arguments, see the slower async_req_slow().
|
|---|
| 1143 | *
|
|---|
| 1144 | * @param phoneid Hash of the phone through which to make the call.
|
|---|
| 1145 | * @param method Method of the call.
|
|---|
| 1146 | * @param arg1 Service-defined payload argument.
|
|---|
| 1147 | * @param arg2 Service-defined payload argument.
|
|---|
| 1148 | * @param arg3 Service-defined payload argument.
|
|---|
| 1149 | * @param arg4 Service-defined payload argument.
|
|---|
| 1150 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
|---|
| 1151 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
|---|
| 1152 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
|---|
| 1153 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
|---|
| 1154 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
|---|
| 1155 | *
|
|---|
| 1156 | * @return Return code of the reply or a negative error code.
|
|---|
| 1157 | *
|
|---|
| 1158 | */
|
|---|
| 1159 | sysarg_t async_req_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
|---|
| 1160 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
|
|---|
| 1161 | sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
|---|
| 1162 | {
|
|---|
| 1163 | ipc_call_t result;
|
|---|
| 1164 | aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
|
|---|
| 1165 | &result);
|
|---|
| 1166 |
|
|---|
| 1167 | sysarg_t rc;
|
|---|
| 1168 | async_wait_for(eid, &rc);
|
|---|
| 1169 |
|
|---|
| 1170 | if (r1)
|
|---|
| 1171 | *r1 = IPC_GET_ARG1(result);
|
|---|
| 1172 |
|
|---|
| 1173 | if (r2)
|
|---|
| 1174 | *r2 = IPC_GET_ARG2(result);
|
|---|
| 1175 |
|
|---|
| 1176 | if (r3)
|
|---|
| 1177 | *r3 = IPC_GET_ARG3(result);
|
|---|
| 1178 |
|
|---|
| 1179 | if (r4)
|
|---|
| 1180 | *r4 = IPC_GET_ARG4(result);
|
|---|
| 1181 |
|
|---|
| 1182 | if (r5)
|
|---|
| 1183 | *r5 = IPC_GET_ARG5(result);
|
|---|
| 1184 |
|
|---|
| 1185 | return rc;
|
|---|
| 1186 | }
|
|---|
| 1187 |
|
|---|
| 1188 | /** Pseudo-synchronous message sending - slow version.
|
|---|
| 1189 | *
|
|---|
| 1190 | * Send message asynchronously and return only after the reply arrives.
|
|---|
| 1191 | *
|
|---|
| 1192 | * @param phoneid Hash of the phone through which to make the call.
|
|---|
| 1193 | * @param method Method of the call.
|
|---|
| 1194 | * @param arg1 Service-defined payload argument.
|
|---|
| 1195 | * @param arg2 Service-defined payload argument.
|
|---|
| 1196 | * @param arg3 Service-defined payload argument.
|
|---|
| 1197 | * @param arg4 Service-defined payload argument.
|
|---|
| 1198 | * @param arg5 Service-defined payload argument.
|
|---|
| 1199 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
|---|
| 1200 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
|---|
| 1201 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
|---|
| 1202 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
|---|
| 1203 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
|---|
| 1204 | *
|
|---|
| 1205 | * @return Return code of the reply or a negative error code.
|
|---|
| 1206 | *
|
|---|
| 1207 | */
|
|---|
| 1208 | sysarg_t async_req_slow(int phoneid, sysarg_t method, sysarg_t arg1,
|
|---|
| 1209 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
|
|---|
| 1210 | sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
|---|
| 1211 | {
|
|---|
| 1212 | ipc_call_t result;
|
|---|
| 1213 | aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
|
|---|
| 1214 | &result);
|
|---|
| 1215 |
|
|---|
| 1216 | sysarg_t rc;
|
|---|
| 1217 | async_wait_for(eid, &rc);
|
|---|
| 1218 |
|
|---|
| 1219 | if (r1)
|
|---|
| 1220 | *r1 = IPC_GET_ARG1(result);
|
|---|
| 1221 |
|
|---|
| 1222 | if (r2)
|
|---|
| 1223 | *r2 = IPC_GET_ARG2(result);
|
|---|
| 1224 |
|
|---|
| 1225 | if (r3)
|
|---|
| 1226 | *r3 = IPC_GET_ARG3(result);
|
|---|
| 1227 |
|
|---|
| 1228 | if (r4)
|
|---|
| 1229 | *r4 = IPC_GET_ARG4(result);
|
|---|
| 1230 |
|
|---|
| 1231 | if (r5)
|
|---|
| 1232 | *r5 = IPC_GET_ARG5(result);
|
|---|
| 1233 |
|
|---|
| 1234 | return rc;
|
|---|
| 1235 | }
|
|---|
| 1236 |
|
|---|
| 1237 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
|---|
| 1238 | *
|
|---|
| 1239 | * Ask through phone for a new connection to some service.
|
|---|
| 1240 | *
|
|---|
| 1241 | * @param phoneid Phone handle used for contacting the other side.
|
|---|
| 1242 | * @param arg1 User defined argument.
|
|---|
| 1243 | * @param arg2 User defined argument.
|
|---|
| 1244 | * @param arg3 User defined argument.
|
|---|
| 1245 | *
|
|---|
| 1246 | * @return New phone handle on success or a negative error code.
|
|---|
| 1247 | */
|
|---|
| 1248 | int
|
|---|
| 1249 | async_connect_me_to(int phoneid, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
|---|
| 1250 | {
|
|---|
| 1251 | int rc;
|
|---|
| 1252 | sysarg_t newphid;
|
|---|
| 1253 |
|
|---|
| 1254 | rc = async_req_3_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, NULL,
|
|---|
| 1255 | NULL, NULL, NULL, &newphid);
|
|---|
| 1256 |
|
|---|
| 1257 | if (rc != EOK)
|
|---|
| 1258 | return rc;
|
|---|
| 1259 |
|
|---|
| 1260 | return newphid;
|
|---|
| 1261 | }
|
|---|
| 1262 |
|
|---|
| 1263 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
|---|
| 1264 | *
|
|---|
| 1265 | * Ask through phone for a new connection to some service and block until
|
|---|
| 1266 | * success.
|
|---|
| 1267 | *
|
|---|
| 1268 | * @param phoneid Phone handle used for contacting the other side.
|
|---|
| 1269 | * @param arg1 User defined argument.
|
|---|
| 1270 | * @param arg2 User defined argument.
|
|---|
| 1271 | * @param arg3 User defined argument.
|
|---|
| 1272 | *
|
|---|
| 1273 | * @return New phone handle on success or a negative error code.
|
|---|
| 1274 | */
|
|---|
| 1275 | int
|
|---|
| 1276 | async_connect_me_to_blocking(int phoneid, sysarg_t arg1, sysarg_t arg2,
|
|---|
| 1277 | sysarg_t arg3)
|
|---|
| 1278 | {
|
|---|
| 1279 | int rc;
|
|---|
| 1280 | sysarg_t newphid;
|
|---|
| 1281 |
|
|---|
| 1282 | rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
|
|---|
| 1283 | IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
|
|---|
| 1284 |
|
|---|
| 1285 | if (rc != EOK)
|
|---|
| 1286 | return rc;
|
|---|
| 1287 |
|
|---|
| 1288 | return newphid;
|
|---|
| 1289 | }
|
|---|
| 1290 |
|
|---|
| 1291 | /** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
|
|---|
| 1292 | *
|
|---|
| 1293 | * @param phoneid Phone that will be used to contact the receiving side.
|
|---|
| 1294 | * @param dst Destination address space area base.
|
|---|
| 1295 | * @param size Size of the destination address space area.
|
|---|
| 1296 | * @param arg User defined argument.
|
|---|
| 1297 | * @param flags Storage where the received flags will be stored. Can be
|
|---|
| 1298 | * NULL.
|
|---|
| 1299 | *
|
|---|
| 1300 | * @return Zero on success or a negative error code from errno.h.
|
|---|
| 1301 | */
|
|---|
| 1302 | int async_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
|
|---|
| 1303 | int *flags)
|
|---|
| 1304 | {
|
|---|
| 1305 | int res;
|
|---|
| 1306 | sysarg_t tmp_flags;
|
|---|
| 1307 | res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
|
|---|
| 1308 | (sysarg_t) size, arg, NULL, &tmp_flags);
|
|---|
| 1309 | if (flags)
|
|---|
| 1310 | *flags = tmp_flags;
|
|---|
| 1311 | return res;
|
|---|
| 1312 | }
|
|---|
| 1313 |
|
|---|
| 1314 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
|---|
| 1315 | *
|
|---|
| 1316 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN calls
|
|---|
| 1317 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
|---|
| 1318 | *
|
|---|
| 1319 | * So far, this wrapper is to be used from within a connection fibril.
|
|---|
| 1320 | *
|
|---|
| 1321 | * @param callid Storage where the hash of the IPC_M_SHARE_IN call will
|
|---|
| 1322 | * be stored.
|
|---|
| 1323 | * @param size Destination address space area size.
|
|---|
| 1324 | *
|
|---|
| 1325 | * @return Non-zero on success, zero on failure.
|
|---|
| 1326 | */
|
|---|
| 1327 | int async_share_in_receive(ipc_callid_t *callid, size_t *size)
|
|---|
| 1328 | {
|
|---|
| 1329 | ipc_call_t data;
|
|---|
| 1330 |
|
|---|
| 1331 | assert(callid);
|
|---|
| 1332 | assert(size);
|
|---|
| 1333 |
|
|---|
| 1334 | *callid = async_get_call(&data);
|
|---|
| 1335 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
|
|---|
| 1336 | return 0;
|
|---|
| 1337 | *size = (size_t) IPC_GET_ARG2(data);
|
|---|
| 1338 | return 1;
|
|---|
| 1339 | }
|
|---|
| 1340 |
|
|---|
| 1341 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
|---|
| 1342 | *
|
|---|
| 1343 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
|
|---|
| 1344 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
|---|
| 1345 | *
|
|---|
| 1346 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
|---|
| 1347 | * @param src Source address space base.
|
|---|
| 1348 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
|---|
| 1349 | *
|
|---|
| 1350 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 1351 | */
|
|---|
| 1352 | int async_share_in_finalize(ipc_callid_t callid, void *src, int flags)
|
|---|
| 1353 | {
|
|---|
| 1354 | return ipc_share_in_finalize(callid, src, flags);
|
|---|
| 1355 | }
|
|---|
| 1356 |
|
|---|
| 1357 | /** Wrapper for making IPC_M_SHARE_OUT calls using the async framework.
|
|---|
| 1358 | *
|
|---|
| 1359 | * @param phoneid Phone that will be used to contact the receiving side.
|
|---|
| 1360 | * @param src Source address space area base address.
|
|---|
| 1361 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
|---|
| 1362 | *
|
|---|
| 1363 | * @return Zero on success or a negative error code from errno.h.
|
|---|
| 1364 | */
|
|---|
| 1365 | int async_share_out_start(int phoneid, void *src, int flags)
|
|---|
| 1366 | {
|
|---|
| 1367 | return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
|
|---|
| 1368 | (sysarg_t) flags);
|
|---|
| 1369 | }
|
|---|
| 1370 |
|
|---|
| 1371 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
|---|
| 1372 | *
|
|---|
| 1373 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT calls
|
|---|
| 1374 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
|---|
| 1375 | *
|
|---|
| 1376 | * So far, this wrapper is to be used from within a connection fibril.
|
|---|
| 1377 | *
|
|---|
| 1378 | * @param callid Storage where the hash of the IPC_M_SHARE_OUT call will
|
|---|
| 1379 | * be stored.
|
|---|
| 1380 | * @param size Storage where the source address space area size will be
|
|---|
| 1381 | * stored.
|
|---|
| 1382 | * @param flags Storage where the sharing flags will be stored.
|
|---|
| 1383 | *
|
|---|
| 1384 | * @return Non-zero on success, zero on failure.
|
|---|
| 1385 | */
|
|---|
| 1386 | int async_share_out_receive(ipc_callid_t *callid, size_t *size, int *flags)
|
|---|
| 1387 | {
|
|---|
| 1388 | ipc_call_t data;
|
|---|
| 1389 |
|
|---|
| 1390 | assert(callid);
|
|---|
| 1391 | assert(size);
|
|---|
| 1392 | assert(flags);
|
|---|
| 1393 |
|
|---|
| 1394 | *callid = async_get_call(&data);
|
|---|
| 1395 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
|
|---|
| 1396 | return 0;
|
|---|
| 1397 | *size = (size_t) IPC_GET_ARG2(data);
|
|---|
| 1398 | *flags = (int) IPC_GET_ARG3(data);
|
|---|
| 1399 | return 1;
|
|---|
| 1400 | }
|
|---|
| 1401 |
|
|---|
| 1402 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
|---|
| 1403 | *
|
|---|
| 1404 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT calls
|
|---|
| 1405 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
|---|
| 1406 | *
|
|---|
| 1407 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
|---|
| 1408 | * @param dst Destination address space area base address.
|
|---|
| 1409 | *
|
|---|
| 1410 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 1411 | */
|
|---|
| 1412 | int async_share_out_finalize(ipc_callid_t callid, void *dst)
|
|---|
| 1413 | {
|
|---|
| 1414 | return ipc_share_out_finalize(callid, dst);
|
|---|
| 1415 | }
|
|---|
| 1416 |
|
|---|
| 1417 |
|
|---|
| 1418 | /** Wrapper for making IPC_M_DATA_READ calls using the async framework.
|
|---|
| 1419 | *
|
|---|
| 1420 | * @param phoneid Phone that will be used to contact the receiving side.
|
|---|
| 1421 | * @param dst Address of the beginning of the destination buffer.
|
|---|
| 1422 | * @param size Size of the destination buffer.
|
|---|
| 1423 | *
|
|---|
| 1424 | * @return Zero on success or a negative error code from errno.h.
|
|---|
| 1425 | */
|
|---|
| 1426 | int async_data_read_start(int phoneid, void *dst, size_t size)
|
|---|
| 1427 | {
|
|---|
| 1428 | return async_req_2_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
|
|---|
| 1429 | (sysarg_t) size);
|
|---|
| 1430 | }
|
|---|
| 1431 |
|
|---|
| 1432 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
|---|
| 1433 | *
|
|---|
| 1434 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ calls
|
|---|
| 1435 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
|---|
| 1436 | *
|
|---|
| 1437 | * So far, this wrapper is to be used from within a connection fibril.
|
|---|
| 1438 | *
|
|---|
| 1439 | * @param callid Storage where the hash of the IPC_M_DATA_READ call will
|
|---|
| 1440 | * be stored.
|
|---|
| 1441 | * @param size Storage where the maximum size will be stored. Can be
|
|---|
| 1442 | * NULL.
|
|---|
| 1443 | *
|
|---|
| 1444 | * @return Non-zero on success, zero on failure.
|
|---|
| 1445 | */
|
|---|
| 1446 | int async_data_read_receive(ipc_callid_t *callid, size_t *size)
|
|---|
| 1447 | {
|
|---|
| 1448 | ipc_call_t data;
|
|---|
| 1449 |
|
|---|
| 1450 | assert(callid);
|
|---|
| 1451 |
|
|---|
| 1452 | *callid = async_get_call(&data);
|
|---|
| 1453 | if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
|
|---|
| 1454 | return 0;
|
|---|
| 1455 | if (size)
|
|---|
| 1456 | *size = (size_t) IPC_GET_ARG2(data);
|
|---|
| 1457 | return 1;
|
|---|
| 1458 | }
|
|---|
| 1459 |
|
|---|
| 1460 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
|---|
| 1461 | *
|
|---|
| 1462 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
|
|---|
| 1463 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
|---|
| 1464 | *
|
|---|
| 1465 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
|---|
| 1466 | * @param src Source address for the IPC_M_DATA_READ call.
|
|---|
| 1467 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
|---|
| 1468 | * the maximum size announced by the sender.
|
|---|
| 1469 | *
|
|---|
| 1470 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 1471 | */
|
|---|
| 1472 | int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
|
|---|
| 1473 | {
|
|---|
| 1474 | return ipc_data_read_finalize(callid, src, size);
|
|---|
| 1475 | }
|
|---|
| 1476 |
|
|---|
| 1477 | /** Wrapper for forwarding any read request
|
|---|
| 1478 | *
|
|---|
| 1479 | *
|
|---|
| 1480 | */
|
|---|
| 1481 | int async_data_read_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
|---|
| 1482 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
|---|
| 1483 | {
|
|---|
| 1484 | ipc_callid_t callid;
|
|---|
| 1485 | if (!async_data_read_receive(&callid, NULL)) {
|
|---|
| 1486 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1487 | return EINVAL;
|
|---|
| 1488 | }
|
|---|
| 1489 |
|
|---|
| 1490 | aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
|
|---|
| 1491 | dataptr);
|
|---|
| 1492 | if (msg == 0) {
|
|---|
| 1493 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1494 | return EINVAL;
|
|---|
| 1495 | }
|
|---|
| 1496 |
|
|---|
| 1497 | int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
|
|---|
| 1498 | IPC_FF_ROUTE_FROM_ME);
|
|---|
| 1499 | if (retval != EOK) {
|
|---|
| 1500 | async_wait_for(msg, NULL);
|
|---|
| 1501 | ipc_answer_0(callid, retval);
|
|---|
| 1502 | return retval;
|
|---|
| 1503 | }
|
|---|
| 1504 |
|
|---|
| 1505 | sysarg_t rc;
|
|---|
| 1506 | async_wait_for(msg, &rc);
|
|---|
| 1507 |
|
|---|
| 1508 | return (int) rc;
|
|---|
| 1509 | }
|
|---|
| 1510 |
|
|---|
| 1511 | /** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
|
|---|
| 1512 | *
|
|---|
| 1513 | * @param phoneid Phone that will be used to contact the receiving side.
|
|---|
| 1514 | * @param src Address of the beginning of the source buffer.
|
|---|
| 1515 | * @param size Size of the source buffer.
|
|---|
| 1516 | *
|
|---|
| 1517 | * @return Zero on success or a negative error code from errno.h.
|
|---|
| 1518 | *
|
|---|
| 1519 | */
|
|---|
| 1520 | int async_data_write_start(int phoneid, const void *src, size_t size)
|
|---|
| 1521 | {
|
|---|
| 1522 | return async_req_2_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
|
|---|
| 1523 | (sysarg_t) size);
|
|---|
| 1524 | }
|
|---|
| 1525 |
|
|---|
| 1526 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
|---|
| 1527 | *
|
|---|
| 1528 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE calls
|
|---|
| 1529 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
|---|
| 1530 | *
|
|---|
| 1531 | * So far, this wrapper is to be used from within a connection fibril.
|
|---|
| 1532 | *
|
|---|
| 1533 | * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
|
|---|
| 1534 | * be stored.
|
|---|
| 1535 | * @param size Storage where the suggested size will be stored. May be
|
|---|
| 1536 | * NULL
|
|---|
| 1537 | *
|
|---|
| 1538 | * @return Non-zero on success, zero on failure.
|
|---|
| 1539 | *
|
|---|
| 1540 | */
|
|---|
| 1541 | int async_data_write_receive(ipc_callid_t *callid, size_t *size)
|
|---|
| 1542 | {
|
|---|
| 1543 | ipc_call_t data;
|
|---|
| 1544 |
|
|---|
| 1545 | assert(callid);
|
|---|
| 1546 |
|
|---|
| 1547 | *callid = async_get_call(&data);
|
|---|
| 1548 | if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
|
|---|
| 1549 | return 0;
|
|---|
| 1550 |
|
|---|
| 1551 | if (size)
|
|---|
| 1552 | *size = (size_t) IPC_GET_ARG2(data);
|
|---|
| 1553 |
|
|---|
| 1554 | return 1;
|
|---|
| 1555 | }
|
|---|
| 1556 |
|
|---|
| 1557 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
|---|
| 1558 | *
|
|---|
| 1559 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
|
|---|
| 1560 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
|---|
| 1561 | *
|
|---|
| 1562 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
|---|
| 1563 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
|---|
| 1564 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
|---|
| 1565 | *
|
|---|
| 1566 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 1567 | *
|
|---|
| 1568 | */
|
|---|
| 1569 | int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
|
|---|
| 1570 | {
|
|---|
| 1571 | return ipc_data_write_finalize(callid, dst, size);
|
|---|
| 1572 | }
|
|---|
| 1573 |
|
|---|
| 1574 | /** Wrapper for receiving binary data or strings
|
|---|
| 1575 | *
|
|---|
| 1576 | * This wrapper only makes it more comfortable to use async_data_write_*
|
|---|
| 1577 | * functions to receive binary data or strings.
|
|---|
| 1578 | *
|
|---|
| 1579 | * @param data Pointer to data pointer (which should be later disposed
|
|---|
| 1580 | * by free()). If the operation fails, the pointer is not
|
|---|
| 1581 | * touched.
|
|---|
| 1582 | * @param nullterm If true then the received data is always zero terminated.
|
|---|
| 1583 | * This also causes to allocate one extra byte beyond the
|
|---|
| 1584 | * raw transmitted data.
|
|---|
| 1585 | * @param min_size Minimum size (in bytes) of the data to receive.
|
|---|
| 1586 | * @param max_size Maximum size (in bytes) of the data to receive. 0 means
|
|---|
| 1587 | * no limit.
|
|---|
| 1588 | * @param granulariy If non-zero then the size of the received data has to
|
|---|
| 1589 | * be divisible by this value.
|
|---|
| 1590 | * @param received If not NULL, the size of the received data is stored here.
|
|---|
| 1591 | *
|
|---|
| 1592 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 1593 | *
|
|---|
| 1594 | */
|
|---|
| 1595 | int async_data_write_accept(void **data, const bool nullterm,
|
|---|
| 1596 | const size_t min_size, const size_t max_size, const size_t granularity,
|
|---|
| 1597 | size_t *received)
|
|---|
| 1598 | {
|
|---|
| 1599 | ipc_callid_t callid;
|
|---|
| 1600 | size_t size;
|
|---|
| 1601 | if (!async_data_write_receive(&callid, &size)) {
|
|---|
| 1602 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1603 | return EINVAL;
|
|---|
| 1604 | }
|
|---|
| 1605 |
|
|---|
| 1606 | if (size < min_size) {
|
|---|
| 1607 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1608 | return EINVAL;
|
|---|
| 1609 | }
|
|---|
| 1610 |
|
|---|
| 1611 | if ((max_size > 0) && (size > max_size)) {
|
|---|
| 1612 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1613 | return EINVAL;
|
|---|
| 1614 | }
|
|---|
| 1615 |
|
|---|
| 1616 | if ((granularity > 0) && ((size % granularity) != 0)) {
|
|---|
| 1617 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1618 | return EINVAL;
|
|---|
| 1619 | }
|
|---|
| 1620 |
|
|---|
| 1621 | void *_data;
|
|---|
| 1622 |
|
|---|
| 1623 | if (nullterm)
|
|---|
| 1624 | _data = malloc(size + 1);
|
|---|
| 1625 | else
|
|---|
| 1626 | _data = malloc(size);
|
|---|
| 1627 |
|
|---|
| 1628 | if (_data == NULL) {
|
|---|
| 1629 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 1630 | return ENOMEM;
|
|---|
| 1631 | }
|
|---|
| 1632 |
|
|---|
| 1633 | int rc = async_data_write_finalize(callid, _data, size);
|
|---|
| 1634 | if (rc != EOK) {
|
|---|
| 1635 | free(_data);
|
|---|
| 1636 | return rc;
|
|---|
| 1637 | }
|
|---|
| 1638 |
|
|---|
| 1639 | if (nullterm)
|
|---|
| 1640 | ((char *) _data)[size] = 0;
|
|---|
| 1641 |
|
|---|
| 1642 | *data = _data;
|
|---|
| 1643 | if (received != NULL)
|
|---|
| 1644 | *received = size;
|
|---|
| 1645 |
|
|---|
| 1646 | return EOK;
|
|---|
| 1647 | }
|
|---|
| 1648 |
|
|---|
| 1649 | /** Wrapper for voiding any data that is about to be received
|
|---|
| 1650 | *
|
|---|
| 1651 | * This wrapper can be used to void any pending data
|
|---|
| 1652 | *
|
|---|
| 1653 | * @param retval Error value from @ref errno.h to be returned to the caller.
|
|---|
| 1654 | *
|
|---|
| 1655 | */
|
|---|
| 1656 | void async_data_write_void(const int retval)
|
|---|
| 1657 | {
|
|---|
| 1658 | ipc_callid_t callid;
|
|---|
| 1659 | async_data_write_receive(&callid, NULL);
|
|---|
| 1660 | ipc_answer_0(callid, retval);
|
|---|
| 1661 | }
|
|---|
| 1662 |
|
|---|
| 1663 | /** Wrapper for forwarding any data that is about to be received
|
|---|
| 1664 | *
|
|---|
| 1665 | *
|
|---|
| 1666 | */
|
|---|
| 1667 | int async_data_write_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
|---|
| 1668 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
|---|
| 1669 | {
|
|---|
| 1670 | ipc_callid_t callid;
|
|---|
| 1671 | if (!async_data_write_receive(&callid, NULL)) {
|
|---|
| 1672 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1673 | return EINVAL;
|
|---|
| 1674 | }
|
|---|
| 1675 |
|
|---|
| 1676 | aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
|
|---|
| 1677 | dataptr);
|
|---|
| 1678 | if (msg == 0) {
|
|---|
| 1679 | ipc_answer_0(callid, EINVAL);
|
|---|
| 1680 | return EINVAL;
|
|---|
| 1681 | }
|
|---|
| 1682 |
|
|---|
| 1683 | int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
|
|---|
| 1684 | IPC_FF_ROUTE_FROM_ME);
|
|---|
| 1685 | if (retval != EOK) {
|
|---|
| 1686 | async_wait_for(msg, NULL);
|
|---|
| 1687 | ipc_answer_0(callid, retval);
|
|---|
| 1688 | return retval;
|
|---|
| 1689 | }
|
|---|
| 1690 |
|
|---|
| 1691 | sysarg_t rc;
|
|---|
| 1692 | async_wait_for(msg, &rc);
|
|---|
| 1693 |
|
|---|
| 1694 | return (int) rc;
|
|---|
| 1695 | }
|
|---|
| 1696 |
|
|---|
| 1697 | /** @}
|
|---|
| 1698 | */
|
|---|