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