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