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