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