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