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