source: mainline/uspace/lib/c/generic/async.c@ 57dea62

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 57dea62 was 57dea62, checked in by Martin Decky <martin@…>, 10 years ago

coding style (no change in functionality)

  • Property mode set to 100644
File size: 73.2 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 *
[b688fd8]79 * port_handler(icallid, *icall)
[c07544d3]80 * {
81 * if (want_refuse) {
[64d2b10]82 * async_answer_0(icallid, ELIMIT);
[c07544d3]83 * return;
84 * }
[64d2b10]85 * async_answer_0(icallid, EOK);
[80649a91]86 *
[c07544d3]87 * callid = async_get_call(&call);
[0772aff]88 * somehow_handle_the_call(callid, call);
[64d2b10]89 * async_answer_2(callid, 1, 2, 3);
[53ca318]90 *
[c07544d3]91 * callid = async_get_call(&call);
92 * ...
93 * }
[a2cd194]94 *
[80649a91]95 */
[9591265]96
[64d2b10]97#define LIBC_ASYNC_C_
98#include <ipc/ipc.h>
[80649a91]99#include <async.h>
[b76a7329]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>
108#include <adt/list.h>
[80649a91]109#include <assert.h>
110#include <errno.h>
[daa90e8]111#include <sys/time.h>
[c0699467]112#include <libarch/barrier.h>
[3e6a98c5]113#include <stdbool.h>
[c7bbf029]114#include <malloc.h>
[79ae36dd]115#include <mem.h>
116#include <stdlib.h>
[e2ab36f1]117#include <macros.h>
[d7978525]118#include "private/libc.h"
[80649a91]119
[5da7199]120/** Session data */
121struct async_sess {
122 /** List of inactive exchanges */
123 list_t exch_list;
124
125 /** Exchange management style */
126 exch_mgmt_t mgmt;
127
128 /** Session identification */
129 int phone;
130
131 /** First clone connection argument */
132 sysarg_t arg1;
133
134 /** Second clone connection argument */
135 sysarg_t arg2;
136
137 /** Third clone connection argument */
138 sysarg_t arg3;
139
140 /** Exchange mutex */
141 fibril_mutex_t mutex;
142
143 /** Number of opened exchanges */
144 atomic_t refcnt;
145
146 /** Mutex for stateful connections */
147 fibril_mutex_t remote_state_mtx;
148
149 /** Data for stateful connections */
150 void *remote_state_data;
151};
152
153/** Exchange data */
154struct async_exch {
155 /** Link into list of inactive exchanges */
156 link_t sess_link;
157
158 /** Link into global list of inactive exchanges */
159 link_t global_link;
160
161 /** Session pointer */
162 async_sess_t *sess;
163
164 /** Exchange identification */
165 int phone;
166};
167
[79ae36dd]168/** Async framework global futex */
[927a181e]169futex_t async_futex = FUTEX_INITIALIZER;
[80649a91]170
[8619f25]171/** Number of threads waiting for IPC in the kernel. */
172atomic_t threads_in_ipc_wait = { 0 };
173
[79ae36dd]174/** Naming service session */
175async_sess_t *session_ns;
[01ff41c]176
[79ae36dd]177/** Call data */
[80649a91]178typedef struct {
179 link_t link;
[79ae36dd]180
[80649a91]181 ipc_callid_t callid;
182 ipc_call_t call;
183} msg_t;
184
[5da7199]185/** Message data */
186typedef struct {
187 awaiter_t wdata;
188
189 /** If reply was received. */
190 bool done;
[57dea62]191
[47c9a8c]192 /** If the message / reply should be discarded on arrival. */
193 bool forget;
[57dea62]194
[47c9a8c]195 /** If already destroyed. */
196 bool destroyed;
[5da7199]197
198 /** Pointer to where the answer data is stored. */
199 ipc_call_t *dataptr;
200
201 sysarg_t retval;
202} amsg_t;
203
[79ae36dd]204/* Client connection data */
[c80fdd0]205typedef struct {
[062d900]206 ht_link_t link;
[79ae36dd]207
[649f087]208 task_id_t in_task_id;
[79ae36dd]209 atomic_t refcnt;
[c80fdd0]210 void *data;
211} client_t;
212
[79ae36dd]213/* Server connection data */
[80649a91]214typedef struct {
[49d072e]215 awaiter_t wdata;
[c07544d3]216
[e70bfa5]217 /** Hash table link. */
[062d900]218 ht_link_t link;
[c07544d3]219
[e2ab36f1]220 /** Incoming client task ID. */
221 task_id_t in_task_id;
[79ae36dd]222
[e70bfa5]223 /** Incoming phone hash. */
[96b02eb9]224 sysarg_t in_phone_hash;
[c07544d3]225
[23882034]226 /** Link to the client tracking structure. */
227 client_t *client;
[47b7006]228
[e70bfa5]229 /** Messages that should be delivered to this fibril. */
[b72efe8]230 list_t msg_queue;
[c07544d3]231
[e70bfa5]232 /** Identification of the opening call. */
[80649a91]233 ipc_callid_t callid;
[b688fd8]234
[e70bfa5]235 /** Call data of the opening call. */
[80649a91]236 ipc_call_t call;
[c07544d3]237
[e70bfa5]238 /** Identification of the closing call. */
239 ipc_callid_t close_callid;
[c07544d3]240
[e70bfa5]241 /** Fibril function that will be used to handle the connection. */
[b688fd8]242 async_port_handler_t handler;
243
244 /** Client data */
245 void *data;
[80649a91]246} connection_t;
247
[8820544]248/* Notification data */
249typedef struct {
250 ht_link_t link;
251
252 /** Notification method */
253 sysarg_t imethod;
254
255 /** Notification handler */
256 async_notification_handler_t handler;
257
258 /** Notification data */
259 void *data;
260} notification_t;
261
[bc1f1c2]262/** Identifier of the incoming connection handled by the current fibril. */
[79ae36dd]263static fibril_local connection_t *fibril_connection;
[e70bfa5]264
[47c9a8c]265static void to_event_initialize(to_event_t *to)
266{
[aeeddeb]267 struct timeval tv = { 0, 0 };
[57dea62]268
[47c9a8c]269 to->inlist = false;
270 to->occurred = false;
271 link_initialize(&to->link);
272 to->expires = tv;
273}
274
275static void wu_event_initialize(wu_event_t *wu)
276{
277 wu->inlist = false;
278 link_initialize(&wu->link);
279}
280
281void awaiter_initialize(awaiter_t *aw)
282{
283 aw->fid = 0;
284 aw->active = false;
285 to_event_initialize(&aw->to_event);
286 wu_event_initialize(&aw->wu_event);
287}
288
289static amsg_t *amsg_create(void)
290{
[57dea62]291 amsg_t *msg = malloc(sizeof(amsg_t));
[47c9a8c]292 if (msg) {
293 msg->done = false;
294 msg->forget = false;
295 msg->destroyed = false;
296 msg->dataptr = NULL;
297 msg->retval = (sysarg_t) EINVAL;
298 awaiter_initialize(&msg->wdata);
299 }
[57dea62]300
[47c9a8c]301 return msg;
302}
303
304static void amsg_destroy(amsg_t *msg)
305{
306 assert(!msg->destroyed);
307 msg->destroyed = true;
308 free(msg);
309}
310
[46eec3b]311static void *default_client_data_constructor(void)
312{
313 return NULL;
314}
315
316static void default_client_data_destructor(void *data)
317{
318}
319
320static async_client_data_ctor_t async_client_data_create =
321 default_client_data_constructor;
322static async_client_data_dtor_t async_client_data_destroy =
323 default_client_data_destructor;
324
325void async_set_client_data_constructor(async_client_data_ctor_t ctor)
326{
[f302586]327 assert(async_client_data_create == default_client_data_constructor);
[46eec3b]328 async_client_data_create = ctor;
329}
330
331void async_set_client_data_destructor(async_client_data_dtor_t dtor)
332{
[f302586]333 assert(async_client_data_destroy == default_client_data_destructor);
[46eec3b]334 async_client_data_destroy = dtor;
335}
336
[b688fd8]337/** Default fallback fibril function.
[47b7006]338 *
[b688fd8]339 * This fallback fibril function gets called on incomming
340 * connections that do not have a specific handler defined.
[47b7006]341 *
[3815efb]342 * @param callid Hash of the incoming call.
343 * @param call Data of the incoming call.
344 * @param arg Local argument
[47b7006]345 *
346 */
[b688fd8]347static void default_fallback_port_handler(ipc_callid_t callid, ipc_call_t *call,
[9934f7d]348 void *arg)
[47b7006]349{
350 ipc_answer_0(callid, ENOENT);
351}
[36c9234]352
[b688fd8]353static async_port_handler_t fallback_port_handler =
354 default_fallback_port_handler;
355static void *fallback_port_data = NULL;
[da0c91e7]356
[b688fd8]357static size_t notification_handler_stksz = FIBRIL_DFLT_STK_SIZE;
[79ae36dd]358
[8820544]359/** Set the stack size for the notification handler notification fibrils.
[b4df8db]360 *
[eceff5f]361 * @param size Stack size in bytes.
[b4df8db]362 */
[8820544]363void async_set_notification_handler_stack_size(size_t size)
[b4df8db]364{
[8820544]365 notification_handler_stksz = size;
[b4df8db]366}
367
[79ae36dd]368/** Mutex protecting inactive_exch_list and avail_phone_cv.
369 *
370 */
371static FIBRIL_MUTEX_INITIALIZE(async_sess_mutex);
372
373/** List of all currently inactive exchanges.
374 *
375 */
376static LIST_INITIALIZE(inactive_exch_list);
377
378/** Condition variable to wait for a phone to become available.
379 *
380 */
381static FIBRIL_CONDVAR_INITIALIZE(avail_phone_cv);
382
[b688fd8]383void async_set_fallback_port_handler(async_port_handler_t handler, void *data)
384{
385 assert(handler != NULL);
386
387 fallback_port_handler = handler;
388 fallback_port_data = data;
389}
390
[c80fdd0]391static hash_table_t client_hash_table;
[c07544d3]392static hash_table_t conn_hash_table;
[8820544]393static hash_table_t notification_hash_table;
[c07544d3]394static LIST_INITIALIZE(timeout_list);
395
[8820544]396static sysarg_t notification_avail = 0;
397
398static size_t client_key_hash(void *key)
[c80fdd0]399{
[8820544]400 task_id_t in_task_id = *(task_id_t *) key;
401 return in_task_id;
[c80fdd0]402}
403
[062d900]404static size_t client_hash(const ht_link_t *item)
[c80fdd0]405{
[062d900]406 client_t *client = hash_table_get_inst(item, client_t, link);
407 return client_key_hash(&client->in_task_id);
[c80fdd0]408}
409
[8820544]410static bool client_key_equal(void *key, const ht_link_t *item)
[c80fdd0]411{
[8820544]412 task_id_t in_task_id = *(task_id_t *) key;
[062d900]413 client_t *client = hash_table_get_inst(item, client_t, link);
[8820544]414 return in_task_id == client->in_task_id;
[c80fdd0]415}
416
417/** Operations for the client hash table. */
[062d900]418static hash_table_ops_t client_hash_table_ops = {
[c80fdd0]419 .hash = client_hash,
[062d900]420 .key_hash = client_key_hash,
421 .key_equal = client_key_equal,
[4e00f87]422 .equal = NULL,
423 .remove_callback = NULL
[c80fdd0]424};
[80649a91]425
[e70bfa5]426/** Compute hash into the connection hash table based on the source phone hash.
427 *
[c07544d3]428 * @param key Pointer to source phone hash.
429 *
430 * @return Index into the connection hash table.
[e70bfa5]431 *
432 */
[062d900]433static size_t conn_key_hash(void *key)
[450cd3a]434{
[8820544]435 sysarg_t in_phone_hash = *(sysarg_t *) key;
436 return in_phone_hash;
[450cd3a]437}
[06502f7d]438
[062d900]439static size_t conn_hash(const ht_link_t *item)
[450cd3a]440{
[062d900]441 connection_t *conn = hash_table_get_inst(item, connection_t, link);
442 return conn_key_hash(&conn->in_phone_hash);
[450cd3a]443}
[06502f7d]444
[062d900]445static bool conn_key_equal(void *key, const ht_link_t *item)
[450cd3a]446{
[8820544]447 sysarg_t in_phone_hash = *(sysarg_t *) key;
[062d900]448 connection_t *conn = hash_table_get_inst(item, connection_t, link);
449 return (in_phone_hash == conn->in_phone_hash);
[450cd3a]450}
451
[e70bfa5]452/** Operations for the connection hash table. */
[062d900]453static hash_table_ops_t conn_hash_table_ops = {
[80649a91]454 .hash = conn_hash,
[062d900]455 .key_hash = conn_key_hash,
456 .key_equal = conn_key_equal,
[4e00f87]457 .equal = NULL,
458 .remove_callback = NULL
[80649a91]459};
460
[8820544]461static size_t notification_key_hash(void *key)
462{
463 sysarg_t id = *(sysarg_t *) key;
464 return id;
465}
466
467static size_t notification_hash(const ht_link_t *item)
468{
469 notification_t *notification =
470 hash_table_get_inst(item, notification_t, link);
471 return notification_key_hash(&notification->imethod);
472}
473
474static bool notification_key_equal(void *key, const ht_link_t *item)
475{
476 sysarg_t id = *(sysarg_t *) key;
477 notification_t *notification =
478 hash_table_get_inst(item, notification_t, link);
479 return id == notification->imethod;
480}
481
482/** Operations for the notification hash table. */
483static hash_table_ops_t notification_hash_table_ops = {
484 .hash = notification_hash,
485 .key_hash = notification_key_hash,
486 .key_equal = notification_key_equal,
487 .equal = NULL,
488 .remove_callback = NULL
489};
490
[e70bfa5]491/** Sort in current fibril's timeout request.
[49d072e]492 *
[c07544d3]493 * @param wd Wait data of the current fibril.
494 *
[49d072e]495 */
[b6ee5b1]496void async_insert_timeout(awaiter_t *wd)
[49d072e]497{
[79ae36dd]498 assert(wd);
499
[f53cc81]500 wd->to_event.occurred = false;
501 wd->to_event.inlist = true;
[c07544d3]502
[b72efe8]503 link_t *tmp = timeout_list.head.next;
504 while (tmp != &timeout_list.head) {
[47b7006]505 awaiter_t *cur
506 = list_get_instance(tmp, awaiter_t, to_event.link);
[c07544d3]507
[f53cc81]508 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
[49d072e]509 break;
[47b7006]510
[49d072e]511 tmp = tmp->next;
512 }
[c07544d3]513
[b72efe8]514 list_insert_before(&wd->to_event.link, tmp);
[49d072e]515}
516
[e70bfa5]517/** Try to route a call to an appropriate connection fibril.
[80649a91]518 *
[36c9234]519 * If the proper connection fibril is found, a message with the call is added to
520 * its message queue. If the fibril was not active, it is activated and all
521 * timeouts are unregistered.
522 *
[c07544d3]523 * @param callid Hash of the incoming call.
524 * @param call Data of the incoming call.
525 *
526 * @return False if the call doesn't match any connection.
[47b7006]527 * @return True if the call was passed to the respective connection fibril.
[36c9234]528 *
[80649a91]529 */
[c07544d3]530static bool route_call(ipc_callid_t callid, ipc_call_t *call)
[450cd3a]531{
[79ae36dd]532 assert(call);
533
[01ff41c]534 futex_down(&async_futex);
[c07544d3]535
[8820544]536 ht_link_t *link = hash_table_find(&conn_hash_table, &call->in_phone_hash);
537 if (!link) {
[01ff41c]538 futex_up(&async_futex);
[c07544d3]539 return false;
[450cd3a]540 }
[c07544d3]541
[8820544]542 connection_t *conn = hash_table_get_inst(link, connection_t, link);
[c07544d3]543
544 msg_t *msg = malloc(sizeof(*msg));
545 if (!msg) {
546 futex_up(&async_futex);
547 return false;
548 }
549
[80649a91]550 msg->callid = callid;
551 msg->call = *call;
552 list_append(&msg->link, &conn->msg_queue);
[c07544d3]553
[228e490]554 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
[41269bd]555 conn->close_callid = callid;
[80649a91]556
[36c9234]557 /* If the connection fibril is waiting for an event, activate it */
[49d072e]558 if (!conn->wdata.active) {
[c07544d3]559
[49d072e]560 /* If in timeout list, remove it */
[f53cc81]561 if (conn->wdata.to_event.inlist) {
562 conn->wdata.to_event.inlist = false;
563 list_remove(&conn->wdata.to_event.link);
[49d072e]564 }
[c07544d3]565
566 conn->wdata.active = true;
[bc1f1c2]567 fibril_add_ready(conn->wdata.fid);
[80649a91]568 }
[c07544d3]569
[01ff41c]570 futex_up(&async_futex);
[c07544d3]571 return true;
572}
[80649a91]573
[c07544d3]574/** Notification fibril.
575 *
576 * When a notification arrives, a fibril with this implementing function is
[8820544]577 * created. It calls the corresponding notification handler and does the final
578 * cleanup.
[c07544d3]579 *
580 * @param arg Message structure pointer.
581 *
582 * @return Always zero.
583 *
584 */
585static int notification_fibril(void *arg)
586{
[79ae36dd]587 assert(arg);
588
[c07544d3]589 msg_t *msg = (msg_t *) arg;
[8820544]590 async_notification_handler_t handler = NULL;
591 void *data = NULL;
592
593 futex_down(&async_futex);
594
595 ht_link_t *link = hash_table_find(&notification_hash_table,
596 &IPC_GET_IMETHOD(msg->call));
597 if (link) {
598 notification_t *notification =
599 hash_table_get_inst(link, notification_t, link);
600 handler = notification->handler;
601 data = notification->data;
602 }
603
604 futex_up(&async_futex);
605
606 if (handler)
607 handler(msg->callid, &msg->call, data);
[c07544d3]608
609 free(msg);
610 return 0;
611}
612
[8820544]613/** Process notification.
[c07544d3]614 *
615 * A new fibril is created which would process the notification.
616 *
617 * @param callid Hash of the incoming call.
618 * @param call Data of the incoming call.
619 *
620 * @return False if an error occured.
621 * True if the call was passed to the notification fibril.
622 *
623 */
624static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
625{
[79ae36dd]626 assert(call);
627
[c07544d3]628 futex_down(&async_futex);
629
630 msg_t *msg = malloc(sizeof(*msg));
631 if (!msg) {
632 futex_up(&async_futex);
633 return false;
634 }
635
636 msg->callid = callid;
637 msg->call = *call;
638
[b4df8db]639 fid_t fid = fibril_create_generic(notification_fibril, msg,
[8820544]640 notification_handler_stksz);
[86d7bfa]641 if (fid == 0) {
642 free(msg);
643 futex_up(&async_futex);
644 return false;
645 }
646
[c07544d3]647 fibril_add_ready(fid);
648
649 futex_up(&async_futex);
650 return true;
[80649a91]651}
652
[8820544]653/** Subscribe to IRQ notification.
654 *
655 * @param inr IRQ number.
656 * @param devno Device number of the device generating inr.
657 * @param handler Notification handler.
658 * @param data Notification handler client data.
659 * @param ucode Top-half pseudocode handler.
660 *
661 * @return Zero on success or a negative error code.
662 *
663 */
664int async_irq_subscribe(int inr, int devno,
665 async_notification_handler_t handler, void *data, const irq_code_t *ucode)
666{
667 notification_t *notification =
668 (notification_t *) malloc(sizeof(notification_t));
669 if (!notification)
670 return ENOMEM;
671
672 futex_down(&async_futex);
673
674 sysarg_t imethod = notification_avail;
675 notification_avail++;
676
677 notification->imethod = imethod;
678 notification->handler = handler;
679 notification->data = data;
680
681 hash_table_insert(&notification_hash_table, &notification->link);
682
683 futex_up(&async_futex);
684
685 return ipc_irq_subscribe(inr, devno, imethod, ucode);
686}
687
688/** Unsubscribe from IRQ notification.
689 *
690 * @param inr IRQ number.
691 * @param devno Device number of the device generating inr.
692 *
693 * @return Zero on success or a negative error code.
694 *
695 */
696int async_irq_unsubscribe(int inr, int devno)
697{
698 // TODO: Remove entry from hash table
699 // to avoid memory leak
700
701 return ipc_irq_unsubscribe(inr, devno);
702}
703
704/** Subscribe to event notifications.
705 *
706 * @param evno Event type to subscribe.
707 * @param handler Notification handler.
708 * @param data Notification handler client data.
709 *
710 * @return Zero on success or a negative error code.
711 *
712 */
713int async_event_subscribe(event_type_t evno,
714 async_notification_handler_t handler, void *data)
715{
716 notification_t *notification =
717 (notification_t *) malloc(sizeof(notification_t));
718 if (!notification)
719 return ENOMEM;
720
721 futex_down(&async_futex);
722
723 sysarg_t imethod = notification_avail;
724 notification_avail++;
725
726 notification->imethod = imethod;
727 notification->handler = handler;
728 notification->data = data;
729
730 hash_table_insert(&notification_hash_table, &notification->link);
731
732 futex_up(&async_futex);
733
734 return ipc_event_subscribe(evno, imethod);
735}
736
737/** Subscribe to task event notifications.
738 *
739 * @param evno Event type to subscribe.
740 * @param handler Notification handler.
741 * @param data Notification handler client data.
742 *
743 * @return Zero on success or a negative error code.
744 *
745 */
746int async_event_task_subscribe(event_task_type_t evno,
747 async_notification_handler_t handler, void *data)
748{
749 notification_t *notification =
750 (notification_t *) malloc(sizeof(notification_t));
751 if (!notification)
752 return ENOMEM;
753
754 futex_down(&async_futex);
755
756 sysarg_t imethod = notification_avail;
757 notification_avail++;
758
759 notification->imethod = imethod;
760 notification->handler = handler;
761 notification->data = data;
762
763 hash_table_insert(&notification_hash_table, &notification->link);
764
765 futex_up(&async_futex);
766
767 return ipc_event_task_subscribe(evno, imethod);
768}
769
770/** Unmask event notifications.
771 *
772 * @param evno Event type to unmask.
773 *
774 * @return Value returned by the kernel.
775 *
776 */
777int async_event_unmask(event_type_t evno)
778{
779 return ipc_event_unmask(evno);
780}
781
782/** Unmask task event notifications.
783 *
784 * @param evno Event type to unmask.
785 *
786 * @return Value returned by the kernel.
787 *
788 */
789int async_event_task_unmask(event_task_type_t evno)
790{
791 return ipc_event_task_unmask(evno);
792}
793
[e70bfa5]794/** Return new incoming message for the current (fibril-local) connection.
795 *
[c07544d3]796 * @param call Storage where the incoming call data will be stored.
797 * @param usecs Timeout in microseconds. Zero denotes no timeout.
798 *
799 * @return If no timeout was specified, then a hash of the
800 * incoming call is returned. If a timeout is specified,
801 * then a hash of the incoming call is returned unless
802 * the timeout expires prior to receiving a message. In
803 * that case zero is returned.
[e70bfa5]804 *
805 */
[49d072e]806ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
[80649a91]807{
[79ae36dd]808 assert(call);
809 assert(fibril_connection);
[c07544d3]810
811 /* Why doing this?
[79ae36dd]812 * GCC 4.1.0 coughs on fibril_connection-> dereference.
[6c46350]813 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
[c07544d3]814 * I would never expect to find so many errors in
815 * a compiler.
[6c46350]816 */
[79ae36dd]817 connection_t *conn = fibril_connection;
[c07544d3]818
[01ff41c]819 futex_down(&async_futex);
[c07544d3]820
[49d072e]821 if (usecs) {
[45cbcaf4]822 getuptime(&conn->wdata.to_event.expires);
[7f9d97f3]823 tv_add_diff(&conn->wdata.to_event.expires, usecs);
[c07544d3]824 } else
[f53cc81]825 conn->wdata.to_event.inlist = false;
[c07544d3]826
[e70bfa5]827 /* If nothing in queue, wait until something arrives */
[6c46350]828 while (list_empty(&conn->msg_queue)) {
[8c8f8d6]829 if (conn->close_callid) {
830 /*
831 * Handle the case when the connection was already
832 * closed by the client but the server did not notice
833 * the first IPC_M_PHONE_HUNGUP call and continues to
834 * call async_get_call_timeout(). Repeat
[47b7006]835 * IPC_M_PHONE_HUNGUP until the caller notices.
[8c8f8d6]836 */
837 memset(call, 0, sizeof(ipc_call_t));
[228e490]838 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
[8c8f8d6]839 futex_up(&async_futex);
840 return conn->close_callid;
841 }
[47b7006]842
[085bd54]843 if (usecs)
[b6ee5b1]844 async_insert_timeout(&conn->wdata);
[c07544d3]845
846 conn->wdata.active = false;
847
[c7509e5]848 /*
849 * Note: the current fibril will be rescheduled either due to a
850 * timeout or due to an arriving message destined to it. In the
851 * former case, handle_expired_timeouts() and, in the latter
852 * case, route_call() will perform the wakeup.
853 */
[116d3f6f]854 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]855
[e70bfa5]856 /*
[c07544d3]857 * Futex is up after getting back from async_manager.
858 * Get it again.
[c7509e5]859 */
[49d072e]860 futex_down(&async_futex);
[f53cc81]861 if ((usecs) && (conn->wdata.to_event.occurred)
[c07544d3]862 && (list_empty(&conn->msg_queue))) {
[e70bfa5]863 /* If we timed out -> exit */
[49d072e]864 futex_up(&async_futex);
865 return 0;
866 }
[450cd3a]867 }
868
[57dea62]869 msg_t *msg = list_get_instance(list_first(&conn->msg_queue),
870 msg_t, link);
[80649a91]871 list_remove(&msg->link);
[c07544d3]872
873 ipc_callid_t callid = msg->callid;
[80649a91]874 *call = msg->call;
875 free(msg);
876
[01ff41c]877 futex_up(&async_futex);
[80649a91]878 return callid;
879}
880
[e2ab36f1]881static client_t *async_client_get(task_id_t client_id, bool create)
[26fbb7bb]882{
883 client_t *client = NULL;
884
885 futex_down(&async_futex);
[8820544]886 ht_link_t *link = hash_table_find(&client_hash_table, &client_id);
887 if (link) {
888 client = hash_table_get_inst(link, client_t, link);
[26fbb7bb]889 atomic_inc(&client->refcnt);
890 } else if (create) {
891 client = malloc(sizeof(client_t));
892 if (client) {
[e2ab36f1]893 client->in_task_id = client_id;
[26fbb7bb]894 client->data = async_client_data_create();
895
896 atomic_set(&client->refcnt, 1);
[062d900]897 hash_table_insert(&client_hash_table, &client->link);
[26fbb7bb]898 }
899 }
900
901 futex_up(&async_futex);
902 return client;
903}
904
905static void async_client_put(client_t *client)
906{
907 bool destroy;
[062d900]908
[26fbb7bb]909 futex_down(&async_futex);
910
911 if (atomic_predec(&client->refcnt) == 0) {
[062d900]912 hash_table_remove(&client_hash_table, &client->in_task_id);
[26fbb7bb]913 destroy = true;
914 } else
915 destroy = false;
916
917 futex_up(&async_futex);
918
919 if (destroy) {
920 if (client->data)
921 async_client_data_destroy(client->data);
922
923 free(client);
924 }
925}
926
[455f190]927void *async_get_client_data(void)
928{
929 assert(fibril_connection);
930 return fibril_connection->client->data;
931}
932
[e2ab36f1]933void *async_get_client_data_by_id(task_id_t client_id)
[455f190]934{
[e2ab36f1]935 client_t *client = async_client_get(client_id, false);
[455f190]936 if (!client)
937 return NULL;
[57dea62]938
[455f190]939 if (!client->data) {
940 async_client_put(client);
941 return NULL;
942 }
[57dea62]943
[455f190]944 return client->data;
945}
946
[e2ab36f1]947void async_put_client_data_by_id(task_id_t client_id)
[455f190]948{
[e2ab36f1]949 client_t *client = async_client_get(client_id, false);
[57dea62]950
[455f190]951 assert(client);
952 assert(client->data);
[57dea62]953
[cdc8ee2d]954 /* Drop the reference we got in async_get_client_data_by_hash(). */
955 async_client_put(client);
[57dea62]956
[cdc8ee2d]957 /* Drop our own reference we got at the beginning of this function. */
[455f190]958 async_client_put(client);
959}
960
[f2f0392]961/** Wrapper for client connection fibril.
962 *
[36c9234]963 * When a new connection arrives, a fibril with this implementing function is
[b688fd8]964 * created. It calls handler() and does the final cleanup.
[a2cd194]965 *
[c07544d3]966 * @param arg Connection structure pointer.
967 *
968 * @return Always zero.
[a2cd194]969 *
970 */
[c07544d3]971static int connection_fibril(void *arg)
[80649a91]972{
[79ae36dd]973 assert(arg);
974
[c07544d3]975 /*
[c80fdd0]976 * Setup fibril-local connection pointer.
[c07544d3]977 */
[79ae36dd]978 fibril_connection = (connection_t *) arg;
[47b7006]979
[c80fdd0]980 /*
981 * Add our reference for the current connection in the client task
982 * tracking structure. If this is the first reference, create and
983 * hash in a new tracking structure.
984 */
[26fbb7bb]985
[e2ab36f1]986 client_t *client = async_client_get(fibril_connection->in_task_id, true);
[26fbb7bb]987 if (!client) {
988 ipc_answer_0(fibril_connection->callid, ENOMEM);
989 return 0;
[c80fdd0]990 }
[26fbb7bb]991
[79ae36dd]992 fibril_connection->client = client;
[47b7006]993
[c80fdd0]994 /*
995 * Call the connection handler function.
996 */
[b688fd8]997 fibril_connection->handler(fibril_connection->callid,
998 &fibril_connection->call, fibril_connection->data);
[a46da63]999
[c80fdd0]1000 /*
1001 * Remove the reference for this client task connection.
1002 */
[26fbb7bb]1003 async_client_put(client);
[47b7006]1004
[c80fdd0]1005 /*
1006 * Remove myself from the connection hash table.
1007 */
1008 futex_down(&async_futex);
[062d900]1009 hash_table_remove(&conn_hash_table, &fibril_connection->in_phone_hash);
[01ff41c]1010 futex_up(&async_futex);
[a46da63]1011
[c80fdd0]1012 /*
1013 * Answer all remaining messages with EHANGUP.
1014 */
[79ae36dd]1015 while (!list_empty(&fibril_connection->msg_queue)) {
[47b7006]1016 msg_t *msg =
[b72efe8]1017 list_get_instance(list_first(&fibril_connection->msg_queue),
1018 msg_t, link);
[c07544d3]1019
[a2cd194]1020 list_remove(&msg->link);
[b74959bd]1021 ipc_answer_0(msg->callid, EHANGUP);
[a2cd194]1022 free(msg);
1023 }
[c07544d3]1024
[c80fdd0]1025 /*
1026 * If the connection was hung-up, answer the last call,
1027 * i.e. IPC_M_PHONE_HUNGUP.
1028 */
[79ae36dd]1029 if (fibril_connection->close_callid)
1030 ipc_answer_0(fibril_connection->close_callid, EOK);
[a46da63]1031
[79ae36dd]1032 free(fibril_connection);
[a46da63]1033 return 0;
[80649a91]1034}
1035
[f2f0392]1036/** Create a new fibril for a new connection.
[80649a91]1037 *
[79ae36dd]1038 * Create new fibril for connection, fill in connection structures and insert
[f2f0392]1039 * it into the hash table, so that later we can easily do routing of messages to
1040 * particular fibrils.
[53ca318]1041 *
[e2ab36f1]1042 * @param in_task_id Identification of the incoming connection.
[c07544d3]1043 * @param in_phone_hash Identification of the incoming connection.
1044 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
1045 * If callid is zero, the connection was opened by
1046 * accepting the IPC_M_CONNECT_TO_ME call and this function
1047 * is called directly by the server.
1048 * @param call Call data of the opening call.
[b688fd8]1049 * @param handler Fibril function that should be called upon opening the
[c07544d3]1050 * connection.
[b688fd8]1051 * @param data Extra argument to pass to the connection fibril
[c07544d3]1052 *
1053 * @return New fibril id or NULL on failure.
[36c9234]1054 *
[80649a91]1055 */
[e2ab36f1]1056fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
[3c22f70]1057 ipc_callid_t callid, ipc_call_t *call,
[b688fd8]1058 async_port_handler_t handler, void *data)
[80649a91]1059{
[c07544d3]1060 connection_t *conn = malloc(sizeof(*conn));
[80649a91]1061 if (!conn) {
[6675c70]1062 if (callid)
[b74959bd]1063 ipc_answer_0(callid, ENOMEM);
[47b7006]1064
[0b4a67a]1065 return (uintptr_t) NULL;
[80649a91]1066 }
[c07544d3]1067
[e2ab36f1]1068 conn->in_task_id = in_task_id;
[44c6d88d]1069 conn->in_phone_hash = in_phone_hash;
[80649a91]1070 list_initialize(&conn->msg_queue);
1071 conn->callid = callid;
[c4702804]1072 conn->close_callid = 0;
[b688fd8]1073 conn->data = data;
[c07544d3]1074
[eaf34f7]1075 if (call)
1076 conn->call = *call;
[6b21292]1077
[c07544d3]1078 /* We will activate the fibril ASAP */
1079 conn->wdata.active = true;
[b688fd8]1080 conn->handler = handler;
[bc1f1c2]1081 conn->wdata.fid = fibril_create(connection_fibril, conn);
[c07544d3]1082
[86d7bfa]1083 if (conn->wdata.fid == 0) {
[80649a91]1084 free(conn);
[86d7bfa]1085
[6675c70]1086 if (callid)
[b74959bd]1087 ipc_answer_0(callid, ENOMEM);
[86d7bfa]1088
[0b4a67a]1089 return (uintptr_t) NULL;
[80649a91]1090 }
[6b21292]1091
[36c9234]1092 /* Add connection to the connection hash table */
[c07544d3]1093
[01ff41c]1094 futex_down(&async_futex);
[062d900]1095 hash_table_insert(&conn_hash_table, &conn->link);
[01ff41c]1096 futex_up(&async_futex);
[6b21292]1097
[bc1f1c2]1098 fibril_add_ready(conn->wdata.fid);
[6b21292]1099
[bc1f1c2]1100 return conn->wdata.fid;
[80649a91]1101}
1102
[36c9234]1103/** Handle a call that was received.
1104 *
1105 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
1106 * Otherwise the call is routed to its connection fibril.
1107 *
[c07544d3]1108 * @param callid Hash of the incoming call.
1109 * @param call Data of the incoming call.
[6b21292]1110 *
[36c9234]1111 */
[80649a91]1112static void handle_call(ipc_callid_t callid, ipc_call_t *call)
1113{
[79ae36dd]1114 assert(call);
1115
[b688fd8]1116 /* Kernel notification */
[15039b67]1117 if ((callid & IPC_CALLID_NOTIFICATION)) {
[c07544d3]1118 process_notification(callid, call);
[47b7006]1119 return;
[6b21292]1120 }
1121
[228e490]1122 switch (IPC_GET_IMETHOD(*call)) {
[6aae539d]1123 case IPC_M_CLONE_ESTABLISH:
[80649a91]1124 case IPC_M_CONNECT_ME_TO:
[47b7006]1125 /* Open new connection with fibril, etc. */
[e2ab36f1]1126 async_new_connection(call->in_task_id, IPC_GET_ARG5(*call),
[b688fd8]1127 callid, call, fallback_port_handler, fallback_port_data);
[47b7006]1128 return;
[80649a91]1129 }
[6b21292]1130
[36c9234]1131 /* Try to route the call through the connection hash table */
[44c6d88d]1132 if (route_call(callid, call))
[47b7006]1133 return;
[6b21292]1134
[44c6d88d]1135 /* Unknown call from unknown phone - hang it up */
[b74959bd]1136 ipc_answer_0(callid, EHANGUP);
[450cd3a]1137}
1138
[f2f0392]1139/** Fire all timeouts that expired. */
[c042bdd]1140static void handle_expired_timeouts(void)
1141{
1142 struct timeval tv;
[45cbcaf4]1143 getuptime(&tv);
[c07544d3]1144
[c042bdd]1145 futex_down(&async_futex);
[c07544d3]1146
[b72efe8]1147 link_t *cur = list_first(&timeout_list);
1148 while (cur != NULL) {
[47b7006]1149 awaiter_t *waiter =
1150 list_get_instance(cur, awaiter_t, to_event.link);
[c07544d3]1151
[f53cc81]1152 if (tv_gt(&waiter->to_event.expires, &tv))
[c042bdd]1153 break;
[47b7006]1154
[f53cc81]1155 list_remove(&waiter->to_event.link);
1156 waiter->to_event.inlist = false;
1157 waiter->to_event.occurred = true;
[c07544d3]1158
[36c9234]1159 /*
[c07544d3]1160 * Redundant condition?
1161 * The fibril should not be active when it gets here.
[c042bdd]1162 */
[49d072e]1163 if (!waiter->active) {
[c07544d3]1164 waiter->active = true;
[bc1f1c2]1165 fibril_add_ready(waiter->fid);
[c042bdd]1166 }
[b72efe8]1167
1168 cur = list_first(&timeout_list);
[c042bdd]1169 }
[c07544d3]1170
[c042bdd]1171 futex_up(&async_futex);
1172}
1173
[36c9234]1174/** Endless loop dispatching incoming calls and answers.
1175 *
[c07544d3]1176 * @return Never returns.
1177 *
[36c9234]1178 */
[085bd54]1179static int async_manager_worker(void)
[80649a91]1180{
[c07544d3]1181 while (true) {
[116d3f6f]1182 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
[47b7006]1183 futex_up(&async_futex);
[36c9234]1184 /*
1185 * async_futex is always held when entering a manager
1186 * fibril.
[a46da63]1187 */
[80649a91]1188 continue;
1189 }
[c07544d3]1190
[c042bdd]1191 futex_down(&async_futex);
[c07544d3]1192
1193 suseconds_t timeout;
[1db6dfd]1194 unsigned int flags = SYNCH_FLAGS_NONE;
[c042bdd]1195 if (!list_empty(&timeout_list)) {
[b72efe8]1196 awaiter_t *waiter = list_get_instance(
1197 list_first(&timeout_list), awaiter_t, to_event.link);
[c07544d3]1198
1199 struct timeval tv;
[45cbcaf4]1200 getuptime(&tv);
[c07544d3]1201
[f53cc81]1202 if (tv_gteq(&tv, &waiter->to_event.expires)) {
[6c46350]1203 futex_up(&async_futex);
[c042bdd]1204 handle_expired_timeouts();
[1db6dfd]1205 /*
1206 * Notice that even if the event(s) already
1207 * expired (and thus the other fibril was
1208 * supposed to be running already),
1209 * we check for incoming IPC.
1210 *
1211 * Otherwise, a fibril that continuously
1212 * creates (almost) expired events could
1213 * prevent IPC retrieval from the kernel.
1214 */
1215 timeout = 0;
1216 flags = SYNCH_FLAGS_NON_BLOCKING;
1217
1218 } else {
[7f9d97f3]1219 timeout = tv_sub_diff(&waiter->to_event.expires,
1220 &tv);
[1db6dfd]1221 futex_up(&async_futex);
1222 }
1223 } else {
1224 futex_up(&async_futex);
[0b99e40]1225 timeout = SYNCH_NO_TIMEOUT;
[1db6dfd]1226 }
[47b7006]1227
[8619f25]1228 atomic_inc(&threads_in_ipc_wait);
[c07544d3]1229
1230 ipc_call_t call;
[1db6dfd]1231 ipc_callid_t callid = ipc_wait_cycle(&call, timeout, flags);
[c07544d3]1232
[8619f25]1233 atomic_dec(&threads_in_ipc_wait);
[47b7006]1234
[0b99e40]1235 if (!callid) {
[c042bdd]1236 handle_expired_timeouts();
[0b99e40]1237 continue;
1238 }
[c07544d3]1239
1240 if (callid & IPC_CALLID_ANSWERED)
[80649a91]1241 continue;
[c07544d3]1242
[80649a91]1243 handle_call(callid, &call);
1244 }
[a46da63]1245
1246 return 0;
[80649a91]1247}
1248
[36c9234]1249/** Function to start async_manager as a standalone fibril.
[c07544d3]1250 *
[36c9234]1251 * When more kernel threads are used, one async manager should exist per thread.
1252 *
[c07544d3]1253 * @param arg Unused.
1254 * @return Never returns.
[36c9234]1255 *
[a2cd194]1256 */
[9591265]1257static int async_manager_fibril(void *arg)
[80649a91]1258{
[a46da63]1259 futex_up(&async_futex);
[c07544d3]1260
[36c9234]1261 /*
1262 * async_futex is always locked when entering manager
1263 */
[085bd54]1264 async_manager_worker();
[a46da63]1265
1266 return 0;
[80649a91]1267}
[450cd3a]1268
[36c9234]1269/** Add one manager to manager list. */
[80649a91]1270void async_create_manager(void)
[450cd3a]1271{
[c07544d3]1272 fid_t fid = fibril_create(async_manager_fibril, NULL);
[86d7bfa]1273 if (fid != 0)
1274 fibril_add_manager(fid);
[80649a91]1275}
1276
1277/** Remove one manager from manager list */
1278void async_destroy_manager(void)
1279{
[bc1f1c2]1280 fibril_remove_manager();
[80649a91]1281}
1282
[36c9234]1283/** Initialize the async framework.
1284 *
1285 */
[47b7006]1286void __async_init(void)
[80649a91]1287{
[062d900]1288 if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
[47b7006]1289 abort();
[80649a91]1290
[062d900]1291 if (!hash_table_create(&conn_hash_table, 0, 0, &conn_hash_table_ops))
[47b7006]1292 abort();
[79ae36dd]1293
[8820544]1294 if (!hash_table_create(&notification_hash_table, 0, 0,
1295 &notification_hash_table_ops))
1296 abort();
1297
[79ae36dd]1298 session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
1299 if (session_ns == NULL)
1300 abort();
1301
1302 session_ns->mgmt = EXCHANGE_ATOMIC;
1303 session_ns->phone = PHONE_NS;
1304 session_ns->arg1 = 0;
1305 session_ns->arg2 = 0;
1306 session_ns->arg3 = 0;
1307
[58cbf8d5]1308 fibril_mutex_initialize(&session_ns->remote_state_mtx);
1309 session_ns->remote_state_data = NULL;
1310
[79ae36dd]1311 list_initialize(&session_ns->exch_list);
1312 fibril_mutex_initialize(&session_ns->mutex);
1313 atomic_set(&session_ns->refcnt, 0);
[450cd3a]1314}
[01ff41c]1315
[36c9234]1316/** Reply received callback.
[01ff41c]1317 *
[36c9234]1318 * This function is called whenever a reply for an asynchronous message sent out
1319 * by the asynchronous framework is received.
1320 *
1321 * Notify the fibril which is waiting for this message that it has arrived.
1322 *
[c07544d3]1323 * @param arg Pointer to the asynchronous message record.
1324 * @param retval Value returned in the answer.
1325 * @param data Call data of the answer.
[47b7006]1326 *
[01ff41c]1327 */
[79ae36dd]1328void reply_received(void *arg, int retval, ipc_call_t *data)
[01ff41c]1329{
[79ae36dd]1330 assert(arg);
1331
[9db9b10]1332 futex_down(&async_futex);
1333
[c07544d3]1334 amsg_t *msg = (amsg_t *) arg;
[01ff41c]1335 msg->retval = retval;
[c07544d3]1336
[36c9234]1337 /* Copy data after futex_down, just in case the call was detached */
[9db9b10]1338 if ((msg->dataptr) && (data))
[c07544d3]1339 *msg->dataptr = *data;
1340
[c042bdd]1341 write_barrier();
[c07544d3]1342
[c042bdd]1343 /* Remove message from timeout list */
[f53cc81]1344 if (msg->wdata.to_event.inlist)
1345 list_remove(&msg->wdata.to_event.link);
[c07544d3]1346
1347 msg->done = true;
[57dea62]1348
[47c9a8c]1349 if (msg->forget) {
1350 assert(msg->wdata.active);
1351 amsg_destroy(msg);
1352 } else if (!msg->wdata.active) {
[c07544d3]1353 msg->wdata.active = true;
[bc1f1c2]1354 fibril_add_ready(msg->wdata.fid);
[01ff41c]1355 }
[57dea62]1356
[01ff41c]1357 futex_up(&async_futex);
1358}
1359
[36c9234]1360/** Send message and return id of the sent message.
1361 *
1362 * The return value can be used as input for async_wait() to wait for
1363 * completion.
[01ff41c]1364 *
[79ae36dd]1365 * @param exch Exchange for sending the message.
1366 * @param imethod Service-defined interface and method.
[c07544d3]1367 * @param arg1 Service-defined payload argument.
1368 * @param arg2 Service-defined payload argument.
1369 * @param arg3 Service-defined payload argument.
1370 * @param arg4 Service-defined payload argument.
1371 * @param dataptr If non-NULL, storage where the reply data will be
1372 * stored.
1373 *
1374 * @return Hash of the sent message or 0 on error.
[36c9234]1375 *
[01ff41c]1376 */
[79ae36dd]1377aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1378 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[01ff41c]1379{
[79ae36dd]1380 if (exch == NULL)
1381 return 0;
[c07544d3]1382
[47c9a8c]1383 amsg_t *msg = amsg_create();
[79ae36dd]1384 if (msg == NULL)
[c07544d3]1385 return 0;
[6b21292]1386
[01ff41c]1387 msg->dataptr = dataptr;
[c07544d3]1388 msg->wdata.active = true;
1389
[79ae36dd]1390 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
[c07544d3]1391 reply_received, true);
[6b21292]1392
[01ff41c]1393 return (aid_t) msg;
1394}
1395
[90f5d64]1396/** Send message and return id of the sent message
1397 *
[36c9234]1398 * The return value can be used as input for async_wait() to wait for
1399 * completion.
1400 *
[79ae36dd]1401 * @param exch Exchange for sending the message.
1402 * @param imethod Service-defined interface and method.
[c07544d3]1403 * @param arg1 Service-defined payload argument.
1404 * @param arg2 Service-defined payload argument.
1405 * @param arg3 Service-defined payload argument.
1406 * @param arg4 Service-defined payload argument.
1407 * @param arg5 Service-defined payload argument.
1408 * @param dataptr If non-NULL, storage where the reply data will be
1409 * stored.
1410 *
1411 * @return Hash of the sent message or 0 on error.
[36c9234]1412 *
[90f5d64]1413 */
[79ae36dd]1414aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1415 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[0cc4313]1416 ipc_call_t *dataptr)
[90f5d64]1417{
[79ae36dd]1418 if (exch == NULL)
1419 return 0;
1420
[47c9a8c]1421 amsg_t *msg = amsg_create();
[79ae36dd]1422 if (msg == NULL)
[c07544d3]1423 return 0;
1424
[90f5d64]1425 msg->dataptr = dataptr;
[c07544d3]1426 msg->wdata.active = true;
[6b21292]1427
[79ae36dd]1428 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
1429 msg, reply_received, true);
[6b21292]1430
[90f5d64]1431 return (aid_t) msg;
1432}
1433
[36c9234]1434/** Wait for a message sent by the async framework.
[01ff41c]1435 *
[c07544d3]1436 * @param amsgid Hash of the message to wait for.
1437 * @param retval Pointer to storage where the retval of the answer will
1438 * be stored.
1439 *
[01ff41c]1440 */
[96b02eb9]1441void async_wait_for(aid_t amsgid, sysarg_t *retval)
[01ff41c]1442{
[79ae36dd]1443 assert(amsgid);
1444
[01ff41c]1445 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]1446
[01ff41c]1447 futex_down(&async_futex);
[57dea62]1448
[47c9a8c]1449 assert(!msg->forget);
1450 assert(!msg->destroyed);
[57dea62]1451
[01ff41c]1452 if (msg->done) {
1453 futex_up(&async_futex);
1454 goto done;
1455 }
[c07544d3]1456
[bc1f1c2]1457 msg->wdata.fid = fibril_get_id();
[c07544d3]1458 msg->wdata.active = false;
[f53cc81]1459 msg->wdata.to_event.inlist = false;
[c07544d3]1460
[36c9234]1461 /* Leave the async_futex locked when entering this function */
[116d3f6f]1462 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1463
1464 /* Futex is up automatically after fibril_switch */
1465
[01ff41c]1466done:
1467 if (retval)
1468 *retval = msg->retval;
[c07544d3]1469
[47c9a8c]1470 amsg_destroy(msg);
[01ff41c]1471}
[0b99e40]1472
[36c9234]1473/** Wait for a message sent by the async framework, timeout variant.
[47c9a8c]1474 *
1475 * If the wait times out, the caller may choose to either wait again by calling
1476 * async_wait_for() or async_wait_timeout(), or forget the message via
1477 * async_forget().
[c042bdd]1478 *
[c07544d3]1479 * @param amsgid Hash of the message to wait for.
1480 * @param retval Pointer to storage where the retval of the answer will
1481 * be stored.
1482 * @param timeout Timeout in microseconds.
1483 *
1484 * @return Zero on success, ETIMEOUT if the timeout has expired.
[c042bdd]1485 *
1486 */
[96b02eb9]1487int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
[c042bdd]1488{
[79ae36dd]1489 assert(amsgid);
1490
[c042bdd]1491 amsg_t *msg = (amsg_t *) amsgid;
[57dea62]1492
[c042bdd]1493 futex_down(&async_futex);
[57dea62]1494
[47c9a8c]1495 assert(!msg->forget);
1496 assert(!msg->destroyed);
[57dea62]1497
[c042bdd]1498 if (msg->done) {
1499 futex_up(&async_futex);
1500 goto done;
1501 }
[c07544d3]1502
[1db6dfd]1503 /*
1504 * Negative timeout is converted to zero timeout to avoid
1505 * using tv_add with negative augmenter.
1506 */
1507 if (timeout < 0)
1508 timeout = 0;
[57dea62]1509
[45cbcaf4]1510 getuptime(&msg->wdata.to_event.expires);
[7f9d97f3]1511 tv_add_diff(&msg->wdata.to_event.expires, timeout);
[c07544d3]1512
[1db6dfd]1513 /*
1514 * Current fibril is inserted as waiting regardless of the
1515 * "size" of the timeout.
1516 *
1517 * Checking for msg->done and immediately bailing out when
1518 * timeout == 0 would mean that the manager fibril would never
1519 * run (consider single threaded program).
1520 * Thus the IPC answer would be never retrieved from the kernel.
1521 *
1522 * Notice that the actual delay would be very small because we
1523 * - switch to manager fibril
1524 * - the manager sees expired timeout
1525 * - and thus adds us back to ready queue
1526 * - manager switches back to some ready fibril
1527 * (prior it, it checks for incoming IPC).
1528 *
1529 */
[bc1f1c2]1530 msg->wdata.fid = fibril_get_id();
[c07544d3]1531 msg->wdata.active = false;
[b6ee5b1]1532 async_insert_timeout(&msg->wdata);
[c07544d3]1533
[36c9234]1534 /* Leave the async_futex locked when entering this function */
[116d3f6f]1535 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1536
1537 /* Futex is up automatically after fibril_switch */
1538
[c042bdd]1539 if (!msg->done)
1540 return ETIMEOUT;
[c07544d3]1541
[c042bdd]1542done:
1543 if (retval)
1544 *retval = msg->retval;
[c07544d3]1545
[47c9a8c]1546 amsg_destroy(msg);
[c07544d3]1547
[c042bdd]1548 return 0;
1549}
[47c9a8c]1550
1551/** Discard the message / reply on arrival.
1552 *
1553 * The message will be marked to be discarded once the reply arrives in
1554 * reply_received(). It is not allowed to call async_wait_for() or
1555 * async_wait_timeout() on this message after a call to this function.
1556 *
1557 * @param amsgid Hash of the message to forget.
1558 */
1559void async_forget(aid_t amsgid)
1560{
1561 amsg_t *msg = (amsg_t *) amsgid;
[57dea62]1562
[47c9a8c]1563 assert(msg);
1564 assert(!msg->forget);
1565 assert(!msg->destroyed);
[57dea62]1566
[47c9a8c]1567 futex_down(&async_futex);
[57dea62]1568
[375e501]1569 if (msg->done) {
[47c9a8c]1570 amsg_destroy(msg);
[375e501]1571 } else {
1572 msg->dataptr = NULL;
[47c9a8c]1573 msg->forget = true;
[375e501]1574 }
[57dea62]1575
[47c9a8c]1576 futex_up(&async_futex);
1577}
[0b99e40]1578
[36c9234]1579/** Wait for specified time.
[44c6d88d]1580 *
[36c9234]1581 * The current fibril is suspended but the thread continues to execute.
1582 *
[c07544d3]1583 * @param timeout Duration of the wait in microseconds.
1584 *
[44c6d88d]1585 */
1586void async_usleep(suseconds_t timeout)
1587{
[47c9a8c]1588 amsg_t *msg = amsg_create();
[44c6d88d]1589 if (!msg)
1590 return;
[6b21292]1591
[bc1f1c2]1592 msg->wdata.fid = fibril_get_id();
[6b21292]1593
[45cbcaf4]1594 getuptime(&msg->wdata.to_event.expires);
[7f9d97f3]1595 tv_add_diff(&msg->wdata.to_event.expires, timeout);
[6b21292]1596
[44c6d88d]1597 futex_down(&async_futex);
[c07544d3]1598
[b6ee5b1]1599 async_insert_timeout(&msg->wdata);
[c07544d3]1600
[36c9234]1601 /* Leave the async_futex locked when entering this function */
[116d3f6f]1602 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1603
1604 /* Futex is up automatically after fibril_switch() */
1605
[47c9a8c]1606 amsg_destroy(msg);
[44c6d88d]1607}
[da0c91e7]1608
[0cc4313]1609/** Pseudo-synchronous message sending - fast version.
1610 *
1611 * Send message asynchronously and return only after the reply arrives.
1612 *
1613 * This function can only transfer 4 register payload arguments. For
1614 * transferring more arguments, see the slower async_req_slow().
1615 *
[79ae36dd]1616 * @param exch Exchange for sending the message.
1617 * @param imethod Interface and method of the call.
[c07544d3]1618 * @param arg1 Service-defined payload argument.
1619 * @param arg2 Service-defined payload argument.
1620 * @param arg3 Service-defined payload argument.
1621 * @param arg4 Service-defined payload argument.
1622 * @param r1 If non-NULL, storage for the 1st reply argument.
1623 * @param r2 If non-NULL, storage for the 2nd reply argument.
1624 * @param r3 If non-NULL, storage for the 3rd reply argument.
1625 * @param r4 If non-NULL, storage for the 4th reply argument.
1626 * @param r5 If non-NULL, storage for the 5th reply argument.
1627 *
1628 * @return Return code of the reply or a negative error code.
1629 *
[0cc4313]1630 */
[79ae36dd]1631sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1632 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1633 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1634{
[79ae36dd]1635 if (exch == NULL)
1636 return ENOENT;
1637
[0cc4313]1638 ipc_call_t result;
[79ae36dd]1639 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
[0cc4313]1640 &result);
[c07544d3]1641
[96b02eb9]1642 sysarg_t rc;
[79ae36dd]1643 async_wait_for(aid, &rc);
[c07544d3]1644
1645 if (r1)
[0cc4313]1646 *r1 = IPC_GET_ARG1(result);
[c07544d3]1647
[0cc4313]1648 if (r2)
1649 *r2 = IPC_GET_ARG2(result);
[c07544d3]1650
[0cc4313]1651 if (r3)
1652 *r3 = IPC_GET_ARG3(result);
[c07544d3]1653
[0cc4313]1654 if (r4)
1655 *r4 = IPC_GET_ARG4(result);
[c07544d3]1656
[0cc4313]1657 if (r5)
1658 *r5 = IPC_GET_ARG5(result);
[c07544d3]1659
[0cc4313]1660 return rc;
[085bd54]1661}
1662
[0cc4313]1663/** Pseudo-synchronous message sending - slow version.
1664 *
1665 * Send message asynchronously and return only after the reply arrives.
1666 *
[79ae36dd]1667 * @param exch Exchange for sending the message.
1668 * @param imethod Interface and method of the call.
[c07544d3]1669 * @param arg1 Service-defined payload argument.
1670 * @param arg2 Service-defined payload argument.
1671 * @param arg3 Service-defined payload argument.
1672 * @param arg4 Service-defined payload argument.
1673 * @param arg5 Service-defined payload argument.
1674 * @param r1 If non-NULL, storage for the 1st reply argument.
1675 * @param r2 If non-NULL, storage for the 2nd reply argument.
1676 * @param r3 If non-NULL, storage for the 3rd reply argument.
1677 * @param r4 If non-NULL, storage for the 4th reply argument.
1678 * @param r5 If non-NULL, storage for the 5th reply argument.
1679 *
1680 * @return Return code of the reply or a negative error code.
1681 *
[0cc4313]1682 */
[79ae36dd]1683sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1684 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1685 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1686{
[79ae36dd]1687 if (exch == NULL)
1688 return ENOENT;
1689
[0cc4313]1690 ipc_call_t result;
[79ae36dd]1691 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
[0cc4313]1692 &result);
[c07544d3]1693
[96b02eb9]1694 sysarg_t rc;
[79ae36dd]1695 async_wait_for(aid, &rc);
[c07544d3]1696
1697 if (r1)
[0cc4313]1698 *r1 = IPC_GET_ARG1(result);
[c07544d3]1699
[0cc4313]1700 if (r2)
1701 *r2 = IPC_GET_ARG2(result);
[c07544d3]1702
[0cc4313]1703 if (r3)
1704 *r3 = IPC_GET_ARG3(result);
[c07544d3]1705
[0cc4313]1706 if (r4)
1707 *r4 = IPC_GET_ARG4(result);
[c07544d3]1708
[0cc4313]1709 if (r5)
1710 *r5 = IPC_GET_ARG5(result);
[c07544d3]1711
[0cc4313]1712 return rc;
[085bd54]1713}
[b2951e2]1714
[79ae36dd]1715void async_msg_0(async_exch_t *exch, sysarg_t imethod)
[64d2b10]1716{
[79ae36dd]1717 if (exch != NULL)
1718 ipc_call_async_0(exch->phone, imethod, NULL, NULL, true);
[64d2b10]1719}
1720
[79ae36dd]1721void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
[64d2b10]1722{
[79ae36dd]1723 if (exch != NULL)
1724 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL, true);
[64d2b10]1725}
1726
[79ae36dd]1727void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1728 sysarg_t arg2)
[64d2b10]1729{
[79ae36dd]1730 if (exch != NULL)
1731 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL,
1732 true);
[64d2b10]1733}
1734
[79ae36dd]1735void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1736 sysarg_t arg2, sysarg_t arg3)
[64d2b10]1737{
[79ae36dd]1738 if (exch != NULL)
1739 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
1740 NULL, true);
[64d2b10]1741}
1742
[79ae36dd]1743void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1744 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[64d2b10]1745{
[79ae36dd]1746 if (exch != NULL)
1747 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
1748 NULL, NULL, true);
[64d2b10]1749}
1750
[79ae36dd]1751void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1752 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
[64d2b10]1753{
[79ae36dd]1754 if (exch != NULL)
1755 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
1756 arg5, NULL, NULL, true);
[64d2b10]1757}
1758
1759sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
1760{
1761 return ipc_answer_0(callid, retval);
1762}
1763
1764sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
1765{
1766 return ipc_answer_1(callid, retval, arg1);
1767}
1768
1769sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1770 sysarg_t arg2)
1771{
1772 return ipc_answer_2(callid, retval, arg1, arg2);
1773}
1774
1775sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1776 sysarg_t arg2, sysarg_t arg3)
1777{
1778 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
1779}
1780
1781sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1782 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1783{
1784 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
1785}
1786
1787sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1788 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1789{
1790 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
1791}
1792
[79ae36dd]1793int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
1794 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[64d2b10]1795{
[79ae36dd]1796 if (exch == NULL)
1797 return ENOENT;
1798
1799 return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
[64d2b10]1800}
1801
[79ae36dd]1802int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
1803 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
1804 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
[64d2b10]1805{
[79ae36dd]1806 if (exch == NULL)
1807 return ENOENT;
1808
1809 return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
1810 arg4, arg5, mode);
[64d2b10]1811}
1812
[007e6efa]1813/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1814 *
1815 * Ask through phone for a new connection to some service.
1816 *
[79ae36dd]1817 * @param exch Exchange for sending the message.
[007e6efa]1818 * @param arg1 User defined argument.
1819 * @param arg2 User defined argument.
1820 * @param arg3 User defined argument.
1821 * @param client_receiver Connection handing routine.
1822 *
[79ae36dd]1823 * @return Zero on success or a negative error code.
[007e6efa]1824 *
1825 */
[79ae36dd]1826int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
[b688fd8]1827 sysarg_t arg3, async_port_handler_t client_receiver, void *data)
[007e6efa]1828{
[79ae36dd]1829 if (exch == NULL)
1830 return ENOENT;
1831
[007e6efa]1832 sysarg_t phone_hash;
[ab34cc9]1833 sysarg_t rc;
1834
1835 aid_t req;
1836 ipc_call_t answer;
1837 req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1838 &answer);
1839 async_wait_for(req, &rc);
[007e6efa]1840 if (rc != EOK)
[ab34cc9]1841 return (int) rc;
1842
1843 phone_hash = IPC_GET_ARG5(answer);
[36b16bc]1844
[007e6efa]1845 if (client_receiver != NULL)
[ab34cc9]1846 async_new_connection(answer.in_task_id, phone_hash, 0, NULL,
[b688fd8]1847 client_receiver, data);
[007e6efa]1848
1849 return EOK;
1850}
1851
[6aae539d]1852/** Wrapper for making IPC_M_CLONE_ESTABLISH calls using the async framework.
[007e6efa]1853 *
[6aae539d]1854 * Ask for a cloned connection to some service.
[f74392f]1855 *
[79ae36dd]1856 * @param mgmt Exchange management style.
1857 * @param exch Exchange for sending the message.
[007e6efa]1858 *
[79ae36dd]1859 * @return New session on success or NULL on error.
[f74392f]1860 *
1861 */
[6aae539d]1862async_sess_t *async_clone_establish(exch_mgmt_t mgmt, async_exch_t *exch)
[79ae36dd]1863{
1864 if (exch == NULL) {
1865 errno = ENOENT;
1866 return NULL;
1867 }
1868
1869 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1870 if (sess == NULL) {
1871 errno = ENOMEM;
1872 return NULL;
1873 }
1874
1875 ipc_call_t result;
1876
[47c9a8c]1877 amsg_t *msg = amsg_create();
1878 if (!msg) {
[79ae36dd]1879 free(sess);
1880 errno = ENOMEM;
1881 return NULL;
1882 }
1883
1884 msg->dataptr = &result;
1885 msg->wdata.active = true;
1886
[6aae539d]1887 ipc_call_async_0(exch->phone, IPC_M_CLONE_ESTABLISH, msg,
[79ae36dd]1888 reply_received, true);
1889
1890 sysarg_t rc;
1891 async_wait_for((aid_t) msg, &rc);
1892
1893 if (rc != EOK) {
1894 errno = rc;
1895 free(sess);
1896 return NULL;
1897 }
1898
1899 int phone = (int) IPC_GET_ARG5(result);
1900
1901 if (phone < 0) {
1902 errno = phone;
1903 free(sess);
1904 return NULL;
1905 }
1906
1907 sess->mgmt = mgmt;
1908 sess->phone = phone;
1909 sess->arg1 = 0;
1910 sess->arg2 = 0;
1911 sess->arg3 = 0;
1912
[58cbf8d5]1913 fibril_mutex_initialize(&sess->remote_state_mtx);
1914 sess->remote_state_data = NULL;
1915
[79ae36dd]1916 list_initialize(&sess->exch_list);
1917 fibril_mutex_initialize(&sess->mutex);
1918 atomic_set(&sess->refcnt, 0);
1919
1920 return sess;
1921}
1922
1923static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
1924 sysarg_t arg3, sysarg_t arg4)
[f74392f]1925{
[79ae36dd]1926 ipc_call_t result;
1927
[47c9a8c]1928 amsg_t *msg = amsg_create();
1929 if (!msg)
[79ae36dd]1930 return ENOENT;
1931
1932 msg->dataptr = &result;
1933 msg->wdata.active = true;
1934
1935 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
1936 msg, reply_received, true);
1937
1938 sysarg_t rc;
1939 async_wait_for((aid_t) msg, &rc);
[f74392f]1940
[007e6efa]1941 if (rc != EOK)
[f74392f]1942 return rc;
[007e6efa]1943
[79ae36dd]1944 return (int) IPC_GET_ARG5(result);
1945}
1946
1947/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1948 *
1949 * Ask through for a new connection to some service.
1950 *
1951 * @param mgmt Exchange management style.
1952 * @param exch Exchange for sending the message.
1953 * @param arg1 User defined argument.
1954 * @param arg2 User defined argument.
1955 * @param arg3 User defined argument.
1956 *
1957 * @return New session on success or NULL on error.
1958 *
1959 */
1960async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
1961 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1962{
1963 if (exch == NULL) {
1964 errno = ENOENT;
1965 return NULL;
1966 }
1967
1968 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1969 if (sess == NULL) {
1970 errno = ENOMEM;
1971 return NULL;
1972 }
1973
1974 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1975 0);
1976
1977 if (phone < 0) {
1978 errno = phone;
1979 free(sess);
1980 return NULL;
1981 }
1982
1983 sess->mgmt = mgmt;
1984 sess->phone = phone;
1985 sess->arg1 = arg1;
1986 sess->arg2 = arg2;
1987 sess->arg3 = arg3;
1988
[58cbf8d5]1989 fibril_mutex_initialize(&sess->remote_state_mtx);
1990 sess->remote_state_data = NULL;
1991
[79ae36dd]1992 list_initialize(&sess->exch_list);
1993 fibril_mutex_initialize(&sess->mutex);
1994 atomic_set(&sess->refcnt, 0);
1995
1996 return sess;
[f74392f]1997}
1998
[93ad49a8]1999/** Set arguments for new connections.
[0f4532e]2000 *
2001 * FIXME This is an ugly hack to work around the problem that parallel
2002 * exchanges are implemented using parallel connections. When we create
[93ad49a8]2003 * a callback session, the framework does not know arguments for the new
2004 * connections.
[0f4532e]2005 *
2006 * The proper solution seems to be to implement parallel exchanges using
2007 * tagging.
2008 */
[93ad49a8]2009void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
2010 sysarg_t arg3)
[0f4532e]2011{
[93ad49a8]2012 sess->arg1 = arg1;
2013 sess->arg2 = arg2;
2014 sess->arg3 = arg3;
[0f4532e]2015}
2016
[f74392f]2017/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
[007e6efa]2018 *
[f74392f]2019 * Ask through phone for a new connection to some service and block until
2020 * success.
2021 *
[79ae36dd]2022 * @param mgmt Exchange management style.
2023 * @param exch Exchange for sending the message.
2024 * @param arg1 User defined argument.
2025 * @param arg2 User defined argument.
2026 * @param arg3 User defined argument.
[007e6efa]2027 *
[79ae36dd]2028 * @return New session on success or NULL on error.
[f74392f]2029 *
2030 */
[79ae36dd]2031async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
2032 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
[f74392f]2033{
[79ae36dd]2034 if (exch == NULL) {
2035 errno = ENOENT;
2036 return NULL;
2037 }
[f74392f]2038
[79ae36dd]2039 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2040 if (sess == NULL) {
2041 errno = ENOMEM;
2042 return NULL;
2043 }
[007e6efa]2044
[79ae36dd]2045 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
2046 IPC_FLAG_BLOCKING);
2047
2048 if (phone < 0) {
2049 errno = phone;
2050 free(sess);
2051 return NULL;
2052 }
2053
2054 sess->mgmt = mgmt;
2055 sess->phone = phone;
2056 sess->arg1 = arg1;
2057 sess->arg2 = arg2;
2058 sess->arg3 = arg3;
2059
[58cbf8d5]2060 fibril_mutex_initialize(&sess->remote_state_mtx);
2061 sess->remote_state_data = NULL;
2062
[79ae36dd]2063 list_initialize(&sess->exch_list);
2064 fibril_mutex_initialize(&sess->mutex);
2065 atomic_set(&sess->refcnt, 0);
2066
2067 return sess;
[f74392f]2068}
2069
[64d2b10]2070/** Connect to a task specified by id.
2071 *
2072 */
[79ae36dd]2073async_sess_t *async_connect_kbox(task_id_t id)
[64d2b10]2074{
[79ae36dd]2075 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2076 if (sess == NULL) {
2077 errno = ENOMEM;
2078 return NULL;
2079 }
2080
2081 int phone = ipc_connect_kbox(id);
2082 if (phone < 0) {
2083 errno = phone;
2084 free(sess);
2085 return NULL;
2086 }
2087
2088 sess->mgmt = EXCHANGE_ATOMIC;
2089 sess->phone = phone;
2090 sess->arg1 = 0;
2091 sess->arg2 = 0;
2092 sess->arg3 = 0;
2093
[58cbf8d5]2094 fibril_mutex_initialize(&sess->remote_state_mtx);
2095 sess->remote_state_data = NULL;
2096
[79ae36dd]2097 list_initialize(&sess->exch_list);
2098 fibril_mutex_initialize(&sess->mutex);
2099 atomic_set(&sess->refcnt, 0);
2100
2101 return sess;
2102}
2103
2104static int async_hangup_internal(int phone)
2105{
2106 return ipc_hangup(phone);
[64d2b10]2107}
2108
2109/** Wrapper for ipc_hangup.
2110 *
[79ae36dd]2111 * @param sess Session to hung up.
[64d2b10]2112 *
2113 * @return Zero on success or a negative error code.
2114 *
2115 */
[79ae36dd]2116int async_hangup(async_sess_t *sess)
[64d2b10]2117{
[36e2b55]2118 async_exch_t *exch;
2119
[79ae36dd]2120 assert(sess);
2121
2122 if (atomic_get(&sess->refcnt) > 0)
2123 return EBUSY;
2124
[36e2b55]2125 fibril_mutex_lock(&async_sess_mutex);
[972c60ce]2126
[cff3fb6]2127 int rc = async_hangup_internal(sess->phone);
[36e2b55]2128
2129 while (!list_empty(&sess->exch_list)) {
2130 exch = (async_exch_t *)
2131 list_get_instance(list_first(&sess->exch_list),
2132 async_exch_t, sess_link);
2133
2134 list_remove(&exch->sess_link);
2135 list_remove(&exch->global_link);
2136 async_hangup_internal(exch->phone);
2137 free(exch);
2138 }
[4c50c8d]2139
2140 free(sess);
[36e2b55]2141
2142 fibril_mutex_unlock(&async_sess_mutex);
2143
[79ae36dd]2144 return rc;
[64d2b10]2145}
2146
2147/** Interrupt one thread of this task from waiting for IPC. */
2148void async_poke(void)
2149{
2150 ipc_poke();
2151}
2152
[79ae36dd]2153/** Start new exchange in a session.
2154 *
2155 * @param session Session.
2156 *
2157 * @return New exchange or NULL on error.
2158 *
2159 */
2160async_exch_t *async_exchange_begin(async_sess_t *sess)
2161{
2162 if (sess == NULL)
2163 return NULL;
2164
2165 async_exch_t *exch;
2166
2167 fibril_mutex_lock(&async_sess_mutex);
2168
2169 if (!list_empty(&sess->exch_list)) {
2170 /*
2171 * There are inactive exchanges in the session.
2172 */
2173 exch = (async_exch_t *)
[b72efe8]2174 list_get_instance(list_first(&sess->exch_list),
2175 async_exch_t, sess_link);
2176
[79ae36dd]2177 list_remove(&exch->sess_link);
2178 list_remove(&exch->global_link);
2179 } else {
2180 /*
2181 * There are no available exchanges in the session.
2182 */
2183
2184 if ((sess->mgmt == EXCHANGE_ATOMIC) ||
2185 (sess->mgmt == EXCHANGE_SERIALIZE)) {
2186 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2187 if (exch != NULL) {
[b72efe8]2188 link_initialize(&exch->sess_link);
2189 link_initialize(&exch->global_link);
[79ae36dd]2190 exch->sess = sess;
2191 exch->phone = sess->phone;
2192 }
2193 } else { /* EXCHANGE_PARALLEL */
2194 /*
2195 * Make a one-time attempt to connect a new data phone.
2196 */
2197
2198 int phone;
2199
2200retry:
2201 phone = async_connect_me_to_internal(sess->phone, sess->arg1,
2202 sess->arg2, sess->arg3, 0);
2203 if (phone >= 0) {
2204 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2205 if (exch != NULL) {
[b72efe8]2206 link_initialize(&exch->sess_link);
2207 link_initialize(&exch->global_link);
[79ae36dd]2208 exch->sess = sess;
2209 exch->phone = phone;
2210 } else
2211 async_hangup_internal(phone);
2212 } else if (!list_empty(&inactive_exch_list)) {
2213 /*
2214 * We did not manage to connect a new phone. But we
2215 * can try to close some of the currently inactive
2216 * connections in other sessions and try again.
2217 */
2218 exch = (async_exch_t *)
[b72efe8]2219 list_get_instance(list_first(&inactive_exch_list),
2220 async_exch_t, global_link);
2221
[79ae36dd]2222 list_remove(&exch->sess_link);
2223 list_remove(&exch->global_link);
2224 async_hangup_internal(exch->phone);
2225 free(exch);
2226 goto retry;
2227 } else {
2228 /*
2229 * Wait for a phone to become available.
2230 */
2231 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
2232 goto retry;
2233 }
2234 }
2235 }
2236
2237 fibril_mutex_unlock(&async_sess_mutex);
2238
2239 if (exch != NULL) {
2240 atomic_inc(&sess->refcnt);
2241
2242 if (sess->mgmt == EXCHANGE_SERIALIZE)
2243 fibril_mutex_lock(&sess->mutex);
2244 }
2245
2246 return exch;
2247}
2248
2249/** Finish an exchange.
2250 *
2251 * @param exch Exchange to finish.
2252 *
2253 */
2254void async_exchange_end(async_exch_t *exch)
2255{
2256 if (exch == NULL)
2257 return;
2258
2259 async_sess_t *sess = exch->sess;
[3ca2e36]2260 assert(sess != NULL);
[79ae36dd]2261
[1c6436a]2262 atomic_dec(&sess->refcnt);
2263
[79ae36dd]2264 if (sess->mgmt == EXCHANGE_SERIALIZE)
2265 fibril_mutex_unlock(&sess->mutex);
2266
2267 fibril_mutex_lock(&async_sess_mutex);
2268
2269 list_append(&exch->sess_link, &sess->exch_list);
2270 list_append(&exch->global_link, &inactive_exch_list);
2271 fibril_condvar_signal(&avail_phone_cv);
2272
2273 fibril_mutex_unlock(&async_sess_mutex);
2274}
2275
[47b7006]2276/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
2277 *
[79ae36dd]2278 * @param exch Exchange for sending the message.
2279 * @param size Size of the destination address space area.
2280 * @param arg User defined argument.
2281 * @param flags Storage for the received flags. Can be NULL.
[df956b9b]2282 * @param dst Address of the storage for the destination address space area
2283 * base address. Cannot be NULL.
[0da4e41]2284 *
[47b7006]2285 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2286 *
2287 */
[fbcdeb8]2288int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
2289 unsigned int *flags, void **dst)
[0da4e41]2290{
[79ae36dd]2291 if (exch == NULL)
2292 return ENOENT;
2293
[fbcdeb8]2294 sysarg_t _flags = 0;
2295 sysarg_t _dst = (sysarg_t) -1;
2296 int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
2297 arg, NULL, &_flags, NULL, &_dst);
[47b7006]2298
[0da4e41]2299 if (flags)
[fbcdeb8]2300 *flags = (unsigned int) _flags;
[47b7006]2301
[fbcdeb8]2302 *dst = (void *) _dst;
[0da4e41]2303 return res;
2304}
2305
2306/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
2307 *
[47b7006]2308 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
2309 * calls so that the user doesn't have to remember the meaning of each IPC
2310 * argument.
[0da4e41]2311 *
2312 * So far, this wrapper is to be used from within a connection fibril.
2313 *
[47b7006]2314 * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
2315 * @param size Destination address space area size.
2316 *
2317 * @return True on success, false on failure.
[0da4e41]2318 *
2319 */
[47b7006]2320bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
[0da4e41]2321{
2322 assert(callid);
2323 assert(size);
[47b7006]2324
2325 ipc_call_t data;
[0da4e41]2326 *callid = async_get_call(&data);
[47b7006]2327
[228e490]2328 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
[47b7006]2329 return false;
2330
[fbcdeb8]2331 *size = (size_t) IPC_GET_ARG1(data);
[47b7006]2332 return true;
[0da4e41]2333}
2334
2335/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
2336 *
[fbcdeb8]2337 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
[47b7006]2338 * calls so that the user doesn't have to remember the meaning of each IPC
2339 * argument.
[0da4e41]2340 *
[47b7006]2341 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2342 * @param src Source address space base.
2343 * @param flags Flags to be used for sharing. Bits can be only cleared.
2344 *
2345 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2346 *
2347 */
[47b7006]2348int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
[0da4e41]2349{
[d7978525]2350 return ipc_answer_3(callid, EOK, (sysarg_t) src, (sysarg_t) flags,
2351 (sysarg_t) __entry);
[0da4e41]2352}
2353
[47b7006]2354/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
[0da4e41]2355 *
[79ae36dd]2356 * @param exch Exchange for sending the message.
2357 * @param src Source address space area base address.
2358 * @param flags Flags to be used for sharing. Bits can be only cleared.
[47b7006]2359 *
2360 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2361 *
2362 */
[79ae36dd]2363int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
[0da4e41]2364{
[79ae36dd]2365 if (exch == NULL)
2366 return ENOENT;
2367
2368 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
[96b02eb9]2369 (sysarg_t) flags);
[0da4e41]2370}
2371
2372/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
2373 *
[47b7006]2374 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
2375 * calls so that the user doesn't have to remember the meaning of each IPC
2376 * argument.
[0da4e41]2377 *
2378 * So far, this wrapper is to be used from within a connection fibril.
2379 *
[47b7006]2380 * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
2381 * @param size Storage for the source address space area size.
2382 * @param flags Storage for the sharing flags.
2383 *
2384 * @return True on success, false on failure.
[0da4e41]2385 *
2386 */
[47b7006]2387bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
[0da4e41]2388{
2389 assert(callid);
2390 assert(size);
2391 assert(flags);
[47b7006]2392
2393 ipc_call_t data;
[0da4e41]2394 *callid = async_get_call(&data);
[47b7006]2395
[228e490]2396 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
[47b7006]2397 return false;
2398
[0da4e41]2399 *size = (size_t) IPC_GET_ARG2(data);
[47b7006]2400 *flags = (unsigned int) IPC_GET_ARG3(data);
2401 return true;
[0da4e41]2402}
2403
2404/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2405 *
[47b7006]2406 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2407 * calls so that the user doesn't have to remember the meaning of each IPC
2408 * argument.
[0da4e41]2409 *
[47b7006]2410 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
[df956b9b]2411 * @param dst Address of the storage for the destination address space area
2412 * base address.
[47b7006]2413 *
2414 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2415 *
2416 */
[fbcdeb8]2417int async_share_out_finalize(ipc_callid_t callid, void **dst)
[0da4e41]2418{
[d7978525]2419 return ipc_answer_2(callid, EOK, (sysarg_t) __entry, (sysarg_t) dst);
[0da4e41]2420}
2421
[8bf1eeb]2422/** Start IPC_M_DATA_READ using the async framework.
2423 *
[79ae36dd]2424 * @param exch Exchange for sending the message.
2425 * @param dst Address of the beginning of the destination buffer.
2426 * @param size Size of the destination buffer (in bytes).
[8bf1eeb]2427 * @param dataptr Storage of call data (arg 2 holds actual data size).
[79ae36dd]2428 *
[8bf1eeb]2429 * @return Hash of the sent message or 0 on error.
[79ae36dd]2430 *
[8bf1eeb]2431 */
[79ae36dd]2432aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2433 ipc_call_t *dataptr)
[8bf1eeb]2434{
[79ae36dd]2435 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
[8bf1eeb]2436 (sysarg_t) size, dataptr);
2437}
2438
[47b7006]2439/** Wrapper for IPC_M_DATA_READ calls using the async framework.
[0da4e41]2440 *
[79ae36dd]2441 * @param exch Exchange for sending the message.
2442 * @param dst Address of the beginning of the destination buffer.
2443 * @param size Size of the destination buffer.
[47b7006]2444 *
2445 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2446 *
2447 */
[79ae36dd]2448int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
[0da4e41]2449{
[79ae36dd]2450 if (exch == NULL)
2451 return ENOENT;
2452
2453 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2454 (sysarg_t) size);
[0da4e41]2455}
2456
2457/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2458 *
[47b7006]2459 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2460 * calls so that the user doesn't have to remember the meaning of each IPC
2461 * argument.
[0da4e41]2462 *
2463 * So far, this wrapper is to be used from within a connection fibril.
2464 *
[47b7006]2465 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2466 * @param size Storage for the maximum size. Can be NULL.
2467 *
2468 * @return True on success, false on failure.
[0da4e41]2469 *
2470 */
[47b7006]2471bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
[d768d4c8]2472{
2473 ipc_call_t data;
2474 return async_data_read_receive_call(callid, &data, size);
2475}
2476
2477/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2478 *
2479 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2480 * calls so that the user doesn't have to remember the meaning of each IPC
2481 * argument.
2482 *
2483 * So far, this wrapper is to be used from within a connection fibril.
2484 *
2485 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2486 * @param size Storage for the maximum size. Can be NULL.
2487 *
2488 * @return True on success, false on failure.
2489 *
2490 */
2491bool async_data_read_receive_call(ipc_callid_t *callid, ipc_call_t *data,
2492 size_t *size)
[0da4e41]2493{
2494 assert(callid);
[d768d4c8]2495 assert(data);
[47b7006]2496
[d768d4c8]2497 *callid = async_get_call(data);
[47b7006]2498
[d768d4c8]2499 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_READ)
[47b7006]2500 return false;
2501
[0da4e41]2502 if (size)
[d768d4c8]2503 *size = (size_t) IPC_GET_ARG2(*data);
[47b7006]2504
2505 return true;
[0da4e41]2506}
2507
2508/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2509 *
[47b7006]2510 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2511 * calls so that the user doesn't have to remember the meaning of each IPC
2512 * argument.
[0da4e41]2513 *
[47b7006]2514 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2515 * @param src Source address for the IPC_M_DATA_READ call.
2516 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2517 * the maximum size announced by the sender.
2518 *
2519 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2520 *
2521 */
2522int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
2523{
[d7978525]2524 return ipc_answer_2(callid, EOK, (sysarg_t) src, (sysarg_t) size);
[0da4e41]2525}
2526
[b4cbef1]2527/** Wrapper for forwarding any read request
2528 *
2529 */
[79ae36dd]2530int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2531 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2532 ipc_call_t *dataptr)
[b4cbef1]2533{
[79ae36dd]2534 if (exch == NULL)
2535 return ENOENT;
2536
[b4cbef1]2537 ipc_callid_t callid;
2538 if (!async_data_read_receive(&callid, NULL)) {
2539 ipc_answer_0(callid, EINVAL);
2540 return EINVAL;
2541 }
2542
[79ae36dd]2543 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]2544 dataptr);
2545 if (msg == 0) {
2546 ipc_answer_0(callid, EINVAL);
2547 return EINVAL;
2548 }
2549
[79ae36dd]2550 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
[b4cbef1]2551 IPC_FF_ROUTE_FROM_ME);
2552 if (retval != EOK) {
[ab9f443]2553 async_forget(msg);
[b4cbef1]2554 ipc_answer_0(callid, retval);
2555 return retval;
2556 }
2557
[96b02eb9]2558 sysarg_t rc;
[b4cbef1]2559 async_wait_for(msg, &rc);
2560
2561 return (int) rc;
2562}
2563
[47b7006]2564/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
[0da4e41]2565 *
[79ae36dd]2566 * @param exch Exchange for sending the message.
2567 * @param src Address of the beginning of the source buffer.
2568 * @param size Size of the source buffer.
[b4cbef1]2569 *
2570 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2571 *
2572 */
[79ae36dd]2573int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
[0da4e41]2574{
[79ae36dd]2575 if (exch == NULL)
2576 return ENOENT;
2577
2578 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2579 (sysarg_t) size);
[0da4e41]2580}
2581
2582/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2583 *
[47b7006]2584 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2585 * calls so that the user doesn't have to remember the meaning of each IPC
2586 * argument.
[0da4e41]2587 *
2588 * So far, this wrapper is to be used from within a connection fibril.
2589 *
[47b7006]2590 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2591 * @param size Storage for the suggested size. May be NULL.
[b4cbef1]2592 *
[47b7006]2593 * @return True on success, false on failure.
[0da4e41]2594 *
2595 */
[47b7006]2596bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
[5ae1c51]2597{
2598 ipc_call_t data;
2599 return async_data_write_receive_call(callid, &data, size);
2600}
2601
2602/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2603 *
2604 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2605 * calls so that the user doesn't have to remember the meaning of each IPC
2606 * argument.
2607 *
2608 * So far, this wrapper is to be used from within a connection fibril.
2609 *
2610 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2611 * @param data Storage for the ipc call data.
2612 * @param size Storage for the suggested size. May be NULL.
2613 *
2614 * @return True on success, false on failure.
2615 *
2616 */
2617bool async_data_write_receive_call(ipc_callid_t *callid, ipc_call_t *data,
2618 size_t *size)
[0da4e41]2619{
2620 assert(callid);
[5ae1c51]2621 assert(data);
[b4cbef1]2622
[5ae1c51]2623 *callid = async_get_call(data);
[47b7006]2624
[5ae1c51]2625 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_WRITE)
[47b7006]2626 return false;
[b4cbef1]2627
[0da4e41]2628 if (size)
[5ae1c51]2629 *size = (size_t) IPC_GET_ARG2(*data);
[b4cbef1]2630
[47b7006]2631 return true;
[0da4e41]2632}
2633
2634/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2635 *
[47b7006]2636 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2637 * calls so that the user doesn't have to remember the meaning of each IPC
2638 * argument.
[0da4e41]2639 *
[b4cbef1]2640 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2641 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2642 * @param size Final size for the IPC_M_DATA_WRITE call.
2643 *
2644 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2645 *
2646 */
2647int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
2648{
[d7978525]2649 return ipc_answer_2(callid, EOK, (sysarg_t) dst, (sysarg_t) size);
[0da4e41]2650}
2651
[eda925a]2652/** Wrapper for receiving binary data or strings
[8aa42e3]2653 *
2654 * This wrapper only makes it more comfortable to use async_data_write_*
[eda925a]2655 * functions to receive binary data or strings.
[8aa42e3]2656 *
[472c09d]2657 * @param data Pointer to data pointer (which should be later disposed
2658 * by free()). If the operation fails, the pointer is not
2659 * touched.
[eda925a]2660 * @param nullterm If true then the received data is always zero terminated.
2661 * This also causes to allocate one extra byte beyond the
2662 * raw transmitted data.
[b4cbef1]2663 * @param min_size Minimum size (in bytes) of the data to receive.
[472c09d]2664 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2665 * no limit.
[eda925a]2666 * @param granulariy If non-zero then the size of the received data has to
[472c09d]2667 * be divisible by this value.
2668 * @param received If not NULL, the size of the received data is stored here.
[8aa42e3]2669 *
2670 * @return Zero on success or a value from @ref errno.h on failure.
2671 *
2672 */
[eda925a]2673int async_data_write_accept(void **data, const bool nullterm,
2674 const size_t min_size, const size_t max_size, const size_t granularity,
2675 size_t *received)
[8aa42e3]2676{
[79ae36dd]2677 assert(data);
2678
[8aa42e3]2679 ipc_callid_t callid;
2680 size_t size;
2681 if (!async_data_write_receive(&callid, &size)) {
2682 ipc_answer_0(callid, EINVAL);
2683 return EINVAL;
2684 }
2685
[b4cbef1]2686 if (size < min_size) {
2687 ipc_answer_0(callid, EINVAL);
2688 return EINVAL;
2689 }
2690
[8aa42e3]2691 if ((max_size > 0) && (size > max_size)) {
2692 ipc_answer_0(callid, EINVAL);
2693 return EINVAL;
2694 }
2695
[472c09d]2696 if ((granularity > 0) && ((size % granularity) != 0)) {
2697 ipc_answer_0(callid, EINVAL);
2698 return EINVAL;
2699 }
2700
[57dea62]2701 void *arg_data;
[eda925a]2702
2703 if (nullterm)
[57dea62]2704 arg_data = malloc(size + 1);
[eda925a]2705 else
[57dea62]2706 arg_data = malloc(size);
[eda925a]2707
[57dea62]2708 if (arg_data == NULL) {
[8aa42e3]2709 ipc_answer_0(callid, ENOMEM);
2710 return ENOMEM;
2711 }
2712
[57dea62]2713 int rc = async_data_write_finalize(callid, arg_data, size);
[8aa42e3]2714 if (rc != EOK) {
[57dea62]2715 free(arg_data);
[8aa42e3]2716 return rc;
2717 }
2718
[eda925a]2719 if (nullterm)
[57dea62]2720 ((char *) arg_data)[size] = 0;
[8aa42e3]2721
[57dea62]2722 *data = arg_data;
[472c09d]2723 if (received != NULL)
2724 *received = size;
2725
[8aa42e3]2726 return EOK;
2727}
2728
[b4cbef1]2729/** Wrapper for voiding any data that is about to be received
2730 *
2731 * This wrapper can be used to void any pending data
2732 *
2733 * @param retval Error value from @ref errno.h to be returned to the caller.
2734 *
2735 */
[47b7006]2736void async_data_write_void(sysarg_t retval)
[b4cbef1]2737{
2738 ipc_callid_t callid;
2739 async_data_write_receive(&callid, NULL);
2740 ipc_answer_0(callid, retval);
2741}
2742
2743/** Wrapper for forwarding any data that is about to be received
2744 *
2745 */
[79ae36dd]2746int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
2747 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2748 ipc_call_t *dataptr)
[b4cbef1]2749{
[79ae36dd]2750 if (exch == NULL)
2751 return ENOENT;
2752
[b4cbef1]2753 ipc_callid_t callid;
2754 if (!async_data_write_receive(&callid, NULL)) {
2755 ipc_answer_0(callid, EINVAL);
2756 return EINVAL;
2757 }
2758
[79ae36dd]2759 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]2760 dataptr);
2761 if (msg == 0) {
2762 ipc_answer_0(callid, EINVAL);
2763 return EINVAL;
2764 }
2765
[79ae36dd]2766 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
[b4cbef1]2767 IPC_FF_ROUTE_FROM_ME);
2768 if (retval != EOK) {
[ab9f443]2769 async_forget(msg);
[b4cbef1]2770 ipc_answer_0(callid, retval);
2771 return retval;
2772 }
2773
[96b02eb9]2774 sysarg_t rc;
[b4cbef1]2775 async_wait_for(msg, &rc);
2776
2777 return (int) rc;
2778}
2779
[79ae36dd]2780/** Wrapper for sending an exchange over different exchange for cloning
2781 *
2782 * @param exch Exchange to be used for sending.
2783 * @param clone_exch Exchange to be cloned.
2784 *
2785 */
2786int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
2787{
2788 return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
2789}
2790
2791/** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
2792 *
2793 * If the current call is IPC_M_CONNECTION_CLONE then a new
2794 * async session is created for the accepted phone.
2795 *
2796 * @param mgmt Exchange management style.
2797 *
2798 * @return New async session or NULL on failure.
2799 *
2800 */
2801async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
2802{
2803 /* Accept the phone */
2804 ipc_call_t call;
2805 ipc_callid_t callid = async_get_call(&call);
2806 int phone = (int) IPC_GET_ARG1(call);
2807
2808 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
2809 (phone < 0)) {
2810 async_answer_0(callid, EINVAL);
2811 return NULL;
2812 }
2813
2814 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2815 if (sess == NULL) {
2816 async_answer_0(callid, ENOMEM);
2817 return NULL;
2818 }
2819
2820 sess->mgmt = mgmt;
2821 sess->phone = phone;
2822 sess->arg1 = 0;
2823 sess->arg2 = 0;
2824 sess->arg3 = 0;
2825
[58cbf8d5]2826 fibril_mutex_initialize(&sess->remote_state_mtx);
2827 sess->remote_state_data = NULL;
2828
[79ae36dd]2829 list_initialize(&sess->exch_list);
2830 fibril_mutex_initialize(&sess->mutex);
2831 atomic_set(&sess->refcnt, 0);
2832
2833 /* Acknowledge the cloned phone */
2834 async_answer_0(callid, EOK);
2835
2836 return sess;
2837}
2838
2839/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2840 *
2841 * If the current call is IPC_M_CONNECT_TO_ME then a new
2842 * async session is created for the accepted phone.
2843 *
2844 * @param mgmt Exchange management style.
2845 *
[8869f7b]2846 * @return New async session.
2847 * @return NULL on failure.
[79ae36dd]2848 *
2849 */
2850async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
2851{
2852 /* Accept the phone */
2853 ipc_call_t call;
2854 ipc_callid_t callid = async_get_call(&call);
2855 int phone = (int) IPC_GET_ARG5(call);
2856
2857 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
2858 (phone < 0)) {
2859 async_answer_0(callid, EINVAL);
2860 return NULL;
2861 }
2862
2863 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2864 if (sess == NULL) {
2865 async_answer_0(callid, ENOMEM);
2866 return NULL;
2867 }
2868
2869 sess->mgmt = mgmt;
2870 sess->phone = phone;
2871 sess->arg1 = 0;
2872 sess->arg2 = 0;
2873 sess->arg3 = 0;
2874
[58cbf8d5]2875 fibril_mutex_initialize(&sess->remote_state_mtx);
2876 sess->remote_state_data = NULL;
2877
[79ae36dd]2878 list_initialize(&sess->exch_list);
2879 fibril_mutex_initialize(&sess->mutex);
2880 atomic_set(&sess->refcnt, 0);
2881
2882 /* Acknowledge the connected phone */
2883 async_answer_0(callid, EOK);
2884
2885 return sess;
2886}
2887
[8869f7b]2888/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2889 *
2890 * If the call is IPC_M_CONNECT_TO_ME then a new
2891 * async session is created. However, the phone is
2892 * not accepted automatically.
2893 *
2894 * @param mgmt Exchange management style.
2895 * @param call Call data.
2896 *
2897 * @return New async session.
2898 * @return NULL on failure.
2899 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
2900 *
2901 */
2902async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
2903{
2904 int phone = (int) IPC_GET_ARG5(*call);
2905
2906 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
2907 (phone < 0))
2908 return NULL;
2909
2910 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2911 if (sess == NULL)
2912 return NULL;
2913
2914 sess->mgmt = mgmt;
2915 sess->phone = phone;
2916 sess->arg1 = 0;
2917 sess->arg2 = 0;
2918 sess->arg3 = 0;
2919
[58cbf8d5]2920 fibril_mutex_initialize(&sess->remote_state_mtx);
2921 sess->remote_state_data = NULL;
2922
[8869f7b]2923 list_initialize(&sess->exch_list);
2924 fibril_mutex_initialize(&sess->mutex);
2925 atomic_set(&sess->refcnt, 0);
2926
2927 return sess;
2928}
2929
[2c4aa39]2930int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
2931 sysarg_t arg3, async_exch_t *other_exch)
2932{
2933 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
2934 arg1, arg2, arg3, 0, other_exch->phone);
2935}
2936
2937bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
2938 sysarg_t *arg2, sysarg_t *arg3)
2939{
2940 assert(callid);
[57dea62]2941
[2c4aa39]2942 ipc_call_t call;
2943 *callid = async_get_call(&call);
[57dea62]2944
[2c4aa39]2945 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
2946 return false;
2947
2948 if (arg1)
2949 *arg1 = IPC_GET_ARG1(call);
2950 if (arg2)
2951 *arg2 = IPC_GET_ARG2(call);
2952 if (arg3)
2953 *arg3 = IPC_GET_ARG3(call);
[57dea62]2954
[2c4aa39]2955 return true;
2956}
2957
2958int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
2959{
2960 return ipc_answer_1(callid, EOK, other_exch->phone);
2961}
2962
[58cbf8d5]2963/** Lock and get session remote state
2964 *
2965 * Lock and get the local replica of the remote state
2966 * in stateful sessions. The call should be paired
2967 * with async_remote_state_release*().
2968 *
2969 * @param[in] sess Stateful session.
2970 *
2971 * @return Local replica of the remote state.
2972 *
2973 */
2974void *async_remote_state_acquire(async_sess_t *sess)
2975{
2976 fibril_mutex_lock(&sess->remote_state_mtx);
2977 return sess->remote_state_data;
2978}
2979
2980/** Update the session remote state
2981 *
2982 * Update the local replica of the remote state
2983 * in stateful sessions. The remote state must
2984 * be already locked.
2985 *
2986 * @param[in] sess Stateful session.
2987 * @param[in] state New local replica of the remote state.
2988 *
2989 */
2990void async_remote_state_update(async_sess_t *sess, void *state)
2991{
2992 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2993 sess->remote_state_data = state;
2994}
2995
2996/** Release the session remote state
2997 *
2998 * Unlock the local replica of the remote state
2999 * in stateful sessions.
3000 *
3001 * @param[in] sess Stateful session.
3002 *
3003 */
3004void async_remote_state_release(async_sess_t *sess)
3005{
3006 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3007
3008 fibril_mutex_unlock(&sess->remote_state_mtx);
3009}
3010
3011/** Release the session remote state and end an exchange
3012 *
3013 * Unlock the local replica of the remote state
3014 * in stateful sessions. This is convenience function
3015 * which gets the session pointer from the exchange
3016 * and also ends the exchange.
3017 *
3018 * @param[in] exch Stateful session's exchange.
3019 *
3020 */
3021void async_remote_state_release_exchange(async_exch_t *exch)
3022{
3023 if (exch == NULL)
3024 return;
3025
3026 async_sess_t *sess = exch->sess;
3027 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
3028
3029 async_exchange_end(exch);
3030 fibril_mutex_unlock(&sess->remote_state_mtx);
3031}
3032
[a46da63]3033/** @}
[b2951e2]3034 */
Note: See TracBrowser for help on using the repository browser.