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