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