| 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 of the synchronization
|
|---|
| 44 | * problems.
|
|---|
| 45 | *
|
|---|
| 46 | * Example of use (pseudo C):
|
|---|
| 47 | *
|
|---|
| 48 | * 1) Multithreaded client application
|
|---|
| 49 | *
|
|---|
| 50 | * fibril_create(fibril1, ...);
|
|---|
| 51 | * fibril_create(fibril2, ...);
|
|---|
| 52 | * ...
|
|---|
| 53 | *
|
|---|
| 54 | * int fibril1(void *arg)
|
|---|
| 55 | * {
|
|---|
| 56 | * conn = async_connect_me_to(...);
|
|---|
| 57 | *
|
|---|
| 58 | * exch = async_exchange_begin(conn);
|
|---|
| 59 | * c1 = async_send(exch);
|
|---|
| 60 | * async_exchange_end(exch);
|
|---|
| 61 | *
|
|---|
| 62 | * exch = async_exchange_begin(conn);
|
|---|
| 63 | * c2 = async_send(exch);
|
|---|
| 64 | * async_exchange_end(exch);
|
|---|
| 65 | *
|
|---|
| 66 | * async_wait_for(c1);
|
|---|
| 67 | * async_wait_for(c2);
|
|---|
| 68 | * ...
|
|---|
| 69 | * }
|
|---|
| 70 | *
|
|---|
| 71 | *
|
|---|
| 72 | * 2) Multithreaded server application
|
|---|
| 73 | *
|
|---|
| 74 | * main()
|
|---|
| 75 | * {
|
|---|
| 76 | * async_manager();
|
|---|
| 77 | * }
|
|---|
| 78 | *
|
|---|
| 79 | * my_client_connection(icallid, *icall)
|
|---|
| 80 | * {
|
|---|
| 81 | * if (want_refuse) {
|
|---|
| 82 | * async_answer_0(icallid, ELIMIT);
|
|---|
| 83 | * return;
|
|---|
| 84 | * }
|
|---|
| 85 | * async_answer_0(icallid, EOK);
|
|---|
| 86 | *
|
|---|
| 87 | * callid = async_get_call(&call);
|
|---|
| 88 | * somehow_handle_the_call(callid, call);
|
|---|
| 89 | * async_answer_2(callid, 1, 2, 3);
|
|---|
| 90 | *
|
|---|
| 91 | * callid = async_get_call(&call);
|
|---|
| 92 | * ...
|
|---|
| 93 | * }
|
|---|
| 94 | *
|
|---|
| 95 | */
|
|---|
| 96 |
|
|---|
| 97 | #define LIBC_ASYNC_C_
|
|---|
| 98 | #include <ipc/ipc.h>
|
|---|
| 99 | #include <async.h>
|
|---|
| 100 | #include "private/async.h"
|
|---|
| 101 | #undef LIBC_ASYNC_C_
|
|---|
| 102 |
|
|---|
| 103 | #include <futex.h>
|
|---|
| 104 | #include <fibril.h>
|
|---|
| 105 | #include <adt/hash_table.h>
|
|---|
| 106 | #include <adt/list.h>
|
|---|
| 107 | #include <assert.h>
|
|---|
| 108 | #include <errno.h>
|
|---|
| 109 | #include <sys/time.h>
|
|---|
| 110 | #include <libarch/barrier.h>
|
|---|
| 111 | #include <bool.h>
|
|---|
| 112 | #include <malloc.h>
|
|---|
| 113 | #include <mem.h>
|
|---|
| 114 | #include <stdlib.h>
|
|---|
| 115 | #include <macros.h>
|
|---|
| 116 | #include "private/libc.h"
|
|---|
| 117 |
|
|---|
| 118 | #define CLIENT_HASH_TABLE_BUCKETS 32
|
|---|
| 119 | #define CONN_HASH_TABLE_BUCKETS 32
|
|---|
| 120 |
|
|---|
| 121 | /** Session data */
|
|---|
| 122 | struct async_sess {
|
|---|
| 123 | /** List of inactive exchanges */
|
|---|
| 124 | list_t exch_list;
|
|---|
| 125 |
|
|---|
| 126 | /** Exchange management style */
|
|---|
| 127 | exch_mgmt_t mgmt;
|
|---|
| 128 |
|
|---|
| 129 | /** Session identification */
|
|---|
| 130 | int phone;
|
|---|
| 131 |
|
|---|
| 132 | /** First clone connection argument */
|
|---|
| 133 | sysarg_t arg1;
|
|---|
| 134 |
|
|---|
| 135 | /** Second clone connection argument */
|
|---|
| 136 | sysarg_t arg2;
|
|---|
| 137 |
|
|---|
| 138 | /** Third clone connection argument */
|
|---|
| 139 | sysarg_t arg3;
|
|---|
| 140 |
|
|---|
| 141 | /** Exchange mutex */
|
|---|
| 142 | fibril_mutex_t mutex;
|
|---|
| 143 |
|
|---|
| 144 | /** Number of opened exchanges */
|
|---|
| 145 | atomic_t refcnt;
|
|---|
| 146 |
|
|---|
| 147 | /** Mutex for stateful connections */
|
|---|
| 148 | fibril_mutex_t remote_state_mtx;
|
|---|
| 149 |
|
|---|
| 150 | /** Data for stateful connections */
|
|---|
| 151 | void *remote_state_data;
|
|---|
| 152 | };
|
|---|
| 153 |
|
|---|
| 154 | /** Exchange data */
|
|---|
| 155 | struct async_exch {
|
|---|
| 156 | /** Link into list of inactive exchanges */
|
|---|
| 157 | link_t sess_link;
|
|---|
| 158 |
|
|---|
| 159 | /** Link into global list of inactive exchanges */
|
|---|
| 160 | link_t global_link;
|
|---|
| 161 |
|
|---|
| 162 | /** Session pointer */
|
|---|
| 163 | async_sess_t *sess;
|
|---|
| 164 |
|
|---|
| 165 | /** Exchange identification */
|
|---|
| 166 | int phone;
|
|---|
| 167 | };
|
|---|
| 168 |
|
|---|
| 169 | /** Async framework global futex */
|
|---|
| 170 | atomic_t async_futex = FUTEX_INITIALIZER;
|
|---|
| 171 |
|
|---|
| 172 | /** Number of threads waiting for IPC in the kernel. */
|
|---|
| 173 | atomic_t threads_in_ipc_wait = { 0 };
|
|---|
| 174 |
|
|---|
| 175 | /** Naming service session */
|
|---|
| 176 | async_sess_t *session_ns;
|
|---|
| 177 |
|
|---|
| 178 | /** Call data */
|
|---|
| 179 | typedef struct {
|
|---|
| 180 | link_t link;
|
|---|
| 181 |
|
|---|
| 182 | ipc_callid_t callid;
|
|---|
| 183 | ipc_call_t call;
|
|---|
| 184 | } msg_t;
|
|---|
| 185 |
|
|---|
| 186 | /** Message data */
|
|---|
| 187 | typedef struct {
|
|---|
| 188 | awaiter_t wdata;
|
|---|
| 189 |
|
|---|
| 190 | /** If reply was received. */
|
|---|
| 191 | bool done;
|
|---|
| 192 |
|
|---|
| 193 | /** If the message / reply should be discarded on arrival. */
|
|---|
| 194 | bool forget;
|
|---|
| 195 |
|
|---|
| 196 | /** If already destroyed. */
|
|---|
| 197 | bool destroyed;
|
|---|
| 198 |
|
|---|
| 199 | /** Pointer to where the answer data is stored. */
|
|---|
| 200 | ipc_call_t *dataptr;
|
|---|
| 201 |
|
|---|
| 202 | sysarg_t retval;
|
|---|
| 203 | } amsg_t;
|
|---|
| 204 |
|
|---|
| 205 | /* Client connection data */
|
|---|
| 206 | typedef struct {
|
|---|
| 207 | link_t link;
|
|---|
| 208 |
|
|---|
| 209 | task_id_t in_task_id;
|
|---|
| 210 | atomic_t refcnt;
|
|---|
| 211 | void *data;
|
|---|
| 212 | } client_t;
|
|---|
| 213 |
|
|---|
| 214 | /* Server connection data */
|
|---|
| 215 | typedef struct {
|
|---|
| 216 | awaiter_t wdata;
|
|---|
| 217 |
|
|---|
| 218 | /** Hash table link. */
|
|---|
| 219 | link_t link;
|
|---|
| 220 |
|
|---|
| 221 | /** Incoming client task ID. */
|
|---|
| 222 | task_id_t in_task_id;
|
|---|
| 223 |
|
|---|
| 224 | /** Incoming phone hash. */
|
|---|
| 225 | sysarg_t in_phone_hash;
|
|---|
| 226 |
|
|---|
| 227 | /** Link to the client tracking structure. */
|
|---|
| 228 | client_t *client;
|
|---|
| 229 |
|
|---|
| 230 | /** Messages that should be delivered to this fibril. */
|
|---|
| 231 | list_t msg_queue;
|
|---|
| 232 |
|
|---|
| 233 | /** Identification of the opening call. */
|
|---|
| 234 | ipc_callid_t callid;
|
|---|
| 235 | /** Call data of the opening call. */
|
|---|
| 236 | ipc_call_t call;
|
|---|
| 237 | /** Local argument or NULL if none. */
|
|---|
| 238 | void *carg;
|
|---|
| 239 |
|
|---|
| 240 | /** Identification of the closing call. */
|
|---|
| 241 | ipc_callid_t close_callid;
|
|---|
| 242 |
|
|---|
| 243 | /** Fibril function that will be used to handle the connection. */
|
|---|
| 244 | async_client_conn_t cfibril;
|
|---|
| 245 | } connection_t;
|
|---|
| 246 |
|
|---|
| 247 | /** Identifier of the incoming connection handled by the current fibril. */
|
|---|
| 248 | static fibril_local connection_t *fibril_connection;
|
|---|
| 249 |
|
|---|
| 250 | static void to_event_initialize(to_event_t *to)
|
|---|
| 251 | {
|
|---|
| 252 | struct timeval tv = { 0, 0 };
|
|---|
| 253 |
|
|---|
| 254 | to->inlist = false;
|
|---|
| 255 | to->occurred = false;
|
|---|
| 256 | link_initialize(&to->link);
|
|---|
| 257 | to->expires = tv;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | static void wu_event_initialize(wu_event_t *wu)
|
|---|
| 261 | {
|
|---|
| 262 | wu->inlist = false;
|
|---|
| 263 | link_initialize(&wu->link);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | void awaiter_initialize(awaiter_t *aw)
|
|---|
| 267 | {
|
|---|
| 268 | aw->fid = 0;
|
|---|
| 269 | aw->active = false;
|
|---|
| 270 | to_event_initialize(&aw->to_event);
|
|---|
| 271 | wu_event_initialize(&aw->wu_event);
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | static amsg_t *amsg_create(void)
|
|---|
| 275 | {
|
|---|
| 276 | amsg_t *msg;
|
|---|
| 277 |
|
|---|
| 278 | msg = malloc(sizeof(amsg_t));
|
|---|
| 279 | if (msg) {
|
|---|
| 280 | msg->done = false;
|
|---|
| 281 | msg->forget = false;
|
|---|
| 282 | msg->destroyed = false;
|
|---|
| 283 | msg->dataptr = NULL;
|
|---|
| 284 | msg->retval = (sysarg_t) EINVAL;
|
|---|
| 285 | awaiter_initialize(&msg->wdata);
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | return msg;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | static void amsg_destroy(amsg_t *msg)
|
|---|
| 292 | {
|
|---|
| 293 | assert(!msg->destroyed);
|
|---|
| 294 | msg->destroyed = true;
|
|---|
| 295 | free(msg);
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | static void *default_client_data_constructor(void)
|
|---|
| 299 | {
|
|---|
| 300 | return NULL;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | static void default_client_data_destructor(void *data)
|
|---|
| 304 | {
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | static async_client_data_ctor_t async_client_data_create =
|
|---|
| 308 | default_client_data_constructor;
|
|---|
| 309 | static async_client_data_dtor_t async_client_data_destroy =
|
|---|
| 310 | default_client_data_destructor;
|
|---|
| 311 |
|
|---|
| 312 | void async_set_client_data_constructor(async_client_data_ctor_t ctor)
|
|---|
| 313 | {
|
|---|
| 314 | assert(async_client_data_create == default_client_data_constructor);
|
|---|
| 315 | async_client_data_create = ctor;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | void async_set_client_data_destructor(async_client_data_dtor_t dtor)
|
|---|
| 319 | {
|
|---|
| 320 | assert(async_client_data_destroy == default_client_data_destructor);
|
|---|
| 321 | async_client_data_destroy = dtor;
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | /** Default fibril function that gets called to handle new connection.
|
|---|
| 325 | *
|
|---|
| 326 | * This function is defined as a weak symbol - to be redefined in user code.
|
|---|
| 327 | *
|
|---|
| 328 | * @param callid Hash of the incoming call.
|
|---|
| 329 | * @param call Data of the incoming call.
|
|---|
| 330 | * @param arg Local argument
|
|---|
| 331 | *
|
|---|
| 332 | */
|
|---|
| 333 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call,
|
|---|
| 334 | void *arg)
|
|---|
| 335 | {
|
|---|
| 336 | ipc_answer_0(callid, ENOENT);
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | /** Default fibril function that gets called to handle interrupt notifications.
|
|---|
| 340 | *
|
|---|
| 341 | * This function is defined as a weak symbol - to be redefined in user code.
|
|---|
| 342 | *
|
|---|
| 343 | * @param callid Hash of the incoming call.
|
|---|
| 344 | * @param call Data of the incoming call.
|
|---|
| 345 | * @param arg Local argument.
|
|---|
| 346 | *
|
|---|
| 347 | */
|
|---|
| 348 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 349 | {
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | static async_client_conn_t client_connection = default_client_connection;
|
|---|
| 353 | static async_interrupt_handler_t interrupt_received = default_interrupt_received;
|
|---|
| 354 |
|
|---|
| 355 | /** Setter for client_connection function pointer.
|
|---|
| 356 | *
|
|---|
| 357 | * @param conn Function that will implement a new connection fibril.
|
|---|
| 358 | *
|
|---|
| 359 | */
|
|---|
| 360 | void async_set_client_connection(async_client_conn_t conn)
|
|---|
| 361 | {
|
|---|
| 362 | assert(client_connection == default_client_connection);
|
|---|
| 363 | client_connection = conn;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | /** Setter for interrupt_received function pointer.
|
|---|
| 367 | *
|
|---|
| 368 | * @param intr Function that will implement a new interrupt
|
|---|
| 369 | * notification fibril.
|
|---|
| 370 | */
|
|---|
| 371 | void async_set_interrupt_received(async_interrupt_handler_t intr)
|
|---|
| 372 | {
|
|---|
| 373 | interrupt_received = intr;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | /** Mutex protecting inactive_exch_list and avail_phone_cv.
|
|---|
| 377 | *
|
|---|
| 378 | */
|
|---|
| 379 | static FIBRIL_MUTEX_INITIALIZE(async_sess_mutex);
|
|---|
| 380 |
|
|---|
| 381 | /** List of all currently inactive exchanges.
|
|---|
| 382 | *
|
|---|
| 383 | */
|
|---|
| 384 | static LIST_INITIALIZE(inactive_exch_list);
|
|---|
| 385 |
|
|---|
| 386 | /** Condition variable to wait for a phone to become available.
|
|---|
| 387 | *
|
|---|
| 388 | */
|
|---|
| 389 | static FIBRIL_CONDVAR_INITIALIZE(avail_phone_cv);
|
|---|
| 390 |
|
|---|
| 391 | static hash_table_t client_hash_table;
|
|---|
| 392 | static hash_table_t conn_hash_table;
|
|---|
| 393 | static LIST_INITIALIZE(timeout_list);
|
|---|
| 394 |
|
|---|
| 395 | static hash_index_t client_hash(unsigned long key[])
|
|---|
| 396 | {
|
|---|
| 397 | assert(key);
|
|---|
| 398 |
|
|---|
| 399 | return (((key[0]) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
|---|
| 403 | {
|
|---|
| 404 | assert(key);
|
|---|
| 405 | assert(keys == 2);
|
|---|
| 406 | assert(item);
|
|---|
| 407 |
|
|---|
| 408 | client_t *client = hash_table_get_instance(item, client_t, link);
|
|---|
| 409 | return (key[0] == LOWER32(client->in_task_id) &&
|
|---|
| 410 | (key[1] == UPPER32(client->in_task_id)));
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | static void client_remove(link_t *item)
|
|---|
| 414 | {
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | /** Operations for the client hash table. */
|
|---|
| 418 | static hash_table_operations_t client_hash_table_ops = {
|
|---|
| 419 | .hash = client_hash,
|
|---|
| 420 | .compare = client_compare,
|
|---|
| 421 | .remove_callback = client_remove
|
|---|
| 422 | };
|
|---|
| 423 |
|
|---|
| 424 | /** Compute hash into the connection hash table based on the source phone hash.
|
|---|
| 425 | *
|
|---|
| 426 | * @param key Pointer to source phone hash.
|
|---|
| 427 | *
|
|---|
| 428 | * @return Index into the connection hash table.
|
|---|
| 429 | *
|
|---|
| 430 | */
|
|---|
| 431 | static hash_index_t conn_hash(unsigned long key[])
|
|---|
| 432 | {
|
|---|
| 433 | assert(key);
|
|---|
| 434 |
|
|---|
| 435 | return (((key[0]) >> 4) % CONN_HASH_TABLE_BUCKETS);
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | /** Compare hash table item with a key.
|
|---|
| 439 | *
|
|---|
| 440 | * @param key Array containing the source phone hash as the only item.
|
|---|
| 441 | * @param keys Expected 1 but ignored.
|
|---|
| 442 | * @param item Connection hash table item.
|
|---|
| 443 | *
|
|---|
| 444 | * @return True on match, false otherwise.
|
|---|
| 445 | *
|
|---|
| 446 | */
|
|---|
| 447 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
|---|
| 448 | {
|
|---|
| 449 | assert(key);
|
|---|
| 450 | assert(item);
|
|---|
| 451 |
|
|---|
| 452 | connection_t *conn = hash_table_get_instance(item, connection_t, link);
|
|---|
| 453 | return (key[0] == conn->in_phone_hash);
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | static void conn_remove(link_t *item)
|
|---|
| 457 | {
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | /** Operations for the connection hash table. */
|
|---|
| 461 | static hash_table_operations_t conn_hash_table_ops = {
|
|---|
| 462 | .hash = conn_hash,
|
|---|
| 463 | .compare = conn_compare,
|
|---|
| 464 | .remove_callback = conn_remove
|
|---|
| 465 | };
|
|---|
| 466 |
|
|---|
| 467 | /** Sort in current fibril's timeout request.
|
|---|
| 468 | *
|
|---|
| 469 | * @param wd Wait data of the current fibril.
|
|---|
| 470 | *
|
|---|
| 471 | */
|
|---|
| 472 | void async_insert_timeout(awaiter_t *wd)
|
|---|
| 473 | {
|
|---|
| 474 | assert(wd);
|
|---|
| 475 |
|
|---|
| 476 | wd->to_event.occurred = false;
|
|---|
| 477 | wd->to_event.inlist = true;
|
|---|
| 478 |
|
|---|
| 479 | link_t *tmp = timeout_list.head.next;
|
|---|
| 480 | while (tmp != &timeout_list.head) {
|
|---|
| 481 | awaiter_t *cur
|
|---|
| 482 | = list_get_instance(tmp, awaiter_t, to_event.link);
|
|---|
| 483 |
|
|---|
| 484 | if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
|
|---|
| 485 | break;
|
|---|
| 486 |
|
|---|
| 487 | tmp = tmp->next;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | list_insert_before(&wd->to_event.link, tmp);
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | /** Try to route a call to an appropriate connection fibril.
|
|---|
| 494 | *
|
|---|
| 495 | * If the proper connection fibril is found, a message with the call is added to
|
|---|
| 496 | * its message queue. If the fibril was not active, it is activated and all
|
|---|
| 497 | * timeouts are unregistered.
|
|---|
| 498 | *
|
|---|
| 499 | * @param callid Hash of the incoming call.
|
|---|
| 500 | * @param call Data of the incoming call.
|
|---|
| 501 | *
|
|---|
| 502 | * @return False if the call doesn't match any connection.
|
|---|
| 503 | * @return True if the call was passed to the respective connection fibril.
|
|---|
| 504 | *
|
|---|
| 505 | */
|
|---|
| 506 | static bool route_call(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 507 | {
|
|---|
| 508 | assert(call);
|
|---|
| 509 |
|
|---|
| 510 | futex_down(&async_futex);
|
|---|
| 511 |
|
|---|
| 512 | unsigned long key = call->in_phone_hash;
|
|---|
| 513 | link_t *hlp = hash_table_find(&conn_hash_table, &key);
|
|---|
| 514 |
|
|---|
| 515 | if (!hlp) {
|
|---|
| 516 | futex_up(&async_futex);
|
|---|
| 517 | return false;
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
|
|---|
| 521 |
|
|---|
| 522 | msg_t *msg = malloc(sizeof(*msg));
|
|---|
| 523 | if (!msg) {
|
|---|
| 524 | futex_up(&async_futex);
|
|---|
| 525 | return false;
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | msg->callid = callid;
|
|---|
| 529 | msg->call = *call;
|
|---|
| 530 | list_append(&msg->link, &conn->msg_queue);
|
|---|
| 531 |
|
|---|
| 532 | if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
|
|---|
| 533 | conn->close_callid = callid;
|
|---|
| 534 |
|
|---|
| 535 | /* If the connection fibril is waiting for an event, activate it */
|
|---|
| 536 | if (!conn->wdata.active) {
|
|---|
| 537 |
|
|---|
| 538 | /* If in timeout list, remove it */
|
|---|
| 539 | if (conn->wdata.to_event.inlist) {
|
|---|
| 540 | conn->wdata.to_event.inlist = false;
|
|---|
| 541 | list_remove(&conn->wdata.to_event.link);
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | conn->wdata.active = true;
|
|---|
| 545 | fibril_add_ready(conn->wdata.fid);
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | futex_up(&async_futex);
|
|---|
| 549 | return true;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | /** Notification fibril.
|
|---|
| 553 | *
|
|---|
| 554 | * When a notification arrives, a fibril with this implementing function is
|
|---|
| 555 | * created. It calls interrupt_received() and does the final cleanup.
|
|---|
| 556 | *
|
|---|
| 557 | * @param arg Message structure pointer.
|
|---|
| 558 | *
|
|---|
| 559 | * @return Always zero.
|
|---|
| 560 | *
|
|---|
| 561 | */
|
|---|
| 562 | static int notification_fibril(void *arg)
|
|---|
| 563 | {
|
|---|
| 564 | assert(arg);
|
|---|
| 565 |
|
|---|
| 566 | msg_t *msg = (msg_t *) arg;
|
|---|
| 567 | interrupt_received(msg->callid, &msg->call);
|
|---|
| 568 |
|
|---|
| 569 | free(msg);
|
|---|
| 570 | return 0;
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | /** Process interrupt notification.
|
|---|
| 574 | *
|
|---|
| 575 | * A new fibril is created which would process the notification.
|
|---|
| 576 | *
|
|---|
| 577 | * @param callid Hash of the incoming call.
|
|---|
| 578 | * @param call Data of the incoming call.
|
|---|
| 579 | *
|
|---|
| 580 | * @return False if an error occured.
|
|---|
| 581 | * True if the call was passed to the notification fibril.
|
|---|
| 582 | *
|
|---|
| 583 | */
|
|---|
| 584 | static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 585 | {
|
|---|
| 586 | assert(call);
|
|---|
| 587 |
|
|---|
| 588 | futex_down(&async_futex);
|
|---|
| 589 |
|
|---|
| 590 | msg_t *msg = malloc(sizeof(*msg));
|
|---|
| 591 | if (!msg) {
|
|---|
| 592 | futex_up(&async_futex);
|
|---|
| 593 | return false;
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | msg->callid = callid;
|
|---|
| 597 | msg->call = *call;
|
|---|
| 598 |
|
|---|
| 599 | fid_t fid = fibril_create(notification_fibril, msg);
|
|---|
| 600 | if (fid == 0) {
|
|---|
| 601 | free(msg);
|
|---|
| 602 | futex_up(&async_futex);
|
|---|
| 603 | return false;
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | fibril_add_ready(fid);
|
|---|
| 607 |
|
|---|
| 608 | futex_up(&async_futex);
|
|---|
| 609 | return true;
|
|---|
| 610 | }
|
|---|
| 611 |
|
|---|
| 612 | /** Return new incoming message for the current (fibril-local) connection.
|
|---|
| 613 | *
|
|---|
| 614 | * @param call Storage where the incoming call data will be stored.
|
|---|
| 615 | * @param usecs Timeout in microseconds. Zero denotes no timeout.
|
|---|
| 616 | *
|
|---|
| 617 | * @return If no timeout was specified, then a hash of the
|
|---|
| 618 | * incoming call is returned. If a timeout is specified,
|
|---|
| 619 | * then a hash of the incoming call is returned unless
|
|---|
| 620 | * the timeout expires prior to receiving a message. In
|
|---|
| 621 | * that case zero is returned.
|
|---|
| 622 | *
|
|---|
| 623 | */
|
|---|
| 624 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
|
|---|
| 625 | {
|
|---|
| 626 | assert(call);
|
|---|
| 627 | assert(fibril_connection);
|
|---|
| 628 |
|
|---|
| 629 | /* Why doing this?
|
|---|
| 630 | * GCC 4.1.0 coughs on fibril_connection-> dereference.
|
|---|
| 631 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
|
|---|
| 632 | * I would never expect to find so many errors in
|
|---|
| 633 | * a compiler.
|
|---|
| 634 | */
|
|---|
| 635 | connection_t *conn = fibril_connection;
|
|---|
| 636 |
|
|---|
| 637 | futex_down(&async_futex);
|
|---|
| 638 |
|
|---|
| 639 | if (usecs) {
|
|---|
| 640 | getuptime(&conn->wdata.to_event.expires);
|
|---|
| 641 | tv_add(&conn->wdata.to_event.expires, usecs);
|
|---|
| 642 | } else
|
|---|
| 643 | conn->wdata.to_event.inlist = false;
|
|---|
| 644 |
|
|---|
| 645 | /* If nothing in queue, wait until something arrives */
|
|---|
| 646 | while (list_empty(&conn->msg_queue)) {
|
|---|
| 647 | if (conn->close_callid) {
|
|---|
| 648 | /*
|
|---|
| 649 | * Handle the case when the connection was already
|
|---|
| 650 | * closed by the client but the server did not notice
|
|---|
| 651 | * the first IPC_M_PHONE_HUNGUP call and continues to
|
|---|
| 652 | * call async_get_call_timeout(). Repeat
|
|---|
| 653 | * IPC_M_PHONE_HUNGUP until the caller notices.
|
|---|
| 654 | */
|
|---|
| 655 | memset(call, 0, sizeof(ipc_call_t));
|
|---|
| 656 | IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
|
|---|
| 657 | futex_up(&async_futex);
|
|---|
| 658 | return conn->close_callid;
|
|---|
| 659 | }
|
|---|
| 660 |
|
|---|
| 661 | if (usecs)
|
|---|
| 662 | async_insert_timeout(&conn->wdata);
|
|---|
| 663 |
|
|---|
| 664 | conn->wdata.active = false;
|
|---|
| 665 |
|
|---|
| 666 | /*
|
|---|
| 667 | * Note: the current fibril will be rescheduled either due to a
|
|---|
| 668 | * timeout or due to an arriving message destined to it. In the
|
|---|
| 669 | * former case, handle_expired_timeouts() and, in the latter
|
|---|
| 670 | * case, route_call() will perform the wakeup.
|
|---|
| 671 | */
|
|---|
| 672 | fibril_switch(FIBRIL_TO_MANAGER);
|
|---|
| 673 |
|
|---|
| 674 | /*
|
|---|
| 675 | * Futex is up after getting back from async_manager.
|
|---|
| 676 | * Get it again.
|
|---|
| 677 | */
|
|---|
| 678 | futex_down(&async_futex);
|
|---|
| 679 | if ((usecs) && (conn->wdata.to_event.occurred)
|
|---|
| 680 | && (list_empty(&conn->msg_queue))) {
|
|---|
| 681 | /* If we timed out -> exit */
|
|---|
| 682 | futex_up(&async_futex);
|
|---|
| 683 | return 0;
|
|---|
| 684 | }
|
|---|
| 685 | }
|
|---|
| 686 |
|
|---|
| 687 | msg_t *msg = list_get_instance(list_first(&conn->msg_queue), msg_t, link);
|
|---|
| 688 | list_remove(&msg->link);
|
|---|
| 689 |
|
|---|
| 690 | ipc_callid_t callid = msg->callid;
|
|---|
| 691 | *call = msg->call;
|
|---|
| 692 | free(msg);
|
|---|
| 693 |
|
|---|
| 694 | futex_up(&async_futex);
|
|---|
| 695 | return callid;
|
|---|
| 696 | }
|
|---|
| 697 |
|
|---|
| 698 | static client_t *async_client_get(task_id_t client_id, bool create)
|
|---|
| 699 | {
|
|---|
| 700 | unsigned long key[2] = {
|
|---|
| 701 | LOWER32(client_id),
|
|---|
| 702 | UPPER32(client_id),
|
|---|
| 703 | };
|
|---|
| 704 | client_t *client = NULL;
|
|---|
| 705 |
|
|---|
| 706 | futex_down(&async_futex);
|
|---|
| 707 | link_t *lnk = hash_table_find(&client_hash_table, key);
|
|---|
| 708 | if (lnk) {
|
|---|
| 709 | client = hash_table_get_instance(lnk, client_t, link);
|
|---|
| 710 | atomic_inc(&client->refcnt);
|
|---|
| 711 | } else if (create) {
|
|---|
| 712 | client = malloc(sizeof(client_t));
|
|---|
| 713 | if (client) {
|
|---|
| 714 | client->in_task_id = client_id;
|
|---|
| 715 | client->data = async_client_data_create();
|
|---|
| 716 |
|
|---|
| 717 | atomic_set(&client->refcnt, 1);
|
|---|
| 718 | hash_table_insert(&client_hash_table, key, &client->link);
|
|---|
| 719 | }
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | futex_up(&async_futex);
|
|---|
| 723 | return client;
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | static void async_client_put(client_t *client)
|
|---|
| 727 | {
|
|---|
| 728 | bool destroy;
|
|---|
| 729 | unsigned long key[2] = {
|
|---|
| 730 | LOWER32(client->in_task_id),
|
|---|
| 731 | UPPER32(client->in_task_id)
|
|---|
| 732 | };
|
|---|
| 733 |
|
|---|
| 734 | futex_down(&async_futex);
|
|---|
| 735 |
|
|---|
| 736 | if (atomic_predec(&client->refcnt) == 0) {
|
|---|
| 737 | hash_table_remove(&client_hash_table, key, 2);
|
|---|
| 738 | destroy = true;
|
|---|
| 739 | } else
|
|---|
| 740 | destroy = false;
|
|---|
| 741 |
|
|---|
| 742 | futex_up(&async_futex);
|
|---|
| 743 |
|
|---|
| 744 | if (destroy) {
|
|---|
| 745 | if (client->data)
|
|---|
| 746 | async_client_data_destroy(client->data);
|
|---|
| 747 |
|
|---|
| 748 | free(client);
|
|---|
| 749 | }
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | void *async_get_client_data(void)
|
|---|
| 753 | {
|
|---|
| 754 | assert(fibril_connection);
|
|---|
| 755 | return fibril_connection->client->data;
|
|---|
| 756 | }
|
|---|
| 757 |
|
|---|
| 758 | void *async_get_client_data_by_id(task_id_t client_id)
|
|---|
| 759 | {
|
|---|
| 760 | client_t *client = async_client_get(client_id, false);
|
|---|
| 761 | if (!client)
|
|---|
| 762 | return NULL;
|
|---|
| 763 | if (!client->data) {
|
|---|
| 764 | async_client_put(client);
|
|---|
| 765 | return NULL;
|
|---|
| 766 | }
|
|---|
| 767 |
|
|---|
| 768 | return client->data;
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | void async_put_client_data_by_id(task_id_t client_id)
|
|---|
| 772 | {
|
|---|
| 773 | client_t *client = async_client_get(client_id, false);
|
|---|
| 774 |
|
|---|
| 775 | assert(client);
|
|---|
| 776 | assert(client->data);
|
|---|
| 777 |
|
|---|
| 778 | /* Drop the reference we got in async_get_client_data_by_hash(). */
|
|---|
| 779 | async_client_put(client);
|
|---|
| 780 |
|
|---|
| 781 | /* Drop our own reference we got at the beginning of this function. */
|
|---|
| 782 | async_client_put(client);
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | /** Wrapper for client connection fibril.
|
|---|
| 786 | *
|
|---|
| 787 | * When a new connection arrives, a fibril with this implementing function is
|
|---|
| 788 | * created. It calls client_connection() and does the final cleanup.
|
|---|
| 789 | *
|
|---|
| 790 | * @param arg Connection structure pointer.
|
|---|
| 791 | *
|
|---|
| 792 | * @return Always zero.
|
|---|
| 793 | *
|
|---|
| 794 | */
|
|---|
| 795 | static int connection_fibril(void *arg)
|
|---|
| 796 | {
|
|---|
| 797 | assert(arg);
|
|---|
| 798 |
|
|---|
| 799 | /*
|
|---|
| 800 | * Setup fibril-local connection pointer.
|
|---|
| 801 | */
|
|---|
| 802 | fibril_connection = (connection_t *) arg;
|
|---|
| 803 |
|
|---|
| 804 | /*
|
|---|
| 805 | * Add our reference for the current connection in the client task
|
|---|
| 806 | * tracking structure. If this is the first reference, create and
|
|---|
| 807 | * hash in a new tracking structure.
|
|---|
| 808 | */
|
|---|
| 809 |
|
|---|
| 810 | client_t *client = async_client_get(fibril_connection->in_task_id, true);
|
|---|
| 811 | if (!client) {
|
|---|
| 812 | ipc_answer_0(fibril_connection->callid, ENOMEM);
|
|---|
| 813 | return 0;
|
|---|
| 814 | }
|
|---|
| 815 |
|
|---|
| 816 | fibril_connection->client = client;
|
|---|
| 817 |
|
|---|
| 818 | /*
|
|---|
| 819 | * Call the connection handler function.
|
|---|
| 820 | */
|
|---|
| 821 | fibril_connection->cfibril(fibril_connection->callid,
|
|---|
| 822 | &fibril_connection->call, fibril_connection->carg);
|
|---|
| 823 |
|
|---|
| 824 | /*
|
|---|
| 825 | * Remove the reference for this client task connection.
|
|---|
| 826 | */
|
|---|
| 827 | async_client_put(client);
|
|---|
| 828 |
|
|---|
| 829 | /*
|
|---|
| 830 | * Remove myself from the connection hash table.
|
|---|
| 831 | */
|
|---|
| 832 | futex_down(&async_futex);
|
|---|
| 833 | unsigned long key = fibril_connection->in_phone_hash;
|
|---|
| 834 | hash_table_remove(&conn_hash_table, &key, 1);
|
|---|
| 835 | futex_up(&async_futex);
|
|---|
| 836 |
|
|---|
| 837 | /*
|
|---|
| 838 | * Answer all remaining messages with EHANGUP.
|
|---|
| 839 | */
|
|---|
| 840 | while (!list_empty(&fibril_connection->msg_queue)) {
|
|---|
| 841 | msg_t *msg =
|
|---|
| 842 | list_get_instance(list_first(&fibril_connection->msg_queue),
|
|---|
| 843 | msg_t, link);
|
|---|
| 844 |
|
|---|
| 845 | list_remove(&msg->link);
|
|---|
| 846 | ipc_answer_0(msg->callid, EHANGUP);
|
|---|
| 847 | free(msg);
|
|---|
| 848 | }
|
|---|
| 849 |
|
|---|
| 850 | /*
|
|---|
| 851 | * If the connection was hung-up, answer the last call,
|
|---|
| 852 | * i.e. IPC_M_PHONE_HUNGUP.
|
|---|
| 853 | */
|
|---|
| 854 | if (fibril_connection->close_callid)
|
|---|
| 855 | ipc_answer_0(fibril_connection->close_callid, EOK);
|
|---|
| 856 |
|
|---|
| 857 | free(fibril_connection);
|
|---|
| 858 | return 0;
|
|---|
| 859 | }
|
|---|
| 860 |
|
|---|
| 861 | /** Create a new fibril for a new connection.
|
|---|
| 862 | *
|
|---|
| 863 | * Create new fibril for connection, fill in connection structures and insert
|
|---|
| 864 | * it into the hash table, so that later we can easily do routing of messages to
|
|---|
| 865 | * particular fibrils.
|
|---|
| 866 | *
|
|---|
| 867 | * @param in_task_id Identification of the incoming connection.
|
|---|
| 868 | * @param in_phone_hash Identification of the incoming connection.
|
|---|
| 869 | * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
|
|---|
| 870 | * If callid is zero, the connection was opened by
|
|---|
| 871 | * accepting the IPC_M_CONNECT_TO_ME call and this function
|
|---|
| 872 | * is called directly by the server.
|
|---|
| 873 | * @param call Call data of the opening call.
|
|---|
| 874 | * @param cfibril Fibril function that should be called upon opening the
|
|---|
| 875 | * connection.
|
|---|
| 876 | * @param carg Extra argument to pass to the connection fibril
|
|---|
| 877 | *
|
|---|
| 878 | * @return New fibril id or NULL on failure.
|
|---|
| 879 | *
|
|---|
| 880 | */
|
|---|
| 881 | fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
|
|---|
| 882 | ipc_callid_t callid, ipc_call_t *call,
|
|---|
| 883 | async_client_conn_t cfibril, void *carg)
|
|---|
| 884 | {
|
|---|
| 885 | connection_t *conn = malloc(sizeof(*conn));
|
|---|
| 886 | if (!conn) {
|
|---|
| 887 | if (callid)
|
|---|
| 888 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 889 |
|
|---|
| 890 | return (uintptr_t) NULL;
|
|---|
| 891 | }
|
|---|
| 892 |
|
|---|
| 893 | conn->in_task_id = in_task_id;
|
|---|
| 894 | conn->in_phone_hash = in_phone_hash;
|
|---|
| 895 | list_initialize(&conn->msg_queue);
|
|---|
| 896 | conn->callid = callid;
|
|---|
| 897 | conn->close_callid = 0;
|
|---|
| 898 | conn->carg = carg;
|
|---|
| 899 |
|
|---|
| 900 | if (call)
|
|---|
| 901 | conn->call = *call;
|
|---|
| 902 |
|
|---|
| 903 | /* We will activate the fibril ASAP */
|
|---|
| 904 | conn->wdata.active = true;
|
|---|
| 905 | conn->cfibril = cfibril;
|
|---|
| 906 | conn->wdata.fid = fibril_create(connection_fibril, conn);
|
|---|
| 907 |
|
|---|
| 908 | if (conn->wdata.fid == 0) {
|
|---|
| 909 | free(conn);
|
|---|
| 910 |
|
|---|
| 911 | if (callid)
|
|---|
| 912 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 913 |
|
|---|
| 914 | return (uintptr_t) NULL;
|
|---|
| 915 | }
|
|---|
| 916 |
|
|---|
| 917 | /* Add connection to the connection hash table */
|
|---|
| 918 | unsigned long key = conn->in_phone_hash;
|
|---|
| 919 |
|
|---|
| 920 | futex_down(&async_futex);
|
|---|
| 921 | hash_table_insert(&conn_hash_table, &key, &conn->link);
|
|---|
| 922 | futex_up(&async_futex);
|
|---|
| 923 |
|
|---|
| 924 | fibril_add_ready(conn->wdata.fid);
|
|---|
| 925 |
|
|---|
| 926 | return conn->wdata.fid;
|
|---|
| 927 | }
|
|---|
| 928 |
|
|---|
| 929 | /** Handle a call that was received.
|
|---|
| 930 | *
|
|---|
| 931 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
|---|
| 932 | * Otherwise the call is routed to its connection fibril.
|
|---|
| 933 | *
|
|---|
| 934 | * @param callid Hash of the incoming call.
|
|---|
| 935 | * @param call Data of the incoming call.
|
|---|
| 936 | *
|
|---|
| 937 | */
|
|---|
| 938 | static void handle_call(ipc_callid_t callid, ipc_call_t *call)
|
|---|
| 939 | {
|
|---|
| 940 | assert(call);
|
|---|
| 941 |
|
|---|
| 942 | /* Unrouted call - take some default action */
|
|---|
| 943 | if ((callid & IPC_CALLID_NOTIFICATION)) {
|
|---|
| 944 | process_notification(callid, call);
|
|---|
| 945 | return;
|
|---|
| 946 | }
|
|---|
| 947 |
|
|---|
| 948 | switch (IPC_GET_IMETHOD(*call)) {
|
|---|
| 949 | case IPC_M_CLONE_ESTABLISH:
|
|---|
| 950 | case IPC_M_CONNECT_ME_TO:
|
|---|
| 951 | /* Open new connection with fibril, etc. */
|
|---|
| 952 | async_new_connection(call->in_task_id, IPC_GET_ARG5(*call),
|
|---|
| 953 | callid, call, client_connection, NULL);
|
|---|
| 954 | return;
|
|---|
| 955 | }
|
|---|
| 956 |
|
|---|
| 957 | /* Try to route the call through the connection hash table */
|
|---|
| 958 | if (route_call(callid, call))
|
|---|
| 959 | return;
|
|---|
| 960 |
|
|---|
| 961 | /* Unknown call from unknown phone - hang it up */
|
|---|
| 962 | ipc_answer_0(callid, EHANGUP);
|
|---|
| 963 | }
|
|---|
| 964 |
|
|---|
| 965 | /** Fire all timeouts that expired. */
|
|---|
| 966 | static void handle_expired_timeouts(void)
|
|---|
| 967 | {
|
|---|
| 968 | struct timeval tv;
|
|---|
| 969 | getuptime(&tv);
|
|---|
| 970 |
|
|---|
| 971 | futex_down(&async_futex);
|
|---|
| 972 |
|
|---|
| 973 | link_t *cur = list_first(&timeout_list);
|
|---|
| 974 | while (cur != NULL) {
|
|---|
| 975 | awaiter_t *waiter =
|
|---|
| 976 | list_get_instance(cur, awaiter_t, to_event.link);
|
|---|
| 977 |
|
|---|
| 978 | if (tv_gt(&waiter->to_event.expires, &tv))
|
|---|
| 979 | break;
|
|---|
| 980 |
|
|---|
| 981 | list_remove(&waiter->to_event.link);
|
|---|
| 982 | waiter->to_event.inlist = false;
|
|---|
| 983 | waiter->to_event.occurred = true;
|
|---|
| 984 |
|
|---|
| 985 | /*
|
|---|
| 986 | * Redundant condition?
|
|---|
| 987 | * The fibril should not be active when it gets here.
|
|---|
| 988 | */
|
|---|
| 989 | if (!waiter->active) {
|
|---|
| 990 | waiter->active = true;
|
|---|
| 991 | fibril_add_ready(waiter->fid);
|
|---|
| 992 | }
|
|---|
| 993 |
|
|---|
| 994 | cur = list_first(&timeout_list);
|
|---|
| 995 | }
|
|---|
| 996 |
|
|---|
| 997 | futex_up(&async_futex);
|
|---|
| 998 | }
|
|---|
| 999 |
|
|---|
| 1000 | /** Endless loop dispatching incoming calls and answers.
|
|---|
| 1001 | *
|
|---|
| 1002 | * @return Never returns.
|
|---|
| 1003 | *
|
|---|
| 1004 | */
|
|---|
| 1005 | static int async_manager_worker(void)
|
|---|
| 1006 | {
|
|---|
| 1007 | while (true) {
|
|---|
| 1008 | if (fibril_switch(FIBRIL_FROM_MANAGER)) {
|
|---|
| 1009 | futex_up(&async_futex);
|
|---|
| 1010 | /*
|
|---|
| 1011 | * async_futex is always held when entering a manager
|
|---|
| 1012 | * fibril.
|
|---|
| 1013 | */
|
|---|
| 1014 | continue;
|
|---|
| 1015 | }
|
|---|
| 1016 |
|
|---|
| 1017 | futex_down(&async_futex);
|
|---|
| 1018 |
|
|---|
| 1019 | suseconds_t timeout;
|
|---|
| 1020 | unsigned int flags = SYNCH_FLAGS_NONE;
|
|---|
| 1021 | if (!list_empty(&timeout_list)) {
|
|---|
| 1022 | awaiter_t *waiter = list_get_instance(
|
|---|
| 1023 | list_first(&timeout_list), awaiter_t, to_event.link);
|
|---|
| 1024 |
|
|---|
| 1025 | struct timeval tv;
|
|---|
| 1026 | getuptime(&tv);
|
|---|
| 1027 |
|
|---|
| 1028 | if (tv_gteq(&tv, &waiter->to_event.expires)) {
|
|---|
| 1029 | futex_up(&async_futex);
|
|---|
| 1030 | handle_expired_timeouts();
|
|---|
| 1031 | /*
|
|---|
| 1032 | * Notice that even if the event(s) already
|
|---|
| 1033 | * expired (and thus the other fibril was
|
|---|
| 1034 | * supposed to be running already),
|
|---|
| 1035 | * we check for incoming IPC.
|
|---|
| 1036 | *
|
|---|
| 1037 | * Otherwise, a fibril that continuously
|
|---|
| 1038 | * creates (almost) expired events could
|
|---|
| 1039 | * prevent IPC retrieval from the kernel.
|
|---|
| 1040 | */
|
|---|
| 1041 | timeout = 0;
|
|---|
| 1042 | flags = SYNCH_FLAGS_NON_BLOCKING;
|
|---|
| 1043 |
|
|---|
| 1044 | } else {
|
|---|
| 1045 | timeout = tv_sub(&waiter->to_event.expires, &tv);
|
|---|
| 1046 | futex_up(&async_futex);
|
|---|
| 1047 | }
|
|---|
| 1048 | } else {
|
|---|
| 1049 | futex_up(&async_futex);
|
|---|
| 1050 | timeout = SYNCH_NO_TIMEOUT;
|
|---|
| 1051 | }
|
|---|
| 1052 |
|
|---|
| 1053 | atomic_inc(&threads_in_ipc_wait);
|
|---|
| 1054 |
|
|---|
| 1055 | ipc_call_t call;
|
|---|
| 1056 | ipc_callid_t callid = ipc_wait_cycle(&call, timeout, flags);
|
|---|
| 1057 |
|
|---|
| 1058 | atomic_dec(&threads_in_ipc_wait);
|
|---|
| 1059 |
|
|---|
| 1060 | if (!callid) {
|
|---|
| 1061 | handle_expired_timeouts();
|
|---|
| 1062 | continue;
|
|---|
| 1063 | }
|
|---|
| 1064 |
|
|---|
| 1065 | if (callid & IPC_CALLID_ANSWERED)
|
|---|
| 1066 | continue;
|
|---|
| 1067 |
|
|---|
| 1068 | handle_call(callid, &call);
|
|---|
| 1069 | }
|
|---|
| 1070 |
|
|---|
| 1071 | return 0;
|
|---|
| 1072 | }
|
|---|
| 1073 |
|
|---|
| 1074 | /** Function to start async_manager as a standalone fibril.
|
|---|
| 1075 | *
|
|---|
| 1076 | * When more kernel threads are used, one async manager should exist per thread.
|
|---|
| 1077 | *
|
|---|
| 1078 | * @param arg Unused.
|
|---|
| 1079 | * @return Never returns.
|
|---|
| 1080 | *
|
|---|
| 1081 | */
|
|---|
| 1082 | static int async_manager_fibril(void *arg)
|
|---|
| 1083 | {
|
|---|
| 1084 | futex_up(&async_futex);
|
|---|
| 1085 |
|
|---|
| 1086 | /*
|
|---|
| 1087 | * async_futex is always locked when entering manager
|
|---|
| 1088 | */
|
|---|
| 1089 | async_manager_worker();
|
|---|
| 1090 |
|
|---|
| 1091 | return 0;
|
|---|
| 1092 | }
|
|---|
| 1093 |
|
|---|
| 1094 | /** Add one manager to manager list. */
|
|---|
| 1095 | void async_create_manager(void)
|
|---|
| 1096 | {
|
|---|
| 1097 | fid_t fid = fibril_create(async_manager_fibril, NULL);
|
|---|
| 1098 | if (fid != 0)
|
|---|
| 1099 | fibril_add_manager(fid);
|
|---|
| 1100 | }
|
|---|
| 1101 |
|
|---|
| 1102 | /** Remove one manager from manager list */
|
|---|
| 1103 | void async_destroy_manager(void)
|
|---|
| 1104 | {
|
|---|
| 1105 | fibril_remove_manager();
|
|---|
| 1106 | }
|
|---|
| 1107 |
|
|---|
| 1108 | /** Initialize the async framework.
|
|---|
| 1109 | *
|
|---|
| 1110 | */
|
|---|
| 1111 | void __async_init(void)
|
|---|
| 1112 | {
|
|---|
| 1113 | if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS,
|
|---|
| 1114 | 2, &client_hash_table_ops))
|
|---|
| 1115 | abort();
|
|---|
| 1116 |
|
|---|
| 1117 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_BUCKETS,
|
|---|
| 1118 | 1, &conn_hash_table_ops))
|
|---|
| 1119 | abort();
|
|---|
| 1120 |
|
|---|
| 1121 | session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
|
|---|
| 1122 | if (session_ns == NULL)
|
|---|
| 1123 | abort();
|
|---|
| 1124 |
|
|---|
| 1125 | session_ns->mgmt = EXCHANGE_ATOMIC;
|
|---|
| 1126 | session_ns->phone = PHONE_NS;
|
|---|
| 1127 | session_ns->arg1 = 0;
|
|---|
| 1128 | session_ns->arg2 = 0;
|
|---|
| 1129 | session_ns->arg3 = 0;
|
|---|
| 1130 |
|
|---|
| 1131 | fibril_mutex_initialize(&session_ns->remote_state_mtx);
|
|---|
| 1132 | session_ns->remote_state_data = NULL;
|
|---|
| 1133 |
|
|---|
| 1134 | list_initialize(&session_ns->exch_list);
|
|---|
| 1135 | fibril_mutex_initialize(&session_ns->mutex);
|
|---|
| 1136 | atomic_set(&session_ns->refcnt, 0);
|
|---|
| 1137 | }
|
|---|
| 1138 |
|
|---|
| 1139 | /** Reply received callback.
|
|---|
| 1140 | *
|
|---|
| 1141 | * This function is called whenever a reply for an asynchronous message sent out
|
|---|
| 1142 | * by the asynchronous framework is received.
|
|---|
| 1143 | *
|
|---|
| 1144 | * Notify the fibril which is waiting for this message that it has arrived.
|
|---|
| 1145 | *
|
|---|
| 1146 | * @param arg Pointer to the asynchronous message record.
|
|---|
| 1147 | * @param retval Value returned in the answer.
|
|---|
| 1148 | * @param data Call data of the answer.
|
|---|
| 1149 | *
|
|---|
| 1150 | */
|
|---|
| 1151 | void reply_received(void *arg, int retval, ipc_call_t *data)
|
|---|
| 1152 | {
|
|---|
| 1153 | assert(arg);
|
|---|
| 1154 |
|
|---|
| 1155 | futex_down(&async_futex);
|
|---|
| 1156 |
|
|---|
| 1157 | amsg_t *msg = (amsg_t *) arg;
|
|---|
| 1158 | msg->retval = retval;
|
|---|
| 1159 |
|
|---|
| 1160 | /* Copy data after futex_down, just in case the call was detached */
|
|---|
| 1161 | if ((msg->dataptr) && (data))
|
|---|
| 1162 | *msg->dataptr = *data;
|
|---|
| 1163 |
|
|---|
| 1164 | write_barrier();
|
|---|
| 1165 |
|
|---|
| 1166 | /* Remove message from timeout list */
|
|---|
| 1167 | if (msg->wdata.to_event.inlist)
|
|---|
| 1168 | list_remove(&msg->wdata.to_event.link);
|
|---|
| 1169 |
|
|---|
| 1170 | msg->done = true;
|
|---|
| 1171 |
|
|---|
| 1172 | if (msg->forget) {
|
|---|
| 1173 | assert(msg->wdata.active);
|
|---|
| 1174 | amsg_destroy(msg);
|
|---|
| 1175 | } else if (!msg->wdata.active) {
|
|---|
| 1176 | msg->wdata.active = true;
|
|---|
| 1177 | fibril_add_ready(msg->wdata.fid);
|
|---|
| 1178 | }
|
|---|
| 1179 |
|
|---|
| 1180 | futex_up(&async_futex);
|
|---|
| 1181 | }
|
|---|
| 1182 |
|
|---|
| 1183 | /** Send message and return id of the sent message.
|
|---|
| 1184 | *
|
|---|
| 1185 | * The return value can be used as input for async_wait() to wait for
|
|---|
| 1186 | * completion.
|
|---|
| 1187 | *
|
|---|
| 1188 | * @param exch Exchange for sending the message.
|
|---|
| 1189 | * @param imethod Service-defined interface and method.
|
|---|
| 1190 | * @param arg1 Service-defined payload argument.
|
|---|
| 1191 | * @param arg2 Service-defined payload argument.
|
|---|
| 1192 | * @param arg3 Service-defined payload argument.
|
|---|
| 1193 | * @param arg4 Service-defined payload argument.
|
|---|
| 1194 | * @param dataptr If non-NULL, storage where the reply data will be
|
|---|
| 1195 | * stored.
|
|---|
| 1196 | *
|
|---|
| 1197 | * @return Hash of the sent message or 0 on error.
|
|---|
| 1198 | *
|
|---|
| 1199 | */
|
|---|
| 1200 | aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
|---|
| 1201 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
|---|
| 1202 | {
|
|---|
| 1203 | if (exch == NULL)
|
|---|
| 1204 | return 0;
|
|---|
| 1205 |
|
|---|
| 1206 | amsg_t *msg = amsg_create();
|
|---|
| 1207 | if (msg == NULL)
|
|---|
| 1208 | return 0;
|
|---|
| 1209 |
|
|---|
| 1210 | msg->dataptr = dataptr;
|
|---|
| 1211 | msg->wdata.active = true;
|
|---|
| 1212 |
|
|---|
| 1213 | ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
|
|---|
| 1214 | reply_received, true);
|
|---|
| 1215 |
|
|---|
| 1216 | return (aid_t) msg;
|
|---|
| 1217 | }
|
|---|
| 1218 |
|
|---|
| 1219 | /** Send message and return id of the sent message
|
|---|
| 1220 | *
|
|---|
| 1221 | * The return value can be used as input for async_wait() to wait for
|
|---|
| 1222 | * completion.
|
|---|
| 1223 | *
|
|---|
| 1224 | * @param exch Exchange for sending the message.
|
|---|
| 1225 | * @param imethod Service-defined interface and method.
|
|---|
| 1226 | * @param arg1 Service-defined payload argument.
|
|---|
| 1227 | * @param arg2 Service-defined payload argument.
|
|---|
| 1228 | * @param arg3 Service-defined payload argument.
|
|---|
| 1229 | * @param arg4 Service-defined payload argument.
|
|---|
| 1230 | * @param arg5 Service-defined payload argument.
|
|---|
| 1231 | * @param dataptr If non-NULL, storage where the reply data will be
|
|---|
| 1232 | * stored.
|
|---|
| 1233 | *
|
|---|
| 1234 | * @return Hash of the sent message or 0 on error.
|
|---|
| 1235 | *
|
|---|
| 1236 | */
|
|---|
| 1237 | aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
|---|
| 1238 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
|---|
| 1239 | ipc_call_t *dataptr)
|
|---|
| 1240 | {
|
|---|
| 1241 | if (exch == NULL)
|
|---|
| 1242 | return 0;
|
|---|
| 1243 |
|
|---|
| 1244 | amsg_t *msg = amsg_create();
|
|---|
| 1245 | if (msg == NULL)
|
|---|
| 1246 | return 0;
|
|---|
| 1247 |
|
|---|
| 1248 | msg->dataptr = dataptr;
|
|---|
| 1249 | msg->wdata.active = true;
|
|---|
| 1250 |
|
|---|
| 1251 | ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
|
|---|
| 1252 | msg, reply_received, true);
|
|---|
| 1253 |
|
|---|
| 1254 | return (aid_t) msg;
|
|---|
| 1255 | }
|
|---|
| 1256 |
|
|---|
| 1257 | /** Wait for a message sent by the async framework.
|
|---|
| 1258 | *
|
|---|
| 1259 | * @param amsgid Hash of the message to wait for.
|
|---|
| 1260 | * @param retval Pointer to storage where the retval of the answer will
|
|---|
| 1261 | * be stored.
|
|---|
| 1262 | *
|
|---|
| 1263 | */
|
|---|
| 1264 | void async_wait_for(aid_t amsgid, sysarg_t *retval)
|
|---|
| 1265 | {
|
|---|
| 1266 | assert(amsgid);
|
|---|
| 1267 |
|
|---|
| 1268 | amsg_t *msg = (amsg_t *) amsgid;
|
|---|
| 1269 |
|
|---|
| 1270 | futex_down(&async_futex);
|
|---|
| 1271 |
|
|---|
| 1272 | assert(!msg->forget);
|
|---|
| 1273 | assert(!msg->destroyed);
|
|---|
| 1274 |
|
|---|
| 1275 | if (msg->done) {
|
|---|
| 1276 | futex_up(&async_futex);
|
|---|
| 1277 | goto done;
|
|---|
| 1278 | }
|
|---|
| 1279 |
|
|---|
| 1280 | msg->wdata.fid = fibril_get_id();
|
|---|
| 1281 | msg->wdata.active = false;
|
|---|
| 1282 | msg->wdata.to_event.inlist = false;
|
|---|
| 1283 |
|
|---|
| 1284 | /* Leave the async_futex locked when entering this function */
|
|---|
| 1285 | fibril_switch(FIBRIL_TO_MANAGER);
|
|---|
| 1286 |
|
|---|
| 1287 | /* Futex is up automatically after fibril_switch */
|
|---|
| 1288 |
|
|---|
| 1289 | done:
|
|---|
| 1290 | if (retval)
|
|---|
| 1291 | *retval = msg->retval;
|
|---|
| 1292 |
|
|---|
| 1293 | amsg_destroy(msg);
|
|---|
| 1294 | }
|
|---|
| 1295 |
|
|---|
| 1296 | /** Wait for a message sent by the async framework, timeout variant.
|
|---|
| 1297 | *
|
|---|
| 1298 | * If the wait times out, the caller may choose to either wait again by calling
|
|---|
| 1299 | * async_wait_for() or async_wait_timeout(), or forget the message via
|
|---|
| 1300 | * async_forget().
|
|---|
| 1301 | *
|
|---|
| 1302 | * @param amsgid Hash of the message to wait for.
|
|---|
| 1303 | * @param retval Pointer to storage where the retval of the answer will
|
|---|
| 1304 | * be stored.
|
|---|
| 1305 | * @param timeout Timeout in microseconds.
|
|---|
| 1306 | *
|
|---|
| 1307 | * @return Zero on success, ETIMEOUT if the timeout has expired.
|
|---|
| 1308 | *
|
|---|
| 1309 | */
|
|---|
| 1310 | int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
|
|---|
| 1311 | {
|
|---|
| 1312 | assert(amsgid);
|
|---|
| 1313 |
|
|---|
| 1314 | amsg_t *msg = (amsg_t *) amsgid;
|
|---|
| 1315 |
|
|---|
| 1316 | futex_down(&async_futex);
|
|---|
| 1317 |
|
|---|
| 1318 | assert(!msg->forget);
|
|---|
| 1319 | assert(!msg->destroyed);
|
|---|
| 1320 |
|
|---|
| 1321 | if (msg->done) {
|
|---|
| 1322 | futex_up(&async_futex);
|
|---|
| 1323 | goto done;
|
|---|
| 1324 | }
|
|---|
| 1325 |
|
|---|
| 1326 | /*
|
|---|
| 1327 | * Negative timeout is converted to zero timeout to avoid
|
|---|
| 1328 | * using tv_add with negative augmenter.
|
|---|
| 1329 | */
|
|---|
| 1330 | if (timeout < 0)
|
|---|
| 1331 | timeout = 0;
|
|---|
| 1332 |
|
|---|
| 1333 | getuptime(&msg->wdata.to_event.expires);
|
|---|
| 1334 | tv_add(&msg->wdata.to_event.expires, timeout);
|
|---|
| 1335 |
|
|---|
| 1336 | /*
|
|---|
| 1337 | * Current fibril is inserted as waiting regardless of the
|
|---|
| 1338 | * "size" of the timeout.
|
|---|
| 1339 | *
|
|---|
| 1340 | * Checking for msg->done and immediately bailing out when
|
|---|
| 1341 | * timeout == 0 would mean that the manager fibril would never
|
|---|
| 1342 | * run (consider single threaded program).
|
|---|
| 1343 | * Thus the IPC answer would be never retrieved from the kernel.
|
|---|
| 1344 | *
|
|---|
| 1345 | * Notice that the actual delay would be very small because we
|
|---|
| 1346 | * - switch to manager fibril
|
|---|
| 1347 | * - the manager sees expired timeout
|
|---|
| 1348 | * - and thus adds us back to ready queue
|
|---|
| 1349 | * - manager switches back to some ready fibril
|
|---|
| 1350 | * (prior it, it checks for incoming IPC).
|
|---|
| 1351 | *
|
|---|
| 1352 | */
|
|---|
| 1353 | msg->wdata.fid = fibril_get_id();
|
|---|
| 1354 | msg->wdata.active = false;
|
|---|
| 1355 | async_insert_timeout(&msg->wdata);
|
|---|
| 1356 |
|
|---|
| 1357 | /* Leave the async_futex locked when entering this function */
|
|---|
| 1358 | fibril_switch(FIBRIL_TO_MANAGER);
|
|---|
| 1359 |
|
|---|
| 1360 | /* Futex is up automatically after fibril_switch */
|
|---|
| 1361 |
|
|---|
| 1362 | if (!msg->done)
|
|---|
| 1363 | return ETIMEOUT;
|
|---|
| 1364 |
|
|---|
| 1365 | done:
|
|---|
| 1366 | if (retval)
|
|---|
| 1367 | *retval = msg->retval;
|
|---|
| 1368 |
|
|---|
| 1369 | amsg_destroy(msg);
|
|---|
| 1370 |
|
|---|
| 1371 | return 0;
|
|---|
| 1372 | }
|
|---|
| 1373 |
|
|---|
| 1374 | /** Discard the message / reply on arrival.
|
|---|
| 1375 | *
|
|---|
| 1376 | * The message will be marked to be discarded once the reply arrives in
|
|---|
| 1377 | * reply_received(). It is not allowed to call async_wait_for() or
|
|---|
| 1378 | * async_wait_timeout() on this message after a call to this function.
|
|---|
| 1379 | *
|
|---|
| 1380 | * @param amsgid Hash of the message to forget.
|
|---|
| 1381 | */
|
|---|
| 1382 | void async_forget(aid_t amsgid)
|
|---|
| 1383 | {
|
|---|
| 1384 | amsg_t *msg = (amsg_t *) amsgid;
|
|---|
| 1385 |
|
|---|
| 1386 | assert(msg);
|
|---|
| 1387 | assert(!msg->forget);
|
|---|
| 1388 | assert(!msg->destroyed);
|
|---|
| 1389 |
|
|---|
| 1390 | futex_down(&async_futex);
|
|---|
| 1391 | if (msg->done) {
|
|---|
| 1392 | amsg_destroy(msg);
|
|---|
| 1393 | } else {
|
|---|
| 1394 | msg->dataptr = NULL;
|
|---|
| 1395 | msg->forget = true;
|
|---|
| 1396 | }
|
|---|
| 1397 | futex_up(&async_futex);
|
|---|
| 1398 | }
|
|---|
| 1399 |
|
|---|
| 1400 | /** Wait for specified time.
|
|---|
| 1401 | *
|
|---|
| 1402 | * The current fibril is suspended but the thread continues to execute.
|
|---|
| 1403 | *
|
|---|
| 1404 | * @param timeout Duration of the wait in microseconds.
|
|---|
| 1405 | *
|
|---|
| 1406 | */
|
|---|
| 1407 | void async_usleep(suseconds_t timeout)
|
|---|
| 1408 | {
|
|---|
| 1409 | amsg_t *msg = amsg_create();
|
|---|
| 1410 | if (!msg)
|
|---|
| 1411 | return;
|
|---|
| 1412 |
|
|---|
| 1413 | msg->wdata.fid = fibril_get_id();
|
|---|
| 1414 |
|
|---|
| 1415 | getuptime(&msg->wdata.to_event.expires);
|
|---|
| 1416 | tv_add(&msg->wdata.to_event.expires, timeout);
|
|---|
| 1417 |
|
|---|
| 1418 | futex_down(&async_futex);
|
|---|
| 1419 |
|
|---|
| 1420 | async_insert_timeout(&msg->wdata);
|
|---|
| 1421 |
|
|---|
| 1422 | /* Leave the async_futex locked when entering this function */
|
|---|
| 1423 | fibril_switch(FIBRIL_TO_MANAGER);
|
|---|
| 1424 |
|
|---|
| 1425 | /* Futex is up automatically after fibril_switch() */
|
|---|
| 1426 |
|
|---|
| 1427 | amsg_destroy(msg);
|
|---|
| 1428 | }
|
|---|
| 1429 |
|
|---|
| 1430 | /** Pseudo-synchronous message sending - fast version.
|
|---|
| 1431 | *
|
|---|
| 1432 | * Send message asynchronously and return only after the reply arrives.
|
|---|
| 1433 | *
|
|---|
| 1434 | * This function can only transfer 4 register payload arguments. For
|
|---|
| 1435 | * transferring more arguments, see the slower async_req_slow().
|
|---|
| 1436 | *
|
|---|
| 1437 | * @param exch Exchange for sending the message.
|
|---|
| 1438 | * @param imethod Interface and method of the call.
|
|---|
| 1439 | * @param arg1 Service-defined payload argument.
|
|---|
| 1440 | * @param arg2 Service-defined payload argument.
|
|---|
| 1441 | * @param arg3 Service-defined payload argument.
|
|---|
| 1442 | * @param arg4 Service-defined payload argument.
|
|---|
| 1443 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
|---|
| 1444 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
|---|
| 1445 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
|---|
| 1446 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
|---|
| 1447 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
|---|
| 1448 | *
|
|---|
| 1449 | * @return Return code of the reply or a negative error code.
|
|---|
| 1450 | *
|
|---|
| 1451 | */
|
|---|
| 1452 | sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
|---|
| 1453 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
|
|---|
| 1454 | sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
|---|
| 1455 | {
|
|---|
| 1456 | if (exch == NULL)
|
|---|
| 1457 | return ENOENT;
|
|---|
| 1458 |
|
|---|
| 1459 | ipc_call_t result;
|
|---|
| 1460 | aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
|
|---|
| 1461 | &result);
|
|---|
| 1462 |
|
|---|
| 1463 | sysarg_t rc;
|
|---|
| 1464 | async_wait_for(aid, &rc);
|
|---|
| 1465 |
|
|---|
| 1466 | if (r1)
|
|---|
| 1467 | *r1 = IPC_GET_ARG1(result);
|
|---|
| 1468 |
|
|---|
| 1469 | if (r2)
|
|---|
| 1470 | *r2 = IPC_GET_ARG2(result);
|
|---|
| 1471 |
|
|---|
| 1472 | if (r3)
|
|---|
| 1473 | *r3 = IPC_GET_ARG3(result);
|
|---|
| 1474 |
|
|---|
| 1475 | if (r4)
|
|---|
| 1476 | *r4 = IPC_GET_ARG4(result);
|
|---|
| 1477 |
|
|---|
| 1478 | if (r5)
|
|---|
| 1479 | *r5 = IPC_GET_ARG5(result);
|
|---|
| 1480 |
|
|---|
| 1481 | return rc;
|
|---|
| 1482 | }
|
|---|
| 1483 |
|
|---|
| 1484 | /** Pseudo-synchronous message sending - slow version.
|
|---|
| 1485 | *
|
|---|
| 1486 | * Send message asynchronously and return only after the reply arrives.
|
|---|
| 1487 | *
|
|---|
| 1488 | * @param exch Exchange for sending the message.
|
|---|
| 1489 | * @param imethod Interface and method of the call.
|
|---|
| 1490 | * @param arg1 Service-defined payload argument.
|
|---|
| 1491 | * @param arg2 Service-defined payload argument.
|
|---|
| 1492 | * @param arg3 Service-defined payload argument.
|
|---|
| 1493 | * @param arg4 Service-defined payload argument.
|
|---|
| 1494 | * @param arg5 Service-defined payload argument.
|
|---|
| 1495 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
|---|
| 1496 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
|---|
| 1497 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
|---|
| 1498 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
|---|
| 1499 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
|---|
| 1500 | *
|
|---|
| 1501 | * @return Return code of the reply or a negative error code.
|
|---|
| 1502 | *
|
|---|
| 1503 | */
|
|---|
| 1504 | sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
|---|
| 1505 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
|
|---|
| 1506 | sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
|---|
| 1507 | {
|
|---|
| 1508 | if (exch == NULL)
|
|---|
| 1509 | return ENOENT;
|
|---|
| 1510 |
|
|---|
| 1511 | ipc_call_t result;
|
|---|
| 1512 | aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
|
|---|
| 1513 | &result);
|
|---|
| 1514 |
|
|---|
| 1515 | sysarg_t rc;
|
|---|
| 1516 | async_wait_for(aid, &rc);
|
|---|
| 1517 |
|
|---|
| 1518 | if (r1)
|
|---|
| 1519 | *r1 = IPC_GET_ARG1(result);
|
|---|
| 1520 |
|
|---|
| 1521 | if (r2)
|
|---|
| 1522 | *r2 = IPC_GET_ARG2(result);
|
|---|
| 1523 |
|
|---|
| 1524 | if (r3)
|
|---|
| 1525 | *r3 = IPC_GET_ARG3(result);
|
|---|
| 1526 |
|
|---|
| 1527 | if (r4)
|
|---|
| 1528 | *r4 = IPC_GET_ARG4(result);
|
|---|
| 1529 |
|
|---|
| 1530 | if (r5)
|
|---|
| 1531 | *r5 = IPC_GET_ARG5(result);
|
|---|
| 1532 |
|
|---|
| 1533 | return rc;
|
|---|
| 1534 | }
|
|---|
| 1535 |
|
|---|
| 1536 | void async_msg_0(async_exch_t *exch, sysarg_t imethod)
|
|---|
| 1537 | {
|
|---|
| 1538 | if (exch != NULL)
|
|---|
| 1539 | ipc_call_async_0(exch->phone, imethod, NULL, NULL, true);
|
|---|
| 1540 | }
|
|---|
| 1541 |
|
|---|
| 1542 | void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
|
|---|
| 1543 | {
|
|---|
| 1544 | if (exch != NULL)
|
|---|
| 1545 | ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL, true);
|
|---|
| 1546 | }
|
|---|
| 1547 |
|
|---|
| 1548 | void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
|---|
| 1549 | sysarg_t arg2)
|
|---|
| 1550 | {
|
|---|
| 1551 | if (exch != NULL)
|
|---|
| 1552 | ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL,
|
|---|
| 1553 | true);
|
|---|
| 1554 | }
|
|---|
| 1555 |
|
|---|
| 1556 | void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
|---|
| 1557 | sysarg_t arg2, sysarg_t arg3)
|
|---|
| 1558 | {
|
|---|
| 1559 | if (exch != NULL)
|
|---|
| 1560 | ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
|
|---|
| 1561 | NULL, true);
|
|---|
| 1562 | }
|
|---|
| 1563 |
|
|---|
| 1564 | void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
|---|
| 1565 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
|---|
| 1566 | {
|
|---|
| 1567 | if (exch != NULL)
|
|---|
| 1568 | ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
|
|---|
| 1569 | NULL, NULL, true);
|
|---|
| 1570 | }
|
|---|
| 1571 |
|
|---|
| 1572 | void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
|---|
| 1573 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
|---|
| 1574 | {
|
|---|
| 1575 | if (exch != NULL)
|
|---|
| 1576 | ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
|
|---|
| 1577 | arg5, NULL, NULL, true);
|
|---|
| 1578 | }
|
|---|
| 1579 |
|
|---|
| 1580 | sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
|
|---|
| 1581 | {
|
|---|
| 1582 | return ipc_answer_0(callid, retval);
|
|---|
| 1583 | }
|
|---|
| 1584 |
|
|---|
| 1585 | sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
|
|---|
| 1586 | {
|
|---|
| 1587 | return ipc_answer_1(callid, retval, arg1);
|
|---|
| 1588 | }
|
|---|
| 1589 |
|
|---|
| 1590 | sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
|---|
| 1591 | sysarg_t arg2)
|
|---|
| 1592 | {
|
|---|
| 1593 | return ipc_answer_2(callid, retval, arg1, arg2);
|
|---|
| 1594 | }
|
|---|
| 1595 |
|
|---|
| 1596 | sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
|---|
| 1597 | sysarg_t arg2, sysarg_t arg3)
|
|---|
| 1598 | {
|
|---|
| 1599 | return ipc_answer_3(callid, retval, arg1, arg2, arg3);
|
|---|
| 1600 | }
|
|---|
| 1601 |
|
|---|
| 1602 | sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
|---|
| 1603 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
|---|
| 1604 | {
|
|---|
| 1605 | return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
|
|---|
| 1606 | }
|
|---|
| 1607 |
|
|---|
| 1608 | sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
|---|
| 1609 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
|---|
| 1610 | {
|
|---|
| 1611 | return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
|
|---|
| 1612 | }
|
|---|
| 1613 |
|
|---|
| 1614 | int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
|
|---|
| 1615 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
|---|
| 1616 | {
|
|---|
| 1617 | if (exch == NULL)
|
|---|
| 1618 | return ENOENT;
|
|---|
| 1619 |
|
|---|
| 1620 | return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
|
|---|
| 1621 | }
|
|---|
| 1622 |
|
|---|
| 1623 | int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
|
|---|
| 1624 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
|---|
| 1625 | sysarg_t arg4, sysarg_t arg5, unsigned int mode)
|
|---|
| 1626 | {
|
|---|
| 1627 | if (exch == NULL)
|
|---|
| 1628 | return ENOENT;
|
|---|
| 1629 |
|
|---|
| 1630 | return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
|
|---|
| 1631 | arg4, arg5, mode);
|
|---|
| 1632 | }
|
|---|
| 1633 |
|
|---|
| 1634 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
|---|
| 1635 | *
|
|---|
| 1636 | * Ask through phone for a new connection to some service.
|
|---|
| 1637 | *
|
|---|
| 1638 | * @param exch Exchange for sending the message.
|
|---|
| 1639 | * @param arg1 User defined argument.
|
|---|
| 1640 | * @param arg2 User defined argument.
|
|---|
| 1641 | * @param arg3 User defined argument.
|
|---|
| 1642 | * @param client_receiver Connection handing routine.
|
|---|
| 1643 | *
|
|---|
| 1644 | * @return Zero on success or a negative error code.
|
|---|
| 1645 | *
|
|---|
| 1646 | */
|
|---|
| 1647 | int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
|
|---|
| 1648 | sysarg_t arg3, async_client_conn_t client_receiver, void *carg)
|
|---|
| 1649 | {
|
|---|
| 1650 | if (exch == NULL)
|
|---|
| 1651 | return ENOENT;
|
|---|
| 1652 |
|
|---|
| 1653 | sysarg_t phone_hash;
|
|---|
| 1654 | sysarg_t rc;
|
|---|
| 1655 |
|
|---|
| 1656 | aid_t req;
|
|---|
| 1657 | ipc_call_t answer;
|
|---|
| 1658 | req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
|
|---|
| 1659 | &answer);
|
|---|
| 1660 | async_wait_for(req, &rc);
|
|---|
| 1661 | if (rc != EOK)
|
|---|
| 1662 | return (int) rc;
|
|---|
| 1663 |
|
|---|
| 1664 | phone_hash = IPC_GET_ARG5(answer);
|
|---|
| 1665 |
|
|---|
| 1666 | if (client_receiver != NULL)
|
|---|
| 1667 | async_new_connection(answer.in_task_id, phone_hash, 0, NULL,
|
|---|
| 1668 | client_receiver, carg);
|
|---|
| 1669 |
|
|---|
| 1670 | return EOK;
|
|---|
| 1671 | }
|
|---|
| 1672 |
|
|---|
| 1673 | /** Wrapper for making IPC_M_CLONE_ESTABLISH calls using the async framework.
|
|---|
| 1674 | *
|
|---|
| 1675 | * Ask for a cloned connection to some service.
|
|---|
| 1676 | *
|
|---|
| 1677 | * @param mgmt Exchange management style.
|
|---|
| 1678 | * @param exch Exchange for sending the message.
|
|---|
| 1679 | *
|
|---|
| 1680 | * @return New session on success or NULL on error.
|
|---|
| 1681 | *
|
|---|
| 1682 | */
|
|---|
| 1683 | async_sess_t *async_clone_establish(exch_mgmt_t mgmt, async_exch_t *exch)
|
|---|
| 1684 | {
|
|---|
| 1685 | if (exch == NULL) {
|
|---|
| 1686 | errno = ENOENT;
|
|---|
| 1687 | return NULL;
|
|---|
| 1688 | }
|
|---|
| 1689 |
|
|---|
| 1690 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
|---|
| 1691 | if (sess == NULL) {
|
|---|
| 1692 | errno = ENOMEM;
|
|---|
| 1693 | return NULL;
|
|---|
| 1694 | }
|
|---|
| 1695 |
|
|---|
| 1696 | ipc_call_t result;
|
|---|
| 1697 |
|
|---|
| 1698 | amsg_t *msg = amsg_create();
|
|---|
| 1699 | if (!msg) {
|
|---|
| 1700 | free(sess);
|
|---|
| 1701 | errno = ENOMEM;
|
|---|
| 1702 | return NULL;
|
|---|
| 1703 | }
|
|---|
| 1704 |
|
|---|
| 1705 | msg->dataptr = &result;
|
|---|
| 1706 | msg->wdata.active = true;
|
|---|
| 1707 |
|
|---|
| 1708 | ipc_call_async_0(exch->phone, IPC_M_CLONE_ESTABLISH, msg,
|
|---|
| 1709 | reply_received, true);
|
|---|
| 1710 |
|
|---|
| 1711 | sysarg_t rc;
|
|---|
| 1712 | async_wait_for((aid_t) msg, &rc);
|
|---|
| 1713 |
|
|---|
| 1714 | if (rc != EOK) {
|
|---|
| 1715 | errno = rc;
|
|---|
| 1716 | free(sess);
|
|---|
| 1717 | return NULL;
|
|---|
| 1718 | }
|
|---|
| 1719 |
|
|---|
| 1720 | int phone = (int) IPC_GET_ARG5(result);
|
|---|
| 1721 |
|
|---|
| 1722 | if (phone < 0) {
|
|---|
| 1723 | errno = phone;
|
|---|
| 1724 | free(sess);
|
|---|
| 1725 | return NULL;
|
|---|
| 1726 | }
|
|---|
| 1727 |
|
|---|
| 1728 | sess->mgmt = mgmt;
|
|---|
| 1729 | sess->phone = phone;
|
|---|
| 1730 | sess->arg1 = 0;
|
|---|
| 1731 | sess->arg2 = 0;
|
|---|
| 1732 | sess->arg3 = 0;
|
|---|
| 1733 |
|
|---|
| 1734 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
|---|
| 1735 | sess->remote_state_data = NULL;
|
|---|
| 1736 |
|
|---|
| 1737 | list_initialize(&sess->exch_list);
|
|---|
| 1738 | fibril_mutex_initialize(&sess->mutex);
|
|---|
| 1739 | atomic_set(&sess->refcnt, 0);
|
|---|
| 1740 |
|
|---|
| 1741 | return sess;
|
|---|
| 1742 | }
|
|---|
| 1743 |
|
|---|
| 1744 | static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
|
|---|
| 1745 | sysarg_t arg3, sysarg_t arg4)
|
|---|
| 1746 | {
|
|---|
| 1747 | ipc_call_t result;
|
|---|
| 1748 |
|
|---|
| 1749 | amsg_t *msg = amsg_create();
|
|---|
| 1750 | if (!msg)
|
|---|
| 1751 | return ENOENT;
|
|---|
| 1752 |
|
|---|
| 1753 | msg->dataptr = &result;
|
|---|
| 1754 | msg->wdata.active = true;
|
|---|
| 1755 |
|
|---|
| 1756 | ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
|
|---|
| 1757 | msg, reply_received, true);
|
|---|
| 1758 |
|
|---|
| 1759 | sysarg_t rc;
|
|---|
| 1760 | async_wait_for((aid_t) msg, &rc);
|
|---|
| 1761 |
|
|---|
| 1762 | if (rc != EOK)
|
|---|
| 1763 | return rc;
|
|---|
| 1764 |
|
|---|
| 1765 | return (int) IPC_GET_ARG5(result);
|
|---|
| 1766 | }
|
|---|
| 1767 |
|
|---|
| 1768 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
|---|
| 1769 | *
|
|---|
| 1770 | * Ask through for a new connection to some service.
|
|---|
| 1771 | *
|
|---|
| 1772 | * @param mgmt Exchange management style.
|
|---|
| 1773 | * @param exch Exchange for sending the message.
|
|---|
| 1774 | * @param arg1 User defined argument.
|
|---|
| 1775 | * @param arg2 User defined argument.
|
|---|
| 1776 | * @param arg3 User defined argument.
|
|---|
| 1777 | *
|
|---|
| 1778 | * @return New session on success or NULL on error.
|
|---|
| 1779 | *
|
|---|
| 1780 | */
|
|---|
| 1781 | async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
|
|---|
| 1782 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
|---|
| 1783 | {
|
|---|
| 1784 | if (exch == NULL) {
|
|---|
| 1785 | errno = ENOENT;
|
|---|
| 1786 | return NULL;
|
|---|
| 1787 | }
|
|---|
| 1788 |
|
|---|
| 1789 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
|---|
| 1790 | if (sess == NULL) {
|
|---|
| 1791 | errno = ENOMEM;
|
|---|
| 1792 | return NULL;
|
|---|
| 1793 | }
|
|---|
| 1794 |
|
|---|
| 1795 | int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
|
|---|
| 1796 | 0);
|
|---|
| 1797 |
|
|---|
| 1798 | if (phone < 0) {
|
|---|
| 1799 | errno = phone;
|
|---|
| 1800 | free(sess);
|
|---|
| 1801 | return NULL;
|
|---|
| 1802 | }
|
|---|
| 1803 |
|
|---|
| 1804 | sess->mgmt = mgmt;
|
|---|
| 1805 | sess->phone = phone;
|
|---|
| 1806 | sess->arg1 = arg1;
|
|---|
| 1807 | sess->arg2 = arg2;
|
|---|
| 1808 | sess->arg3 = arg3;
|
|---|
| 1809 |
|
|---|
| 1810 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
|---|
| 1811 | sess->remote_state_data = NULL;
|
|---|
| 1812 |
|
|---|
| 1813 | list_initialize(&sess->exch_list);
|
|---|
| 1814 | fibril_mutex_initialize(&sess->mutex);
|
|---|
| 1815 | atomic_set(&sess->refcnt, 0);
|
|---|
| 1816 |
|
|---|
| 1817 | return sess;
|
|---|
| 1818 | }
|
|---|
| 1819 |
|
|---|
| 1820 | /** Set arguments for new connections.
|
|---|
| 1821 | *
|
|---|
| 1822 | * FIXME This is an ugly hack to work around the problem that parallel
|
|---|
| 1823 | * exchanges are implemented using parallel connections. When we create
|
|---|
| 1824 | * a callback session, the framework does not know arguments for the new
|
|---|
| 1825 | * connections.
|
|---|
| 1826 | *
|
|---|
| 1827 | * The proper solution seems to be to implement parallel exchanges using
|
|---|
| 1828 | * tagging.
|
|---|
| 1829 | */
|
|---|
| 1830 | void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
|
|---|
| 1831 | sysarg_t arg3)
|
|---|
| 1832 | {
|
|---|
| 1833 | sess->arg1 = arg1;
|
|---|
| 1834 | sess->arg2 = arg2;
|
|---|
| 1835 | sess->arg3 = arg3;
|
|---|
| 1836 | }
|
|---|
| 1837 |
|
|---|
| 1838 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
|---|
| 1839 | *
|
|---|
| 1840 | * Ask through phone for a new connection to some service and block until
|
|---|
| 1841 | * success.
|
|---|
| 1842 | *
|
|---|
| 1843 | * @param mgmt Exchange management style.
|
|---|
| 1844 | * @param exch Exchange for sending the message.
|
|---|
| 1845 | * @param arg1 User defined argument.
|
|---|
| 1846 | * @param arg2 User defined argument.
|
|---|
| 1847 | * @param arg3 User defined argument.
|
|---|
| 1848 | *
|
|---|
| 1849 | * @return New session on success or NULL on error.
|
|---|
| 1850 | *
|
|---|
| 1851 | */
|
|---|
| 1852 | async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
|
|---|
| 1853 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
|---|
| 1854 | {
|
|---|
| 1855 | if (exch == NULL) {
|
|---|
| 1856 | errno = ENOENT;
|
|---|
| 1857 | return NULL;
|
|---|
| 1858 | }
|
|---|
| 1859 |
|
|---|
| 1860 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
|---|
| 1861 | if (sess == NULL) {
|
|---|
| 1862 | errno = ENOMEM;
|
|---|
| 1863 | return NULL;
|
|---|
| 1864 | }
|
|---|
| 1865 |
|
|---|
| 1866 | int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
|
|---|
| 1867 | IPC_FLAG_BLOCKING);
|
|---|
| 1868 |
|
|---|
| 1869 | if (phone < 0) {
|
|---|
| 1870 | errno = phone;
|
|---|
| 1871 | free(sess);
|
|---|
| 1872 | return NULL;
|
|---|
| 1873 | }
|
|---|
| 1874 |
|
|---|
| 1875 | sess->mgmt = mgmt;
|
|---|
| 1876 | sess->phone = phone;
|
|---|
| 1877 | sess->arg1 = arg1;
|
|---|
| 1878 | sess->arg2 = arg2;
|
|---|
| 1879 | sess->arg3 = arg3;
|
|---|
| 1880 |
|
|---|
| 1881 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
|---|
| 1882 | sess->remote_state_data = NULL;
|
|---|
| 1883 |
|
|---|
| 1884 | list_initialize(&sess->exch_list);
|
|---|
| 1885 | fibril_mutex_initialize(&sess->mutex);
|
|---|
| 1886 | atomic_set(&sess->refcnt, 0);
|
|---|
| 1887 |
|
|---|
| 1888 | return sess;
|
|---|
| 1889 | }
|
|---|
| 1890 |
|
|---|
| 1891 | /** Connect to a task specified by id.
|
|---|
| 1892 | *
|
|---|
| 1893 | */
|
|---|
| 1894 | async_sess_t *async_connect_kbox(task_id_t id)
|
|---|
| 1895 | {
|
|---|
| 1896 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
|---|
| 1897 | if (sess == NULL) {
|
|---|
| 1898 | errno = ENOMEM;
|
|---|
| 1899 | return NULL;
|
|---|
| 1900 | }
|
|---|
| 1901 |
|
|---|
| 1902 | int phone = ipc_connect_kbox(id);
|
|---|
| 1903 | if (phone < 0) {
|
|---|
| 1904 | errno = phone;
|
|---|
| 1905 | free(sess);
|
|---|
| 1906 | return NULL;
|
|---|
| 1907 | }
|
|---|
| 1908 |
|
|---|
| 1909 | sess->mgmt = EXCHANGE_ATOMIC;
|
|---|
| 1910 | sess->phone = phone;
|
|---|
| 1911 | sess->arg1 = 0;
|
|---|
| 1912 | sess->arg2 = 0;
|
|---|
| 1913 | sess->arg3 = 0;
|
|---|
| 1914 |
|
|---|
| 1915 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
|---|
| 1916 | sess->remote_state_data = NULL;
|
|---|
| 1917 |
|
|---|
| 1918 | list_initialize(&sess->exch_list);
|
|---|
| 1919 | fibril_mutex_initialize(&sess->mutex);
|
|---|
| 1920 | atomic_set(&sess->refcnt, 0);
|
|---|
| 1921 |
|
|---|
| 1922 | return sess;
|
|---|
| 1923 | }
|
|---|
| 1924 |
|
|---|
| 1925 | static int async_hangup_internal(int phone)
|
|---|
| 1926 | {
|
|---|
| 1927 | return ipc_hangup(phone);
|
|---|
| 1928 | }
|
|---|
| 1929 |
|
|---|
| 1930 | /** Wrapper for ipc_hangup.
|
|---|
| 1931 | *
|
|---|
| 1932 | * @param sess Session to hung up.
|
|---|
| 1933 | *
|
|---|
| 1934 | * @return Zero on success or a negative error code.
|
|---|
| 1935 | *
|
|---|
| 1936 | */
|
|---|
| 1937 | int async_hangup(async_sess_t *sess)
|
|---|
| 1938 | {
|
|---|
| 1939 | async_exch_t *exch;
|
|---|
| 1940 |
|
|---|
| 1941 | assert(sess);
|
|---|
| 1942 |
|
|---|
| 1943 | if (atomic_get(&sess->refcnt) > 0)
|
|---|
| 1944 | return EBUSY;
|
|---|
| 1945 |
|
|---|
| 1946 | fibril_mutex_lock(&async_sess_mutex);
|
|---|
| 1947 |
|
|---|
| 1948 | int rc = async_hangup_internal(sess->phone);
|
|---|
| 1949 |
|
|---|
| 1950 | while (!list_empty(&sess->exch_list)) {
|
|---|
| 1951 | exch = (async_exch_t *)
|
|---|
| 1952 | list_get_instance(list_first(&sess->exch_list),
|
|---|
| 1953 | async_exch_t, sess_link);
|
|---|
| 1954 |
|
|---|
| 1955 | list_remove(&exch->sess_link);
|
|---|
| 1956 | list_remove(&exch->global_link);
|
|---|
| 1957 | async_hangup_internal(exch->phone);
|
|---|
| 1958 | free(exch);
|
|---|
| 1959 | }
|
|---|
| 1960 |
|
|---|
| 1961 | free(sess);
|
|---|
| 1962 |
|
|---|
| 1963 | fibril_mutex_unlock(&async_sess_mutex);
|
|---|
| 1964 |
|
|---|
| 1965 | return rc;
|
|---|
| 1966 | }
|
|---|
| 1967 |
|
|---|
| 1968 | /** Interrupt one thread of this task from waiting for IPC. */
|
|---|
| 1969 | void async_poke(void)
|
|---|
| 1970 | {
|
|---|
| 1971 | ipc_poke();
|
|---|
| 1972 | }
|
|---|
| 1973 |
|
|---|
| 1974 | /** Start new exchange in a session.
|
|---|
| 1975 | *
|
|---|
| 1976 | * @param session Session.
|
|---|
| 1977 | *
|
|---|
| 1978 | * @return New exchange or NULL on error.
|
|---|
| 1979 | *
|
|---|
| 1980 | */
|
|---|
| 1981 | async_exch_t *async_exchange_begin(async_sess_t *sess)
|
|---|
| 1982 | {
|
|---|
| 1983 | if (sess == NULL)
|
|---|
| 1984 | return NULL;
|
|---|
| 1985 |
|
|---|
| 1986 | async_exch_t *exch;
|
|---|
| 1987 |
|
|---|
| 1988 | fibril_mutex_lock(&async_sess_mutex);
|
|---|
| 1989 |
|
|---|
| 1990 | if (!list_empty(&sess->exch_list)) {
|
|---|
| 1991 | /*
|
|---|
| 1992 | * There are inactive exchanges in the session.
|
|---|
| 1993 | */
|
|---|
| 1994 | exch = (async_exch_t *)
|
|---|
| 1995 | list_get_instance(list_first(&sess->exch_list),
|
|---|
| 1996 | async_exch_t, sess_link);
|
|---|
| 1997 |
|
|---|
| 1998 | list_remove(&exch->sess_link);
|
|---|
| 1999 | list_remove(&exch->global_link);
|
|---|
| 2000 | } else {
|
|---|
| 2001 | /*
|
|---|
| 2002 | * There are no available exchanges in the session.
|
|---|
| 2003 | */
|
|---|
| 2004 |
|
|---|
| 2005 | if ((sess->mgmt == EXCHANGE_ATOMIC) ||
|
|---|
| 2006 | (sess->mgmt == EXCHANGE_SERIALIZE)) {
|
|---|
| 2007 | exch = (async_exch_t *) malloc(sizeof(async_exch_t));
|
|---|
| 2008 | if (exch != NULL) {
|
|---|
| 2009 | link_initialize(&exch->sess_link);
|
|---|
| 2010 | link_initialize(&exch->global_link);
|
|---|
| 2011 | exch->sess = sess;
|
|---|
| 2012 | exch->phone = sess->phone;
|
|---|
| 2013 | }
|
|---|
| 2014 | } else { /* EXCHANGE_PARALLEL */
|
|---|
| 2015 | /*
|
|---|
| 2016 | * Make a one-time attempt to connect a new data phone.
|
|---|
| 2017 | */
|
|---|
| 2018 |
|
|---|
| 2019 | int phone;
|
|---|
| 2020 |
|
|---|
| 2021 | retry:
|
|---|
| 2022 | phone = async_connect_me_to_internal(sess->phone, sess->arg1,
|
|---|
| 2023 | sess->arg2, sess->arg3, 0);
|
|---|
| 2024 | if (phone >= 0) {
|
|---|
| 2025 | exch = (async_exch_t *) malloc(sizeof(async_exch_t));
|
|---|
| 2026 | if (exch != NULL) {
|
|---|
| 2027 | link_initialize(&exch->sess_link);
|
|---|
| 2028 | link_initialize(&exch->global_link);
|
|---|
| 2029 | exch->sess = sess;
|
|---|
| 2030 | exch->phone = phone;
|
|---|
| 2031 | } else
|
|---|
| 2032 | async_hangup_internal(phone);
|
|---|
| 2033 | } else if (!list_empty(&inactive_exch_list)) {
|
|---|
| 2034 | /*
|
|---|
| 2035 | * We did not manage to connect a new phone. But we
|
|---|
| 2036 | * can try to close some of the currently inactive
|
|---|
| 2037 | * connections in other sessions and try again.
|
|---|
| 2038 | */
|
|---|
| 2039 | exch = (async_exch_t *)
|
|---|
| 2040 | list_get_instance(list_first(&inactive_exch_list),
|
|---|
| 2041 | async_exch_t, global_link);
|
|---|
| 2042 |
|
|---|
| 2043 | list_remove(&exch->sess_link);
|
|---|
| 2044 | list_remove(&exch->global_link);
|
|---|
| 2045 | async_hangup_internal(exch->phone);
|
|---|
| 2046 | free(exch);
|
|---|
| 2047 | goto retry;
|
|---|
| 2048 | } else {
|
|---|
| 2049 | /*
|
|---|
| 2050 | * Wait for a phone to become available.
|
|---|
| 2051 | */
|
|---|
| 2052 | fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
|
|---|
| 2053 | goto retry;
|
|---|
| 2054 | }
|
|---|
| 2055 | }
|
|---|
| 2056 | }
|
|---|
| 2057 |
|
|---|
| 2058 | fibril_mutex_unlock(&async_sess_mutex);
|
|---|
| 2059 |
|
|---|
| 2060 | if (exch != NULL) {
|
|---|
| 2061 | atomic_inc(&sess->refcnt);
|
|---|
| 2062 |
|
|---|
| 2063 | if (sess->mgmt == EXCHANGE_SERIALIZE)
|
|---|
| 2064 | fibril_mutex_lock(&sess->mutex);
|
|---|
| 2065 | }
|
|---|
| 2066 |
|
|---|
| 2067 | return exch;
|
|---|
| 2068 | }
|
|---|
| 2069 |
|
|---|
| 2070 | /** Finish an exchange.
|
|---|
| 2071 | *
|
|---|
| 2072 | * @param exch Exchange to finish.
|
|---|
| 2073 | *
|
|---|
| 2074 | */
|
|---|
| 2075 | void async_exchange_end(async_exch_t *exch)
|
|---|
| 2076 | {
|
|---|
| 2077 | if (exch == NULL)
|
|---|
| 2078 | return;
|
|---|
| 2079 |
|
|---|
| 2080 | async_sess_t *sess = exch->sess;
|
|---|
| 2081 |
|
|---|
| 2082 | atomic_dec(&sess->refcnt);
|
|---|
| 2083 |
|
|---|
| 2084 | if (sess->mgmt == EXCHANGE_SERIALIZE)
|
|---|
| 2085 | fibril_mutex_unlock(&sess->mutex);
|
|---|
| 2086 |
|
|---|
| 2087 | fibril_mutex_lock(&async_sess_mutex);
|
|---|
| 2088 |
|
|---|
| 2089 | list_append(&exch->sess_link, &sess->exch_list);
|
|---|
| 2090 | list_append(&exch->global_link, &inactive_exch_list);
|
|---|
| 2091 | fibril_condvar_signal(&avail_phone_cv);
|
|---|
| 2092 |
|
|---|
| 2093 | fibril_mutex_unlock(&async_sess_mutex);
|
|---|
| 2094 | }
|
|---|
| 2095 |
|
|---|
| 2096 | /** Wrapper for IPC_M_SHARE_IN calls using the async framework.
|
|---|
| 2097 | *
|
|---|
| 2098 | * @param exch Exchange for sending the message.
|
|---|
| 2099 | * @param size Size of the destination address space area.
|
|---|
| 2100 | * @param arg User defined argument.
|
|---|
| 2101 | * @param flags Storage for the received flags. Can be NULL.
|
|---|
| 2102 | * @param dst Destination address space area base. Cannot be NULL.
|
|---|
| 2103 | *
|
|---|
| 2104 | * @return Zero on success or a negative error code from errno.h.
|
|---|
| 2105 | *
|
|---|
| 2106 | */
|
|---|
| 2107 | int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
|
|---|
| 2108 | unsigned int *flags, void **dst)
|
|---|
| 2109 | {
|
|---|
| 2110 | if (exch == NULL)
|
|---|
| 2111 | return ENOENT;
|
|---|
| 2112 |
|
|---|
| 2113 | sysarg_t _flags = 0;
|
|---|
| 2114 | sysarg_t _dst = (sysarg_t) -1;
|
|---|
| 2115 | int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
|
|---|
| 2116 | arg, NULL, &_flags, NULL, &_dst);
|
|---|
| 2117 |
|
|---|
| 2118 | if (flags)
|
|---|
| 2119 | *flags = (unsigned int) _flags;
|
|---|
| 2120 |
|
|---|
| 2121 | *dst = (void *) _dst;
|
|---|
| 2122 | return res;
|
|---|
| 2123 | }
|
|---|
| 2124 |
|
|---|
| 2125 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
|---|
| 2126 | *
|
|---|
| 2127 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
|
|---|
| 2128 | * calls so that the user doesn't have to remember the meaning of each IPC
|
|---|
| 2129 | * argument.
|
|---|
| 2130 | *
|
|---|
| 2131 | * So far, this wrapper is to be used from within a connection fibril.
|
|---|
| 2132 | *
|
|---|
| 2133 | * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
|
|---|
| 2134 | * @param size Destination address space area size.
|
|---|
| 2135 | *
|
|---|
| 2136 | * @return True on success, false on failure.
|
|---|
| 2137 | *
|
|---|
| 2138 | */
|
|---|
| 2139 | bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
|
|---|
| 2140 | {
|
|---|
| 2141 | assert(callid);
|
|---|
| 2142 | assert(size);
|
|---|
| 2143 |
|
|---|
| 2144 | ipc_call_t data;
|
|---|
| 2145 | *callid = async_get_call(&data);
|
|---|
| 2146 |
|
|---|
| 2147 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
|
|---|
| 2148 | return false;
|
|---|
| 2149 |
|
|---|
| 2150 | *size = (size_t) IPC_GET_ARG1(data);
|
|---|
| 2151 | return true;
|
|---|
| 2152 | }
|
|---|
| 2153 |
|
|---|
| 2154 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
|---|
| 2155 | *
|
|---|
| 2156 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
|
|---|
| 2157 | * calls so that the user doesn't have to remember the meaning of each IPC
|
|---|
| 2158 | * argument.
|
|---|
| 2159 | *
|
|---|
| 2160 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
|---|
| 2161 | * @param src Source address space base.
|
|---|
| 2162 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
|---|
| 2163 | *
|
|---|
| 2164 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 2165 | *
|
|---|
| 2166 | */
|
|---|
| 2167 | int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
|
|---|
| 2168 | {
|
|---|
| 2169 | return ipc_answer_3(callid, EOK, (sysarg_t) src, (sysarg_t) flags,
|
|---|
| 2170 | (sysarg_t) __entry);
|
|---|
| 2171 | }
|
|---|
| 2172 |
|
|---|
| 2173 | /** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
|
|---|
| 2174 | *
|
|---|
| 2175 | * @param exch Exchange for sending the message.
|
|---|
| 2176 | * @param src Source address space area base address.
|
|---|
| 2177 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
|---|
| 2178 | *
|
|---|
| 2179 | * @return Zero on success or a negative error code from errno.h.
|
|---|
| 2180 | *
|
|---|
| 2181 | */
|
|---|
| 2182 | int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
|
|---|
| 2183 | {
|
|---|
| 2184 | if (exch == NULL)
|
|---|
| 2185 | return ENOENT;
|
|---|
| 2186 |
|
|---|
| 2187 | return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
|
|---|
| 2188 | (sysarg_t) flags);
|
|---|
| 2189 | }
|
|---|
| 2190 |
|
|---|
| 2191 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
|---|
| 2192 | *
|
|---|
| 2193 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
|
|---|
| 2194 | * calls so that the user doesn't have to remember the meaning of each IPC
|
|---|
| 2195 | * argument.
|
|---|
| 2196 | *
|
|---|
| 2197 | * So far, this wrapper is to be used from within a connection fibril.
|
|---|
| 2198 | *
|
|---|
| 2199 | * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
|
|---|
| 2200 | * @param size Storage for the source address space area size.
|
|---|
| 2201 | * @param flags Storage for the sharing flags.
|
|---|
| 2202 | *
|
|---|
| 2203 | * @return True on success, false on failure.
|
|---|
| 2204 | *
|
|---|
| 2205 | */
|
|---|
| 2206 | bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
|
|---|
| 2207 | {
|
|---|
| 2208 | assert(callid);
|
|---|
| 2209 | assert(size);
|
|---|
| 2210 | assert(flags);
|
|---|
| 2211 |
|
|---|
| 2212 | ipc_call_t data;
|
|---|
| 2213 | *callid = async_get_call(&data);
|
|---|
| 2214 |
|
|---|
| 2215 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
|
|---|
| 2216 | return false;
|
|---|
| 2217 |
|
|---|
| 2218 | *size = (size_t) IPC_GET_ARG2(data);
|
|---|
| 2219 | *flags = (unsigned int) IPC_GET_ARG3(data);
|
|---|
| 2220 | return true;
|
|---|
| 2221 | }
|
|---|
| 2222 |
|
|---|
| 2223 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
|---|
| 2224 | *
|
|---|
| 2225 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
|
|---|
| 2226 | * calls so that the user doesn't have to remember the meaning of each IPC
|
|---|
| 2227 | * argument.
|
|---|
| 2228 | *
|
|---|
| 2229 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
|---|
| 2230 | * @param dst Destination address space area base address.
|
|---|
| 2231 | *
|
|---|
| 2232 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 2233 | *
|
|---|
| 2234 | */
|
|---|
| 2235 | int async_share_out_finalize(ipc_callid_t callid, void **dst)
|
|---|
| 2236 | {
|
|---|
| 2237 | return ipc_answer_2(callid, EOK, (sysarg_t) __entry, (sysarg_t) dst);
|
|---|
| 2238 | }
|
|---|
| 2239 |
|
|---|
| 2240 | /** Start IPC_M_DATA_READ using the async framework.
|
|---|
| 2241 | *
|
|---|
| 2242 | * @param exch Exchange for sending the message.
|
|---|
| 2243 | * @param dst Address of the beginning of the destination buffer.
|
|---|
| 2244 | * @param size Size of the destination buffer (in bytes).
|
|---|
| 2245 | * @param dataptr Storage of call data (arg 2 holds actual data size).
|
|---|
| 2246 | *
|
|---|
| 2247 | * @return Hash of the sent message or 0 on error.
|
|---|
| 2248 | *
|
|---|
| 2249 | */
|
|---|
| 2250 | aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
|
|---|
| 2251 | ipc_call_t *dataptr)
|
|---|
| 2252 | {
|
|---|
| 2253 | return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
|
|---|
| 2254 | (sysarg_t) size, dataptr);
|
|---|
| 2255 | }
|
|---|
| 2256 |
|
|---|
| 2257 | /** Wrapper for IPC_M_DATA_READ calls using the async framework.
|
|---|
| 2258 | *
|
|---|
| 2259 | * @param exch Exchange for sending the message.
|
|---|
| 2260 | * @param dst Address of the beginning of the destination buffer.
|
|---|
| 2261 | * @param size Size of the destination buffer.
|
|---|
| 2262 | *
|
|---|
| 2263 | * @return Zero on success or a negative error code from errno.h.
|
|---|
| 2264 | *
|
|---|
| 2265 | */
|
|---|
| 2266 | int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
|
|---|
| 2267 | {
|
|---|
| 2268 | if (exch == NULL)
|
|---|
| 2269 | return ENOENT;
|
|---|
| 2270 |
|
|---|
| 2271 | return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
|
|---|
| 2272 | (sysarg_t) size);
|
|---|
| 2273 | }
|
|---|
| 2274 |
|
|---|
| 2275 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
|---|
| 2276 | *
|
|---|
| 2277 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
|
|---|
| 2278 | * calls so that the user doesn't have to remember the meaning of each IPC
|
|---|
| 2279 | * argument.
|
|---|
| 2280 | *
|
|---|
| 2281 | * So far, this wrapper is to be used from within a connection fibril.
|
|---|
| 2282 | *
|
|---|
| 2283 | * @param callid Storage for the hash of the IPC_M_DATA_READ.
|
|---|
| 2284 | * @param size Storage for the maximum size. Can be NULL.
|
|---|
| 2285 | *
|
|---|
| 2286 | * @return True on success, false on failure.
|
|---|
| 2287 | *
|
|---|
| 2288 | */
|
|---|
| 2289 | bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
|
|---|
| 2290 | {
|
|---|
| 2291 | assert(callid);
|
|---|
| 2292 |
|
|---|
| 2293 | ipc_call_t data;
|
|---|
| 2294 | *callid = async_get_call(&data);
|
|---|
| 2295 |
|
|---|
| 2296 | if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
|
|---|
| 2297 | return false;
|
|---|
| 2298 |
|
|---|
| 2299 | if (size)
|
|---|
| 2300 | *size = (size_t) IPC_GET_ARG2(data);
|
|---|
| 2301 |
|
|---|
| 2302 | return true;
|
|---|
| 2303 | }
|
|---|
| 2304 |
|
|---|
| 2305 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
|---|
| 2306 | *
|
|---|
| 2307 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
|---|
| 2308 | * calls so that the user doesn't have to remember the meaning of each IPC
|
|---|
| 2309 | * argument.
|
|---|
| 2310 | *
|
|---|
| 2311 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
|---|
| 2312 | * @param src Source address for the IPC_M_DATA_READ call.
|
|---|
| 2313 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
|---|
| 2314 | * the maximum size announced by the sender.
|
|---|
| 2315 | *
|
|---|
| 2316 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 2317 | *
|
|---|
| 2318 | */
|
|---|
| 2319 | int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
|
|---|
| 2320 | {
|
|---|
| 2321 | return ipc_answer_2(callid, EOK, (sysarg_t) src, (sysarg_t) size);
|
|---|
| 2322 | }
|
|---|
| 2323 |
|
|---|
| 2324 | /** Wrapper for forwarding any read request
|
|---|
| 2325 | *
|
|---|
| 2326 | */
|
|---|
| 2327 | int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
|
|---|
| 2328 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
|---|
| 2329 | ipc_call_t *dataptr)
|
|---|
| 2330 | {
|
|---|
| 2331 | if (exch == NULL)
|
|---|
| 2332 | return ENOENT;
|
|---|
| 2333 |
|
|---|
| 2334 | ipc_callid_t callid;
|
|---|
| 2335 | if (!async_data_read_receive(&callid, NULL)) {
|
|---|
| 2336 | ipc_answer_0(callid, EINVAL);
|
|---|
| 2337 | return EINVAL;
|
|---|
| 2338 | }
|
|---|
| 2339 |
|
|---|
| 2340 | aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
|
|---|
| 2341 | dataptr);
|
|---|
| 2342 | if (msg == 0) {
|
|---|
| 2343 | ipc_answer_0(callid, EINVAL);
|
|---|
| 2344 | return EINVAL;
|
|---|
| 2345 | }
|
|---|
| 2346 |
|
|---|
| 2347 | int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
|
|---|
| 2348 | IPC_FF_ROUTE_FROM_ME);
|
|---|
| 2349 | if (retval != EOK) {
|
|---|
| 2350 | async_forget(msg);
|
|---|
| 2351 | ipc_answer_0(callid, retval);
|
|---|
| 2352 | return retval;
|
|---|
| 2353 | }
|
|---|
| 2354 |
|
|---|
| 2355 | sysarg_t rc;
|
|---|
| 2356 | async_wait_for(msg, &rc);
|
|---|
| 2357 |
|
|---|
| 2358 | return (int) rc;
|
|---|
| 2359 | }
|
|---|
| 2360 |
|
|---|
| 2361 | /** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
|
|---|
| 2362 | *
|
|---|
| 2363 | * @param exch Exchange for sending the message.
|
|---|
| 2364 | * @param src Address of the beginning of the source buffer.
|
|---|
| 2365 | * @param size Size of the source buffer.
|
|---|
| 2366 | *
|
|---|
| 2367 | * @return Zero on success or a negative error code from errno.h.
|
|---|
| 2368 | *
|
|---|
| 2369 | */
|
|---|
| 2370 | int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
|
|---|
| 2371 | {
|
|---|
| 2372 | if (exch == NULL)
|
|---|
| 2373 | return ENOENT;
|
|---|
| 2374 |
|
|---|
| 2375 | return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
|
|---|
| 2376 | (sysarg_t) size);
|
|---|
| 2377 | }
|
|---|
| 2378 |
|
|---|
| 2379 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
|---|
| 2380 | *
|
|---|
| 2381 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
|
|---|
| 2382 | * calls so that the user doesn't have to remember the meaning of each IPC
|
|---|
| 2383 | * argument.
|
|---|
| 2384 | *
|
|---|
| 2385 | * So far, this wrapper is to be used from within a connection fibril.
|
|---|
| 2386 | *
|
|---|
| 2387 | * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
|
|---|
| 2388 | * @param size Storage for the suggested size. May be NULL.
|
|---|
| 2389 | *
|
|---|
| 2390 | * @return True on success, false on failure.
|
|---|
| 2391 | *
|
|---|
| 2392 | */
|
|---|
| 2393 | bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
|
|---|
| 2394 | {
|
|---|
| 2395 | assert(callid);
|
|---|
| 2396 |
|
|---|
| 2397 | ipc_call_t data;
|
|---|
| 2398 | *callid = async_get_call(&data);
|
|---|
| 2399 |
|
|---|
| 2400 | if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
|
|---|
| 2401 | return false;
|
|---|
| 2402 |
|
|---|
| 2403 | if (size)
|
|---|
| 2404 | *size = (size_t) IPC_GET_ARG2(data);
|
|---|
| 2405 |
|
|---|
| 2406 | return true;
|
|---|
| 2407 | }
|
|---|
| 2408 |
|
|---|
| 2409 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
|---|
| 2410 | *
|
|---|
| 2411 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
|
|---|
| 2412 | * calls so that the user doesn't have to remember the meaning of each IPC
|
|---|
| 2413 | * argument.
|
|---|
| 2414 | *
|
|---|
| 2415 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
|---|
| 2416 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
|---|
| 2417 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
|---|
| 2418 | *
|
|---|
| 2419 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 2420 | *
|
|---|
| 2421 | */
|
|---|
| 2422 | int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
|
|---|
| 2423 | {
|
|---|
| 2424 | return ipc_answer_2(callid, EOK, (sysarg_t) dst, (sysarg_t) size);
|
|---|
| 2425 | }
|
|---|
| 2426 |
|
|---|
| 2427 | /** Wrapper for receiving binary data or strings
|
|---|
| 2428 | *
|
|---|
| 2429 | * This wrapper only makes it more comfortable to use async_data_write_*
|
|---|
| 2430 | * functions to receive binary data or strings.
|
|---|
| 2431 | *
|
|---|
| 2432 | * @param data Pointer to data pointer (which should be later disposed
|
|---|
| 2433 | * by free()). If the operation fails, the pointer is not
|
|---|
| 2434 | * touched.
|
|---|
| 2435 | * @param nullterm If true then the received data is always zero terminated.
|
|---|
| 2436 | * This also causes to allocate one extra byte beyond the
|
|---|
| 2437 | * raw transmitted data.
|
|---|
| 2438 | * @param min_size Minimum size (in bytes) of the data to receive.
|
|---|
| 2439 | * @param max_size Maximum size (in bytes) of the data to receive. 0 means
|
|---|
| 2440 | * no limit.
|
|---|
| 2441 | * @param granulariy If non-zero then the size of the received data has to
|
|---|
| 2442 | * be divisible by this value.
|
|---|
| 2443 | * @param received If not NULL, the size of the received data is stored here.
|
|---|
| 2444 | *
|
|---|
| 2445 | * @return Zero on success or a value from @ref errno.h on failure.
|
|---|
| 2446 | *
|
|---|
| 2447 | */
|
|---|
| 2448 | int async_data_write_accept(void **data, const bool nullterm,
|
|---|
| 2449 | const size_t min_size, const size_t max_size, const size_t granularity,
|
|---|
| 2450 | size_t *received)
|
|---|
| 2451 | {
|
|---|
| 2452 | assert(data);
|
|---|
| 2453 |
|
|---|
| 2454 | ipc_callid_t callid;
|
|---|
| 2455 | size_t size;
|
|---|
| 2456 | if (!async_data_write_receive(&callid, &size)) {
|
|---|
| 2457 | ipc_answer_0(callid, EINVAL);
|
|---|
| 2458 | return EINVAL;
|
|---|
| 2459 | }
|
|---|
| 2460 |
|
|---|
| 2461 | if (size < min_size) {
|
|---|
| 2462 | ipc_answer_0(callid, EINVAL);
|
|---|
| 2463 | return EINVAL;
|
|---|
| 2464 | }
|
|---|
| 2465 |
|
|---|
| 2466 | if ((max_size > 0) && (size > max_size)) {
|
|---|
| 2467 | ipc_answer_0(callid, EINVAL);
|
|---|
| 2468 | return EINVAL;
|
|---|
| 2469 | }
|
|---|
| 2470 |
|
|---|
| 2471 | if ((granularity > 0) && ((size % granularity) != 0)) {
|
|---|
| 2472 | ipc_answer_0(callid, EINVAL);
|
|---|
| 2473 | return EINVAL;
|
|---|
| 2474 | }
|
|---|
| 2475 |
|
|---|
| 2476 | void *_data;
|
|---|
| 2477 |
|
|---|
| 2478 | if (nullterm)
|
|---|
| 2479 | _data = malloc(size + 1);
|
|---|
| 2480 | else
|
|---|
| 2481 | _data = malloc(size);
|
|---|
| 2482 |
|
|---|
| 2483 | if (_data == NULL) {
|
|---|
| 2484 | ipc_answer_0(callid, ENOMEM);
|
|---|
| 2485 | return ENOMEM;
|
|---|
| 2486 | }
|
|---|
| 2487 |
|
|---|
| 2488 | int rc = async_data_write_finalize(callid, _data, size);
|
|---|
| 2489 | if (rc != EOK) {
|
|---|
| 2490 | free(_data);
|
|---|
| 2491 | return rc;
|
|---|
| 2492 | }
|
|---|
| 2493 |
|
|---|
| 2494 | if (nullterm)
|
|---|
| 2495 | ((char *) _data)[size] = 0;
|
|---|
| 2496 |
|
|---|
| 2497 | *data = _data;
|
|---|
| 2498 | if (received != NULL)
|
|---|
| 2499 | *received = size;
|
|---|
| 2500 |
|
|---|
| 2501 | return EOK;
|
|---|
| 2502 | }
|
|---|
| 2503 |
|
|---|
| 2504 | /** Wrapper for voiding any data that is about to be received
|
|---|
| 2505 | *
|
|---|
| 2506 | * This wrapper can be used to void any pending data
|
|---|
| 2507 | *
|
|---|
| 2508 | * @param retval Error value from @ref errno.h to be returned to the caller.
|
|---|
| 2509 | *
|
|---|
| 2510 | */
|
|---|
| 2511 | void async_data_write_void(sysarg_t retval)
|
|---|
| 2512 | {
|
|---|
| 2513 | ipc_callid_t callid;
|
|---|
| 2514 | async_data_write_receive(&callid, NULL);
|
|---|
| 2515 | ipc_answer_0(callid, retval);
|
|---|
| 2516 | }
|
|---|
| 2517 |
|
|---|
| 2518 | /** Wrapper for forwarding any data that is about to be received
|
|---|
| 2519 | *
|
|---|
| 2520 | */
|
|---|
| 2521 | int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
|
|---|
| 2522 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
|---|
| 2523 | ipc_call_t *dataptr)
|
|---|
| 2524 | {
|
|---|
| 2525 | if (exch == NULL)
|
|---|
| 2526 | return ENOENT;
|
|---|
| 2527 |
|
|---|
| 2528 | ipc_callid_t callid;
|
|---|
| 2529 | if (!async_data_write_receive(&callid, NULL)) {
|
|---|
| 2530 | ipc_answer_0(callid, EINVAL);
|
|---|
| 2531 | return EINVAL;
|
|---|
| 2532 | }
|
|---|
| 2533 |
|
|---|
| 2534 | aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
|
|---|
| 2535 | dataptr);
|
|---|
| 2536 | if (msg == 0) {
|
|---|
| 2537 | ipc_answer_0(callid, EINVAL);
|
|---|
| 2538 | return EINVAL;
|
|---|
| 2539 | }
|
|---|
| 2540 |
|
|---|
| 2541 | int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
|
|---|
| 2542 | IPC_FF_ROUTE_FROM_ME);
|
|---|
| 2543 | if (retval != EOK) {
|
|---|
| 2544 | async_forget(msg);
|
|---|
| 2545 | ipc_answer_0(callid, retval);
|
|---|
| 2546 | return retval;
|
|---|
| 2547 | }
|
|---|
| 2548 |
|
|---|
| 2549 | sysarg_t rc;
|
|---|
| 2550 | async_wait_for(msg, &rc);
|
|---|
| 2551 |
|
|---|
| 2552 | return (int) rc;
|
|---|
| 2553 | }
|
|---|
| 2554 |
|
|---|
| 2555 | /** Wrapper for sending an exchange over different exchange for cloning
|
|---|
| 2556 | *
|
|---|
| 2557 | * @param exch Exchange to be used for sending.
|
|---|
| 2558 | * @param clone_exch Exchange to be cloned.
|
|---|
| 2559 | *
|
|---|
| 2560 | */
|
|---|
| 2561 | int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
|
|---|
| 2562 | {
|
|---|
| 2563 | return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
|
|---|
| 2564 | }
|
|---|
| 2565 |
|
|---|
| 2566 | /** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
|
|---|
| 2567 | *
|
|---|
| 2568 | * If the current call is IPC_M_CONNECTION_CLONE then a new
|
|---|
| 2569 | * async session is created for the accepted phone.
|
|---|
| 2570 | *
|
|---|
| 2571 | * @param mgmt Exchange management style.
|
|---|
| 2572 | *
|
|---|
| 2573 | * @return New async session or NULL on failure.
|
|---|
| 2574 | *
|
|---|
| 2575 | */
|
|---|
| 2576 | async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
|
|---|
| 2577 | {
|
|---|
| 2578 | /* Accept the phone */
|
|---|
| 2579 | ipc_call_t call;
|
|---|
| 2580 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 2581 | int phone = (int) IPC_GET_ARG1(call);
|
|---|
| 2582 |
|
|---|
| 2583 | if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
|
|---|
| 2584 | (phone < 0)) {
|
|---|
| 2585 | async_answer_0(callid, EINVAL);
|
|---|
| 2586 | return NULL;
|
|---|
| 2587 | }
|
|---|
| 2588 |
|
|---|
| 2589 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
|---|
| 2590 | if (sess == NULL) {
|
|---|
| 2591 | async_answer_0(callid, ENOMEM);
|
|---|
| 2592 | return NULL;
|
|---|
| 2593 | }
|
|---|
| 2594 |
|
|---|
| 2595 | sess->mgmt = mgmt;
|
|---|
| 2596 | sess->phone = phone;
|
|---|
| 2597 | sess->arg1 = 0;
|
|---|
| 2598 | sess->arg2 = 0;
|
|---|
| 2599 | sess->arg3 = 0;
|
|---|
| 2600 |
|
|---|
| 2601 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
|---|
| 2602 | sess->remote_state_data = NULL;
|
|---|
| 2603 |
|
|---|
| 2604 | list_initialize(&sess->exch_list);
|
|---|
| 2605 | fibril_mutex_initialize(&sess->mutex);
|
|---|
| 2606 | atomic_set(&sess->refcnt, 0);
|
|---|
| 2607 |
|
|---|
| 2608 | /* Acknowledge the cloned phone */
|
|---|
| 2609 | async_answer_0(callid, EOK);
|
|---|
| 2610 |
|
|---|
| 2611 | return sess;
|
|---|
| 2612 | }
|
|---|
| 2613 |
|
|---|
| 2614 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
|---|
| 2615 | *
|
|---|
| 2616 | * If the current call is IPC_M_CONNECT_TO_ME then a new
|
|---|
| 2617 | * async session is created for the accepted phone.
|
|---|
| 2618 | *
|
|---|
| 2619 | * @param mgmt Exchange management style.
|
|---|
| 2620 | *
|
|---|
| 2621 | * @return New async session.
|
|---|
| 2622 | * @return NULL on failure.
|
|---|
| 2623 | *
|
|---|
| 2624 | */
|
|---|
| 2625 | async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
|
|---|
| 2626 | {
|
|---|
| 2627 | /* Accept the phone */
|
|---|
| 2628 | ipc_call_t call;
|
|---|
| 2629 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 2630 | int phone = (int) IPC_GET_ARG5(call);
|
|---|
| 2631 |
|
|---|
| 2632 | if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
|
|---|
| 2633 | (phone < 0)) {
|
|---|
| 2634 | async_answer_0(callid, EINVAL);
|
|---|
| 2635 | return NULL;
|
|---|
| 2636 | }
|
|---|
| 2637 |
|
|---|
| 2638 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
|---|
| 2639 | if (sess == NULL) {
|
|---|
| 2640 | async_answer_0(callid, ENOMEM);
|
|---|
| 2641 | return NULL;
|
|---|
| 2642 | }
|
|---|
| 2643 |
|
|---|
| 2644 | sess->mgmt = mgmt;
|
|---|
| 2645 | sess->phone = phone;
|
|---|
| 2646 | sess->arg1 = 0;
|
|---|
| 2647 | sess->arg2 = 0;
|
|---|
| 2648 | sess->arg3 = 0;
|
|---|
| 2649 |
|
|---|
| 2650 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
|---|
| 2651 | sess->remote_state_data = NULL;
|
|---|
| 2652 |
|
|---|
| 2653 | list_initialize(&sess->exch_list);
|
|---|
| 2654 | fibril_mutex_initialize(&sess->mutex);
|
|---|
| 2655 | atomic_set(&sess->refcnt, 0);
|
|---|
| 2656 |
|
|---|
| 2657 | /* Acknowledge the connected phone */
|
|---|
| 2658 | async_answer_0(callid, EOK);
|
|---|
| 2659 |
|
|---|
| 2660 | return sess;
|
|---|
| 2661 | }
|
|---|
| 2662 |
|
|---|
| 2663 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
|---|
| 2664 | *
|
|---|
| 2665 | * If the call is IPC_M_CONNECT_TO_ME then a new
|
|---|
| 2666 | * async session is created. However, the phone is
|
|---|
| 2667 | * not accepted automatically.
|
|---|
| 2668 | *
|
|---|
| 2669 | * @param mgmt Exchange management style.
|
|---|
| 2670 | * @param call Call data.
|
|---|
| 2671 | *
|
|---|
| 2672 | * @return New async session.
|
|---|
| 2673 | * @return NULL on failure.
|
|---|
| 2674 | * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
|
|---|
| 2675 | *
|
|---|
| 2676 | */
|
|---|
| 2677 | async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
|
|---|
| 2678 | {
|
|---|
| 2679 | int phone = (int) IPC_GET_ARG5(*call);
|
|---|
| 2680 |
|
|---|
| 2681 | if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
|
|---|
| 2682 | (phone < 0))
|
|---|
| 2683 | return NULL;
|
|---|
| 2684 |
|
|---|
| 2685 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
|---|
| 2686 | if (sess == NULL)
|
|---|
| 2687 | return NULL;
|
|---|
| 2688 |
|
|---|
| 2689 | sess->mgmt = mgmt;
|
|---|
| 2690 | sess->phone = phone;
|
|---|
| 2691 | sess->arg1 = 0;
|
|---|
| 2692 | sess->arg2 = 0;
|
|---|
| 2693 | sess->arg3 = 0;
|
|---|
| 2694 |
|
|---|
| 2695 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
|---|
| 2696 | sess->remote_state_data = NULL;
|
|---|
| 2697 |
|
|---|
| 2698 | list_initialize(&sess->exch_list);
|
|---|
| 2699 | fibril_mutex_initialize(&sess->mutex);
|
|---|
| 2700 | atomic_set(&sess->refcnt, 0);
|
|---|
| 2701 |
|
|---|
| 2702 | return sess;
|
|---|
| 2703 | }
|
|---|
| 2704 |
|
|---|
| 2705 | int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
|
|---|
| 2706 | sysarg_t arg3, async_exch_t *other_exch)
|
|---|
| 2707 | {
|
|---|
| 2708 | return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
|
|---|
| 2709 | arg1, arg2, arg3, 0, other_exch->phone);
|
|---|
| 2710 | }
|
|---|
| 2711 |
|
|---|
| 2712 | bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
|
|---|
| 2713 | sysarg_t *arg2, sysarg_t *arg3)
|
|---|
| 2714 | {
|
|---|
| 2715 | assert(callid);
|
|---|
| 2716 |
|
|---|
| 2717 | ipc_call_t call;
|
|---|
| 2718 | *callid = async_get_call(&call);
|
|---|
| 2719 |
|
|---|
| 2720 | if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
|
|---|
| 2721 | return false;
|
|---|
| 2722 |
|
|---|
| 2723 | if (arg1)
|
|---|
| 2724 | *arg1 = IPC_GET_ARG1(call);
|
|---|
| 2725 | if (arg2)
|
|---|
| 2726 | *arg2 = IPC_GET_ARG2(call);
|
|---|
| 2727 | if (arg3)
|
|---|
| 2728 | *arg3 = IPC_GET_ARG3(call);
|
|---|
| 2729 |
|
|---|
| 2730 | return true;
|
|---|
| 2731 | }
|
|---|
| 2732 |
|
|---|
| 2733 | int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
|
|---|
| 2734 | {
|
|---|
| 2735 | return ipc_answer_1(callid, EOK, other_exch->phone);
|
|---|
| 2736 | }
|
|---|
| 2737 |
|
|---|
| 2738 | /** Lock and get session remote state
|
|---|
| 2739 | *
|
|---|
| 2740 | * Lock and get the local replica of the remote state
|
|---|
| 2741 | * in stateful sessions. The call should be paired
|
|---|
| 2742 | * with async_remote_state_release*().
|
|---|
| 2743 | *
|
|---|
| 2744 | * @param[in] sess Stateful session.
|
|---|
| 2745 | *
|
|---|
| 2746 | * @return Local replica of the remote state.
|
|---|
| 2747 | *
|
|---|
| 2748 | */
|
|---|
| 2749 | void *async_remote_state_acquire(async_sess_t *sess)
|
|---|
| 2750 | {
|
|---|
| 2751 | fibril_mutex_lock(&sess->remote_state_mtx);
|
|---|
| 2752 | return sess->remote_state_data;
|
|---|
| 2753 | }
|
|---|
| 2754 |
|
|---|
| 2755 | /** Update the session remote state
|
|---|
| 2756 | *
|
|---|
| 2757 | * Update the local replica of the remote state
|
|---|
| 2758 | * in stateful sessions. The remote state must
|
|---|
| 2759 | * be already locked.
|
|---|
| 2760 | *
|
|---|
| 2761 | * @param[in] sess Stateful session.
|
|---|
| 2762 | * @param[in] state New local replica of the remote state.
|
|---|
| 2763 | *
|
|---|
| 2764 | */
|
|---|
| 2765 | void async_remote_state_update(async_sess_t *sess, void *state)
|
|---|
| 2766 | {
|
|---|
| 2767 | assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
|
|---|
| 2768 | sess->remote_state_data = state;
|
|---|
| 2769 | }
|
|---|
| 2770 |
|
|---|
| 2771 | /** Release the session remote state
|
|---|
| 2772 | *
|
|---|
| 2773 | * Unlock the local replica of the remote state
|
|---|
| 2774 | * in stateful sessions.
|
|---|
| 2775 | *
|
|---|
| 2776 | * @param[in] sess Stateful session.
|
|---|
| 2777 | *
|
|---|
| 2778 | */
|
|---|
| 2779 | void async_remote_state_release(async_sess_t *sess)
|
|---|
| 2780 | {
|
|---|
| 2781 | assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
|
|---|
| 2782 |
|
|---|
| 2783 | fibril_mutex_unlock(&sess->remote_state_mtx);
|
|---|
| 2784 | }
|
|---|
| 2785 |
|
|---|
| 2786 | /** Release the session remote state and end an exchange
|
|---|
| 2787 | *
|
|---|
| 2788 | * Unlock the local replica of the remote state
|
|---|
| 2789 | * in stateful sessions. This is convenience function
|
|---|
| 2790 | * which gets the session pointer from the exchange
|
|---|
| 2791 | * and also ends the exchange.
|
|---|
| 2792 | *
|
|---|
| 2793 | * @param[in] exch Stateful session's exchange.
|
|---|
| 2794 | *
|
|---|
| 2795 | */
|
|---|
| 2796 | void async_remote_state_release_exchange(async_exch_t *exch)
|
|---|
| 2797 | {
|
|---|
| 2798 | if (exch == NULL)
|
|---|
| 2799 | return;
|
|---|
| 2800 |
|
|---|
| 2801 | async_sess_t *sess = exch->sess;
|
|---|
| 2802 | assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
|
|---|
| 2803 |
|
|---|
| 2804 | async_exchange_end(exch);
|
|---|
| 2805 | fibril_mutex_unlock(&sess->remote_state_mtx);
|
|---|
| 2806 | }
|
|---|
| 2807 |
|
|---|
| 2808 | /** @}
|
|---|
| 2809 | */
|
|---|