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