[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 | *
|
---|
[984a9ba] | 79 | * port_handler(ipc_call_t *icall)
|
---|
[c07544d3] | 80 | * {
|
---|
| 81 | * if (want_refuse) {
|
---|
[984a9ba] | 82 | * async_answer_0(icall, ELIMIT);
|
---|
[c07544d3] | 83 | * return;
|
---|
| 84 | * }
|
---|
[80649a91] | 85 | *
|
---|
[984a9ba] | 86 | * async_answer_0(icall, EOK);
|
---|
[53ca318] | 87 | *
|
---|
[984a9ba] | 88 | * async_get_call(&call);
|
---|
| 89 | * somehow_handle_the_call(&call);
|
---|
| 90 | * async_answer_2(&call, 1, 2, 3);
|
---|
| 91 | *
|
---|
| 92 | * async_get_call(&call);
|
---|
[c07544d3] | 93 | * ...
|
---|
| 94 | * }
|
---|
[a2cd194] | 95 | *
|
---|
[80649a91] | 96 | */
|
---|
[9591265] | 97 |
|
---|
[64d2b10] | 98 | #define LIBC_ASYNC_C_
|
---|
| 99 | #include <ipc/ipc.h>
|
---|
[80649a91] | 100 | #include <async.h>
|
---|
[49a796f1] | 101 | #include "../private/async.h"
|
---|
[64d2b10] | 102 | #undef LIBC_ASYNC_C_
|
---|
| 103 |
|
---|
[8820544] | 104 | #include <ipc/irq.h>
|
---|
| 105 | #include <ipc/event.h>
|
---|
[bc1f1c2] | 106 | #include <fibril.h>
|
---|
[d9c8c81] | 107 | #include <adt/hash_table.h>
|
---|
[853802e] | 108 | #include <adt/hash.h>
|
---|
[d9c8c81] | 109 | #include <adt/list.h>
|
---|
[80649a91] | 110 | #include <assert.h>
|
---|
| 111 | #include <errno.h>
|
---|
[bd41ac52] | 112 | #include <time.h>
|
---|
[3e6a98c5] | 113 | #include <stdbool.h>
|
---|
[38d150e] | 114 | #include <stdlib.h>
|
---|
[79ae36dd] | 115 | #include <mem.h>
|
---|
| 116 | #include <stdlib.h>
|
---|
[e2ab36f1] | 117 | #include <macros.h>
|
---|
[514d561] | 118 | #include <str_error.h>
|
---|
[101516d] | 119 | #include <as.h>
|
---|
[ae6021d] | 120 | #include <abi/mm/as.h>
|
---|
[49a796f1] | 121 | #include "../private/libc.h"
|
---|
[d73d992] | 122 | #include "../private/fibril.h"
|
---|
[5da7199] | 123 |
|
---|
[8dab988] | 124 | #define DPRINTF(...) ((void) 0)
|
---|
| 125 |
|
---|
[79ae36dd] | 126 | /* Client connection data */
|
---|
[c80fdd0] | 127 | typedef struct {
|
---|
[062d900] | 128 | ht_link_t link;
|
---|
[a35b458] | 129 |
|
---|
[649f087] | 130 | task_id_t in_task_id;
|
---|
[498ced1] | 131 | int refcnt;
|
---|
[c80fdd0] | 132 | void *data;
|
---|
| 133 | } client_t;
|
---|
| 134 |
|
---|
[79ae36dd] | 135 | /* Server connection data */
|
---|
[80649a91] | 136 | typedef struct {
|
---|
[514d561] | 137 | /** Fibril handling the connection. */
|
---|
| 138 | fid_t fid;
|
---|
[a35b458] | 139 |
|
---|
[e70bfa5] | 140 | /** Hash table link. */
|
---|
[062d900] | 141 | ht_link_t link;
|
---|
[a35b458] | 142 |
|
---|
[e2ab36f1] | 143 | /** Incoming client task ID. */
|
---|
| 144 | task_id_t in_task_id;
|
---|
[a35b458] | 145 |
|
---|
[23882034] | 146 | /** Link to the client tracking structure. */
|
---|
| 147 | client_t *client;
|
---|
[a35b458] | 148 |
|
---|
[1de92fb0] | 149 | /** Channel for messages that should be delivered to this fibril. */
|
---|
| 150 | mpsc_t *msg_channel;
|
---|
[a35b458] | 151 |
|
---|
[e70bfa5] | 152 | /** Call data of the opening call. */
|
---|
[80649a91] | 153 | ipc_call_t call;
|
---|
[a35b458] | 154 |
|
---|
[e70bfa5] | 155 | /** Fibril function that will be used to handle the connection. */
|
---|
[b688fd8] | 156 | async_port_handler_t handler;
|
---|
[a35b458] | 157 |
|
---|
[b688fd8] | 158 | /** Client data */
|
---|
| 159 | void *data;
|
---|
[80649a91] | 160 | } connection_t;
|
---|
| 161 |
|
---|
[8dab988] | 162 | /* Member of notification_t::msg_list. */
|
---|
| 163 | typedef struct {
|
---|
| 164 | link_t link;
|
---|
| 165 | ipc_call_t calldata;
|
---|
| 166 | } notification_msg_t;
|
---|
| 167 |
|
---|
[8820544] | 168 | /* Notification data */
|
---|
| 169 | typedef struct {
|
---|
[3b1cc8d] | 170 | /** notification_hash_table link */
|
---|
| 171 | ht_link_t htlink;
|
---|
| 172 |
|
---|
| 173 | /** notification_queue link */
|
---|
| 174 | link_t qlink;
|
---|
[a35b458] | 175 |
|
---|
[8820544] | 176 | /** Notification method */
|
---|
| 177 | sysarg_t imethod;
|
---|
[a35b458] | 178 |
|
---|
[8820544] | 179 | /** Notification handler */
|
---|
| 180 | async_notification_handler_t handler;
|
---|
[a35b458] | 181 |
|
---|
[3b1cc8d] | 182 | /** Notification handler argument */
|
---|
| 183 | void *arg;
|
---|
| 184 |
|
---|
[8dab988] | 185 | /** List of arrived notifications. */
|
---|
| 186 | list_t msg_list;
|
---|
[8820544] | 187 | } notification_t;
|
---|
| 188 |
|
---|
[bc1f1c2] | 189 | /** Identifier of the incoming connection handled by the current fibril. */
|
---|
[79ae36dd] | 190 | static fibril_local connection_t *fibril_connection;
|
---|
[e70bfa5] | 191 |
|
---|
[46eec3b] | 192 | static void *default_client_data_constructor(void)
|
---|
| 193 | {
|
---|
| 194 | return NULL;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | static void default_client_data_destructor(void *data)
|
---|
| 198 | {
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | static async_client_data_ctor_t async_client_data_create =
|
---|
| 202 | default_client_data_constructor;
|
---|
| 203 | static async_client_data_dtor_t async_client_data_destroy =
|
---|
| 204 | default_client_data_destructor;
|
---|
| 205 |
|
---|
| 206 | void async_set_client_data_constructor(async_client_data_ctor_t ctor)
|
---|
| 207 | {
|
---|
[f302586] | 208 | assert(async_client_data_create == default_client_data_constructor);
|
---|
[46eec3b] | 209 | async_client_data_create = ctor;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | void async_set_client_data_destructor(async_client_data_dtor_t dtor)
|
---|
| 213 | {
|
---|
[f302586] | 214 | assert(async_client_data_destroy == default_client_data_destructor);
|
---|
[46eec3b] | 215 | async_client_data_destroy = dtor;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[45c8eea] | 218 | static fibril_rmutex_t client_mutex;
|
---|
[c80fdd0] | 219 | static hash_table_t client_hash_table;
|
---|
[3b1cc8d] | 220 |
|
---|
| 221 | // TODO: lockfree notification_queue?
|
---|
[45c8eea] | 222 | static fibril_rmutex_t notification_mutex;
|
---|
[8820544] | 223 | static hash_table_t notification_hash_table;
|
---|
[3b1cc8d] | 224 | static LIST_INITIALIZE(notification_queue);
|
---|
| 225 | static FIBRIL_SEMAPHORE_INITIALIZE(notification_semaphore, 0);
|
---|
| 226 |
|
---|
[8dab988] | 227 | static LIST_INITIALIZE(notification_freelist);
|
---|
| 228 | static long notification_freelist_total = 0;
|
---|
| 229 | static long notification_freelist_used = 0;
|
---|
| 230 |
|
---|
[8820544] | 231 | static sysarg_t notification_avail = 0;
|
---|
| 232 |
|
---|
| 233 | static size_t client_key_hash(void *key)
|
---|
[c80fdd0] | 234 | {
|
---|
[8820544] | 235 | task_id_t in_task_id = *(task_id_t *) key;
|
---|
| 236 | return in_task_id;
|
---|
[c80fdd0] | 237 | }
|
---|
| 238 |
|
---|
[062d900] | 239 | static size_t client_hash(const ht_link_t *item)
|
---|
[c80fdd0] | 240 | {
|
---|
[062d900] | 241 | client_t *client = hash_table_get_inst(item, client_t, link);
|
---|
| 242 | return client_key_hash(&client->in_task_id);
|
---|
[c80fdd0] | 243 | }
|
---|
| 244 |
|
---|
[8820544] | 245 | static bool client_key_equal(void *key, const ht_link_t *item)
|
---|
[c80fdd0] | 246 | {
|
---|
[8820544] | 247 | task_id_t in_task_id = *(task_id_t *) key;
|
---|
[062d900] | 248 | client_t *client = hash_table_get_inst(item, client_t, link);
|
---|
[8820544] | 249 | return in_task_id == client->in_task_id;
|
---|
[c80fdd0] | 250 | }
|
---|
| 251 |
|
---|
| 252 | /** Operations for the client hash table. */
|
---|
[062d900] | 253 | static hash_table_ops_t client_hash_table_ops = {
|
---|
[c80fdd0] | 254 | .hash = client_hash,
|
---|
[062d900] | 255 | .key_hash = client_key_hash,
|
---|
| 256 | .key_equal = client_key_equal,
|
---|
[4e00f87] | 257 | .equal = NULL,
|
---|
| 258 | .remove_callback = NULL
|
---|
[c80fdd0] | 259 | };
|
---|
[80649a91] | 260 |
|
---|
[9ef495f] | 261 | static client_t *async_client_get(task_id_t client_id, bool create)
|
---|
| 262 | {
|
---|
| 263 | client_t *client = NULL;
|
---|
[a35b458] | 264 |
|
---|
[9bde0d5] | 265 | fibril_rmutex_lock(&client_mutex);
|
---|
[9ef495f] | 266 | ht_link_t *link = hash_table_find(&client_hash_table, &client_id);
|
---|
| 267 | if (link) {
|
---|
| 268 | client = hash_table_get_inst(link, client_t, link);
|
---|
[498ced1] | 269 | client->refcnt++;
|
---|
[9ef495f] | 270 | } else if (create) {
|
---|
[3bd1d7d4] | 271 | // TODO: move the malloc out of critical section
|
---|
[9bde0d5] | 272 | /* malloc() is rmutex safe. */
|
---|
[9ef495f] | 273 | client = malloc(sizeof(client_t));
|
---|
| 274 | if (client) {
|
---|
| 275 | client->in_task_id = client_id;
|
---|
| 276 | client->data = async_client_data_create();
|
---|
[a35b458] | 277 |
|
---|
[498ced1] | 278 | client->refcnt = 1;
|
---|
[9ef495f] | 279 | hash_table_insert(&client_hash_table, &client->link);
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
[a35b458] | 282 |
|
---|
[9bde0d5] | 283 | fibril_rmutex_unlock(&client_mutex);
|
---|
[9ef495f] | 284 | return client;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | static void async_client_put(client_t *client)
|
---|
| 288 | {
|
---|
| 289 | bool destroy;
|
---|
[a35b458] | 290 |
|
---|
[9bde0d5] | 291 | fibril_rmutex_lock(&client_mutex);
|
---|
[a35b458] | 292 |
|
---|
[498ced1] | 293 | if (--client->refcnt == 0) {
|
---|
[9ef495f] | 294 | hash_table_remove(&client_hash_table, &client->in_task_id);
|
---|
| 295 | destroy = true;
|
---|
| 296 | } else
|
---|
| 297 | destroy = false;
|
---|
[a35b458] | 298 |
|
---|
[9bde0d5] | 299 | fibril_rmutex_unlock(&client_mutex);
|
---|
[a35b458] | 300 |
|
---|
[9ef495f] | 301 | if (destroy) {
|
---|
| 302 | if (client->data)
|
---|
| 303 | async_client_data_destroy(client->data);
|
---|
[a35b458] | 304 |
|
---|
[9ef495f] | 305 | free(client);
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /** Wrapper for client connection fibril.
|
---|
| 310 | *
|
---|
| 311 | * When a new connection arrives, a fibril with this implementing
|
---|
| 312 | * function is created.
|
---|
| 313 | *
|
---|
| 314 | * @param arg Connection structure pointer.
|
---|
| 315 | *
|
---|
| 316 | * @return Always zero.
|
---|
| 317 | *
|
---|
| 318 | */
|
---|
[b7fd2a0] | 319 | static errno_t connection_fibril(void *arg)
|
---|
[9ef495f] | 320 | {
|
---|
| 321 | assert(arg);
|
---|
[a35b458] | 322 |
|
---|
[9ef495f] | 323 | /*
|
---|
| 324 | * Setup fibril-local connection pointer.
|
---|
| 325 | */
|
---|
| 326 | fibril_connection = (connection_t *) arg;
|
---|
[a35b458] | 327 |
|
---|
[6769005] | 328 | mpsc_t *c = fibril_connection->msg_channel;
|
---|
| 329 |
|
---|
[9ef495f] | 330 | /*
|
---|
| 331 | * Add our reference for the current connection in the client task
|
---|
| 332 | * tracking structure. If this is the first reference, create and
|
---|
| 333 | * hash in a new tracking structure.
|
---|
| 334 | */
|
---|
[a35b458] | 335 |
|
---|
[9ef495f] | 336 | client_t *client = async_client_get(fibril_connection->in_task_id, true);
|
---|
| 337 | if (!client) {
|
---|
[061274f] | 338 | ipc_answer_0(fibril_connection->call.cap_handle, ENOMEM);
|
---|
[6769005] | 339 | goto out;
|
---|
[9ef495f] | 340 | }
|
---|
[a35b458] | 341 |
|
---|
[9ef495f] | 342 | fibril_connection->client = client;
|
---|
[a35b458] | 343 |
|
---|
[9ef495f] | 344 | /*
|
---|
| 345 | * Call the connection handler function.
|
---|
| 346 | */
|
---|
[984a9ba] | 347 | fibril_connection->handler(&fibril_connection->call,
|
---|
| 348 | fibril_connection->data);
|
---|
[a35b458] | 349 |
|
---|
[9ef495f] | 350 | /*
|
---|
| 351 | * Remove the reference for this client task connection.
|
---|
| 352 | */
|
---|
| 353 | async_client_put(client);
|
---|
[a35b458] | 354 |
|
---|
[1de92fb0] | 355 | /*
|
---|
| 356 | * Close the channel, if it isn't closed already.
|
---|
| 357 | */
|
---|
| 358 | mpsc_close(c);
|
---|
| 359 |
|
---|
[9ef495f] | 360 | /*
|
---|
| 361 | * Answer all remaining messages with EHANGUP.
|
---|
| 362 | */
|
---|
[1de92fb0] | 363 | ipc_call_t call;
|
---|
| 364 | while (mpsc_receive(c, &call, NULL) == EOK)
|
---|
| 365 | ipc_answer_0(call.cap_handle, EHANGUP);
|
---|
[a35b458] | 366 |
|
---|
[9ef495f] | 367 | /*
|
---|
[1de92fb0] | 368 | * Clean up memory.
|
---|
[9ef495f] | 369 | */
|
---|
[6769005] | 370 | out:
|
---|
[1de92fb0] | 371 | mpsc_destroy(c);
|
---|
[9ef495f] | 372 | free(fibril_connection);
|
---|
[d5c1051] | 373 | return EOK;
|
---|
[9ef495f] | 374 | }
|
---|
| 375 |
|
---|
[6769005] | 376 | /** Return label usable during replies to IPC_M_CONNECT_ME_TO. */
|
---|
| 377 | sysarg_t async_get_label(void)
|
---|
| 378 | {
|
---|
[53ee7a0] | 379 | return (sysarg_t) fibril_connection;
|
---|
[6769005] | 380 | }
|
---|
| 381 |
|
---|
[9ef495f] | 382 | /** Create a new fibril for a new connection.
|
---|
| 383 | *
|
---|
[01c3bb4] | 384 | * Create new fibril for connection, fill in connection structures and insert it
|
---|
| 385 | * into the hash table, so that later we can easily do routing of messages to
|
---|
| 386 | * particular fibrils.
|
---|
[9ef495f] | 387 | *
|
---|
[6769005] | 388 | * @param conn Pointer to the connection structure. Will be used as the
|
---|
| 389 | * label of the connected phone and request_label of incoming
|
---|
| 390 | * calls routed through that phone.
|
---|
| 391 | * @param in_task_id Identification of the incoming connection.
|
---|
| 392 | * @param call Call data of the opening call. If call is NULL, the
|
---|
| 393 | * connection was opened by accepting the
|
---|
| 394 | * IPC_M_CONNECT_TO_ME call and this function is called
|
---|
| 395 | * directly by the server.
|
---|
| 396 | * @param handler Connection handler.
|
---|
| 397 | * @param data Client argument to pass to the connection handler.
|
---|
[9ef495f] | 398 | *
|
---|
[01c3bb4] | 399 | * @return New fibril id or NULL on failure.
|
---|
[9ef495f] | 400 | *
|
---|
| 401 | */
|
---|
[6769005] | 402 | static fid_t async_new_connection(connection_t *conn, task_id_t in_task_id,
|
---|
[061274f] | 403 | ipc_call_t *call, async_port_handler_t handler, void *data)
|
---|
[9ef495f] | 404 | {
|
---|
| 405 | conn->in_task_id = in_task_id;
|
---|
[1de92fb0] | 406 | conn->msg_channel = mpsc_create(sizeof(ipc_call_t));
|
---|
[9ef495f] | 407 | conn->handler = handler;
|
---|
| 408 | conn->data = data;
|
---|
[a35b458] | 409 |
|
---|
[6769005] | 410 | if (!conn->msg_channel)
|
---|
| 411 | goto error;
|
---|
| 412 |
|
---|
[9ef495f] | 413 | if (call)
|
---|
| 414 | conn->call = *call;
|
---|
[061274f] | 415 | else
|
---|
| 416 | conn->call.cap_handle = CAP_NIL;
|
---|
[a35b458] | 417 |
|
---|
[9ef495f] | 418 | /* We will activate the fibril ASAP */
|
---|
[514d561] | 419 | conn->fid = fibril_create(connection_fibril, conn);
|
---|
[a35b458] | 420 |
|
---|
[6769005] | 421 | if (conn->fid == 0)
|
---|
| 422 | goto error;
|
---|
[a35b458] | 423 |
|
---|
[6769005] | 424 | fibril_start(conn->fid);
|
---|
[a35b458] | 425 |
|
---|
[6769005] | 426 | return conn->fid;
|
---|
[a35b458] | 427 |
|
---|
[6769005] | 428 | error:
|
---|
| 429 | if (conn->msg_channel)
|
---|
| 430 | mpsc_destroy(conn->msg_channel);
|
---|
| 431 | free(conn);
|
---|
[a35b458] | 432 |
|
---|
[6769005] | 433 | if (call)
|
---|
| 434 | ipc_answer_0(call->cap_handle, ENOMEM);
|
---|
[a35b458] | 435 |
|
---|
[6769005] | 436 | return (fid_t) NULL;
|
---|
[9ef495f] | 437 | }
|
---|
| 438 |
|
---|
[78bb04b] | 439 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
| 440 | *
|
---|
| 441 | * Ask through phone for a new connection to some service.
|
---|
| 442 | *
|
---|
| 443 | * @param exch Exchange for sending the message.
|
---|
| 444 | * @param iface Callback interface.
|
---|
| 445 | * @param arg1 User defined argument.
|
---|
| 446 | * @param arg2 User defined argument.
|
---|
| 447 | * @param handler Callback handler.
|
---|
| 448 | * @param data Handler data.
|
---|
| 449 | * @param port_id ID of the newly created port.
|
---|
| 450 | *
|
---|
[cde999a] | 451 | * @return Zero on success or an error code.
|
---|
[78bb04b] | 452 | *
|
---|
| 453 | */
|
---|
[b7fd2a0] | 454 | errno_t async_create_callback_port(async_exch_t *exch, iface_t iface, sysarg_t arg1,
|
---|
[78bb04b] | 455 | sysarg_t arg2, async_port_handler_t handler, void *data, port_id_t *port_id)
|
---|
| 456 | {
|
---|
| 457 | if ((iface & IFACE_MOD_CALLBACK) != IFACE_MOD_CALLBACK)
|
---|
| 458 | return EINVAL;
|
---|
[a35b458] | 459 |
|
---|
[78bb04b] | 460 | if (exch == NULL)
|
---|
| 461 | return ENOENT;
|
---|
[a35b458] | 462 |
|
---|
[6769005] | 463 | connection_t *conn = calloc(1, sizeof(*conn));
|
---|
| 464 | if (!conn)
|
---|
| 465 | return ENOMEM;
|
---|
| 466 |
|
---|
[78bb04b] | 467 | ipc_call_t answer;
|
---|
[6769005] | 468 | aid_t req = async_send_5(exch, IPC_M_CONNECT_TO_ME, iface, arg1, arg2,
|
---|
| 469 | 0, (sysarg_t) conn, &answer);
|
---|
[a35b458] | 470 |
|
---|
[fda19b8] | 471 | errno_t rc;
|
---|
| 472 | async_wait_for(req, &rc);
|
---|
[6769005] | 473 | if (rc != EOK) {
|
---|
| 474 | free(conn);
|
---|
[fda19b8] | 475 | return rc;
|
---|
[6769005] | 476 | }
|
---|
[a35b458] | 477 |
|
---|
[fda19b8] | 478 | rc = async_create_port_internal(iface, handler, data, port_id);
|
---|
[6769005] | 479 | if (rc != EOK) {
|
---|
| 480 | free(conn);
|
---|
[fda19b8] | 481 | return rc;
|
---|
[6769005] | 482 | }
|
---|
[a35b458] | 483 |
|
---|
[6769005] | 484 | fid_t fid = async_new_connection(conn, answer.task_id, NULL, handler,
|
---|
| 485 | data);
|
---|
[514d561] | 486 | if (fid == (fid_t) NULL)
|
---|
[78bb04b] | 487 | return ENOMEM;
|
---|
[a35b458] | 488 |
|
---|
[78bb04b] | 489 | return EOK;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
[8820544] | 492 | static size_t notification_key_hash(void *key)
|
---|
| 493 | {
|
---|
| 494 | sysarg_t id = *(sysarg_t *) key;
|
---|
| 495 | return id;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
| 498 | static size_t notification_hash(const ht_link_t *item)
|
---|
| 499 | {
|
---|
| 500 | notification_t *notification =
|
---|
[3b1cc8d] | 501 | hash_table_get_inst(item, notification_t, htlink);
|
---|
[8820544] | 502 | return notification_key_hash(¬ification->imethod);
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 | static bool notification_key_equal(void *key, const ht_link_t *item)
|
---|
| 506 | {
|
---|
| 507 | sysarg_t id = *(sysarg_t *) key;
|
---|
| 508 | notification_t *notification =
|
---|
[3b1cc8d] | 509 | hash_table_get_inst(item, notification_t, htlink);
|
---|
[8820544] | 510 | return id == notification->imethod;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | /** Operations for the notification hash table. */
|
---|
| 514 | static hash_table_ops_t notification_hash_table_ops = {
|
---|
| 515 | .hash = notification_hash,
|
---|
| 516 | .key_hash = notification_key_hash,
|
---|
| 517 | .key_equal = notification_key_equal,
|
---|
| 518 | .equal = NULL,
|
---|
| 519 | .remove_callback = NULL
|
---|
| 520 | };
|
---|
| 521 |
|
---|
[e70bfa5] | 522 | /** Try to route a call to an appropriate connection fibril.
|
---|
[80649a91] | 523 | *
|
---|
[36c9234] | 524 | * If the proper connection fibril is found, a message with the call is added to
|
---|
| 525 | * its message queue. If the fibril was not active, it is activated and all
|
---|
| 526 | * timeouts are unregistered.
|
---|
| 527 | *
|
---|
[061274f] | 528 | * @param call Data of the incoming call.
|
---|
[c07544d3] | 529 | *
|
---|
[1de92fb0] | 530 | * @return EOK if the call was successfully passed to the respective fibril.
|
---|
| 531 | * @return ENOENT if the call doesn't match any connection.
|
---|
| 532 | * @return Other error code if routing failed for other reasons.
|
---|
[36c9234] | 533 | *
|
---|
[80649a91] | 534 | */
|
---|
[1de92fb0] | 535 | static errno_t route_call(ipc_call_t *call)
|
---|
[450cd3a] | 536 | {
|
---|
[79ae36dd] | 537 | assert(call);
|
---|
[a35b458] | 538 |
|
---|
[6769005] | 539 | connection_t *conn = (connection_t *) call->request_label;
|
---|
[a35b458] | 540 |
|
---|
[6769005] | 541 | if (!conn)
|
---|
[1de92fb0] | 542 | return ENOENT;
|
---|
[a35b458] | 543 |
|
---|
[6769005] | 544 | assert(conn->msg_channel);
|
---|
[a35b458] | 545 |
|
---|
[1de92fb0] | 546 | errno_t rc = mpsc_send(conn->msg_channel, call);
|
---|
[a35b458] | 547 |
|
---|
[1de92fb0] | 548 | if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP) {
|
---|
| 549 | /* Close the channel, but let the connection fibril answer. */
|
---|
| 550 | mpsc_close(conn->msg_channel);
|
---|
| 551 | // FIXME: Ideally, we should be able to discard/answer the
|
---|
| 552 | // hungup message here and just close the channel without
|
---|
| 553 | // passing it out. Unfortunatelly, somehow that breaks
|
---|
| 554 | // handling of CPU exceptions.
|
---|
| 555 | }
|
---|
[a35b458] | 556 |
|
---|
[1de92fb0] | 557 | return rc;
|
---|
[c07544d3] | 558 | }
|
---|
[80649a91] | 559 |
|
---|
[3b1cc8d] | 560 | /** Function implementing the notification handler fibril. Never returns. */
|
---|
| 561 | static errno_t notification_fibril_func(void *arg)
|
---|
| 562 | {
|
---|
| 563 | (void) arg;
|
---|
| 564 |
|
---|
| 565 | while (true) {
|
---|
| 566 | fibril_semaphore_down(¬ification_semaphore);
|
---|
| 567 |
|
---|
[9bde0d5] | 568 | fibril_rmutex_lock(¬ification_mutex);
|
---|
[3b1cc8d] | 569 |
|
---|
| 570 | /*
|
---|
| 571 | * The semaphore ensures that if we get this far,
|
---|
| 572 | * the queue must be non-empty.
|
---|
| 573 | */
|
---|
| 574 | assert(!list_empty(¬ification_queue));
|
---|
| 575 |
|
---|
| 576 | notification_t *notification = list_get_instance(
|
---|
| 577 | list_first(¬ification_queue), notification_t, qlink);
|
---|
| 578 |
|
---|
| 579 | async_notification_handler_t handler = notification->handler;
|
---|
| 580 | void *arg = notification->arg;
|
---|
| 581 |
|
---|
[8dab988] | 582 | notification_msg_t *m = list_pop(¬ification->msg_list,
|
---|
| 583 | notification_msg_t, link);
|
---|
| 584 | assert(m);
|
---|
| 585 | ipc_call_t calldata = m->calldata;
|
---|
| 586 |
|
---|
| 587 | notification_freelist_used--;
|
---|
| 588 |
|
---|
| 589 | if (notification_freelist_total > 64 &&
|
---|
| 590 | notification_freelist_total > 2 * notification_freelist_used) {
|
---|
| 591 | /* Going to free the structure if we have too much. */
|
---|
| 592 | notification_freelist_total--;
|
---|
| 593 | } else {
|
---|
| 594 | /* Otherwise add to freelist. */
|
---|
| 595 | list_append(&m->link, ¬ification_freelist);
|
---|
| 596 | m = NULL;
|
---|
| 597 | }
|
---|
[3b1cc8d] | 598 |
|
---|
[8dab988] | 599 | if (list_empty(¬ification->msg_list))
|
---|
| 600 | list_remove(¬ification->qlink);
|
---|
[3b1cc8d] | 601 |
|
---|
[9bde0d5] | 602 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[3b1cc8d] | 603 |
|
---|
| 604 | if (handler)
|
---|
| 605 | handler(&calldata, arg);
|
---|
[8dab988] | 606 |
|
---|
| 607 | free(m);
|
---|
[3b1cc8d] | 608 | }
|
---|
| 609 |
|
---|
| 610 | /* Not reached. */
|
---|
| 611 | return EOK;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | /**
|
---|
| 615 | * Creates a new dedicated fibril for handling notifications.
|
---|
| 616 | * By default, there is one such fibril. This function can be used to
|
---|
| 617 | * create more in order to increase the number of notification that can
|
---|
| 618 | * be processed concurrently.
|
---|
| 619 | *
|
---|
| 620 | * Currently, there is no way to destroy those fibrils after they are created.
|
---|
| 621 | */
|
---|
| 622 | errno_t async_spawn_notification_handler(void)
|
---|
| 623 | {
|
---|
| 624 | fid_t f = fibril_create(notification_fibril_func, NULL);
|
---|
| 625 | if (f == 0)
|
---|
| 626 | return ENOMEM;
|
---|
| 627 |
|
---|
| 628 | fibril_add_ready(f);
|
---|
| 629 | return EOK;
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | /** Queue notification.
|
---|
[c07544d3] | 633 | *
|
---|
[c170438] | 634 | * @param call Data of the incoming call.
|
---|
[58563585] | 635 | *
|
---|
[c07544d3] | 636 | */
|
---|
[3b1cc8d] | 637 | static void queue_notification(ipc_call_t *call)
|
---|
[c07544d3] | 638 | {
|
---|
[c170438] | 639 | assert(call);
|
---|
[a35b458] | 640 |
|
---|
[9bde0d5] | 641 | fibril_rmutex_lock(¬ification_mutex);
|
---|
[a35b458] | 642 |
|
---|
[8dab988] | 643 | notification_msg_t *m = list_pop(¬ification_freelist,
|
---|
| 644 | notification_msg_t, link);
|
---|
| 645 |
|
---|
| 646 | if (!m) {
|
---|
[9bde0d5] | 647 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[8dab988] | 648 | m = malloc(sizeof(notification_msg_t));
|
---|
| 649 | if (!m) {
|
---|
| 650 | DPRINTF("Out of memory.\n");
|
---|
| 651 | abort();
|
---|
| 652 | }
|
---|
| 653 |
|
---|
[9bde0d5] | 654 | fibril_rmutex_lock(¬ification_mutex);
|
---|
[8dab988] | 655 | notification_freelist_total++;
|
---|
| 656 | }
|
---|
| 657 |
|
---|
[8820544] | 658 | ht_link_t *link = hash_table_find(¬ification_hash_table,
|
---|
[c170438] | 659 | &IPC_GET_IMETHOD(*call));
|
---|
[3b1cc8d] | 660 | if (!link) {
|
---|
| 661 | /* Invalid notification. */
|
---|
| 662 | // TODO: Make sure this can't happen and turn it into assert.
|
---|
[8dab988] | 663 | notification_freelist_total--;
|
---|
[9bde0d5] | 664 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[8dab988] | 665 | free(m);
|
---|
[3b1cc8d] | 666 | return;
|
---|
[8820544] | 667 | }
|
---|
[a35b458] | 668 |
|
---|
[3b1cc8d] | 669 | notification_t *notification =
|
---|
| 670 | hash_table_get_inst(link, notification_t, htlink);
|
---|
| 671 |
|
---|
[8dab988] | 672 | notification_freelist_used++;
|
---|
| 673 | m->calldata = *call;
|
---|
| 674 | list_append(&m->link, ¬ification->msg_list);
|
---|
[3b1cc8d] | 675 |
|
---|
[8dab988] | 676 | if (!link_in_use(¬ification->qlink))
|
---|
| 677 | list_append(¬ification->qlink, ¬ification_queue);
|
---|
[3b1cc8d] | 678 |
|
---|
[9bde0d5] | 679 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[3b1cc8d] | 680 |
|
---|
| 681 | fibril_semaphore_up(¬ification_semaphore);
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | /**
|
---|
| 685 | * Creates a new notification structure and inserts it into the hash table.
|
---|
| 686 | *
|
---|
| 687 | * @param handler Function to call when notification is received.
|
---|
| 688 | * @param arg Argument for the handler function.
|
---|
| 689 | * @return The newly created notification structure.
|
---|
| 690 | */
|
---|
| 691 | static notification_t *notification_create(async_notification_handler_t handler, void *arg)
|
---|
| 692 | {
|
---|
| 693 | notification_t *notification = calloc(1, sizeof(notification_t));
|
---|
| 694 | if (!notification)
|
---|
| 695 | return NULL;
|
---|
| 696 |
|
---|
| 697 | notification->handler = handler;
|
---|
| 698 | notification->arg = arg;
|
---|
| 699 |
|
---|
[8dab988] | 700 | list_initialize(¬ification->msg_list);
|
---|
| 701 |
|
---|
[3b1cc8d] | 702 | fid_t fib = 0;
|
---|
| 703 |
|
---|
[9bde0d5] | 704 | fibril_rmutex_lock(¬ification_mutex);
|
---|
[3b1cc8d] | 705 |
|
---|
| 706 | if (notification_avail == 0) {
|
---|
| 707 | /* Attempt to create the first handler fibril. */
|
---|
| 708 | fib = fibril_create(notification_fibril_func, NULL);
|
---|
| 709 | if (fib == 0) {
|
---|
[9bde0d5] | 710 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[3b1cc8d] | 711 | free(notification);
|
---|
| 712 | return NULL;
|
---|
| 713 | }
|
---|
| 714 | }
|
---|
| 715 |
|
---|
| 716 | sysarg_t imethod = notification_avail;
|
---|
| 717 | notification_avail++;
|
---|
| 718 |
|
---|
| 719 | notification->imethod = imethod;
|
---|
| 720 | hash_table_insert(¬ification_hash_table, ¬ification->htlink);
|
---|
| 721 |
|
---|
[9bde0d5] | 722 | fibril_rmutex_unlock(¬ification_mutex);
|
---|
[3b1cc8d] | 723 |
|
---|
| 724 | if (imethod == 0) {
|
---|
| 725 | assert(fib);
|
---|
| 726 | fibril_add_ready(fib);
|
---|
| 727 | }
|
---|
[a35b458] | 728 |
|
---|
[3b1cc8d] | 729 | return notification;
|
---|
[80649a91] | 730 | }
|
---|
| 731 |
|
---|
[8820544] | 732 | /** Subscribe to IRQ notification.
|
---|
| 733 | *
|
---|
| 734 | * @param inr IRQ number.
|
---|
| 735 | * @param handler Notification handler.
|
---|
| 736 | * @param data Notification handler client data.
|
---|
| 737 | * @param ucode Top-half pseudocode handler.
|
---|
| 738 | *
|
---|
[071a1ddb] | 739 | * @param[out] handle IRQ capability handle on success.
|
---|
| 740 | *
|
---|
[cde999a] | 741 | * @return An error code.
|
---|
[8820544] | 742 | *
|
---|
| 743 | */
|
---|
[b7fd2a0] | 744 | errno_t async_irq_subscribe(int inr, async_notification_handler_t handler,
|
---|
[eadaeae8] | 745 | void *data, const irq_code_t *ucode, cap_irq_handle_t *handle)
|
---|
[8820544] | 746 | {
|
---|
[3b1cc8d] | 747 | notification_t *notification = notification_create(handler, data);
|
---|
[8820544] | 748 | if (!notification)
|
---|
| 749 | return ENOMEM;
|
---|
[a35b458] | 750 |
|
---|
[eadaeae8] | 751 | cap_irq_handle_t ihandle;
|
---|
[3b1cc8d] | 752 | errno_t rc = ipc_irq_subscribe(inr, notification->imethod, ucode,
|
---|
| 753 | &ihandle);
|
---|
[071a1ddb] | 754 | if (rc == EOK && handle != NULL) {
|
---|
[eadaeae8] | 755 | *handle = ihandle;
|
---|
[9233e9d] | 756 | }
|
---|
[071a1ddb] | 757 | return rc;
|
---|
[8820544] | 758 | }
|
---|
| 759 |
|
---|
| 760 | /** Unsubscribe from IRQ notification.
|
---|
| 761 | *
|
---|
[eadaeae8] | 762 | * @param handle IRQ capability handle.
|
---|
[8820544] | 763 | *
|
---|
[cde999a] | 764 | * @return Zero on success or an error code.
|
---|
[8820544] | 765 | *
|
---|
| 766 | */
|
---|
[eadaeae8] | 767 | errno_t async_irq_unsubscribe(cap_irq_handle_t ihandle)
|
---|
[8820544] | 768 | {
|
---|
| 769 | // TODO: Remove entry from hash table
|
---|
| 770 | // to avoid memory leak
|
---|
[a35b458] | 771 |
|
---|
[eadaeae8] | 772 | return ipc_irq_unsubscribe(ihandle);
|
---|
[8820544] | 773 | }
|
---|
| 774 |
|
---|
| 775 | /** Subscribe to event notifications.
|
---|
| 776 | *
|
---|
| 777 | * @param evno Event type to subscribe.
|
---|
| 778 | * @param handler Notification handler.
|
---|
| 779 | * @param data Notification handler client data.
|
---|
| 780 | *
|
---|
[cde999a] | 781 | * @return Zero on success or an error code.
|
---|
[8820544] | 782 | *
|
---|
| 783 | */
|
---|
[b7fd2a0] | 784 | errno_t async_event_subscribe(event_type_t evno,
|
---|
[8820544] | 785 | async_notification_handler_t handler, void *data)
|
---|
| 786 | {
|
---|
[3b1cc8d] | 787 | notification_t *notification = notification_create(handler, data);
|
---|
[8820544] | 788 | if (!notification)
|
---|
| 789 | return ENOMEM;
|
---|
[a35b458] | 790 |
|
---|
[3b1cc8d] | 791 | return ipc_event_subscribe(evno, notification->imethod);
|
---|
[8820544] | 792 | }
|
---|
| 793 |
|
---|
| 794 | /** Subscribe to task event notifications.
|
---|
| 795 | *
|
---|
| 796 | * @param evno Event type to subscribe.
|
---|
| 797 | * @param handler Notification handler.
|
---|
| 798 | * @param data Notification handler client data.
|
---|
| 799 | *
|
---|
[cde999a] | 800 | * @return Zero on success or an error code.
|
---|
[8820544] | 801 | *
|
---|
| 802 | */
|
---|
[b7fd2a0] | 803 | errno_t async_event_task_subscribe(event_task_type_t evno,
|
---|
[8820544] | 804 | async_notification_handler_t handler, void *data)
|
---|
| 805 | {
|
---|
[3b1cc8d] | 806 | notification_t *notification = notification_create(handler, data);
|
---|
[8820544] | 807 | if (!notification)
|
---|
| 808 | return ENOMEM;
|
---|
[a35b458] | 809 |
|
---|
[3b1cc8d] | 810 | return ipc_event_task_subscribe(evno, notification->imethod);
|
---|
[8820544] | 811 | }
|
---|
| 812 |
|
---|
| 813 | /** Unmask event notifications.
|
---|
| 814 | *
|
---|
| 815 | * @param evno Event type to unmask.
|
---|
| 816 | *
|
---|
| 817 | * @return Value returned by the kernel.
|
---|
| 818 | *
|
---|
| 819 | */
|
---|
[b7fd2a0] | 820 | errno_t async_event_unmask(event_type_t evno)
|
---|
[8820544] | 821 | {
|
---|
| 822 | return ipc_event_unmask(evno);
|
---|
| 823 | }
|
---|
| 824 |
|
---|
| 825 | /** Unmask task event notifications.
|
---|
| 826 | *
|
---|
| 827 | * @param evno Event type to unmask.
|
---|
| 828 | *
|
---|
| 829 | * @return Value returned by the kernel.
|
---|
| 830 | *
|
---|
| 831 | */
|
---|
[b7fd2a0] | 832 | errno_t async_event_task_unmask(event_task_type_t evno)
|
---|
[8820544] | 833 | {
|
---|
| 834 | return ipc_event_task_unmask(evno);
|
---|
| 835 | }
|
---|
| 836 |
|
---|
[e70bfa5] | 837 | /** Return new incoming message for the current (fibril-local) connection.
|
---|
| 838 | *
|
---|
[984a9ba] | 839 | * @param call Storage where the incoming call data will be stored.
|
---|
| 840 | * @param usecs Timeout in microseconds. Zero denotes no timeout.
|
---|
| 841 | *
|
---|
| 842 | * @return If no timeout was specified, then true is returned.
|
---|
| 843 | * @return If a timeout is specified, then true is returned unless
|
---|
| 844 | * the timeout expires prior to receiving a message.
|
---|
[e70bfa5] | 845 | *
|
---|
| 846 | */
|
---|
[bd41ac52] | 847 | bool async_get_call_timeout(ipc_call_t *call, usec_t usecs)
|
---|
[80649a91] | 848 | {
|
---|
[79ae36dd] | 849 | assert(call);
|
---|
| 850 | assert(fibril_connection);
|
---|
[a35b458] | 851 |
|
---|
[bd41ac52] | 852 | struct timespec ts;
|
---|
| 853 | struct timespec *expires = NULL;
|
---|
[49d072e] | 854 | if (usecs) {
|
---|
[bd41ac52] | 855 | getuptime(&ts);
|
---|
| 856 | ts_add_diff(&ts, USEC2NSEC(usecs));
|
---|
| 857 | expires = &ts;
|
---|
[514d561] | 858 | }
|
---|
| 859 |
|
---|
[1de92fb0] | 860 | errno_t rc = mpsc_receive(fibril_connection->msg_channel,
|
---|
| 861 | call, expires);
|
---|
[a35b458] | 862 |
|
---|
[1de92fb0] | 863 | if (rc == ETIMEOUT)
|
---|
| 864 | return false;
|
---|
[a35b458] | 865 |
|
---|
[1de92fb0] | 866 | if (rc != EOK) {
|
---|
| 867 | /*
|
---|
| 868 | * The async_get_call_timeout() interface doesn't support
|
---|
| 869 | * propagating errors. Return a null call instead.
|
---|
| 870 | */
|
---|
[514d561] | 871 |
|
---|
[1de92fb0] | 872 | memset(call, 0, sizeof(ipc_call_t));
|
---|
[de9a18e] | 873 | IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
|
---|
| 874 | call->cap_handle = CAP_NIL;
|
---|
[450cd3a] | 875 | }
|
---|
[a35b458] | 876 |
|
---|
[984a9ba] | 877 | return true;
|
---|
[80649a91] | 878 | }
|
---|
| 879 |
|
---|
[455f190] | 880 | void *async_get_client_data(void)
|
---|
| 881 | {
|
---|
| 882 | assert(fibril_connection);
|
---|
| 883 | return fibril_connection->client->data;
|
---|
| 884 | }
|
---|
| 885 |
|
---|
[e2ab36f1] | 886 | void *async_get_client_data_by_id(task_id_t client_id)
|
---|
[455f190] | 887 | {
|
---|
[e2ab36f1] | 888 | client_t *client = async_client_get(client_id, false);
|
---|
[455f190] | 889 | if (!client)
|
---|
| 890 | return NULL;
|
---|
[a35b458] | 891 |
|
---|
[455f190] | 892 | if (!client->data) {
|
---|
| 893 | async_client_put(client);
|
---|
| 894 | return NULL;
|
---|
| 895 | }
|
---|
[a35b458] | 896 |
|
---|
[455f190] | 897 | return client->data;
|
---|
| 898 | }
|
---|
| 899 |
|
---|
[e2ab36f1] | 900 | void async_put_client_data_by_id(task_id_t client_id)
|
---|
[455f190] | 901 | {
|
---|
[e2ab36f1] | 902 | client_t *client = async_client_get(client_id, false);
|
---|
[a35b458] | 903 |
|
---|
[455f190] | 904 | assert(client);
|
---|
| 905 | assert(client->data);
|
---|
[a35b458] | 906 |
|
---|
[cdc8ee2d] | 907 | /* Drop the reference we got in async_get_client_data_by_hash(). */
|
---|
| 908 | async_client_put(client);
|
---|
[a35b458] | 909 |
|
---|
[cdc8ee2d] | 910 | /* Drop our own reference we got at the beginning of this function. */
|
---|
[455f190] | 911 | async_client_put(client);
|
---|
| 912 | }
|
---|
| 913 |
|
---|
[36c9234] | 914 | /** Handle a call that was received.
|
---|
| 915 | *
|
---|
| 916 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
---|
| 917 | * Otherwise the call is routed to its connection fibril.
|
---|
| 918 | *
|
---|
[061274f] | 919 | * @param call Data of the incoming call.
|
---|
[6b21292] | 920 | *
|
---|
[36c9234] | 921 | */
|
---|
[061274f] | 922 | static void handle_call(ipc_call_t *call)
|
---|
[80649a91] | 923 | {
|
---|
[79ae36dd] | 924 | assert(call);
|
---|
[a35b458] | 925 |
|
---|
[d054ad3] | 926 | if (call->flags & IPC_CALL_ANSWERED) {
|
---|
| 927 | /* Answer to a call made by us. */
|
---|
| 928 | async_reply_received(call);
|
---|
[e768aea] | 929 | return;
|
---|
[d054ad3] | 930 | }
|
---|
[e768aea] | 931 |
|
---|
[061274f] | 932 | if (call->cap_handle == CAP_NIL) {
|
---|
[e768aea] | 933 | if (call->flags & IPC_CALL_NOTIF) {
|
---|
| 934 | /* Kernel notification */
|
---|
| 935 | queue_notification(call);
|
---|
| 936 | }
|
---|
[47b7006] | 937 | return;
|
---|
[6b21292] | 938 | }
|
---|
[a35b458] | 939 |
|
---|
[566992e1] | 940 | /* New connection */
|
---|
| 941 | if (IPC_GET_IMETHOD(*call) == IPC_M_CONNECT_ME_TO) {
|
---|
[6769005] | 942 | connection_t *conn = calloc(1, sizeof(*conn));
|
---|
| 943 | if (!conn) {
|
---|
| 944 | ipc_answer_0(call->cap_handle, ENOMEM);
|
---|
| 945 | return;
|
---|
| 946 | }
|
---|
| 947 |
|
---|
[566992e1] | 948 | iface_t iface = (iface_t) IPC_GET_ARG1(*call);
|
---|
[a35b458] | 949 |
|
---|
[49a796f1] | 950 | // TODO: Currently ignores all ports but the first one.
|
---|
| 951 | void *data;
|
---|
| 952 | async_port_handler_t handler =
|
---|
| 953 | async_get_port_handler(iface, 0, &data);
|
---|
[a35b458] | 954 |
|
---|
[6769005] | 955 | async_new_connection(conn, call->task_id, call, handler, data);
|
---|
[566992e1] | 956 | return;
|
---|
| 957 | }
|
---|
[a35b458] | 958 |
|
---|
[6769005] | 959 | /* Route the call according to its request label */
|
---|
[1de92fb0] | 960 | errno_t rc = route_call(call);
|
---|
| 961 | if (rc == EOK)
|
---|
[47b7006] | 962 | return;
|
---|
[a35b458] | 963 |
|
---|
[1de92fb0] | 964 | // TODO: Log the error.
|
---|
| 965 |
|
---|
| 966 | if (call->cap_handle != CAP_NIL)
|
---|
| 967 | /* Unknown call from unknown phone - hang it up */
|
---|
| 968 | ipc_answer_0(call->cap_handle, EHANGUP);
|
---|
[450cd3a] | 969 | }
|
---|
| 970 |
|
---|
[36c9234] | 971 | /** Endless loop dispatching incoming calls and answers.
|
---|
| 972 | *
|
---|
[c07544d3] | 973 | * @return Never returns.
|
---|
| 974 | *
|
---|
[36c9234] | 975 | */
|
---|
[b7fd2a0] | 976 | static errno_t async_manager_worker(void)
|
---|
[80649a91] | 977 | {
|
---|
[514d561] | 978 | ipc_call_t call;
|
---|
| 979 | errno_t rc;
|
---|
[a35b458] | 980 |
|
---|
[514d561] | 981 | while (true) {
|
---|
| 982 | rc = fibril_ipc_wait(&call, NULL);
|
---|
[acf6b55] | 983 | if (rc == EOK)
|
---|
| 984 | handle_call(&call);
|
---|
[80649a91] | 985 | }
|
---|
[01c3bb4] | 986 |
|
---|
[a46da63] | 987 | return 0;
|
---|
[80649a91] | 988 | }
|
---|
| 989 |
|
---|
[36c9234] | 990 | /** Function to start async_manager as a standalone fibril.
|
---|
[c07544d3] | 991 | *
|
---|
[36c9234] | 992 | * When more kernel threads are used, one async manager should exist per thread.
|
---|
| 993 | *
|
---|
[c07544d3] | 994 | * @param arg Unused.
|
---|
| 995 | * @return Never returns.
|
---|
[36c9234] | 996 | *
|
---|
[a2cd194] | 997 | */
|
---|
[b7fd2a0] | 998 | static errno_t async_manager_fibril(void *arg)
|
---|
[80649a91] | 999 | {
|
---|
[085bd54] | 1000 | async_manager_worker();
|
---|
[a46da63] | 1001 | return 0;
|
---|
[80649a91] | 1002 | }
|
---|
[450cd3a] | 1003 |
|
---|
[36c9234] | 1004 | /** Add one manager to manager list. */
|
---|
[fec7ba0] | 1005 | static fid_t async_create_manager(void)
|
---|
[450cd3a] | 1006 | {
|
---|
[c170438] | 1007 | fid_t fid = fibril_create_generic(async_manager_fibril, NULL, PAGE_SIZE);
|
---|
[514d561] | 1008 | fibril_start(fid);
|
---|
| 1009 | return fid;
|
---|
[80649a91] | 1010 | }
|
---|
| 1011 |
|
---|
[36c9234] | 1012 | /** Initialize the async framework.
|
---|
| 1013 | *
|
---|
| 1014 | */
|
---|
[49a796f1] | 1015 | void __async_server_init(void)
|
---|
[80649a91] | 1016 | {
|
---|
[45c8eea] | 1017 | if (fibril_rmutex_initialize(&client_mutex) != EOK)
|
---|
| 1018 | abort();
|
---|
| 1019 | if (fibril_rmutex_initialize(¬ification_mutex) != EOK)
|
---|
| 1020 | abort();
|
---|
| 1021 |
|
---|
[062d900] | 1022 | if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
|
---|
[47b7006] | 1023 | abort();
|
---|
[a35b458] | 1024 |
|
---|
[8820544] | 1025 | if (!hash_table_create(¬ification_hash_table, 0, 0,
|
---|
| 1026 | ¬ification_hash_table_ops))
|
---|
| 1027 | abort();
|
---|
[514d561] | 1028 |
|
---|
| 1029 | async_create_manager();
|
---|
[49a796f1] | 1030 | }
|
---|
[a35b458] | 1031 |
|
---|
[25f6bddb] | 1032 | void __async_server_fini(void)
|
---|
| 1033 | {
|
---|
| 1034 | fibril_rmutex_destroy(&client_mutex);
|
---|
| 1035 | fibril_rmutex_destroy(¬ification_mutex);
|
---|
| 1036 | }
|
---|
| 1037 |
|
---|
[beb83c1] | 1038 | errno_t async_accept_0(ipc_call_t *call)
|
---|
| 1039 | {
|
---|
[d57c7c2] | 1040 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1041 | assert(chandle != CAP_NIL);
|
---|
| 1042 | call->cap_handle = CAP_NIL;
|
---|
| 1043 | return ipc_answer_5(chandle, EOK, 0, 0, 0, 0, async_get_label());
|
---|
[beb83c1] | 1044 | }
|
---|
| 1045 |
|
---|
[984a9ba] | 1046 | errno_t async_answer_0(ipc_call_t *call, errno_t retval)
|
---|
[49a796f1] | 1047 | {
|
---|
[d57c7c2] | 1048 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1049 | assert(chandle != CAP_NIL);
|
---|
| 1050 | call->cap_handle = CAP_NIL;
|
---|
| 1051 | return ipc_answer_0(chandle, retval);
|
---|
[450cd3a] | 1052 | }
|
---|
[01ff41c] | 1053 |
|
---|
[984a9ba] | 1054 | errno_t async_answer_1(ipc_call_t *call, errno_t retval, sysarg_t arg1)
|
---|
[01ff41c] | 1055 | {
|
---|
[d57c7c2] | 1056 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1057 | assert(chandle != CAP_NIL);
|
---|
| 1058 | call->cap_handle = CAP_NIL;
|
---|
| 1059 | return ipc_answer_1(chandle, retval, arg1);
|
---|
[49a796f1] | 1060 | }
|
---|
[a35b458] | 1061 |
|
---|
[984a9ba] | 1062 | errno_t async_answer_2(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
[49a796f1] | 1063 | sysarg_t arg2)
|
---|
| 1064 | {
|
---|
[d57c7c2] | 1065 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1066 | assert(chandle != CAP_NIL);
|
---|
| 1067 | call->cap_handle = CAP_NIL;
|
---|
| 1068 | return ipc_answer_2(chandle, retval, arg1, arg2);
|
---|
[49a796f1] | 1069 | }
|
---|
[a35b458] | 1070 |
|
---|
[984a9ba] | 1071 | errno_t async_answer_3(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
[49a796f1] | 1072 | sysarg_t arg2, sysarg_t arg3)
|
---|
| 1073 | {
|
---|
[d57c7c2] | 1074 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1075 | assert(chandle != CAP_NIL);
|
---|
| 1076 | call->cap_handle = CAP_NIL;
|
---|
| 1077 | return ipc_answer_3(chandle, retval, arg1, arg2, arg3);
|
---|
[49a796f1] | 1078 | }
|
---|
[a35b458] | 1079 |
|
---|
[984a9ba] | 1080 | errno_t async_answer_4(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
[49a796f1] | 1081 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
| 1082 | {
|
---|
[d57c7c2] | 1083 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1084 | assert(chandle != CAP_NIL);
|
---|
| 1085 | call->cap_handle = CAP_NIL;
|
---|
| 1086 | return ipc_answer_4(chandle, retval, arg1, arg2, arg3, arg4);
|
---|
[49a796f1] | 1087 | }
|
---|
[a35b458] | 1088 |
|
---|
[984a9ba] | 1089 | errno_t async_answer_5(ipc_call_t *call, errno_t retval, sysarg_t arg1,
|
---|
[49a796f1] | 1090 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
| 1091 | {
|
---|
[d57c7c2] | 1092 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1093 | assert(chandle != CAP_NIL);
|
---|
| 1094 | call->cap_handle = CAP_NIL;
|
---|
| 1095 | return ipc_answer_5(chandle, retval, arg1, arg2, arg3, arg4, arg5);
|
---|
[49a796f1] | 1096 | }
|
---|
[a35b458] | 1097 |
|
---|
[984a9ba] | 1098 | errno_t async_forward_fast(ipc_call_t *call, async_exch_t *exch,
|
---|
[49a796f1] | 1099 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
| 1100 | {
|
---|
[984a9ba] | 1101 | assert(call);
|
---|
| 1102 |
|
---|
[d57c7c2] | 1103 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1104 | assert(chandle != CAP_NIL);
|
---|
| 1105 | call->cap_handle = CAP_NIL;
|
---|
| 1106 |
|
---|
[49a796f1] | 1107 | if (exch == NULL)
|
---|
| 1108 | return ENOENT;
|
---|
[a35b458] | 1109 |
|
---|
[d57c7c2] | 1110 | return ipc_forward_fast(chandle, exch->phone, imethod, arg1, arg2,
|
---|
| 1111 | mode);
|
---|
[49a796f1] | 1112 | }
|
---|
[a35b458] | 1113 |
|
---|
[984a9ba] | 1114 | errno_t async_forward_slow(ipc_call_t *call, async_exch_t *exch,
|
---|
[49a796f1] | 1115 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
| 1116 | sysarg_t arg4, sysarg_t arg5, unsigned int mode)
|
---|
| 1117 | {
|
---|
[984a9ba] | 1118 | assert(call);
|
---|
| 1119 |
|
---|
[d57c7c2] | 1120 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1121 | assert(chandle != CAP_NIL);
|
---|
| 1122 | call->cap_handle = CAP_NIL;
|
---|
| 1123 |
|
---|
[49a796f1] | 1124 | if (exch == NULL)
|
---|
| 1125 | return ENOENT;
|
---|
[a35b458] | 1126 |
|
---|
[d57c7c2] | 1127 | return ipc_forward_slow(chandle, exch->phone, imethod, arg1, arg2, arg3,
|
---|
| 1128 | arg4, arg5, mode);
|
---|
[01ff41c] | 1129 | }
|
---|
| 1130 |
|
---|
[49a796f1] | 1131 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
[36c9234] | 1132 | *
|
---|
[49a796f1] | 1133 | * Ask through phone for a new connection to some service.
|
---|
[01ff41c] | 1134 | *
|
---|
[9b1baac] | 1135 | * @param exch Exchange for sending the message.
|
---|
| 1136 | * @param iface Callback interface.
|
---|
| 1137 | * @param arg2 User defined argument.
|
---|
| 1138 | * @param arg3 User defined argument.
|
---|
[c07544d3] | 1139 | *
|
---|
[49a796f1] | 1140 | * @return Zero on success or an error code.
|
---|
[36c9234] | 1141 | *
|
---|
[01ff41c] | 1142 | */
|
---|
[9b1baac] | 1143 | errno_t async_connect_to_me(async_exch_t *exch, iface_t iface, sysarg_t arg2,
|
---|
[49a796f1] | 1144 | sysarg_t arg3)
|
---|
[01ff41c] | 1145 | {
|
---|
[79ae36dd] | 1146 | if (exch == NULL)
|
---|
[49a796f1] | 1147 | return ENOENT;
|
---|
[a35b458] | 1148 |
|
---|
[6769005] | 1149 | sysarg_t label = 0;
|
---|
| 1150 | errno_t rc = async_req_5_0(exch, IPC_M_CONNECT_TO_ME, iface, arg2, arg3,
|
---|
| 1151 | 0, label);
|
---|
[a35b458] | 1152 |
|
---|
[6769005] | 1153 | return rc;
|
---|
[49a796f1] | 1154 | }
|
---|
[a35b458] | 1155 |
|
---|
[49a796f1] | 1156 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
---|
[0da4e41] | 1157 | *
|
---|
[47b7006] | 1158 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
|
---|
| 1159 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1160 | * argument.
|
---|
[0da4e41] | 1161 | *
|
---|
| 1162 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1163 | *
|
---|
[984a9ba] | 1164 | * @param call Storage for the data of the IPC_M_SHARE_IN call.
|
---|
| 1165 | * @param size Destination address space area size.
|
---|
[47b7006] | 1166 | *
|
---|
| 1167 | * @return True on success, false on failure.
|
---|
[0da4e41] | 1168 | *
|
---|
| 1169 | */
|
---|
[984a9ba] | 1170 | bool async_share_in_receive(ipc_call_t *call, size_t *size)
|
---|
[0da4e41] | 1171 | {
|
---|
[984a9ba] | 1172 | assert(call);
|
---|
[0da4e41] | 1173 | assert(size);
|
---|
[a35b458] | 1174 |
|
---|
[984a9ba] | 1175 | async_get_call(call);
|
---|
[a35b458] | 1176 |
|
---|
[984a9ba] | 1177 | if (IPC_GET_IMETHOD(*call) != IPC_M_SHARE_IN)
|
---|
[47b7006] | 1178 | return false;
|
---|
[a35b458] | 1179 |
|
---|
[984a9ba] | 1180 | *size = (size_t) IPC_GET_ARG1(*call);
|
---|
[47b7006] | 1181 | return true;
|
---|
[0da4e41] | 1182 | }
|
---|
| 1183 |
|
---|
| 1184 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
---|
| 1185 | *
|
---|
[fbcdeb8] | 1186 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
|
---|
[47b7006] | 1187 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1188 | * argument.
|
---|
[0da4e41] | 1189 | *
|
---|
[f8048d1] | 1190 | * @param call IPC_M_SHARE_IN call to answer.
|
---|
[984a9ba] | 1191 | * @param src Source address space base.
|
---|
| 1192 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
[47b7006] | 1193 | *
|
---|
| 1194 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1195 | *
|
---|
| 1196 | */
|
---|
[984a9ba] | 1197 | errno_t async_share_in_finalize(ipc_call_t *call, void *src, unsigned int flags)
|
---|
[0da4e41] | 1198 | {
|
---|
[984a9ba] | 1199 | assert(call);
|
---|
| 1200 |
|
---|
[d57c7c2] | 1201 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1202 | assert(chandle != CAP_NIL);
|
---|
| 1203 | call->cap_handle = CAP_NIL;
|
---|
| 1204 |
|
---|
| 1205 | return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) flags);
|
---|
[0da4e41] | 1206 | }
|
---|
| 1207 |
|
---|
| 1208 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
---|
| 1209 | *
|
---|
[47b7006] | 1210 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
|
---|
| 1211 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1212 | * argument.
|
---|
[0da4e41] | 1213 | *
|
---|
| 1214 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1215 | *
|
---|
[984a9ba] | 1216 | * @param call Storage for the data of the IPC_M_SHARE_OUT call.
|
---|
| 1217 | * @param size Storage for the source address space area size.
|
---|
| 1218 | * @param flags Storage for the sharing flags.
|
---|
[47b7006] | 1219 | *
|
---|
| 1220 | * @return True on success, false on failure.
|
---|
[0da4e41] | 1221 | *
|
---|
| 1222 | */
|
---|
[984a9ba] | 1223 | bool async_share_out_receive(ipc_call_t *call, size_t *size,
|
---|
[01c3bb4] | 1224 | unsigned int *flags)
|
---|
[0da4e41] | 1225 | {
|
---|
[984a9ba] | 1226 | assert(call);
|
---|
[0da4e41] | 1227 | assert(size);
|
---|
| 1228 | assert(flags);
|
---|
[a35b458] | 1229 |
|
---|
[984a9ba] | 1230 | async_get_call(call);
|
---|
[a35b458] | 1231 |
|
---|
[984a9ba] | 1232 | if (IPC_GET_IMETHOD(*call) != IPC_M_SHARE_OUT)
|
---|
[47b7006] | 1233 | return false;
|
---|
[a35b458] | 1234 |
|
---|
[984a9ba] | 1235 | *size = (size_t) IPC_GET_ARG2(*call);
|
---|
| 1236 | *flags = (unsigned int) IPC_GET_ARG3(*call);
|
---|
[47b7006] | 1237 | return true;
|
---|
[0da4e41] | 1238 | }
|
---|
| 1239 |
|
---|
| 1240 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
---|
| 1241 | *
|
---|
[47b7006] | 1242 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
|
---|
| 1243 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1244 | * argument.
|
---|
[0da4e41] | 1245 | *
|
---|
[f8048d1] | 1246 | * @param call IPC_M_SHARE_OUT call to answer.
|
---|
[984a9ba] | 1247 | * @param dst Address of the storage for the destination address space area
|
---|
| 1248 | * base address.
|
---|
[47b7006] | 1249 | *
|
---|
[01c3bb4] | 1250 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1251 | *
|
---|
| 1252 | */
|
---|
[984a9ba] | 1253 | errno_t async_share_out_finalize(ipc_call_t *call, void **dst)
|
---|
[0da4e41] | 1254 | {
|
---|
[984a9ba] | 1255 | assert(call);
|
---|
[0da4e41] | 1256 |
|
---|
[d57c7c2] | 1257 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1258 | assert(chandle != CAP_NIL);
|
---|
| 1259 | call->cap_handle = CAP_NIL;
|
---|
| 1260 |
|
---|
| 1261 | return ipc_answer_2(chandle, EOK, (sysarg_t) __progsymbols.end,
|
---|
[984a9ba] | 1262 | (sysarg_t) dst);
|
---|
[d768d4c8] | 1263 | }
|
---|
| 1264 |
|
---|
| 1265 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
---|
| 1266 | *
|
---|
| 1267 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
|
---|
| 1268 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1269 | * argument.
|
---|
| 1270 | *
|
---|
| 1271 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1272 | *
|
---|
[984a9ba] | 1273 | * @param call Storage for the data of the IPC_M_DATA_READ.
|
---|
| 1274 | * @param size Storage for the maximum size. Can be NULL.
|
---|
[d768d4c8] | 1275 | *
|
---|
| 1276 | * @return True on success, false on failure.
|
---|
| 1277 | *
|
---|
| 1278 | */
|
---|
[984a9ba] | 1279 | bool async_data_read_receive(ipc_call_t *call, size_t *size)
|
---|
[0da4e41] | 1280 | {
|
---|
[984a9ba] | 1281 | assert(call);
|
---|
[a35b458] | 1282 |
|
---|
[984a9ba] | 1283 | async_get_call(call);
|
---|
[a35b458] | 1284 |
|
---|
[984a9ba] | 1285 | if (IPC_GET_IMETHOD(*call) != IPC_M_DATA_READ)
|
---|
[47b7006] | 1286 | return false;
|
---|
[a35b458] | 1287 |
|
---|
[0da4e41] | 1288 | if (size)
|
---|
[984a9ba] | 1289 | *size = (size_t) IPC_GET_ARG2(*call);
|
---|
[a35b458] | 1290 |
|
---|
[47b7006] | 1291 | return true;
|
---|
[0da4e41] | 1292 | }
|
---|
| 1293 |
|
---|
| 1294 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
---|
| 1295 | *
|
---|
[47b7006] | 1296 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
---|
| 1297 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1298 | * argument.
|
---|
[0da4e41] | 1299 | *
|
---|
[984a9ba] | 1300 | * @param call IPC_M_DATA_READ call to answer.
|
---|
| 1301 | * @param src Source address for the IPC_M_DATA_READ call.
|
---|
| 1302 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
---|
| 1303 | * the maximum size announced by the sender.
|
---|
[47b7006] | 1304 | *
|
---|
[01c3bb4] | 1305 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1306 | *
|
---|
| 1307 | */
|
---|
[984a9ba] | 1308 | errno_t async_data_read_finalize(ipc_call_t *call, const void *src, size_t size)
|
---|
[0da4e41] | 1309 | {
|
---|
[984a9ba] | 1310 | assert(call);
|
---|
| 1311 |
|
---|
[d57c7c2] | 1312 | cap_call_handle_t chandle = call->cap_handle;
|
---|
| 1313 | assert(chandle != CAP_NIL);
|
---|
| 1314 | call->cap_handle = CAP_NIL;
|
---|
| 1315 |
|
---|
| 1316 | return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) size);
|
---|
[0da4e41] | 1317 | }
|
---|
| 1318 |
|
---|
[b4cbef1] | 1319 | /** Wrapper for forwarding any read request
|
---|
| 1320 | *
|
---|
| 1321 | */
|
---|
[b7fd2a0] | 1322 | errno_t async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
|
---|
[79ae36dd] | 1323 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
| 1324 | ipc_call_t *dataptr)
|
---|
[b4cbef1] | 1325 | {
|
---|
[79ae36dd] | 1326 | if (exch == NULL)
|
---|
| 1327 | return ENOENT;
|
---|
[a35b458] | 1328 |
|
---|
[984a9ba] | 1329 | ipc_call_t call;
|
---|
| 1330 | if (!async_data_read_receive(&call, NULL)) {
|
---|
| 1331 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1332 | return EINVAL;
|
---|
| 1333 | }
|
---|
[a35b458] | 1334 |
|
---|
[79ae36dd] | 1335 | aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
|
---|
[b4cbef1] | 1336 | dataptr);
|
---|
| 1337 | if (msg == 0) {
|
---|
[984a9ba] | 1338 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1339 | return EINVAL;
|
---|
| 1340 | }
|
---|
[a35b458] | 1341 |
|
---|
[984a9ba] | 1342 | errno_t retval = ipc_forward_fast(call.cap_handle, exch->phone, 0, 0, 0,
|
---|
[b4cbef1] | 1343 | IPC_FF_ROUTE_FROM_ME);
|
---|
| 1344 | if (retval != EOK) {
|
---|
[ab9f443] | 1345 | async_forget(msg);
|
---|
[984a9ba] | 1346 | async_answer_0(&call, retval);
|
---|
[b4cbef1] | 1347 | return retval;
|
---|
| 1348 | }
|
---|
[a35b458] | 1349 |
|
---|
[b7fd2a0] | 1350 | errno_t rc;
|
---|
[b4cbef1] | 1351 | async_wait_for(msg, &rc);
|
---|
[a35b458] | 1352 |
|
---|
[b7fd2a0] | 1353 | return (errno_t) rc;
|
---|
[b4cbef1] | 1354 | }
|
---|
| 1355 |
|
---|
[0da4e41] | 1356 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
---|
| 1357 | *
|
---|
[47b7006] | 1358 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
|
---|
| 1359 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1360 | * argument.
|
---|
[0da4e41] | 1361 | *
|
---|
| 1362 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
| 1363 | *
|
---|
[984a9ba] | 1364 | * @param call Storage for the data of the IPC_M_DATA_WRITE.
|
---|
| 1365 | * @param size Storage for the suggested size. May be NULL.
|
---|
[5ae1c51] | 1366 | *
|
---|
| 1367 | * @return True on success, false on failure.
|
---|
| 1368 | *
|
---|
| 1369 | */
|
---|
[984a9ba] | 1370 | bool async_data_write_receive(ipc_call_t *call, size_t *size)
|
---|
[0da4e41] | 1371 | {
|
---|
[984a9ba] | 1372 | assert(call);
|
---|
[a35b458] | 1373 |
|
---|
[984a9ba] | 1374 | async_get_call(call);
|
---|
[a35b458] | 1375 |
|
---|
[984a9ba] | 1376 | if (IPC_GET_IMETHOD(*call) != IPC_M_DATA_WRITE)
|
---|
[47b7006] | 1377 | return false;
|
---|
[a35b458] | 1378 |
|
---|
[0da4e41] | 1379 | if (size)
|
---|
[984a9ba] | 1380 | *size = (size_t) IPC_GET_ARG2(*call);
|
---|
[a35b458] | 1381 |
|
---|
[47b7006] | 1382 | return true;
|
---|
[0da4e41] | 1383 | }
|
---|
| 1384 |
|
---|
| 1385 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
---|
| 1386 | *
|
---|
[47b7006] | 1387 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
|
---|
| 1388 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
| 1389 | * argument.
|
---|
[0da4e41] | 1390 | *
|
---|
[984a9ba] | 1391 | * @param call IPC_M_DATA_WRITE call to answer.
|
---|
| 1392 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
---|
| 1393 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
---|
[b4cbef1] | 1394 | *
|
---|
[01c3bb4] | 1395 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
[0da4e41] | 1396 | *
|
---|
| 1397 | */
|
---|
[984a9ba] | 1398 | errno_t async_data_write_finalize(ipc_call_t *call, void *dst, size_t size)
|
---|
[0da4e41] | 1399 | {
|
---|
[984a9ba] | 1400 | assert(call);
|
---|
| 1401 |
|
---|
| 1402 | return async_answer_2(call, EOK, (sysarg_t) dst, (sysarg_t) size);
|
---|
[0da4e41] | 1403 | }
|
---|
| 1404 |
|
---|
[eda925a] | 1405 | /** Wrapper for receiving binary data or strings
|
---|
[8aa42e3] | 1406 | *
|
---|
| 1407 | * This wrapper only makes it more comfortable to use async_data_write_*
|
---|
[eda925a] | 1408 | * functions to receive binary data or strings.
|
---|
[8aa42e3] | 1409 | *
|
---|
[472c09d] | 1410 | * @param data Pointer to data pointer (which should be later disposed
|
---|
| 1411 | * by free()). If the operation fails, the pointer is not
|
---|
| 1412 | * touched.
|
---|
[eda925a] | 1413 | * @param nullterm If true then the received data is always zero terminated.
|
---|
| 1414 | * This also causes to allocate one extra byte beyond the
|
---|
| 1415 | * raw transmitted data.
|
---|
[b4cbef1] | 1416 | * @param min_size Minimum size (in bytes) of the data to receive.
|
---|
[472c09d] | 1417 | * @param max_size Maximum size (in bytes) of the data to receive. 0 means
|
---|
| 1418 | * no limit.
|
---|
[eda925a] | 1419 | * @param granulariy If non-zero then the size of the received data has to
|
---|
[472c09d] | 1420 | * be divisible by this value.
|
---|
| 1421 | * @param received If not NULL, the size of the received data is stored here.
|
---|
[8aa42e3] | 1422 | *
|
---|
| 1423 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
| 1424 | *
|
---|
| 1425 | */
|
---|
[b7fd2a0] | 1426 | errno_t async_data_write_accept(void **data, const bool nullterm,
|
---|
[eda925a] | 1427 | const size_t min_size, const size_t max_size, const size_t granularity,
|
---|
| 1428 | size_t *received)
|
---|
[8aa42e3] | 1429 | {
|
---|
[79ae36dd] | 1430 | assert(data);
|
---|
[a35b458] | 1431 |
|
---|
[984a9ba] | 1432 | ipc_call_t call;
|
---|
[8aa42e3] | 1433 | size_t size;
|
---|
[984a9ba] | 1434 | if (!async_data_write_receive(&call, &size)) {
|
---|
| 1435 | async_answer_0(&call, EINVAL);
|
---|
[8aa42e3] | 1436 | return EINVAL;
|
---|
| 1437 | }
|
---|
[a35b458] | 1438 |
|
---|
[b4cbef1] | 1439 | if (size < min_size) {
|
---|
[984a9ba] | 1440 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1441 | return EINVAL;
|
---|
| 1442 | }
|
---|
[a35b458] | 1443 |
|
---|
[8aa42e3] | 1444 | if ((max_size > 0) && (size > max_size)) {
|
---|
[984a9ba] | 1445 | async_answer_0(&call, EINVAL);
|
---|
[8aa42e3] | 1446 | return EINVAL;
|
---|
| 1447 | }
|
---|
[a35b458] | 1448 |
|
---|
[472c09d] | 1449 | if ((granularity > 0) && ((size % granularity) != 0)) {
|
---|
[984a9ba] | 1450 | async_answer_0(&call, EINVAL);
|
---|
[472c09d] | 1451 | return EINVAL;
|
---|
| 1452 | }
|
---|
[a35b458] | 1453 |
|
---|
[57dea62] | 1454 | void *arg_data;
|
---|
[a35b458] | 1455 |
|
---|
[eda925a] | 1456 | if (nullterm)
|
---|
[57dea62] | 1457 | arg_data = malloc(size + 1);
|
---|
[eda925a] | 1458 | else
|
---|
[57dea62] | 1459 | arg_data = malloc(size);
|
---|
[a35b458] | 1460 |
|
---|
[57dea62] | 1461 | if (arg_data == NULL) {
|
---|
[984a9ba] | 1462 | async_answer_0(&call, ENOMEM);
|
---|
[8aa42e3] | 1463 | return ENOMEM;
|
---|
| 1464 | }
|
---|
[a35b458] | 1465 |
|
---|
[984a9ba] | 1466 | errno_t rc = async_data_write_finalize(&call, arg_data, size);
|
---|
[8aa42e3] | 1467 | if (rc != EOK) {
|
---|
[57dea62] | 1468 | free(arg_data);
|
---|
[8aa42e3] | 1469 | return rc;
|
---|
| 1470 | }
|
---|
[a35b458] | 1471 |
|
---|
[eda925a] | 1472 | if (nullterm)
|
---|
[57dea62] | 1473 | ((char *) arg_data)[size] = 0;
|
---|
[a35b458] | 1474 |
|
---|
[57dea62] | 1475 | *data = arg_data;
|
---|
[472c09d] | 1476 | if (received != NULL)
|
---|
| 1477 | *received = size;
|
---|
[a35b458] | 1478 |
|
---|
[8aa42e3] | 1479 | return EOK;
|
---|
| 1480 | }
|
---|
| 1481 |
|
---|
[b4cbef1] | 1482 | /** Wrapper for voiding any data that is about to be received
|
---|
| 1483 | *
|
---|
| 1484 | * This wrapper can be used to void any pending data
|
---|
| 1485 | *
|
---|
| 1486 | * @param retval Error value from @ref errno.h to be returned to the caller.
|
---|
| 1487 | *
|
---|
| 1488 | */
|
---|
[b7fd2a0] | 1489 | void async_data_write_void(errno_t retval)
|
---|
[b4cbef1] | 1490 | {
|
---|
[984a9ba] | 1491 | ipc_call_t call;
|
---|
| 1492 | async_data_write_receive(&call, NULL);
|
---|
| 1493 | async_answer_0(&call, retval);
|
---|
[b4cbef1] | 1494 | }
|
---|
| 1495 |
|
---|
| 1496 | /** Wrapper for forwarding any data that is about to be received
|
---|
| 1497 | *
|
---|
| 1498 | */
|
---|
[b7fd2a0] | 1499 | errno_t async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
|
---|
[79ae36dd] | 1500 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
| 1501 | ipc_call_t *dataptr)
|
---|
[b4cbef1] | 1502 | {
|
---|
[79ae36dd] | 1503 | if (exch == NULL)
|
---|
| 1504 | return ENOENT;
|
---|
[a35b458] | 1505 |
|
---|
[984a9ba] | 1506 | ipc_call_t call;
|
---|
| 1507 | if (!async_data_write_receive(&call, NULL)) {
|
---|
| 1508 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1509 | return EINVAL;
|
---|
| 1510 | }
|
---|
[a35b458] | 1511 |
|
---|
[79ae36dd] | 1512 | aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
|
---|
[b4cbef1] | 1513 | dataptr);
|
---|
| 1514 | if (msg == 0) {
|
---|
[984a9ba] | 1515 | async_answer_0(&call, EINVAL);
|
---|
[b4cbef1] | 1516 | return EINVAL;
|
---|
| 1517 | }
|
---|
[a35b458] | 1518 |
|
---|
[984a9ba] | 1519 | errno_t retval = ipc_forward_fast(call.cap_handle, exch->phone, 0, 0, 0,
|
---|
[b4cbef1] | 1520 | IPC_FF_ROUTE_FROM_ME);
|
---|
| 1521 | if (retval != EOK) {
|
---|
[ab9f443] | 1522 | async_forget(msg);
|
---|
[984a9ba] | 1523 | async_answer_0(&call, retval);
|
---|
[b4cbef1] | 1524 | return retval;
|
---|
| 1525 | }
|
---|
[a35b458] | 1526 |
|
---|
[b7fd2a0] | 1527 | errno_t rc;
|
---|
[b4cbef1] | 1528 | async_wait_for(msg, &rc);
|
---|
[a35b458] | 1529 |
|
---|
[b7fd2a0] | 1530 | return (errno_t) rc;
|
---|
[b4cbef1] | 1531 | }
|
---|
| 1532 |
|
---|
[79ae36dd] | 1533 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
---|
| 1534 | *
|
---|
| 1535 | * If the current call is IPC_M_CONNECT_TO_ME then a new
|
---|
| 1536 | * async session is created for the accepted phone.
|
---|
| 1537 | *
|
---|
| 1538 | * @param mgmt Exchange management style.
|
---|
| 1539 | *
|
---|
[8869f7b] | 1540 | * @return New async session.
|
---|
| 1541 | * @return NULL on failure.
|
---|
[79ae36dd] | 1542 | *
|
---|
| 1543 | */
|
---|
| 1544 | async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
|
---|
| 1545 | {
|
---|
| 1546 | /* Accept the phone */
|
---|
| 1547 | ipc_call_t call;
|
---|
[984a9ba] | 1548 | async_get_call(&call);
|
---|
| 1549 |
|
---|
[eadaeae8] | 1550 | cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(call);
|
---|
[a35b458] | 1551 |
|
---|
[eadaeae8] | 1552 | if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
|
---|
| 1553 | !CAP_HANDLE_VALID((phandle))) {
|
---|
[984a9ba] | 1554 | async_answer_0(&call, EINVAL);
|
---|
[79ae36dd] | 1555 | return NULL;
|
---|
| 1556 | }
|
---|
[a35b458] | 1557 |
|
---|
[498ced1] | 1558 | async_sess_t *sess = calloc(1, sizeof(async_sess_t));
|
---|
[79ae36dd] | 1559 | if (sess == NULL) {
|
---|
[984a9ba] | 1560 | async_answer_0(&call, ENOMEM);
|
---|
[79ae36dd] | 1561 | return NULL;
|
---|
| 1562 | }
|
---|
[a35b458] | 1563 |
|
---|
[566992e1] | 1564 | sess->iface = 0;
|
---|
[79ae36dd] | 1565 | sess->mgmt = mgmt;
|
---|
[01c3bb4] | 1566 | sess->phone = phandle;
|
---|
[a35b458] | 1567 |
|
---|
[58cbf8d5] | 1568 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
[79ae36dd] | 1569 | list_initialize(&sess->exch_list);
|
---|
| 1570 | fibril_mutex_initialize(&sess->mutex);
|
---|
[a35b458] | 1571 |
|
---|
[79ae36dd] | 1572 | /* Acknowledge the connected phone */
|
---|
[984a9ba] | 1573 | async_answer_0(&call, EOK);
|
---|
[a35b458] | 1574 |
|
---|
[79ae36dd] | 1575 | return sess;
|
---|
| 1576 | }
|
---|
| 1577 |
|
---|
[8869f7b] | 1578 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
---|
| 1579 | *
|
---|
| 1580 | * If the call is IPC_M_CONNECT_TO_ME then a new
|
---|
| 1581 | * async session is created. However, the phone is
|
---|
| 1582 | * not accepted automatically.
|
---|
| 1583 | *
|
---|
| 1584 | * @param mgmt Exchange management style.
|
---|
| 1585 | * @param call Call data.
|
---|
| 1586 | *
|
---|
| 1587 | * @return New async session.
|
---|
| 1588 | * @return NULL on failure.
|
---|
| 1589 | * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
|
---|
| 1590 | *
|
---|
| 1591 | */
|
---|
| 1592 | async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
|
---|
| 1593 | {
|
---|
[eadaeae8] | 1594 | cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(*call);
|
---|
[a35b458] | 1595 |
|
---|
[eadaeae8] | 1596 | if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
|
---|
| 1597 | !CAP_HANDLE_VALID((phandle)))
|
---|
[8869f7b] | 1598 | return NULL;
|
---|
[a35b458] | 1599 |
|
---|
[498ced1] | 1600 | async_sess_t *sess = calloc(1, sizeof(async_sess_t));
|
---|
[8869f7b] | 1601 | if (sess == NULL)
|
---|
| 1602 | return NULL;
|
---|
[a35b458] | 1603 |
|
---|
[566992e1] | 1604 | sess->iface = 0;
|
---|
[8869f7b] | 1605 | sess->mgmt = mgmt;
|
---|
[01c3bb4] | 1606 | sess->phone = phandle;
|
---|
[a35b458] | 1607 |
|
---|
[58cbf8d5] | 1608 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
[8869f7b] | 1609 | list_initialize(&sess->exch_list);
|
---|
| 1610 | fibril_mutex_initialize(&sess->mutex);
|
---|
[a35b458] | 1611 |
|
---|
[8869f7b] | 1612 | return sess;
|
---|
| 1613 | }
|
---|
| 1614 |
|
---|
[984a9ba] | 1615 | bool async_state_change_receive(ipc_call_t *call)
|
---|
[2c4aa39] | 1616 | {
|
---|
[984a9ba] | 1617 | assert(call);
|
---|
[a35b458] | 1618 |
|
---|
[984a9ba] | 1619 | async_get_call(call);
|
---|
[a35b458] | 1620 |
|
---|
[984a9ba] | 1621 | if (IPC_GET_IMETHOD(*call) != IPC_M_STATE_CHANGE_AUTHORIZE)
|
---|
[2c4aa39] | 1622 | return false;
|
---|
[a35b458] | 1623 |
|
---|
[2c4aa39] | 1624 | return true;
|
---|
| 1625 | }
|
---|
| 1626 |
|
---|
[984a9ba] | 1627 | errno_t async_state_change_finalize(ipc_call_t *call, async_exch_t *other_exch)
|
---|
[2c4aa39] | 1628 | {
|
---|
[984a9ba] | 1629 | assert(call);
|
---|
| 1630 |
|
---|
| 1631 | return async_answer_1(call, EOK, CAP_HANDLE_RAW(other_exch->phone));
|
---|
[2c4aa39] | 1632 | }
|
---|
| 1633 |
|
---|
[6b96dc06] | 1634 | __noreturn void async_manager(void)
|
---|
[d73d992] | 1635 | {
|
---|
[514d561] | 1636 | fibril_event_t ever = FIBRIL_EVENT_INIT;
|
---|
| 1637 | fibril_wait_for(&ever);
|
---|
[d73d992] | 1638 | __builtin_unreachable();
|
---|
| 1639 | }
|
---|
| 1640 |
|
---|
[a46da63] | 1641 | /** @}
|
---|
[b2951e2] | 1642 | */
|
---|