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