[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 | *
|
---|
[9591265] | 42 | * You should be able to write very simple multithreaded programs, the async
|
---|
| 43 | * framework will automatically take care of most synchronization problems.
|
---|
[80649a91] | 44 | *
|
---|
[9591265] | 45 | * Example of use (pseudo C):
|
---|
[c07544d3] | 46 | *
|
---|
[80649a91] | 47 | * 1) Multithreaded client application
|
---|
[9591265] | 48 | *
|
---|
[c07544d3] | 49 | * fibril_create(fibril1, ...);
|
---|
| 50 | * fibril_create(fibril2, ...);
|
---|
| 51 | * ...
|
---|
| 52 | *
|
---|
| 53 | * int fibril1(void *arg)
|
---|
| 54 | * {
|
---|
[007e6efa] | 55 | * conn = async_connect_me_to();
|
---|
[c07544d3] | 56 | * c1 = async_send(conn);
|
---|
| 57 | * c2 = async_send(conn);
|
---|
| 58 | * async_wait_for(c1);
|
---|
| 59 | * async_wait_for(c2);
|
---|
| 60 | * ...
|
---|
| 61 | * }
|
---|
[80649a91] | 62 | *
|
---|
| 63 | *
|
---|
| 64 | * 2) Multithreaded server application
|
---|
| 65 | *
|
---|
[c07544d3] | 66 | * main()
|
---|
| 67 | * {
|
---|
| 68 | * async_manager();
|
---|
| 69 | * }
|
---|
| 70 | *
|
---|
| 71 | * my_client_connection(icallid, *icall)
|
---|
| 72 | * {
|
---|
| 73 | * if (want_refuse) {
|
---|
[64d2b10] | 74 | * async_answer_0(icallid, ELIMIT);
|
---|
[c07544d3] | 75 | * return;
|
---|
| 76 | * }
|
---|
[64d2b10] | 77 | * async_answer_0(icallid, EOK);
|
---|
[80649a91] | 78 | *
|
---|
[c07544d3] | 79 | * callid = async_get_call(&call);
|
---|
[0772aff] | 80 | * somehow_handle_the_call(callid, call);
|
---|
[64d2b10] | 81 | * async_answer_2(callid, 1, 2, 3);
|
---|
[53ca318] | 82 | *
|
---|
[c07544d3] | 83 | * callid = async_get_call(&call);
|
---|
| 84 | * ...
|
---|
| 85 | * }
|
---|
[a2cd194] | 86 | *
|
---|
[80649a91] | 87 | */
|
---|
[9591265] | 88 |
|
---|
[64d2b10] | 89 | #define LIBC_ASYNC_C_
|
---|
| 90 | #include <ipc/ipc.h>
|
---|
[80649a91] | 91 | #include <async.h>
|
---|
[64d2b10] | 92 | #undef LIBC_ASYNC_C_
|
---|
| 93 |
|
---|
| 94 | #include <futex.h>
|
---|
[bc1f1c2] | 95 | #include <fibril.h>
|
---|
[80649a91] | 96 | #include <stdio.h>
|
---|
[d9c8c81] | 97 | #include <adt/hash_table.h>
|
---|
| 98 | #include <adt/list.h>
|
---|
[80649a91] | 99 | #include <assert.h>
|
---|
| 100 | #include <errno.h>
|
---|
[daa90e8] | 101 | #include <sys/time.h>
|
---|
[c042bdd] | 102 | #include <arch/barrier.h>
|
---|
[0cc4313] | 103 | #include <bool.h>
|
---|
[e26a4633] | 104 | #include "private/async.h"
|
---|
[80649a91] | 105 |
|
---|
[fc42b28] | 106 | atomic_t async_futex = FUTEX_INITIALIZER;
|
---|
[80649a91] | 107 |
|
---|
[8619f25] | 108 | /** Number of threads waiting for IPC in the kernel. */
|
---|
| 109 | atomic_t threads_in_ipc_wait = { 0 };
|
---|
| 110 |
|
---|
[49d072e] | 111 | typedef struct {
|
---|
| 112 | awaiter_t wdata;
|
---|
[e70bfa5] | 113 |
|
---|
| 114 | /** If reply was received. */
|
---|
[c07544d3] | 115 | bool done;
|
---|
| 116 |
|
---|
[e70bfa5] | 117 | /** Pointer to where the answer data is stored. */
|
---|
[c07544d3] | 118 | ipc_call_t *dataptr;
|
---|
| 119 |
|
---|
[96b02eb9] | 120 | sysarg_t retval;
|
---|
[01ff41c] | 121 | } amsg_t;
|
---|
| 122 |
|
---|
[36c9234] | 123 | /**
|
---|
[47b7006] | 124 | * Structures of this type are used to group information about
|
---|
| 125 | * a call and about a message queue link.
|
---|
[36c9234] | 126 | */
|
---|
[80649a91] | 127 | typedef struct {
|
---|
| 128 | link_t link;
|
---|
| 129 | ipc_callid_t callid;
|
---|
| 130 | ipc_call_t call;
|
---|
| 131 | } msg_t;
|
---|
| 132 |
|
---|
[c80fdd0] | 133 | typedef struct {
|
---|
| 134 | sysarg_t in_task_hash;
|
---|
| 135 | link_t link;
|
---|
| 136 | int refcnt;
|
---|
| 137 | void *data;
|
---|
| 138 | } client_t;
|
---|
| 139 |
|
---|
[80649a91] | 140 | typedef struct {
|
---|
[49d072e] | 141 | awaiter_t wdata;
|
---|
[c07544d3] | 142 |
|
---|
[e70bfa5] | 143 | /** Hash table link. */
|
---|
| 144 | link_t link;
|
---|
[c07544d3] | 145 |
|
---|
[3c22f70] | 146 | /** Incoming client task hash. */
|
---|
| 147 | sysarg_t in_task_hash;
|
---|
[e70bfa5] | 148 | /** Incoming phone hash. */
|
---|
[96b02eb9] | 149 | sysarg_t in_phone_hash;
|
---|
[c07544d3] | 150 |
|
---|
[23882034] | 151 | /** Link to the client tracking structure. */
|
---|
| 152 | client_t *client;
|
---|
[47b7006] | 153 |
|
---|
[e70bfa5] | 154 | /** Messages that should be delivered to this fibril. */
|
---|
[c07544d3] | 155 | link_t msg_queue;
|
---|
| 156 |
|
---|
[e70bfa5] | 157 | /** Identification of the opening call. */
|
---|
[80649a91] | 158 | ipc_callid_t callid;
|
---|
[e70bfa5] | 159 | /** Call data of the opening call. */
|
---|
[80649a91] | 160 | ipc_call_t call;
|
---|
[c07544d3] | 161 |
|
---|
[e70bfa5] | 162 | /** Identification of the closing call. */
|
---|
| 163 | ipc_callid_t close_callid;
|
---|
[c07544d3] | 164 |
|
---|
[e70bfa5] | 165 | /** Fibril function that will be used to handle the connection. */
|
---|
[bc1f1c2] | 166 | void (*cfibril)(ipc_callid_t, ipc_call_t *);
|
---|
[80649a91] | 167 | } connection_t;
|
---|
| 168 |
|
---|
[bc1f1c2] | 169 | /** Identifier of the incoming connection handled by the current fibril. */
|
---|
[47b7006] | 170 | static fibril_local connection_t *FIBRIL_connection;
|
---|
[e70bfa5] | 171 |
|
---|
[46eec3b] | 172 | static void *default_client_data_constructor(void)
|
---|
| 173 | {
|
---|
| 174 | return NULL;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | static void default_client_data_destructor(void *data)
|
---|
| 178 | {
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | static async_client_data_ctor_t async_client_data_create =
|
---|
| 182 | default_client_data_constructor;
|
---|
| 183 | static async_client_data_dtor_t async_client_data_destroy =
|
---|
| 184 | default_client_data_destructor;
|
---|
| 185 |
|
---|
| 186 | void async_set_client_data_constructor(async_client_data_ctor_t ctor)
|
---|
| 187 | {
|
---|
| 188 | async_client_data_create = ctor;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | void async_set_client_data_destructor(async_client_data_dtor_t dtor)
|
---|
| 192 | {
|
---|
| 193 | async_client_data_destroy = dtor;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[23882034] | 196 | void *async_client_data_get(void)
|
---|
| 197 | {
|
---|
| 198 | assert(FIBRIL_connection);
|
---|
| 199 | return FIBRIL_connection->client->data;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[47b7006] | 202 | /** Default fibril function that gets called to handle new connection.
|
---|
| 203 | *
|
---|
| 204 | * This function is defined as a weak symbol - to be redefined in user code.
|
---|
| 205 | *
|
---|
| 206 | * @param callid Hash of the incoming call.
|
---|
| 207 | * @param call Data of the incoming call.
|
---|
| 208 | *
|
---|
| 209 | */
|
---|
| 210 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 211 | {
|
---|
| 212 | ipc_answer_0(callid, ENOENT);
|
---|
| 213 | }
|
---|
[36c9234] | 214 |
|
---|
| 215 | /**
|
---|
| 216 | * Pointer to a fibril function that will be used to handle connections.
|
---|
| 217 | */
|
---|
[da0c91e7] | 218 | static async_client_conn_t client_connection = default_client_connection;
|
---|
[c07544d3] | 219 |
|
---|
[47b7006] | 220 | /** Default fibril function that gets called to handle interrupt notifications.
|
---|
| 221 | *
|
---|
| 222 | * This function is defined as a weak symbol - to be redefined in user code.
|
---|
| 223 | *
|
---|
| 224 | * @param callid Hash of the incoming call.
|
---|
| 225 | * @param call Data of the incoming call.
|
---|
| 226 | *
|
---|
| 227 | */
|
---|
| 228 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 229 | {
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[36c9234] | 232 | /**
|
---|
| 233 | * Pointer to a fibril function that will be used to handle interrupt
|
---|
| 234 | * notifications.
|
---|
| 235 | */
|
---|
[51dbadf3] | 236 | static async_client_conn_t interrupt_received = default_interrupt_received;
|
---|
[da0c91e7] | 237 |
|
---|
[c80fdd0] | 238 | static hash_table_t client_hash_table;
|
---|
[c07544d3] | 239 | static hash_table_t conn_hash_table;
|
---|
| 240 | static LIST_INITIALIZE(timeout_list);
|
---|
| 241 |
|
---|
[47b7006] | 242 | #define CLIENT_HASH_TABLE_BUCKETS 32
|
---|
| 243 | #define CONN_HASH_TABLE_BUCKETS 32
|
---|
[c80fdd0] | 244 |
|
---|
[47b7006] | 245 | static hash_index_t client_hash(unsigned long key[])
|
---|
[c80fdd0] | 246 | {
|
---|
| 247 | assert(key);
|
---|
[47b7006] | 248 | return (((key[0]) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
|
---|
[c80fdd0] | 249 | }
|
---|
| 250 |
|
---|
| 251 | static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
| 252 | {
|
---|
[47b7006] | 253 | client_t *client = hash_table_get_instance(item, client_t, link);
|
---|
| 254 | return (key[0] == client->in_task_hash);
|
---|
[c80fdd0] | 255 | }
|
---|
| 256 |
|
---|
| 257 | static void client_remove(link_t *item)
|
---|
| 258 | {
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | /** Operations for the client hash table. */
|
---|
| 262 | static hash_table_operations_t client_hash_table_ops = {
|
---|
| 263 | .hash = client_hash,
|
---|
| 264 | .compare = client_compare,
|
---|
| 265 | .remove_callback = client_remove
|
---|
| 266 | };
|
---|
[80649a91] | 267 |
|
---|
[e70bfa5] | 268 | /** Compute hash into the connection hash table based on the source phone hash.
|
---|
| 269 | *
|
---|
[c07544d3] | 270 | * @param key Pointer to source phone hash.
|
---|
| 271 | *
|
---|
| 272 | * @return Index into the connection hash table.
|
---|
[e70bfa5] | 273 | *
|
---|
| 274 | */
|
---|
[47b7006] | 275 | static hash_index_t conn_hash(unsigned long key[])
|
---|
[450cd3a] | 276 | {
|
---|
[80649a91] | 277 | assert(key);
|
---|
[47b7006] | 278 | return (((key[0]) >> 4) % CONN_HASH_TABLE_BUCKETS);
|
---|
[450cd3a] | 279 | }
|
---|
[06502f7d] | 280 |
|
---|
[e70bfa5] | 281 | /** Compare hash table item with a key.
|
---|
| 282 | *
|
---|
[c07544d3] | 283 | * @param key Array containing the source phone hash as the only item.
|
---|
| 284 | * @param keys Expected 1 but ignored.
|
---|
| 285 | * @param item Connection hash table item.
|
---|
| 286 | *
|
---|
| 287 | * @return True on match, false otherwise.
|
---|
[e70bfa5] | 288 | *
|
---|
| 289 | */
|
---|
[80649a91] | 290 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
[450cd3a] | 291 | {
|
---|
[47b7006] | 292 | connection_t *conn = hash_table_get_instance(item, connection_t, link);
|
---|
| 293 | return (key[0] == conn->in_phone_hash);
|
---|
[450cd3a] | 294 | }
|
---|
[06502f7d] | 295 |
|
---|
[e70bfa5] | 296 | /** Connection hash table removal callback function.
|
---|
| 297 | *
|
---|
| 298 | * This function is called whenever a connection is removed from the connection
|
---|
| 299 | * hash table.
|
---|
| 300 | *
|
---|
[c07544d3] | 301 | * @param item Connection hash table item being removed.
|
---|
| 302 | *
|
---|
[e70bfa5] | 303 | */
|
---|
[80649a91] | 304 | static void conn_remove(link_t *item)
|
---|
[450cd3a] | 305 | {
|
---|
[80649a91] | 306 | free(hash_table_get_instance(item, connection_t, link));
|
---|
[450cd3a] | 307 | }
|
---|
| 308 |
|
---|
[e70bfa5] | 309 | /** Operations for the connection hash table. */
|
---|
[80649a91] | 310 | static hash_table_operations_t conn_hash_table_ops = {
|
---|
| 311 | .hash = conn_hash,
|
---|
| 312 | .compare = conn_compare,
|
---|
| 313 | .remove_callback = conn_remove
|
---|
| 314 | };
|
---|
| 315 |
|
---|
[e70bfa5] | 316 | /** Sort in current fibril's timeout request.
|
---|
[49d072e] | 317 | *
|
---|
[c07544d3] | 318 | * @param wd Wait data of the current fibril.
|
---|
| 319 | *
|
---|
[49d072e] | 320 | */
|
---|
[b6ee5b1] | 321 | void async_insert_timeout(awaiter_t *wd)
|
---|
[49d072e] | 322 | {
|
---|
[f53cc81] | 323 | wd->to_event.occurred = false;
|
---|
| 324 | wd->to_event.inlist = true;
|
---|
[c07544d3] | 325 |
|
---|
| 326 | link_t *tmp = timeout_list.next;
|
---|
[49d072e] | 327 | while (tmp != &timeout_list) {
|
---|
[47b7006] | 328 | awaiter_t *cur
|
---|
| 329 | = list_get_instance(tmp, awaiter_t, to_event.link);
|
---|
[c07544d3] | 330 |
|
---|
[f53cc81] | 331 | if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
|
---|
[49d072e] | 332 | break;
|
---|
[47b7006] | 333 |
|
---|
[49d072e] | 334 | tmp = tmp->next;
|
---|
| 335 | }
|
---|
[c07544d3] | 336 |
|
---|
[f53cc81] | 337 | list_append(&wd->to_event.link, tmp);
|
---|
[49d072e] | 338 | }
|
---|
| 339 |
|
---|
[e70bfa5] | 340 | /** Try to route a call to an appropriate connection fibril.
|
---|
[80649a91] | 341 | *
|
---|
[36c9234] | 342 | * If the proper connection fibril is found, a message with the call is added to
|
---|
| 343 | * its message queue. If the fibril was not active, it is activated and all
|
---|
| 344 | * timeouts are unregistered.
|
---|
| 345 | *
|
---|
[c07544d3] | 346 | * @param callid Hash of the incoming call.
|
---|
| 347 | * @param call Data of the incoming call.
|
---|
| 348 | *
|
---|
| 349 | * @return False if the call doesn't match any connection.
|
---|
[47b7006] | 350 | * @return True if the call was passed to the respective connection fibril.
|
---|
[36c9234] | 351 | *
|
---|
[80649a91] | 352 | */
|
---|
[c07544d3] | 353 | static bool route_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
[450cd3a] | 354 | {
|
---|
[01ff41c] | 355 | futex_down(&async_futex);
|
---|
[c07544d3] | 356 |
|
---|
| 357 | unsigned long key = call->in_phone_hash;
|
---|
| 358 | link_t *hlp = hash_table_find(&conn_hash_table, &key);
|
---|
| 359 |
|
---|
[80649a91] | 360 | if (!hlp) {
|
---|
[01ff41c] | 361 | futex_up(&async_futex);
|
---|
[c07544d3] | 362 | return false;
|
---|
[450cd3a] | 363 | }
|
---|
[c07544d3] | 364 |
|
---|
| 365 | connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
|
---|
| 366 |
|
---|
| 367 | msg_t *msg = malloc(sizeof(*msg));
|
---|
| 368 | if (!msg) {
|
---|
| 369 | futex_up(&async_futex);
|
---|
| 370 | return false;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
[80649a91] | 373 | msg->callid = callid;
|
---|
| 374 | msg->call = *call;
|
---|
| 375 | list_append(&msg->link, &conn->msg_queue);
|
---|
[c07544d3] | 376 |
|
---|
[228e490] | 377 | if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
|
---|
[41269bd] | 378 | conn->close_callid = callid;
|
---|
[80649a91] | 379 |
|
---|
[36c9234] | 380 | /* If the connection fibril is waiting for an event, activate it */
|
---|
[49d072e] | 381 | if (!conn->wdata.active) {
|
---|
[c07544d3] | 382 |
|
---|
[49d072e] | 383 | /* If in timeout list, remove it */
|
---|
[f53cc81] | 384 | if (conn->wdata.to_event.inlist) {
|
---|
| 385 | conn->wdata.to_event.inlist = false;
|
---|
| 386 | list_remove(&conn->wdata.to_event.link);
|
---|
[49d072e] | 387 | }
|
---|
[c07544d3] | 388 |
|
---|
| 389 | conn->wdata.active = true;
|
---|
[bc1f1c2] | 390 | fibril_add_ready(conn->wdata.fid);
|
---|
[80649a91] | 391 | }
|
---|
[c07544d3] | 392 |
|
---|
[01ff41c] | 393 | futex_up(&async_futex);
|
---|
[c07544d3] | 394 | return true;
|
---|
| 395 | }
|
---|
[80649a91] | 396 |
|
---|
[c07544d3] | 397 | /** Notification fibril.
|
---|
| 398 | *
|
---|
| 399 | * When a notification arrives, a fibril with this implementing function is
|
---|
| 400 | * created. It calls interrupt_received() and does the final cleanup.
|
---|
| 401 | *
|
---|
| 402 | * @param arg Message structure pointer.
|
---|
| 403 | *
|
---|
| 404 | * @return Always zero.
|
---|
| 405 | *
|
---|
| 406 | */
|
---|
| 407 | static int notification_fibril(void *arg)
|
---|
| 408 | {
|
---|
| 409 | msg_t *msg = (msg_t *) arg;
|
---|
| 410 | interrupt_received(msg->callid, &msg->call);
|
---|
| 411 |
|
---|
| 412 | free(msg);
|
---|
| 413 | return 0;
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | /** Process interrupt notification.
|
---|
| 417 | *
|
---|
| 418 | * A new fibril is created which would process the notification.
|
---|
| 419 | *
|
---|
| 420 | * @param callid Hash of the incoming call.
|
---|
| 421 | * @param call Data of the incoming call.
|
---|
| 422 | *
|
---|
| 423 | * @return False if an error occured.
|
---|
| 424 | * True if the call was passed to the notification fibril.
|
---|
| 425 | *
|
---|
| 426 | */
|
---|
| 427 | static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 428 | {
|
---|
| 429 | futex_down(&async_futex);
|
---|
| 430 |
|
---|
| 431 | msg_t *msg = malloc(sizeof(*msg));
|
---|
| 432 | if (!msg) {
|
---|
| 433 | futex_up(&async_futex);
|
---|
| 434 | return false;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | msg->callid = callid;
|
---|
| 438 | msg->call = *call;
|
---|
| 439 |
|
---|
| 440 | fid_t fid = fibril_create(notification_fibril, msg);
|
---|
[86d7bfa] | 441 | if (fid == 0) {
|
---|
| 442 | free(msg);
|
---|
| 443 | futex_up(&async_futex);
|
---|
| 444 | return false;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
[c07544d3] | 447 | fibril_add_ready(fid);
|
---|
| 448 |
|
---|
| 449 | futex_up(&async_futex);
|
---|
| 450 | return true;
|
---|
[80649a91] | 451 | }
|
---|
| 452 |
|
---|
[e70bfa5] | 453 | /** Return new incoming message for the current (fibril-local) connection.
|
---|
| 454 | *
|
---|
[c07544d3] | 455 | * @param call Storage where the incoming call data will be stored.
|
---|
| 456 | * @param usecs Timeout in microseconds. Zero denotes no timeout.
|
---|
| 457 | *
|
---|
| 458 | * @return If no timeout was specified, then a hash of the
|
---|
| 459 | * incoming call is returned. If a timeout is specified,
|
---|
| 460 | * then a hash of the incoming call is returned unless
|
---|
| 461 | * the timeout expires prior to receiving a message. In
|
---|
| 462 | * that case zero is returned.
|
---|
[e70bfa5] | 463 | *
|
---|
| 464 | */
|
---|
[49d072e] | 465 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
|
---|
[80649a91] | 466 | {
|
---|
[bc1f1c2] | 467 | assert(FIBRIL_connection);
|
---|
[c07544d3] | 468 |
|
---|
| 469 | /* Why doing this?
|
---|
| 470 | * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
|
---|
[6c46350] | 471 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
|
---|
[c07544d3] | 472 | * I would never expect to find so many errors in
|
---|
| 473 | * a compiler.
|
---|
[6c46350] | 474 | */
|
---|
[c07544d3] | 475 | connection_t *conn = FIBRIL_connection;
|
---|
| 476 |
|
---|
[01ff41c] | 477 | futex_down(&async_futex);
|
---|
[c07544d3] | 478 |
|
---|
[49d072e] | 479 | if (usecs) {
|
---|
[f53cc81] | 480 | gettimeofday(&conn->wdata.to_event.expires, NULL);
|
---|
| 481 | tv_add(&conn->wdata.to_event.expires, usecs);
|
---|
[c07544d3] | 482 | } else
|
---|
[f53cc81] | 483 | conn->wdata.to_event.inlist = false;
|
---|
[c07544d3] | 484 |
|
---|
[e70bfa5] | 485 | /* If nothing in queue, wait until something arrives */
|
---|
[6c46350] | 486 | while (list_empty(&conn->msg_queue)) {
|
---|
[8c8f8d6] | 487 | if (conn->close_callid) {
|
---|
| 488 | /*
|
---|
| 489 | * Handle the case when the connection was already
|
---|
| 490 | * closed by the client but the server did not notice
|
---|
| 491 | * the first IPC_M_PHONE_HUNGUP call and continues to
|
---|
| 492 | * call async_get_call_timeout(). Repeat
|
---|
[47b7006] | 493 | * IPC_M_PHONE_HUNGUP until the caller notices.
|
---|
[8c8f8d6] | 494 | */
|
---|
| 495 | memset(call, 0, sizeof(ipc_call_t));
|
---|
[228e490] | 496 | IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
|
---|
[8c8f8d6] | 497 | futex_up(&async_futex);
|
---|
| 498 | return conn->close_callid;
|
---|
| 499 | }
|
---|
[47b7006] | 500 |
|
---|
[085bd54] | 501 | if (usecs)
|
---|
[b6ee5b1] | 502 | async_insert_timeout(&conn->wdata);
|
---|
[c07544d3] | 503 |
|
---|
| 504 | conn->wdata.active = false;
|
---|
| 505 |
|
---|
[c7509e5] | 506 | /*
|
---|
| 507 | * Note: the current fibril will be rescheduled either due to a
|
---|
| 508 | * timeout or due to an arriving message destined to it. In the
|
---|
| 509 | * former case, handle_expired_timeouts() and, in the latter
|
---|
| 510 | * case, route_call() will perform the wakeup.
|
---|
| 511 | */
|
---|
[116d3f6f] | 512 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
[c07544d3] | 513 |
|
---|
[e70bfa5] | 514 | /*
|
---|
[c07544d3] | 515 | * Futex is up after getting back from async_manager.
|
---|
| 516 | * Get it again.
|
---|
[c7509e5] | 517 | */
|
---|
[49d072e] | 518 | futex_down(&async_futex);
|
---|
[f53cc81] | 519 | if ((usecs) && (conn->wdata.to_event.occurred)
|
---|
[c07544d3] | 520 | && (list_empty(&conn->msg_queue))) {
|
---|
[e70bfa5] | 521 | /* If we timed out -> exit */
|
---|
[49d072e] | 522 | futex_up(&async_futex);
|
---|
| 523 | return 0;
|
---|
| 524 | }
|
---|
[450cd3a] | 525 | }
|
---|
| 526 |
|
---|
[c07544d3] | 527 | msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
|
---|
[80649a91] | 528 | list_remove(&msg->link);
|
---|
[c07544d3] | 529 |
|
---|
| 530 | ipc_callid_t callid = msg->callid;
|
---|
[80649a91] | 531 | *call = msg->call;
|
---|
| 532 | free(msg);
|
---|
| 533 |
|
---|
[01ff41c] | 534 | futex_up(&async_futex);
|
---|
[80649a91] | 535 | return callid;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
[f2f0392] | 538 | /** Wrapper for client connection fibril.
|
---|
| 539 | *
|
---|
[36c9234] | 540 | * When a new connection arrives, a fibril with this implementing function is
|
---|
[f2f0392] | 541 | * created. It calls client_connection() and does the final cleanup.
|
---|
[a2cd194] | 542 | *
|
---|
[c07544d3] | 543 | * @param arg Connection structure pointer.
|
---|
| 544 | *
|
---|
| 545 | * @return Always zero.
|
---|
[a2cd194] | 546 | *
|
---|
| 547 | */
|
---|
[c07544d3] | 548 | static int connection_fibril(void *arg)
|
---|
[80649a91] | 549 | {
|
---|
[c07544d3] | 550 | /*
|
---|
[c80fdd0] | 551 | * Setup fibril-local connection pointer.
|
---|
[c07544d3] | 552 | */
|
---|
[bc1f1c2] | 553 | FIBRIL_connection = (connection_t *) arg;
|
---|
[47b7006] | 554 |
|
---|
| 555 | futex_down(&async_futex);
|
---|
| 556 |
|
---|
[c80fdd0] | 557 | /*
|
---|
| 558 | * Add our reference for the current connection in the client task
|
---|
| 559 | * tracking structure. If this is the first reference, create and
|
---|
| 560 | * hash in a new tracking structure.
|
---|
| 561 | */
|
---|
[47b7006] | 562 |
|
---|
| 563 | unsigned long key = FIBRIL_connection->in_task_hash;
|
---|
| 564 | link_t *lnk = hash_table_find(&client_hash_table, &key);
|
---|
| 565 |
|
---|
| 566 | client_t *client;
|
---|
| 567 |
|
---|
[c80fdd0] | 568 | if (lnk) {
|
---|
[47b7006] | 569 | client = hash_table_get_instance(lnk, client_t, link);
|
---|
| 570 | client->refcnt++;
|
---|
[c80fdd0] | 571 | } else {
|
---|
[47b7006] | 572 | client = malloc(sizeof(client_t));
|
---|
| 573 | if (!client) {
|
---|
[c80fdd0] | 574 | ipc_answer_0(FIBRIL_connection->callid, ENOMEM);
|
---|
| 575 | futex_up(&async_futex);
|
---|
| 576 | return 0;
|
---|
| 577 | }
|
---|
[47b7006] | 578 |
|
---|
| 579 | client->in_task_hash = FIBRIL_connection->in_task_hash;
|
---|
| 580 |
|
---|
[46eec3b] | 581 | async_serialize_start();
|
---|
[47b7006] | 582 | client->data = async_client_data_create();
|
---|
[46eec3b] | 583 | async_serialize_end();
|
---|
[47b7006] | 584 |
|
---|
| 585 | client->refcnt = 1;
|
---|
| 586 | hash_table_insert(&client_hash_table, &key, &client->link);
|
---|
[c80fdd0] | 587 | }
|
---|
[47b7006] | 588 |
|
---|
[c80fdd0] | 589 | futex_up(&async_futex);
|
---|
[47b7006] | 590 |
|
---|
| 591 | FIBRIL_connection->client = client;
|
---|
| 592 |
|
---|
[c80fdd0] | 593 | /*
|
---|
| 594 | * Call the connection handler function.
|
---|
| 595 | */
|
---|
[bc1f1c2] | 596 | FIBRIL_connection->cfibril(FIBRIL_connection->callid,
|
---|
| 597 | &FIBRIL_connection->call);
|
---|
[a46da63] | 598 |
|
---|
[c80fdd0] | 599 | /*
|
---|
| 600 | * Remove the reference for this client task connection.
|
---|
| 601 | */
|
---|
[47b7006] | 602 | bool destroy;
|
---|
| 603 |
|
---|
[01ff41c] | 604 | futex_down(&async_futex);
|
---|
[47b7006] | 605 |
|
---|
| 606 | if (--client->refcnt == 0) {
|
---|
[c80fdd0] | 607 | hash_table_remove(&client_hash_table, &key, 1);
|
---|
[8526e585] | 608 | destroy = true;
|
---|
[47b7006] | 609 | } else
|
---|
| 610 | destroy = false;
|
---|
| 611 |
|
---|
[c80fdd0] | 612 | futex_up(&async_futex);
|
---|
[47b7006] | 613 |
|
---|
[8526e585] | 614 | if (destroy) {
|
---|
[47b7006] | 615 | if (client->data)
|
---|
| 616 | async_client_data_destroy(client->data);
|
---|
| 617 |
|
---|
| 618 | free(client);
|
---|
[8526e585] | 619 | }
|
---|
[47b7006] | 620 |
|
---|
[c80fdd0] | 621 | /*
|
---|
| 622 | * Remove myself from the connection hash table.
|
---|
| 623 | */
|
---|
| 624 | futex_down(&async_futex);
|
---|
| 625 | key = FIBRIL_connection->in_phone_hash;
|
---|
[a2cd194] | 626 | hash_table_remove(&conn_hash_table, &key, 1);
|
---|
[01ff41c] | 627 | futex_up(&async_futex);
|
---|
[a46da63] | 628 |
|
---|
[c80fdd0] | 629 | /*
|
---|
| 630 | * Answer all remaining messages with EHANGUP.
|
---|
| 631 | */
|
---|
[bc1f1c2] | 632 | while (!list_empty(&FIBRIL_connection->msg_queue)) {
|
---|
[47b7006] | 633 | msg_t *msg =
|
---|
| 634 | list_get_instance(FIBRIL_connection->msg_queue.next, msg_t,
|
---|
| 635 | link);
|
---|
[c07544d3] | 636 |
|
---|
[a2cd194] | 637 | list_remove(&msg->link);
|
---|
[b74959bd] | 638 | ipc_answer_0(msg->callid, EHANGUP);
|
---|
[a2cd194] | 639 | free(msg);
|
---|
| 640 | }
|
---|
[c07544d3] | 641 |
|
---|
[c80fdd0] | 642 | /*
|
---|
| 643 | * If the connection was hung-up, answer the last call,
|
---|
| 644 | * i.e. IPC_M_PHONE_HUNGUP.
|
---|
| 645 | */
|
---|
[bc1f1c2] | 646 | if (FIBRIL_connection->close_callid)
|
---|
[b74959bd] | 647 | ipc_answer_0(FIBRIL_connection->close_callid, EOK);
|
---|
[a46da63] | 648 |
|
---|
| 649 | return 0;
|
---|
[80649a91] | 650 | }
|
---|
| 651 |
|
---|
[f2f0392] | 652 | /** Create a new fibril for a new connection.
|
---|
[80649a91] | 653 | *
|
---|
[c07544d3] | 654 | * Create new fibril for connection, fill in connection structures and inserts
|
---|
[f2f0392] | 655 | * it into the hash table, so that later we can easily do routing of messages to
|
---|
| 656 | * particular fibrils.
|
---|
[53ca318] | 657 | *
|
---|
[3c22f70] | 658 | * @param in_task_hash Identification of the incoming connection.
|
---|
[c07544d3] | 659 | * @param in_phone_hash Identification of the incoming connection.
|
---|
| 660 | * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
|
---|
| 661 | * If callid is zero, the connection was opened by
|
---|
| 662 | * accepting the IPC_M_CONNECT_TO_ME call and this function
|
---|
| 663 | * is called directly by the server.
|
---|
| 664 | * @param call Call data of the opening call.
|
---|
| 665 | * @param cfibril Fibril function that should be called upon opening the
|
---|
| 666 | * connection.
|
---|
| 667 | *
|
---|
| 668 | * @return New fibril id or NULL on failure.
|
---|
[36c9234] | 669 | *
|
---|
[80649a91] | 670 | */
|
---|
[3c22f70] | 671 | fid_t async_new_connection(sysarg_t in_task_hash, sysarg_t in_phone_hash,
|
---|
| 672 | ipc_callid_t callid, ipc_call_t *call,
|
---|
| 673 | void (*cfibril)(ipc_callid_t, ipc_call_t *))
|
---|
[80649a91] | 674 | {
|
---|
[c07544d3] | 675 | connection_t *conn = malloc(sizeof(*conn));
|
---|
[80649a91] | 676 | if (!conn) {
|
---|
[6675c70] | 677 | if (callid)
|
---|
[b74959bd] | 678 | ipc_answer_0(callid, ENOMEM);
|
---|
[47b7006] | 679 |
|
---|
[0b4a67a] | 680 | return (uintptr_t) NULL;
|
---|
[80649a91] | 681 | }
|
---|
[c07544d3] | 682 |
|
---|
[3c22f70] | 683 | conn->in_task_hash = in_task_hash;
|
---|
[44c6d88d] | 684 | conn->in_phone_hash = in_phone_hash;
|
---|
[80649a91] | 685 | list_initialize(&conn->msg_queue);
|
---|
| 686 | conn->callid = callid;
|
---|
[c4702804] | 687 | conn->close_callid = 0;
|
---|
[c07544d3] | 688 |
|
---|
[eaf34f7] | 689 | if (call)
|
---|
| 690 | conn->call = *call;
|
---|
[6b21292] | 691 |
|
---|
[c07544d3] | 692 | /* We will activate the fibril ASAP */
|
---|
| 693 | conn->wdata.active = true;
|
---|
| 694 | conn->cfibril = cfibril;
|
---|
[bc1f1c2] | 695 | conn->wdata.fid = fibril_create(connection_fibril, conn);
|
---|
[c07544d3] | 696 |
|
---|
[86d7bfa] | 697 | if (conn->wdata.fid == 0) {
|
---|
[80649a91] | 698 | free(conn);
|
---|
[86d7bfa] | 699 |
|
---|
[6675c70] | 700 | if (callid)
|
---|
[b74959bd] | 701 | ipc_answer_0(callid, ENOMEM);
|
---|
[86d7bfa] | 702 |
|
---|
[0b4a67a] | 703 | return (uintptr_t) NULL;
|
---|
[80649a91] | 704 | }
|
---|
[6b21292] | 705 |
|
---|
[36c9234] | 706 | /* Add connection to the connection hash table */
|
---|
[9db9b10] | 707 | unsigned long key = conn->in_phone_hash;
|
---|
[c07544d3] | 708 |
|
---|
[01ff41c] | 709 | futex_down(&async_futex);
|
---|
[80649a91] | 710 | hash_table_insert(&conn_hash_table, &key, &conn->link);
|
---|
[01ff41c] | 711 | futex_up(&async_futex);
|
---|
[6b21292] | 712 |
|
---|
[bc1f1c2] | 713 | fibril_add_ready(conn->wdata.fid);
|
---|
[6b21292] | 714 |
|
---|
[bc1f1c2] | 715 | return conn->wdata.fid;
|
---|
[80649a91] | 716 | }
|
---|
| 717 |
|
---|
[36c9234] | 718 | /** Handle a call that was received.
|
---|
| 719 | *
|
---|
| 720 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
---|
| 721 | * Otherwise the call is routed to its connection fibril.
|
---|
| 722 | *
|
---|
[c07544d3] | 723 | * @param callid Hash of the incoming call.
|
---|
| 724 | * @param call Data of the incoming call.
|
---|
[6b21292] | 725 | *
|
---|
[36c9234] | 726 | */
|
---|
[80649a91] | 727 | static void handle_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
| 728 | {
|
---|
[47b7006] | 729 | /* Unrouted call - take some default action */
|
---|
[15039b67] | 730 | if ((callid & IPC_CALLID_NOTIFICATION)) {
|
---|
[c07544d3] | 731 | process_notification(callid, call);
|
---|
[47b7006] | 732 | return;
|
---|
[6b21292] | 733 | }
|
---|
| 734 |
|
---|
[228e490] | 735 | switch (IPC_GET_IMETHOD(*call)) {
|
---|
[2c0e5d2] | 736 | case IPC_M_CONNECT_ME:
|
---|
[80649a91] | 737 | case IPC_M_CONNECT_ME_TO:
|
---|
[47b7006] | 738 | /* Open new connection with fibril, etc. */
|
---|
[3c22f70] | 739 | async_new_connection(call->in_task_hash, IPC_GET_ARG5(*call),
|
---|
| 740 | callid, call, client_connection);
|
---|
[47b7006] | 741 | return;
|
---|
[80649a91] | 742 | }
|
---|
[6b21292] | 743 |
|
---|
[36c9234] | 744 | /* Try to route the call through the connection hash table */
|
---|
[44c6d88d] | 745 | if (route_call(callid, call))
|
---|
[47b7006] | 746 | return;
|
---|
[6b21292] | 747 |
|
---|
[44c6d88d] | 748 | /* Unknown call from unknown phone - hang it up */
|
---|
[b74959bd] | 749 | ipc_answer_0(callid, EHANGUP);
|
---|
[450cd3a] | 750 | }
|
---|
| 751 |
|
---|
[f2f0392] | 752 | /** Fire all timeouts that expired. */
|
---|
[c042bdd] | 753 | static void handle_expired_timeouts(void)
|
---|
| 754 | {
|
---|
| 755 | struct timeval tv;
|
---|
[36c9234] | 756 | gettimeofday(&tv, NULL);
|
---|
[c07544d3] | 757 |
|
---|
[c042bdd] | 758 | futex_down(&async_futex);
|
---|
[c07544d3] | 759 |
|
---|
| 760 | link_t *cur = timeout_list.next;
|
---|
[c042bdd] | 761 | while (cur != &timeout_list) {
|
---|
[47b7006] | 762 | awaiter_t *waiter =
|
---|
| 763 | list_get_instance(cur, awaiter_t, to_event.link);
|
---|
[c07544d3] | 764 |
|
---|
[f53cc81] | 765 | if (tv_gt(&waiter->to_event.expires, &tv))
|
---|
[c042bdd] | 766 | break;
|
---|
[47b7006] | 767 |
|
---|
[c042bdd] | 768 | cur = cur->next;
|
---|
[47b7006] | 769 |
|
---|
[f53cc81] | 770 | list_remove(&waiter->to_event.link);
|
---|
| 771 | waiter->to_event.inlist = false;
|
---|
| 772 | waiter->to_event.occurred = true;
|
---|
[c07544d3] | 773 |
|
---|
[36c9234] | 774 | /*
|
---|
[c07544d3] | 775 | * Redundant condition?
|
---|
| 776 | * The fibril should not be active when it gets here.
|
---|
[c042bdd] | 777 | */
|
---|
[49d072e] | 778 | if (!waiter->active) {
|
---|
[c07544d3] | 779 | waiter->active = true;
|
---|
[bc1f1c2] | 780 | fibril_add_ready(waiter->fid);
|
---|
[c042bdd] | 781 | }
|
---|
| 782 | }
|
---|
[c07544d3] | 783 |
|
---|
[c042bdd] | 784 | futex_up(&async_futex);
|
---|
| 785 | }
|
---|
| 786 |
|
---|
[36c9234] | 787 | /** Endless loop dispatching incoming calls and answers.
|
---|
| 788 | *
|
---|
[c07544d3] | 789 | * @return Never returns.
|
---|
| 790 | *
|
---|
[36c9234] | 791 | */
|
---|
[085bd54] | 792 | static int async_manager_worker(void)
|
---|
[80649a91] | 793 | {
|
---|
[c07544d3] | 794 | while (true) {
|
---|
[116d3f6f] | 795 | if (fibril_switch(FIBRIL_FROM_MANAGER)) {
|
---|
[47b7006] | 796 | futex_up(&async_futex);
|
---|
[36c9234] | 797 | /*
|
---|
| 798 | * async_futex is always held when entering a manager
|
---|
| 799 | * fibril.
|
---|
[a46da63] | 800 | */
|
---|
[80649a91] | 801 | continue;
|
---|
| 802 | }
|
---|
[c07544d3] | 803 |
|
---|
[c042bdd] | 804 | futex_down(&async_futex);
|
---|
[c07544d3] | 805 |
|
---|
| 806 | suseconds_t timeout;
|
---|
[c042bdd] | 807 | if (!list_empty(&timeout_list)) {
|
---|
[cc27c8c5] | 808 | awaiter_t *waiter = list_get_instance(timeout_list.next,
|
---|
[f53cc81] | 809 | awaiter_t, to_event.link);
|
---|
[c07544d3] | 810 |
|
---|
| 811 | struct timeval tv;
|
---|
[bc1f1c2] | 812 | gettimeofday(&tv, NULL);
|
---|
[c07544d3] | 813 |
|
---|
[f53cc81] | 814 | if (tv_gteq(&tv, &waiter->to_event.expires)) {
|
---|
[6c46350] | 815 | futex_up(&async_futex);
|
---|
[c042bdd] | 816 | handle_expired_timeouts();
|
---|
| 817 | continue;
|
---|
| 818 | } else
|
---|
[47b7006] | 819 | timeout = tv_sub(&waiter->to_event.expires, &tv);
|
---|
[c042bdd] | 820 | } else
|
---|
[0b99e40] | 821 | timeout = SYNCH_NO_TIMEOUT;
|
---|
[c07544d3] | 822 |
|
---|
[c042bdd] | 823 | futex_up(&async_futex);
|
---|
[47b7006] | 824 |
|
---|
[8619f25] | 825 | atomic_inc(&threads_in_ipc_wait);
|
---|
[c07544d3] | 826 |
|
---|
| 827 | ipc_call_t call;
|
---|
[cc27c8c5] | 828 | ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
|
---|
| 829 | SYNCH_FLAGS_NONE);
|
---|
[c07544d3] | 830 |
|
---|
[8619f25] | 831 | atomic_dec(&threads_in_ipc_wait);
|
---|
[47b7006] | 832 |
|
---|
[0b99e40] | 833 | if (!callid) {
|
---|
[c042bdd] | 834 | handle_expired_timeouts();
|
---|
[0b99e40] | 835 | continue;
|
---|
| 836 | }
|
---|
[c07544d3] | 837 |
|
---|
| 838 | if (callid & IPC_CALLID_ANSWERED)
|
---|
[80649a91] | 839 | continue;
|
---|
[c07544d3] | 840 |
|
---|
[80649a91] | 841 | handle_call(callid, &call);
|
---|
| 842 | }
|
---|
[a46da63] | 843 |
|
---|
| 844 | return 0;
|
---|
[80649a91] | 845 | }
|
---|
| 846 |
|
---|
[36c9234] | 847 | /** Function to start async_manager as a standalone fibril.
|
---|
[c07544d3] | 848 | *
|
---|
[36c9234] | 849 | * When more kernel threads are used, one async manager should exist per thread.
|
---|
| 850 | *
|
---|
[c07544d3] | 851 | * @param arg Unused.
|
---|
| 852 | * @return Never returns.
|
---|
[36c9234] | 853 | *
|
---|
[a2cd194] | 854 | */
|
---|
[9591265] | 855 | static int async_manager_fibril(void *arg)
|
---|
[80649a91] | 856 | {
|
---|
[a46da63] | 857 | futex_up(&async_futex);
|
---|
[c07544d3] | 858 |
|
---|
[36c9234] | 859 | /*
|
---|
| 860 | * async_futex is always locked when entering manager
|
---|
| 861 | */
|
---|
[085bd54] | 862 | async_manager_worker();
|
---|
[a46da63] | 863 |
|
---|
| 864 | return 0;
|
---|
[80649a91] | 865 | }
|
---|
[450cd3a] | 866 |
|
---|
[36c9234] | 867 | /** Add one manager to manager list. */
|
---|
[80649a91] | 868 | void async_create_manager(void)
|
---|
[450cd3a] | 869 | {
|
---|
[c07544d3] | 870 | fid_t fid = fibril_create(async_manager_fibril, NULL);
|
---|
[86d7bfa] | 871 | if (fid != 0)
|
---|
| 872 | fibril_add_manager(fid);
|
---|
[80649a91] | 873 | }
|
---|
| 874 |
|
---|
| 875 | /** Remove one manager from manager list */
|
---|
| 876 | void async_destroy_manager(void)
|
---|
| 877 | {
|
---|
[bc1f1c2] | 878 | fibril_remove_manager();
|
---|
[80649a91] | 879 | }
|
---|
| 880 |
|
---|
[36c9234] | 881 | /** Initialize the async framework.
|
---|
| 882 | *
|
---|
| 883 | */
|
---|
[47b7006] | 884 | void __async_init(void)
|
---|
[80649a91] | 885 | {
|
---|
[c80fdd0] | 886 | if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS, 1,
|
---|
[47b7006] | 887 | &client_hash_table_ops))
|
---|
| 888 | abort();
|
---|
[80649a91] | 889 |
|
---|
[47b7006] | 890 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_BUCKETS, 1,
|
---|
| 891 | &conn_hash_table_ops))
|
---|
| 892 | abort();
|
---|
[450cd3a] | 893 | }
|
---|
[01ff41c] | 894 |
|
---|
[36c9234] | 895 | /** Reply received callback.
|
---|
[01ff41c] | 896 | *
|
---|
[36c9234] | 897 | * This function is called whenever a reply for an asynchronous message sent out
|
---|
| 898 | * by the asynchronous framework is received.
|
---|
| 899 | *
|
---|
| 900 | * Notify the fibril which is waiting for this message that it has arrived.
|
---|
| 901 | *
|
---|
[c07544d3] | 902 | * @param arg Pointer to the asynchronous message record.
|
---|
| 903 | * @param retval Value returned in the answer.
|
---|
| 904 | * @param data Call data of the answer.
|
---|
[47b7006] | 905 | *
|
---|
[01ff41c] | 906 | */
|
---|
[c07544d3] | 907 | static void reply_received(void *arg, int retval, ipc_call_t *data)
|
---|
[01ff41c] | 908 | {
|
---|
[9db9b10] | 909 | futex_down(&async_futex);
|
---|
| 910 |
|
---|
[c07544d3] | 911 | amsg_t *msg = (amsg_t *) arg;
|
---|
[01ff41c] | 912 | msg->retval = retval;
|
---|
[c07544d3] | 913 |
|
---|
[36c9234] | 914 | /* Copy data after futex_down, just in case the call was detached */
|
---|
[9db9b10] | 915 | if ((msg->dataptr) && (data))
|
---|
[c07544d3] | 916 | *msg->dataptr = *data;
|
---|
| 917 |
|
---|
[c042bdd] | 918 | write_barrier();
|
---|
[c07544d3] | 919 |
|
---|
[c042bdd] | 920 | /* Remove message from timeout list */
|
---|
[f53cc81] | 921 | if (msg->wdata.to_event.inlist)
|
---|
| 922 | list_remove(&msg->wdata.to_event.link);
|
---|
[c07544d3] | 923 |
|
---|
| 924 | msg->done = true;
|
---|
[36c9234] | 925 | if (!msg->wdata.active) {
|
---|
[c07544d3] | 926 | msg->wdata.active = true;
|
---|
[bc1f1c2] | 927 | fibril_add_ready(msg->wdata.fid);
|
---|
[01ff41c] | 928 | }
|
---|
[c07544d3] | 929 |
|
---|
[01ff41c] | 930 | futex_up(&async_futex);
|
---|
| 931 | }
|
---|
| 932 |
|
---|
[36c9234] | 933 | /** Send message and return id of the sent message.
|
---|
| 934 | *
|
---|
| 935 | * The return value can be used as input for async_wait() to wait for
|
---|
| 936 | * completion.
|
---|
[01ff41c] | 937 | *
|
---|
[c07544d3] | 938 | * @param phoneid Handle of the phone that will be used for the send.
|
---|
| 939 | * @param method Service-defined method.
|
---|
| 940 | * @param arg1 Service-defined payload argument.
|
---|
| 941 | * @param arg2 Service-defined payload argument.
|
---|
| 942 | * @param arg3 Service-defined payload argument.
|
---|
| 943 | * @param arg4 Service-defined payload argument.
|
---|
| 944 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
| 945 | * stored.
|
---|
| 946 | *
|
---|
| 947 | * @return Hash of the sent message or 0 on error.
|
---|
[36c9234] | 948 | *
|
---|
[01ff41c] | 949 | */
|
---|
[96b02eb9] | 950 | aid_t async_send_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
| 951 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
---|
[01ff41c] | 952 | {
|
---|
[47b7006] | 953 | amsg_t *msg = malloc(sizeof(amsg_t));
|
---|
[c07544d3] | 954 |
|
---|
| 955 | if (!msg)
|
---|
| 956 | return 0;
|
---|
[6b21292] | 957 |
|
---|
[c07544d3] | 958 | msg->done = false;
|
---|
[01ff41c] | 959 | msg->dataptr = dataptr;
|
---|
[6b21292] | 960 |
|
---|
[f53cc81] | 961 | msg->wdata.to_event.inlist = false;
|
---|
[47b7006] | 962 |
|
---|
| 963 | /*
|
---|
| 964 | * We may sleep in the next method,
|
---|
| 965 | * but it will use its own means
|
---|
| 966 | */
|
---|
[c07544d3] | 967 | msg->wdata.active = true;
|
---|
| 968 |
|
---|
[0cc4313] | 969 | ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
|
---|
[c07544d3] | 970 | reply_received, true);
|
---|
[6b21292] | 971 |
|
---|
[01ff41c] | 972 | return (aid_t) msg;
|
---|
| 973 | }
|
---|
| 974 |
|
---|
[90f5d64] | 975 | /** Send message and return id of the sent message
|
---|
| 976 | *
|
---|
[36c9234] | 977 | * The return value can be used as input for async_wait() to wait for
|
---|
| 978 | * completion.
|
---|
| 979 | *
|
---|
[c07544d3] | 980 | * @param phoneid Handle of the phone that will be used for the send.
|
---|
| 981 | * @param method Service-defined method.
|
---|
| 982 | * @param arg1 Service-defined payload argument.
|
---|
| 983 | * @param arg2 Service-defined payload argument.
|
---|
| 984 | * @param arg3 Service-defined payload argument.
|
---|
| 985 | * @param arg4 Service-defined payload argument.
|
---|
| 986 | * @param arg5 Service-defined payload argument.
|
---|
| 987 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
| 988 | * stored.
|
---|
| 989 | *
|
---|
| 990 | * @return Hash of the sent message or 0 on error.
|
---|
[36c9234] | 991 | *
|
---|
[90f5d64] | 992 | */
|
---|
[96b02eb9] | 993 | aid_t async_send_slow(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
| 994 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
[0cc4313] | 995 | ipc_call_t *dataptr)
|
---|
[90f5d64] | 996 | {
|
---|
[47b7006] | 997 | amsg_t *msg = malloc(sizeof(amsg_t));
|
---|
[6b21292] | 998 |
|
---|
[c07544d3] | 999 | if (!msg)
|
---|
| 1000 | return 0;
|
---|
| 1001 |
|
---|
| 1002 | msg->done = false;
|
---|
[90f5d64] | 1003 | msg->dataptr = dataptr;
|
---|
[6b21292] | 1004 |
|
---|
[f53cc81] | 1005 | msg->wdata.to_event.inlist = false;
|
---|
[47b7006] | 1006 |
|
---|
| 1007 | /*
|
---|
| 1008 | * We may sleep in the next method,
|
---|
| 1009 | * but it will use its own means
|
---|
| 1010 | */
|
---|
[c07544d3] | 1011 | msg->wdata.active = true;
|
---|
[6b21292] | 1012 |
|
---|
[0cc4313] | 1013 | ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
|
---|
[c07544d3] | 1014 | reply_received, true);
|
---|
[6b21292] | 1015 |
|
---|
[90f5d64] | 1016 | return (aid_t) msg;
|
---|
| 1017 | }
|
---|
| 1018 |
|
---|
[36c9234] | 1019 | /** Wait for a message sent by the async framework.
|
---|
[01ff41c] | 1020 | *
|
---|
[c07544d3] | 1021 | * @param amsgid Hash of the message to wait for.
|
---|
| 1022 | * @param retval Pointer to storage where the retval of the answer will
|
---|
| 1023 | * be stored.
|
---|
| 1024 | *
|
---|
[01ff41c] | 1025 | */
|
---|
[96b02eb9] | 1026 | void async_wait_for(aid_t amsgid, sysarg_t *retval)
|
---|
[01ff41c] | 1027 | {
|
---|
| 1028 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
[c07544d3] | 1029 |
|
---|
[01ff41c] | 1030 | futex_down(&async_futex);
|
---|
| 1031 | if (msg->done) {
|
---|
| 1032 | futex_up(&async_futex);
|
---|
| 1033 | goto done;
|
---|
| 1034 | }
|
---|
[c07544d3] | 1035 |
|
---|
[bc1f1c2] | 1036 | msg->wdata.fid = fibril_get_id();
|
---|
[c07544d3] | 1037 | msg->wdata.active = false;
|
---|
[f53cc81] | 1038 | msg->wdata.to_event.inlist = false;
|
---|
[c07544d3] | 1039 |
|
---|
[36c9234] | 1040 | /* Leave the async_futex locked when entering this function */
|
---|
[116d3f6f] | 1041 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
[c07544d3] | 1042 |
|
---|
| 1043 | /* Futex is up automatically after fibril_switch */
|
---|
| 1044 |
|
---|
[01ff41c] | 1045 | done:
|
---|
| 1046 | if (retval)
|
---|
| 1047 | *retval = msg->retval;
|
---|
[c07544d3] | 1048 |
|
---|
[01ff41c] | 1049 | free(msg);
|
---|
| 1050 | }
|
---|
[0b99e40] | 1051 |
|
---|
[36c9234] | 1052 | /** Wait for a message sent by the async framework, timeout variant.
|
---|
[c042bdd] | 1053 | *
|
---|
[c07544d3] | 1054 | * @param amsgid Hash of the message to wait for.
|
---|
| 1055 | * @param retval Pointer to storage where the retval of the answer will
|
---|
| 1056 | * be stored.
|
---|
| 1057 | * @param timeout Timeout in microseconds.
|
---|
| 1058 | *
|
---|
| 1059 | * @return Zero on success, ETIMEOUT if the timeout has expired.
|
---|
[c042bdd] | 1060 | *
|
---|
| 1061 | */
|
---|
[96b02eb9] | 1062 | int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
|
---|
[c042bdd] | 1063 | {
|
---|
| 1064 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
[c07544d3] | 1065 |
|
---|
[86029498] | 1066 | /* TODO: Let it go through the event read at least once */
|
---|
| 1067 | if (timeout < 0)
|
---|
| 1068 | return ETIMEOUT;
|
---|
[c07544d3] | 1069 |
|
---|
[c042bdd] | 1070 | futex_down(&async_futex);
|
---|
| 1071 | if (msg->done) {
|
---|
| 1072 | futex_up(&async_futex);
|
---|
| 1073 | goto done;
|
---|
| 1074 | }
|
---|
[c07544d3] | 1075 |
|
---|
[f53cc81] | 1076 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
---|
| 1077 | tv_add(&msg->wdata.to_event.expires, timeout);
|
---|
[c07544d3] | 1078 |
|
---|
[bc1f1c2] | 1079 | msg->wdata.fid = fibril_get_id();
|
---|
[c07544d3] | 1080 | msg->wdata.active = false;
|
---|
[b6ee5b1] | 1081 | async_insert_timeout(&msg->wdata);
|
---|
[c07544d3] | 1082 |
|
---|
[36c9234] | 1083 | /* Leave the async_futex locked when entering this function */
|
---|
[116d3f6f] | 1084 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
[c07544d3] | 1085 |
|
---|
| 1086 | /* Futex is up automatically after fibril_switch */
|
---|
| 1087 |
|
---|
[c042bdd] | 1088 | if (!msg->done)
|
---|
| 1089 | return ETIMEOUT;
|
---|
[c07544d3] | 1090 |
|
---|
[c042bdd] | 1091 | done:
|
---|
| 1092 | if (retval)
|
---|
| 1093 | *retval = msg->retval;
|
---|
[c07544d3] | 1094 |
|
---|
[c042bdd] | 1095 | free(msg);
|
---|
[c07544d3] | 1096 |
|
---|
[c042bdd] | 1097 | return 0;
|
---|
| 1098 | }
|
---|
[0b99e40] | 1099 |
|
---|
[36c9234] | 1100 | /** Wait for specified time.
|
---|
[44c6d88d] | 1101 | *
|
---|
[36c9234] | 1102 | * The current fibril is suspended but the thread continues to execute.
|
---|
| 1103 | *
|
---|
[c07544d3] | 1104 | * @param timeout Duration of the wait in microseconds.
|
---|
| 1105 | *
|
---|
[44c6d88d] | 1106 | */
|
---|
| 1107 | void async_usleep(suseconds_t timeout)
|
---|
| 1108 | {
|
---|
[47b7006] | 1109 | amsg_t *msg = malloc(sizeof(amsg_t));
|
---|
[44c6d88d] | 1110 |
|
---|
| 1111 | if (!msg)
|
---|
| 1112 | return;
|
---|
[6b21292] | 1113 |
|
---|
[bc1f1c2] | 1114 | msg->wdata.fid = fibril_get_id();
|
---|
[c07544d3] | 1115 | msg->wdata.active = false;
|
---|
[6b21292] | 1116 |
|
---|
[f53cc81] | 1117 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
---|
| 1118 | tv_add(&msg->wdata.to_event.expires, timeout);
|
---|
[6b21292] | 1119 |
|
---|
[44c6d88d] | 1120 | futex_down(&async_futex);
|
---|
[c07544d3] | 1121 |
|
---|
[b6ee5b1] | 1122 | async_insert_timeout(&msg->wdata);
|
---|
[c07544d3] | 1123 |
|
---|
[36c9234] | 1124 | /* Leave the async_futex locked when entering this function */
|
---|
[116d3f6f] | 1125 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
[c07544d3] | 1126 |
|
---|
| 1127 | /* Futex is up automatically after fibril_switch() */
|
---|
| 1128 |
|
---|
[44c6d88d] | 1129 | free(msg);
|
---|
| 1130 | }
|
---|
[da0c91e7] | 1131 |
|
---|
[36c9234] | 1132 | /** Setter for client_connection function pointer.
|
---|
[da0c91e7] | 1133 | *
|
---|
[c07544d3] | 1134 | * @param conn Function that will implement a new connection fibril.
|
---|
| 1135 | *
|
---|
[da0c91e7] | 1136 | */
|
---|
| 1137 | void async_set_client_connection(async_client_conn_t conn)
|
---|
| 1138 | {
|
---|
| 1139 | client_connection = conn;
|
---|
| 1140 | }
|
---|
[36c9234] | 1141 |
|
---|
| 1142 | /** Setter for interrupt_received function pointer.
|
---|
| 1143 | *
|
---|
[c07544d3] | 1144 | * @param intr Function that will implement a new interrupt
|
---|
| 1145 | * notification fibril.
|
---|
[36c9234] | 1146 | */
|
---|
[c07544d3] | 1147 | void async_set_interrupt_received(async_client_conn_t intr)
|
---|
[51dbadf3] | 1148 | {
|
---|
[c07544d3] | 1149 | interrupt_received = intr;
|
---|
[51dbadf3] | 1150 | }
|
---|
[085bd54] | 1151 |
|
---|
[0cc4313] | 1152 | /** Pseudo-synchronous message sending - fast version.
|
---|
| 1153 | *
|
---|
| 1154 | * Send message asynchronously and return only after the reply arrives.
|
---|
| 1155 | *
|
---|
| 1156 | * This function can only transfer 4 register payload arguments. For
|
---|
| 1157 | * transferring more arguments, see the slower async_req_slow().
|
---|
| 1158 | *
|
---|
[c07544d3] | 1159 | * @param phoneid Hash of the phone through which to make the call.
|
---|
| 1160 | * @param method Method of the call.
|
---|
| 1161 | * @param arg1 Service-defined payload argument.
|
---|
| 1162 | * @param arg2 Service-defined payload argument.
|
---|
| 1163 | * @param arg3 Service-defined payload argument.
|
---|
| 1164 | * @param arg4 Service-defined payload argument.
|
---|
| 1165 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
| 1166 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
| 1167 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
| 1168 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
| 1169 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
| 1170 | *
|
---|
| 1171 | * @return Return code of the reply or a negative error code.
|
---|
| 1172 | *
|
---|
[0cc4313] | 1173 | */
|
---|
[96b02eb9] | 1174 | sysarg_t async_req_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
| 1175 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
|
---|
| 1176 | sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
---|
[085bd54] | 1177 | {
|
---|
[0cc4313] | 1178 | ipc_call_t result;
|
---|
| 1179 | aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
| 1180 | &result);
|
---|
[c07544d3] | 1181 |
|
---|
[96b02eb9] | 1182 | sysarg_t rc;
|
---|
[0cc4313] | 1183 | async_wait_for(eid, &rc);
|
---|
[c07544d3] | 1184 |
|
---|
| 1185 | if (r1)
|
---|
[0cc4313] | 1186 | *r1 = IPC_GET_ARG1(result);
|
---|
[c07544d3] | 1187 |
|
---|
[0cc4313] | 1188 | if (r2)
|
---|
| 1189 | *r2 = IPC_GET_ARG2(result);
|
---|
[c07544d3] | 1190 |
|
---|
[0cc4313] | 1191 | if (r3)
|
---|
| 1192 | *r3 = IPC_GET_ARG3(result);
|
---|
[c07544d3] | 1193 |
|
---|
[0cc4313] | 1194 | if (r4)
|
---|
| 1195 | *r4 = IPC_GET_ARG4(result);
|
---|
[c07544d3] | 1196 |
|
---|
[0cc4313] | 1197 | if (r5)
|
---|
| 1198 | *r5 = IPC_GET_ARG5(result);
|
---|
[c07544d3] | 1199 |
|
---|
[0cc4313] | 1200 | return rc;
|
---|
[085bd54] | 1201 | }
|
---|
| 1202 |
|
---|
[0cc4313] | 1203 | /** Pseudo-synchronous message sending - slow version.
|
---|
| 1204 | *
|
---|
| 1205 | * Send message asynchronously and return only after the reply arrives.
|
---|
| 1206 | *
|
---|
[c07544d3] | 1207 | * @param phoneid Hash of the phone through which to make the call.
|
---|
| 1208 | * @param method Method of the call.
|
---|
| 1209 | * @param arg1 Service-defined payload argument.
|
---|
| 1210 | * @param arg2 Service-defined payload argument.
|
---|
| 1211 | * @param arg3 Service-defined payload argument.
|
---|
| 1212 | * @param arg4 Service-defined payload argument.
|
---|
| 1213 | * @param arg5 Service-defined payload argument.
|
---|
| 1214 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
| 1215 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
| 1216 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
| 1217 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
| 1218 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
| 1219 | *
|
---|
| 1220 | * @return Return code of the reply or a negative error code.
|
---|
| 1221 | *
|
---|
[0cc4313] | 1222 | */
|
---|
[96b02eb9] | 1223 | sysarg_t async_req_slow(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
| 1224 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
|
---|
| 1225 | sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
---|
[085bd54] | 1226 | {
|
---|
[0cc4313] | 1227 | ipc_call_t result;
|
---|
| 1228 | aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
|
---|
| 1229 | &result);
|
---|
[c07544d3] | 1230 |
|
---|
[96b02eb9] | 1231 | sysarg_t rc;
|
---|
[0cc4313] | 1232 | async_wait_for(eid, &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 | }
|
---|
[b2951e2] | 1251 |
|
---|
[64d2b10] | 1252 | void async_msg_0(int phone, sysarg_t imethod)
|
---|
| 1253 | {
|
---|
| 1254 | ipc_call_async_0(phone, imethod, NULL, NULL, true);
|
---|
| 1255 | }
|
---|
| 1256 |
|
---|
| 1257 | void async_msg_1(int phone, sysarg_t imethod, sysarg_t arg1)
|
---|
| 1258 | {
|
---|
| 1259 | ipc_call_async_1(phone, imethod, arg1, NULL, NULL, true);
|
---|
| 1260 | }
|
---|
| 1261 |
|
---|
| 1262 | void async_msg_2(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2)
|
---|
| 1263 | {
|
---|
| 1264 | ipc_call_async_2(phone, imethod, arg1, arg2, NULL, NULL, true);
|
---|
| 1265 | }
|
---|
| 1266 |
|
---|
| 1267 | void async_msg_3(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
|
---|
| 1268 | sysarg_t arg3)
|
---|
| 1269 | {
|
---|
| 1270 | ipc_call_async_3(phone, imethod, arg1, arg2, arg3, NULL, NULL, true);
|
---|
| 1271 | }
|
---|
| 1272 |
|
---|
| 1273 | void async_msg_4(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
|
---|
| 1274 | sysarg_t arg3, sysarg_t arg4)
|
---|
| 1275 | {
|
---|
| 1276 | ipc_call_async_4(phone, imethod, arg1, arg2, arg3, arg4, NULL, NULL,
|
---|
| 1277 | true);
|
---|
| 1278 | }
|
---|
| 1279 |
|
---|
| 1280 | void async_msg_5(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
|
---|
| 1281 | sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
| 1282 | {
|
---|
| 1283 | ipc_call_async_5(phone, imethod, arg1, arg2, arg3, arg4, arg5, NULL,
|
---|
| 1284 | NULL, true);
|
---|
| 1285 | }
|
---|
| 1286 |
|
---|
| 1287 | sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
|
---|
| 1288 | {
|
---|
| 1289 | return ipc_answer_0(callid, retval);
|
---|
| 1290 | }
|
---|
| 1291 |
|
---|
| 1292 | sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
|
---|
| 1293 | {
|
---|
| 1294 | return ipc_answer_1(callid, retval, arg1);
|
---|
| 1295 | }
|
---|
| 1296 |
|
---|
| 1297 | sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
| 1298 | sysarg_t arg2)
|
---|
| 1299 | {
|
---|
| 1300 | return ipc_answer_2(callid, retval, arg1, arg2);
|
---|
| 1301 | }
|
---|
| 1302 |
|
---|
| 1303 | sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
| 1304 | sysarg_t arg2, sysarg_t arg3)
|
---|
| 1305 | {
|
---|
| 1306 | return ipc_answer_3(callid, retval, arg1, arg2, arg3);
|
---|
| 1307 | }
|
---|
| 1308 |
|
---|
| 1309 | sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
| 1310 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
| 1311 | {
|
---|
| 1312 | return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
|
---|
| 1313 | }
|
---|
| 1314 |
|
---|
| 1315 | sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
| 1316 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
| 1317 | {
|
---|
| 1318 | return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
|
---|
| 1319 | }
|
---|
| 1320 |
|
---|
[47b7006] | 1321 | int async_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
|
---|
| 1322 | sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
[64d2b10] | 1323 | {
|
---|
| 1324 | return ipc_forward_fast(callid, phoneid, imethod, arg1, arg2, mode);
|
---|
| 1325 | }
|
---|
| 1326 |
|
---|
[47b7006] | 1327 | int async_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
|
---|
[64d2b10] | 1328 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
[47b7006] | 1329 | unsigned int mode)
|
---|
[64d2b10] | 1330 | {
|
---|
| 1331 | return ipc_forward_slow(callid, phoneid, imethod, arg1, arg2, arg3, arg4,
|
---|
| 1332 | arg5, mode);
|
---|
| 1333 | }
|
---|
| 1334 |
|
---|
[007e6efa] | 1335 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
| 1336 | *
|
---|
| 1337 | * Ask through phone for a new connection to some service.
|
---|
| 1338 | *
|
---|
| 1339 | * @param phone Phone handle used for contacting the other side.
|
---|
| 1340 | * @param arg1 User defined argument.
|
---|
| 1341 | * @param arg2 User defined argument.
|
---|
| 1342 | * @param arg3 User defined argument.
|
---|
| 1343 | * @param client_receiver Connection handing routine.
|
---|
| 1344 | *
|
---|
| 1345 | * @return New phone handle on success or a negative error code.
|
---|
| 1346 | *
|
---|
| 1347 | */
|
---|
| 1348 | int async_connect_to_me(int phone, sysarg_t arg1, sysarg_t arg2,
|
---|
| 1349 | sysarg_t arg3, async_client_conn_t client_receiver)
|
---|
| 1350 | {
|
---|
| 1351 | sysarg_t task_hash;
|
---|
| 1352 | sysarg_t phone_hash;
|
---|
| 1353 | int rc = async_req_3_5(phone, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
|
---|
| 1354 | NULL, NULL, NULL, &task_hash, &phone_hash);
|
---|
| 1355 | if (rc != EOK)
|
---|
| 1356 | return rc;
|
---|
| 1357 |
|
---|
| 1358 | if (client_receiver != NULL)
|
---|
| 1359 | async_new_connection(task_hash, phone_hash, 0, NULL,
|
---|
| 1360 | client_receiver);
|
---|
| 1361 |
|
---|
| 1362 | return EOK;
|
---|
| 1363 | }
|
---|
| 1364 |
|
---|
[f74392f] | 1365 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
[007e6efa] | 1366 | *
|
---|
[f74392f] | 1367 | * Ask through phone for a new connection to some service.
|
---|
| 1368 | *
|
---|
[007e6efa] | 1369 | * @param phone Phone handle used for contacting the other side.
|
---|
| 1370 | * @param arg1 User defined argument.
|
---|
| 1371 | * @param arg2 User defined argument.
|
---|
| 1372 | * @param arg3 User defined argument.
|
---|
| 1373 | *
|
---|
| 1374 | * @return New phone handle on success or a negative error code.
|
---|
[f74392f] | 1375 | *
|
---|
| 1376 | */
|
---|
[007e6efa] | 1377 | int async_connect_me_to(int phone, sysarg_t arg1, sysarg_t arg2,
|
---|
| 1378 | sysarg_t arg3)
|
---|
[f74392f] | 1379 | {
|
---|
[96b02eb9] | 1380 | sysarg_t newphid;
|
---|
[007e6efa] | 1381 | int rc = async_req_3_5(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
|
---|
| 1382 | NULL, NULL, NULL, NULL, &newphid);
|
---|
[f74392f] | 1383 |
|
---|
[007e6efa] | 1384 | if (rc != EOK)
|
---|
[f74392f] | 1385 | return rc;
|
---|
[007e6efa] | 1386 |
|
---|
[f74392f] | 1387 | return newphid;
|
---|
| 1388 | }
|
---|
| 1389 |
|
---|
| 1390 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
[007e6efa] | 1391 | *
|
---|
[f74392f] | 1392 | * Ask through phone for a new connection to some service and block until
|
---|
| 1393 | * success.
|
---|
| 1394 | *
|
---|
[007e6efa] | 1395 | * @param phoneid Phone handle used for contacting the other side.
|
---|
| 1396 | * @param arg1 User defined argument.
|
---|
| 1397 | * @param arg2 User defined argument.
|
---|
| 1398 | * @param arg3 User defined argument.
|
---|
| 1399 | *
|
---|
| 1400 | * @return New phone handle on success or a negative error code.
|
---|
[f74392f] | 1401 | *
|
---|
| 1402 | */
|
---|
[007e6efa] | 1403 | int async_connect_me_to_blocking(int phoneid, sysarg_t arg1, sysarg_t arg2,
|
---|
[96b02eb9] | 1404 | sysarg_t arg3)
|
---|
[f74392f] | 1405 | {
|
---|
[96b02eb9] | 1406 | sysarg_t newphid;
|
---|
[007e6efa] | 1407 | int rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
|
---|
[f74392f] | 1408 | IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
|
---|
| 1409 |
|
---|
[007e6efa] | 1410 | if (rc != EOK)
|
---|
[f74392f] | 1411 | return rc;
|
---|
[007e6efa] | 1412 |
|
---|
[f74392f] | 1413 | return newphid;
|
---|
| 1414 | }
|
---|
| 1415 |
|
---|
[64d2b10] | 1416 | /** Connect to a task specified by id.
|
---|
| 1417 | *
|
---|
| 1418 | */
|
---|
| 1419 | int async_connect_kbox(task_id_t id)
|
---|
| 1420 | {
|
---|
| 1421 | return ipc_connect_kbox(id);
|
---|
| 1422 | }
|
---|
| 1423 |
|
---|
| 1424 | /** Wrapper for ipc_hangup.
|
---|
| 1425 | *
|
---|
| 1426 | * @param phone Phone handle to hung up.
|
---|
| 1427 | *
|
---|
| 1428 | * @return Zero on success or a negative error code.
|
---|
| 1429 | *
|
---|
| 1430 | */
|
---|
| 1431 | int async_hangup(int phone)
|
---|
| 1432 | {
|
---|
| 1433 | return ipc_hangup(phone);
|
---|
| 1434 | }
|
---|
| 1435 |
|
---|
| 1436 | /** Interrupt one thread of this task from waiting for IPC. */
|
---|
| 1437 | void async_poke(void)
|
---|
| 1438 | {
|
---|
| 1439 | ipc_poke();
|
---|
| 1440 | }
|
---|
| 1441 |
|
---|
[47b7006] | 1442 | /** Wrapper for IPC_M_SHARE_IN calls using the async framework.
|
---|
| 1443 | *
|
---|
| 1444 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
| 1445 | * @param dst Destination address space area base.
|
---|
| 1446 | * @param size Size of the destination address space area.
|
---|
| 1447 | * @param arg User defined argument.
|
---|
| 1448 | * @param flags Storage for the received flags. Can be NULL.
|
---|
[0da4e41] | 1449 | *
|
---|
[47b7006] | 1450 | * @return Zero on success or a negative error code from errno.h.
|
---|
[0da4e41] | 1451 | *
|
---|
| 1452 | */
|
---|
[96b02eb9] | 1453 | int async_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
|
---|
[47b7006] | 1454 | unsigned int *flags)
|
---|
[0da4e41] | 1455 | {
|
---|
| 1456 | sysarg_t tmp_flags;
|
---|
[47b7006] | 1457 | int res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
|
---|
[96b02eb9] | 1458 | (sysarg_t) size, arg, NULL, &tmp_flags);
|
---|
[47b7006] | 1459 |
|
---|
[0da4e41] | 1460 | if (flags)
|
---|
[47b7006] | 1461 | *flags = (unsigned int) tmp_flags;
|
---|
| 1462 |
|
---|
[0da4e41] | 1463 | return res;
|
---|
| 1464 | }
|
---|
| 1465 |
|
---|
| 1466 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
---|
| 1467 | *
|
---|
[47b7006] | 1468 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
|
---|
| 1469 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1470 | * argument.
|
---|
[0da4e41] | 1471 | *
|
---|
| 1472 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1473 | *
|
---|
[47b7006] | 1474 | * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
|
---|
| 1475 | * @param size Destination address space area size.
|
---|
| 1476 | *
|
---|
| 1477 | * @return True on success, false on failure.
|
---|
[0da4e41] | 1478 | *
|
---|
| 1479 | */
|
---|
[47b7006] | 1480 | bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
|
---|
[0da4e41] | 1481 | {
|
---|
| 1482 | assert(callid);
|
---|
| 1483 | assert(size);
|
---|
[47b7006] | 1484 |
|
---|
| 1485 | ipc_call_t data;
|
---|
[0da4e41] | 1486 | *callid = async_get_call(&data);
|
---|
[47b7006] | 1487 |
|
---|
[228e490] | 1488 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
|
---|
[47b7006] | 1489 | return false;
|
---|
| 1490 |
|
---|
[0da4e41] | 1491 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
[47b7006] | 1492 | return true;
|
---|
[0da4e41] | 1493 | }
|
---|
| 1494 |
|
---|
| 1495 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
---|
| 1496 | *
|
---|
[47b7006] | 1497 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
---|
| 1498 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1499 | * argument.
|
---|
[0da4e41] | 1500 | *
|
---|
[47b7006] | 1501 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
| 1502 | * @param src Source address space base.
|
---|
| 1503 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
| 1504 | *
|
---|
| 1505 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1506 | *
|
---|
| 1507 | */
|
---|
[47b7006] | 1508 | int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
|
---|
[0da4e41] | 1509 | {
|
---|
| 1510 | return ipc_share_in_finalize(callid, src, flags);
|
---|
| 1511 | }
|
---|
| 1512 |
|
---|
[47b7006] | 1513 | /** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
|
---|
[0da4e41] | 1514 | *
|
---|
[47b7006] | 1515 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
| 1516 | * @param src Source address space area base address.
|
---|
| 1517 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
| 1518 | *
|
---|
| 1519 | * @return Zero on success or a negative error code from errno.h.
|
---|
[0da4e41] | 1520 | *
|
---|
| 1521 | */
|
---|
[47b7006] | 1522 | int async_share_out_start(int phoneid, void *src, unsigned int flags)
|
---|
[0da4e41] | 1523 | {
|
---|
[96b02eb9] | 1524 | return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
|
---|
| 1525 | (sysarg_t) flags);
|
---|
[0da4e41] | 1526 | }
|
---|
| 1527 |
|
---|
| 1528 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
---|
| 1529 | *
|
---|
[47b7006] | 1530 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
|
---|
| 1531 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1532 | * argument.
|
---|
[0da4e41] | 1533 | *
|
---|
| 1534 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1535 | *
|
---|
[47b7006] | 1536 | * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
|
---|
| 1537 | * @param size Storage for the source address space area size.
|
---|
| 1538 | * @param flags Storage for the sharing flags.
|
---|
| 1539 | *
|
---|
| 1540 | * @return True on success, false on failure.
|
---|
[0da4e41] | 1541 | *
|
---|
| 1542 | */
|
---|
[47b7006] | 1543 | bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
|
---|
[0da4e41] | 1544 | {
|
---|
| 1545 | assert(callid);
|
---|
| 1546 | assert(size);
|
---|
| 1547 | assert(flags);
|
---|
[47b7006] | 1548 |
|
---|
| 1549 | ipc_call_t data;
|
---|
[0da4e41] | 1550 | *callid = async_get_call(&data);
|
---|
[47b7006] | 1551 |
|
---|
[228e490] | 1552 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
|
---|
[47b7006] | 1553 | return false;
|
---|
| 1554 |
|
---|
[0da4e41] | 1555 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
[47b7006] | 1556 | *flags = (unsigned int) IPC_GET_ARG3(data);
|
---|
| 1557 | return true;
|
---|
[0da4e41] | 1558 | }
|
---|
| 1559 |
|
---|
| 1560 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
---|
| 1561 | *
|
---|
[47b7006] | 1562 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
|
---|
| 1563 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1564 | * argument.
|
---|
[0da4e41] | 1565 | *
|
---|
[47b7006] | 1566 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
| 1567 | * @param dst Destination address space area base address.
|
---|
| 1568 | *
|
---|
| 1569 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1570 | *
|
---|
| 1571 | */
|
---|
| 1572 | int async_share_out_finalize(ipc_callid_t callid, void *dst)
|
---|
| 1573 | {
|
---|
| 1574 | return ipc_share_out_finalize(callid, dst);
|
---|
| 1575 | }
|
---|
| 1576 |
|
---|
[47b7006] | 1577 | /** Wrapper for IPC_M_DATA_READ calls using the async framework.
|
---|
[0da4e41] | 1578 | *
|
---|
[47b7006] | 1579 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
| 1580 | * @param dst Address of the beginning of the destination buffer.
|
---|
| 1581 | * @param size Size of the destination buffer.
|
---|
| 1582 | *
|
---|
| 1583 | * @return Zero on success or a negative error code from errno.h.
|
---|
[0da4e41] | 1584 | *
|
---|
| 1585 | */
|
---|
| 1586 | int async_data_read_start(int phoneid, void *dst, size_t size)
|
---|
| 1587 | {
|
---|
[96b02eb9] | 1588 | return async_req_2_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
|
---|
| 1589 | (sysarg_t) size);
|
---|
[0da4e41] | 1590 | }
|
---|
| 1591 |
|
---|
| 1592 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
---|
| 1593 | *
|
---|
[47b7006] | 1594 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
|
---|
| 1595 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1596 | * argument.
|
---|
[0da4e41] | 1597 | *
|
---|
| 1598 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1599 | *
|
---|
[47b7006] | 1600 | * @param callid Storage for the hash of the IPC_M_DATA_READ.
|
---|
| 1601 | * @param size Storage for the maximum size. Can be NULL.
|
---|
| 1602 | *
|
---|
| 1603 | * @return True on success, false on failure.
|
---|
[0da4e41] | 1604 | *
|
---|
| 1605 | */
|
---|
[47b7006] | 1606 | bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
|
---|
[0da4e41] | 1607 | {
|
---|
| 1608 | assert(callid);
|
---|
[47b7006] | 1609 |
|
---|
| 1610 | ipc_call_t data;
|
---|
[0da4e41] | 1611 | *callid = async_get_call(&data);
|
---|
[47b7006] | 1612 |
|
---|
[228e490] | 1613 | if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
|
---|
[47b7006] | 1614 | return false;
|
---|
| 1615 |
|
---|
[0da4e41] | 1616 | if (size)
|
---|
| 1617 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
[47b7006] | 1618 |
|
---|
| 1619 | return true;
|
---|
[0da4e41] | 1620 | }
|
---|
| 1621 |
|
---|
| 1622 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
---|
| 1623 | *
|
---|
[47b7006] | 1624 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
---|
| 1625 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1626 | * argument.
|
---|
[0da4e41] | 1627 | *
|
---|
[47b7006] | 1628 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
| 1629 | * @param src Source address for the IPC_M_DATA_READ call.
|
---|
| 1630 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
---|
| 1631 | * the maximum size announced by the sender.
|
---|
| 1632 | *
|
---|
| 1633 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1634 | *
|
---|
| 1635 | */
|
---|
| 1636 | int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
|
---|
| 1637 | {
|
---|
| 1638 | return ipc_data_read_finalize(callid, src, size);
|
---|
| 1639 | }
|
---|
| 1640 |
|
---|
[b4cbef1] | 1641 | /** Wrapper for forwarding any read request
|
---|
| 1642 | *
|
---|
| 1643 | */
|
---|
[96b02eb9] | 1644 | int async_data_read_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
| 1645 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
---|
[b4cbef1] | 1646 | {
|
---|
| 1647 | ipc_callid_t callid;
|
---|
| 1648 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
| 1649 | ipc_answer_0(callid, EINVAL);
|
---|
| 1650 | return EINVAL;
|
---|
| 1651 | }
|
---|
| 1652 |
|
---|
| 1653 | aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
| 1654 | dataptr);
|
---|
| 1655 | if (msg == 0) {
|
---|
| 1656 | ipc_answer_0(callid, EINVAL);
|
---|
| 1657 | return EINVAL;
|
---|
| 1658 | }
|
---|
| 1659 |
|
---|
| 1660 | int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
|
---|
| 1661 | IPC_FF_ROUTE_FROM_ME);
|
---|
| 1662 | if (retval != EOK) {
|
---|
[a281fc82] | 1663 | async_wait_for(msg, NULL);
|
---|
[b4cbef1] | 1664 | ipc_answer_0(callid, retval);
|
---|
| 1665 | return retval;
|
---|
| 1666 | }
|
---|
| 1667 |
|
---|
[96b02eb9] | 1668 | sysarg_t rc;
|
---|
[b4cbef1] | 1669 | async_wait_for(msg, &rc);
|
---|
| 1670 |
|
---|
| 1671 | return (int) rc;
|
---|
| 1672 | }
|
---|
| 1673 |
|
---|
[47b7006] | 1674 | /** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
|
---|
[0da4e41] | 1675 | *
|
---|
[b4cbef1] | 1676 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
| 1677 | * @param src Address of the beginning of the source buffer.
|
---|
| 1678 | * @param size Size of the source buffer.
|
---|
| 1679 | *
|
---|
| 1680 | * @return Zero on success or a negative error code from errno.h.
|
---|
[0da4e41] | 1681 | *
|
---|
| 1682 | */
|
---|
| 1683 | int async_data_write_start(int phoneid, const void *src, size_t size)
|
---|
| 1684 | {
|
---|
[96b02eb9] | 1685 | return async_req_2_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
|
---|
| 1686 | (sysarg_t) size);
|
---|
[0da4e41] | 1687 | }
|
---|
| 1688 |
|
---|
| 1689 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
---|
| 1690 | *
|
---|
[47b7006] | 1691 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
|
---|
| 1692 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1693 | * argument.
|
---|
[0da4e41] | 1694 | *
|
---|
| 1695 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1696 | *
|
---|
[47b7006] | 1697 | * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
|
---|
| 1698 | * @param size Storage for the suggested size. May be NULL.
|
---|
[b4cbef1] | 1699 | *
|
---|
[47b7006] | 1700 | * @return True on success, false on failure.
|
---|
[0da4e41] | 1701 | *
|
---|
| 1702 | */
|
---|
[47b7006] | 1703 | bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
|
---|
[0da4e41] | 1704 | {
|
---|
| 1705 | assert(callid);
|
---|
[b4cbef1] | 1706 |
|
---|
[47b7006] | 1707 | ipc_call_t data;
|
---|
[0da4e41] | 1708 | *callid = async_get_call(&data);
|
---|
[47b7006] | 1709 |
|
---|
[228e490] | 1710 | if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
|
---|
[47b7006] | 1711 | return false;
|
---|
[b4cbef1] | 1712 |
|
---|
[0da4e41] | 1713 | if (size)
|
---|
| 1714 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
[b4cbef1] | 1715 |
|
---|
[47b7006] | 1716 | return true;
|
---|
[0da4e41] | 1717 | }
|
---|
| 1718 |
|
---|
| 1719 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
---|
| 1720 | *
|
---|
[47b7006] | 1721 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
|
---|
| 1722 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1723 | * argument.
|
---|
[0da4e41] | 1724 | *
|
---|
[b4cbef1] | 1725 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
| 1726 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
---|
| 1727 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
---|
| 1728 | *
|
---|
| 1729 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1730 | *
|
---|
| 1731 | */
|
---|
| 1732 | int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
|
---|
| 1733 | {
|
---|
| 1734 | return ipc_data_write_finalize(callid, dst, size);
|
---|
| 1735 | }
|
---|
| 1736 |
|
---|
[eda925a] | 1737 | /** Wrapper for receiving binary data or strings
|
---|
[8aa42e3] | 1738 | *
|
---|
| 1739 | * This wrapper only makes it more comfortable to use async_data_write_*
|
---|
[eda925a] | 1740 | * functions to receive binary data or strings.
|
---|
[8aa42e3] | 1741 | *
|
---|
[472c09d] | 1742 | * @param data Pointer to data pointer (which should be later disposed
|
---|
| 1743 | * by free()). If the operation fails, the pointer is not
|
---|
| 1744 | * touched.
|
---|
[eda925a] | 1745 | * @param nullterm If true then the received data is always zero terminated.
|
---|
| 1746 | * This also causes to allocate one extra byte beyond the
|
---|
| 1747 | * raw transmitted data.
|
---|
[b4cbef1] | 1748 | * @param min_size Minimum size (in bytes) of the data to receive.
|
---|
[472c09d] | 1749 | * @param max_size Maximum size (in bytes) of the data to receive. 0 means
|
---|
| 1750 | * no limit.
|
---|
[eda925a] | 1751 | * @param granulariy If non-zero then the size of the received data has to
|
---|
[472c09d] | 1752 | * be divisible by this value.
|
---|
| 1753 | * @param received If not NULL, the size of the received data is stored here.
|
---|
[8aa42e3] | 1754 | *
|
---|
| 1755 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
| 1756 | *
|
---|
| 1757 | */
|
---|
[eda925a] | 1758 | int async_data_write_accept(void **data, const bool nullterm,
|
---|
| 1759 | const size_t min_size, const size_t max_size, const size_t granularity,
|
---|
| 1760 | size_t *received)
|
---|
[8aa42e3] | 1761 | {
|
---|
| 1762 | ipc_callid_t callid;
|
---|
| 1763 | size_t size;
|
---|
| 1764 | if (!async_data_write_receive(&callid, &size)) {
|
---|
| 1765 | ipc_answer_0(callid, EINVAL);
|
---|
| 1766 | return EINVAL;
|
---|
| 1767 | }
|
---|
| 1768 |
|
---|
[b4cbef1] | 1769 | if (size < min_size) {
|
---|
| 1770 | ipc_answer_0(callid, EINVAL);
|
---|
| 1771 | return EINVAL;
|
---|
| 1772 | }
|
---|
| 1773 |
|
---|
[8aa42e3] | 1774 | if ((max_size > 0) && (size > max_size)) {
|
---|
| 1775 | ipc_answer_0(callid, EINVAL);
|
---|
| 1776 | return EINVAL;
|
---|
| 1777 | }
|
---|
| 1778 |
|
---|
[472c09d] | 1779 | if ((granularity > 0) && ((size % granularity) != 0)) {
|
---|
| 1780 | ipc_answer_0(callid, EINVAL);
|
---|
| 1781 | return EINVAL;
|
---|
| 1782 | }
|
---|
| 1783 |
|
---|
[eda925a] | 1784 | void *_data;
|
---|
| 1785 |
|
---|
| 1786 | if (nullterm)
|
---|
| 1787 | _data = malloc(size + 1);
|
---|
| 1788 | else
|
---|
| 1789 | _data = malloc(size);
|
---|
| 1790 |
|
---|
[472c09d] | 1791 | if (_data == NULL) {
|
---|
[8aa42e3] | 1792 | ipc_answer_0(callid, ENOMEM);
|
---|
| 1793 | return ENOMEM;
|
---|
| 1794 | }
|
---|
| 1795 |
|
---|
[472c09d] | 1796 | int rc = async_data_write_finalize(callid, _data, size);
|
---|
[8aa42e3] | 1797 | if (rc != EOK) {
|
---|
[472c09d] | 1798 | free(_data);
|
---|
[8aa42e3] | 1799 | return rc;
|
---|
| 1800 | }
|
---|
| 1801 |
|
---|
[eda925a] | 1802 | if (nullterm)
|
---|
| 1803 | ((char *) _data)[size] = 0;
|
---|
[8aa42e3] | 1804 |
|
---|
[eda925a] | 1805 | *data = _data;
|
---|
[472c09d] | 1806 | if (received != NULL)
|
---|
| 1807 | *received = size;
|
---|
| 1808 |
|
---|
[8aa42e3] | 1809 | return EOK;
|
---|
| 1810 | }
|
---|
| 1811 |
|
---|
[b4cbef1] | 1812 | /** Wrapper for voiding any data that is about to be received
|
---|
| 1813 | *
|
---|
| 1814 | * This wrapper can be used to void any pending data
|
---|
| 1815 | *
|
---|
| 1816 | * @param retval Error value from @ref errno.h to be returned to the caller.
|
---|
| 1817 | *
|
---|
| 1818 | */
|
---|
[47b7006] | 1819 | void async_data_write_void(sysarg_t retval)
|
---|
[b4cbef1] | 1820 | {
|
---|
| 1821 | ipc_callid_t callid;
|
---|
| 1822 | async_data_write_receive(&callid, NULL);
|
---|
| 1823 | ipc_answer_0(callid, retval);
|
---|
| 1824 | }
|
---|
| 1825 |
|
---|
| 1826 | /** Wrapper for forwarding any data that is about to be received
|
---|
| 1827 | *
|
---|
| 1828 | */
|
---|
[96b02eb9] | 1829 | int async_data_write_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
| 1830 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
---|
[b4cbef1] | 1831 | {
|
---|
| 1832 | ipc_callid_t callid;
|
---|
| 1833 | if (!async_data_write_receive(&callid, NULL)) {
|
---|
| 1834 | ipc_answer_0(callid, EINVAL);
|
---|
| 1835 | return EINVAL;
|
---|
| 1836 | }
|
---|
| 1837 |
|
---|
| 1838 | aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
| 1839 | dataptr);
|
---|
| 1840 | if (msg == 0) {
|
---|
| 1841 | ipc_answer_0(callid, EINVAL);
|
---|
| 1842 | return EINVAL;
|
---|
| 1843 | }
|
---|
| 1844 |
|
---|
| 1845 | int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
|
---|
| 1846 | IPC_FF_ROUTE_FROM_ME);
|
---|
| 1847 | if (retval != EOK) {
|
---|
[a281fc82] | 1848 | async_wait_for(msg, NULL);
|
---|
[b4cbef1] | 1849 | ipc_answer_0(callid, retval);
|
---|
| 1850 | return retval;
|
---|
| 1851 | }
|
---|
| 1852 |
|
---|
[96b02eb9] | 1853 | sysarg_t rc;
|
---|
[b4cbef1] | 1854 | async_wait_for(msg, &rc);
|
---|
| 1855 |
|
---|
| 1856 | return (int) rc;
|
---|
| 1857 | }
|
---|
| 1858 |
|
---|
[a46da63] | 1859 | /** @}
|
---|
[b2951e2] | 1860 | */
|
---|