source: mainline/uspace/lib/c/generic/async.c@ 2902e1bb

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

rename IPC_M_CONNECT_ME to IPC_M_CLONE_ESTABLISH

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