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