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