source: mainline/uspace/lib/c/generic/async/server.c@ 0a0dff8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0a0dff8 was 3bd1d7d4, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

async: Use a dedicated futex for client hash table.

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