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