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