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