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