source: mainline/uspace/lib/c/generic/async.c@ 8820544

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

support for kernel notification multiplexing in the async framework

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