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