source: mainline/uspace/lib/c/generic/async.c@ 45cbcaf4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 45cbcaf4 was 45cbcaf4, checked in by Maurizio Lombardi <m.lombardi85@…>, 13 years ago

avoid deadlocks by replacing gettimeofday() with getuptime() in lib/async.c and lib/fibril_sync.c

  • 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) {
[45cbcaf4]639 getuptime(&conn->wdata.to_event.expires);
[f53cc81]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;
[45cbcaf4]968 getuptime(&tv);
[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;
[45cbcaf4]1025 getuptime(&tv);
[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
[45cbcaf4]1332 getuptime(&msg->wdata.to_event.expires);
[f53cc81]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);
[375e501]1390 if (msg->done) {
[47c9a8c]1391 amsg_destroy(msg);
[375e501]1392 } else {
1393 msg->dataptr = NULL;
[47c9a8c]1394 msg->forget = true;
[375e501]1395 }
[47c9a8c]1396 futex_up(&async_futex);
1397}
[0b99e40]1398
[36c9234]1399/** Wait for specified time.
[44c6d88d]1400 *
[36c9234]1401 * The current fibril is suspended but the thread continues to execute.
1402 *
[c07544d3]1403 * @param timeout Duration of the wait in microseconds.
1404 *
[44c6d88d]1405 */
1406void async_usleep(suseconds_t timeout)
1407{
[47c9a8c]1408 amsg_t *msg = amsg_create();
[44c6d88d]1409 if (!msg)
1410 return;
[6b21292]1411
[bc1f1c2]1412 msg->wdata.fid = fibril_get_id();
[6b21292]1413
[45cbcaf4]1414 getuptime(&msg->wdata.to_event.expires);
[f53cc81]1415 tv_add(&msg->wdata.to_event.expires, timeout);
[6b21292]1416
[44c6d88d]1417 futex_down(&async_futex);
[c07544d3]1418
[b6ee5b1]1419 async_insert_timeout(&msg->wdata);
[c07544d3]1420
[36c9234]1421 /* Leave the async_futex locked when entering this function */
[116d3f6f]1422 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1423
1424 /* Futex is up automatically after fibril_switch() */
1425
[47c9a8c]1426 amsg_destroy(msg);
[44c6d88d]1427}
[da0c91e7]1428
[0cc4313]1429/** Pseudo-synchronous message sending - fast version.
1430 *
1431 * Send message asynchronously and return only after the reply arrives.
1432 *
1433 * This function can only transfer 4 register payload arguments. For
1434 * transferring more arguments, see the slower async_req_slow().
1435 *
[79ae36dd]1436 * @param exch Exchange for sending the message.
1437 * @param imethod Interface and method of the call.
[c07544d3]1438 * @param arg1 Service-defined payload argument.
1439 * @param arg2 Service-defined payload argument.
1440 * @param arg3 Service-defined payload argument.
1441 * @param arg4 Service-defined payload argument.
1442 * @param r1 If non-NULL, storage for the 1st reply argument.
1443 * @param r2 If non-NULL, storage for the 2nd reply argument.
1444 * @param r3 If non-NULL, storage for the 3rd reply argument.
1445 * @param r4 If non-NULL, storage for the 4th reply argument.
1446 * @param r5 If non-NULL, storage for the 5th reply argument.
1447 *
1448 * @return Return code of the reply or a negative error code.
1449 *
[0cc4313]1450 */
[79ae36dd]1451sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1452 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1453 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1454{
[79ae36dd]1455 if (exch == NULL)
1456 return ENOENT;
1457
[0cc4313]1458 ipc_call_t result;
[79ae36dd]1459 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
[0cc4313]1460 &result);
[c07544d3]1461
[96b02eb9]1462 sysarg_t rc;
[79ae36dd]1463 async_wait_for(aid, &rc);
[c07544d3]1464
1465 if (r1)
[0cc4313]1466 *r1 = IPC_GET_ARG1(result);
[c07544d3]1467
[0cc4313]1468 if (r2)
1469 *r2 = IPC_GET_ARG2(result);
[c07544d3]1470
[0cc4313]1471 if (r3)
1472 *r3 = IPC_GET_ARG3(result);
[c07544d3]1473
[0cc4313]1474 if (r4)
1475 *r4 = IPC_GET_ARG4(result);
[c07544d3]1476
[0cc4313]1477 if (r5)
1478 *r5 = IPC_GET_ARG5(result);
[c07544d3]1479
[0cc4313]1480 return rc;
[085bd54]1481}
1482
[0cc4313]1483/** Pseudo-synchronous message sending - slow version.
1484 *
1485 * Send message asynchronously and return only after the reply arrives.
1486 *
[79ae36dd]1487 * @param exch Exchange for sending the message.
1488 * @param imethod Interface and method of the call.
[c07544d3]1489 * @param arg1 Service-defined payload argument.
1490 * @param arg2 Service-defined payload argument.
1491 * @param arg3 Service-defined payload argument.
1492 * @param arg4 Service-defined payload argument.
1493 * @param arg5 Service-defined payload argument.
1494 * @param r1 If non-NULL, storage for the 1st reply argument.
1495 * @param r2 If non-NULL, storage for the 2nd reply argument.
1496 * @param r3 If non-NULL, storage for the 3rd reply argument.
1497 * @param r4 If non-NULL, storage for the 4th reply argument.
1498 * @param r5 If non-NULL, storage for the 5th reply argument.
1499 *
1500 * @return Return code of the reply or a negative error code.
1501 *
[0cc4313]1502 */
[79ae36dd]1503sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
[96b02eb9]1504 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1505 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1506{
[79ae36dd]1507 if (exch == NULL)
1508 return ENOENT;
1509
[0cc4313]1510 ipc_call_t result;
[79ae36dd]1511 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
[0cc4313]1512 &result);
[c07544d3]1513
[96b02eb9]1514 sysarg_t rc;
[79ae36dd]1515 async_wait_for(aid, &rc);
[c07544d3]1516
1517 if (r1)
[0cc4313]1518 *r1 = IPC_GET_ARG1(result);
[c07544d3]1519
[0cc4313]1520 if (r2)
1521 *r2 = IPC_GET_ARG2(result);
[c07544d3]1522
[0cc4313]1523 if (r3)
1524 *r3 = IPC_GET_ARG3(result);
[c07544d3]1525
[0cc4313]1526 if (r4)
1527 *r4 = IPC_GET_ARG4(result);
[c07544d3]1528
[0cc4313]1529 if (r5)
1530 *r5 = IPC_GET_ARG5(result);
[c07544d3]1531
[0cc4313]1532 return rc;
[085bd54]1533}
[b2951e2]1534
[79ae36dd]1535void async_msg_0(async_exch_t *exch, sysarg_t imethod)
[64d2b10]1536{
[79ae36dd]1537 if (exch != NULL)
1538 ipc_call_async_0(exch->phone, imethod, NULL, NULL, true);
[64d2b10]1539}
1540
[79ae36dd]1541void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
[64d2b10]1542{
[79ae36dd]1543 if (exch != NULL)
1544 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL, true);
[64d2b10]1545}
1546
[79ae36dd]1547void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1548 sysarg_t arg2)
[64d2b10]1549{
[79ae36dd]1550 if (exch != NULL)
1551 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL,
1552 true);
[64d2b10]1553}
1554
[79ae36dd]1555void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1556 sysarg_t arg2, sysarg_t arg3)
[64d2b10]1557{
[79ae36dd]1558 if (exch != NULL)
1559 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
1560 NULL, true);
[64d2b10]1561}
1562
[79ae36dd]1563void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1564 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
[64d2b10]1565{
[79ae36dd]1566 if (exch != NULL)
1567 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
1568 NULL, NULL, true);
[64d2b10]1569}
1570
[79ae36dd]1571void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1572 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
[64d2b10]1573{
[79ae36dd]1574 if (exch != NULL)
1575 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
1576 arg5, NULL, NULL, true);
[64d2b10]1577}
1578
1579sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
1580{
1581 return ipc_answer_0(callid, retval);
1582}
1583
1584sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
1585{
1586 return ipc_answer_1(callid, retval, arg1);
1587}
1588
1589sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1590 sysarg_t arg2)
1591{
1592 return ipc_answer_2(callid, retval, arg1, arg2);
1593}
1594
1595sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1596 sysarg_t arg2, sysarg_t arg3)
1597{
1598 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
1599}
1600
1601sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1602 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1603{
1604 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
1605}
1606
1607sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1608 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1609{
1610 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
1611}
1612
[79ae36dd]1613int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
1614 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
[64d2b10]1615{
[79ae36dd]1616 if (exch == NULL)
1617 return ENOENT;
1618
1619 return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
[64d2b10]1620}
1621
[79ae36dd]1622int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
1623 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
1624 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
[64d2b10]1625{
[79ae36dd]1626 if (exch == NULL)
1627 return ENOENT;
1628
1629 return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
1630 arg4, arg5, mode);
[64d2b10]1631}
1632
[007e6efa]1633/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1634 *
1635 * Ask through phone for a new connection to some service.
1636 *
[79ae36dd]1637 * @param exch Exchange for sending the message.
[007e6efa]1638 * @param arg1 User defined argument.
1639 * @param arg2 User defined argument.
1640 * @param arg3 User defined argument.
1641 * @param client_receiver Connection handing routine.
1642 *
[79ae36dd]1643 * @return Zero on success or a negative error code.
[007e6efa]1644 *
1645 */
[79ae36dd]1646int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
[9934f7d]1647 sysarg_t arg3, async_client_conn_t client_receiver, void *carg)
[007e6efa]1648{
[79ae36dd]1649 if (exch == NULL)
1650 return ENOENT;
1651
[007e6efa]1652 sysarg_t phone_hash;
[ab34cc9]1653 sysarg_t rc;
1654
1655 aid_t req;
1656 ipc_call_t answer;
1657 req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1658 &answer);
1659 async_wait_for(req, &rc);
[007e6efa]1660 if (rc != EOK)
[ab34cc9]1661 return (int) rc;
1662
1663 phone_hash = IPC_GET_ARG5(answer);
[36b16bc]1664
[007e6efa]1665 if (client_receiver != NULL)
[ab34cc9]1666 async_new_connection(answer.in_task_id, phone_hash, 0, NULL,
[9934f7d]1667 client_receiver, carg);
[007e6efa]1668
1669 return EOK;
1670}
1671
[6aae539d]1672/** Wrapper for making IPC_M_CLONE_ESTABLISH calls using the async framework.
[007e6efa]1673 *
[6aae539d]1674 * Ask for a cloned connection to some service.
[f74392f]1675 *
[79ae36dd]1676 * @param mgmt Exchange management style.
1677 * @param exch Exchange for sending the message.
[007e6efa]1678 *
[79ae36dd]1679 * @return New session on success or NULL on error.
[f74392f]1680 *
1681 */
[6aae539d]1682async_sess_t *async_clone_establish(exch_mgmt_t mgmt, async_exch_t *exch)
[79ae36dd]1683{
1684 if (exch == NULL) {
1685 errno = ENOENT;
1686 return NULL;
1687 }
1688
1689 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1690 if (sess == NULL) {
1691 errno = ENOMEM;
1692 return NULL;
1693 }
1694
1695 ipc_call_t result;
1696
[47c9a8c]1697 amsg_t *msg = amsg_create();
1698 if (!msg) {
[79ae36dd]1699 free(sess);
1700 errno = ENOMEM;
1701 return NULL;
1702 }
1703
1704 msg->dataptr = &result;
1705 msg->wdata.active = true;
1706
[6aae539d]1707 ipc_call_async_0(exch->phone, IPC_M_CLONE_ESTABLISH, msg,
[79ae36dd]1708 reply_received, true);
1709
1710 sysarg_t rc;
1711 async_wait_for((aid_t) msg, &rc);
1712
1713 if (rc != EOK) {
1714 errno = rc;
1715 free(sess);
1716 return NULL;
1717 }
1718
1719 int phone = (int) IPC_GET_ARG5(result);
1720
1721 if (phone < 0) {
1722 errno = phone;
1723 free(sess);
1724 return NULL;
1725 }
1726
1727 sess->mgmt = mgmt;
1728 sess->phone = phone;
1729 sess->arg1 = 0;
1730 sess->arg2 = 0;
1731 sess->arg3 = 0;
1732
[58cbf8d5]1733 fibril_mutex_initialize(&sess->remote_state_mtx);
1734 sess->remote_state_data = NULL;
1735
[79ae36dd]1736 list_initialize(&sess->exch_list);
1737 fibril_mutex_initialize(&sess->mutex);
1738 atomic_set(&sess->refcnt, 0);
1739
1740 return sess;
1741}
1742
1743static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
1744 sysarg_t arg3, sysarg_t arg4)
[f74392f]1745{
[79ae36dd]1746 ipc_call_t result;
1747
[47c9a8c]1748 amsg_t *msg = amsg_create();
1749 if (!msg)
[79ae36dd]1750 return ENOENT;
1751
1752 msg->dataptr = &result;
1753 msg->wdata.active = true;
1754
1755 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
1756 msg, reply_received, true);
1757
1758 sysarg_t rc;
1759 async_wait_for((aid_t) msg, &rc);
[f74392f]1760
[007e6efa]1761 if (rc != EOK)
[f74392f]1762 return rc;
[007e6efa]1763
[79ae36dd]1764 return (int) IPC_GET_ARG5(result);
1765}
1766
1767/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1768 *
1769 * Ask through for a new connection to some service.
1770 *
1771 * @param mgmt Exchange management style.
1772 * @param exch Exchange for sending the message.
1773 * @param arg1 User defined argument.
1774 * @param arg2 User defined argument.
1775 * @param arg3 User defined argument.
1776 *
1777 * @return New session on success or NULL on error.
1778 *
1779 */
1780async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
1781 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1782{
1783 if (exch == NULL) {
1784 errno = ENOENT;
1785 return NULL;
1786 }
1787
1788 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1789 if (sess == NULL) {
1790 errno = ENOMEM;
1791 return NULL;
1792 }
1793
1794 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1795 0);
1796
1797 if (phone < 0) {
1798 errno = phone;
1799 free(sess);
1800 return NULL;
1801 }
1802
1803 sess->mgmt = mgmt;
1804 sess->phone = phone;
1805 sess->arg1 = arg1;
1806 sess->arg2 = arg2;
1807 sess->arg3 = arg3;
1808
[58cbf8d5]1809 fibril_mutex_initialize(&sess->remote_state_mtx);
1810 sess->remote_state_data = NULL;
1811
[79ae36dd]1812 list_initialize(&sess->exch_list);
1813 fibril_mutex_initialize(&sess->mutex);
1814 atomic_set(&sess->refcnt, 0);
1815
1816 return sess;
[f74392f]1817}
1818
[93ad49a8]1819/** Set arguments for new connections.
[0f4532e]1820 *
1821 * FIXME This is an ugly hack to work around the problem that parallel
1822 * exchanges are implemented using parallel connections. When we create
[93ad49a8]1823 * a callback session, the framework does not know arguments for the new
1824 * connections.
[0f4532e]1825 *
1826 * The proper solution seems to be to implement parallel exchanges using
1827 * tagging.
1828 */
[93ad49a8]1829void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
1830 sysarg_t arg3)
[0f4532e]1831{
[93ad49a8]1832 sess->arg1 = arg1;
1833 sess->arg2 = arg2;
1834 sess->arg3 = arg3;
[0f4532e]1835}
1836
[f74392f]1837/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
[007e6efa]1838 *
[f74392f]1839 * Ask through phone for a new connection to some service and block until
1840 * success.
1841 *
[79ae36dd]1842 * @param mgmt Exchange management style.
1843 * @param exch Exchange for sending the message.
1844 * @param arg1 User defined argument.
1845 * @param arg2 User defined argument.
1846 * @param arg3 User defined argument.
[007e6efa]1847 *
[79ae36dd]1848 * @return New session on success or NULL on error.
[f74392f]1849 *
1850 */
[79ae36dd]1851async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
1852 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
[f74392f]1853{
[79ae36dd]1854 if (exch == NULL) {
1855 errno = ENOENT;
1856 return NULL;
1857 }
[f74392f]1858
[79ae36dd]1859 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1860 if (sess == NULL) {
1861 errno = ENOMEM;
1862 return NULL;
1863 }
[007e6efa]1864
[79ae36dd]1865 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1866 IPC_FLAG_BLOCKING);
1867
1868 if (phone < 0) {
1869 errno = phone;
1870 free(sess);
1871 return NULL;
1872 }
1873
1874 sess->mgmt = mgmt;
1875 sess->phone = phone;
1876 sess->arg1 = arg1;
1877 sess->arg2 = arg2;
1878 sess->arg3 = arg3;
1879
[58cbf8d5]1880 fibril_mutex_initialize(&sess->remote_state_mtx);
1881 sess->remote_state_data = NULL;
1882
[79ae36dd]1883 list_initialize(&sess->exch_list);
1884 fibril_mutex_initialize(&sess->mutex);
1885 atomic_set(&sess->refcnt, 0);
1886
1887 return sess;
[f74392f]1888}
1889
[64d2b10]1890/** Connect to a task specified by id.
1891 *
1892 */
[79ae36dd]1893async_sess_t *async_connect_kbox(task_id_t id)
[64d2b10]1894{
[79ae36dd]1895 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1896 if (sess == NULL) {
1897 errno = ENOMEM;
1898 return NULL;
1899 }
1900
1901 int phone = ipc_connect_kbox(id);
1902 if (phone < 0) {
1903 errno = phone;
1904 free(sess);
1905 return NULL;
1906 }
1907
1908 sess->mgmt = EXCHANGE_ATOMIC;
1909 sess->phone = phone;
1910 sess->arg1 = 0;
1911 sess->arg2 = 0;
1912 sess->arg3 = 0;
1913
[58cbf8d5]1914 fibril_mutex_initialize(&sess->remote_state_mtx);
1915 sess->remote_state_data = NULL;
1916
[79ae36dd]1917 list_initialize(&sess->exch_list);
1918 fibril_mutex_initialize(&sess->mutex);
1919 atomic_set(&sess->refcnt, 0);
1920
1921 return sess;
1922}
1923
1924static int async_hangup_internal(int phone)
1925{
1926 return ipc_hangup(phone);
[64d2b10]1927}
1928
1929/** Wrapper for ipc_hangup.
1930 *
[79ae36dd]1931 * @param sess Session to hung up.
[64d2b10]1932 *
1933 * @return Zero on success or a negative error code.
1934 *
1935 */
[79ae36dd]1936int async_hangup(async_sess_t *sess)
[64d2b10]1937{
[36e2b55]1938 async_exch_t *exch;
1939
[79ae36dd]1940 assert(sess);
1941
1942 if (atomic_get(&sess->refcnt) > 0)
1943 return EBUSY;
1944
[36e2b55]1945 fibril_mutex_lock(&async_sess_mutex);
[972c60ce]1946
[cff3fb6]1947 int rc = async_hangup_internal(sess->phone);
[36e2b55]1948
1949 while (!list_empty(&sess->exch_list)) {
1950 exch = (async_exch_t *)
1951 list_get_instance(list_first(&sess->exch_list),
1952 async_exch_t, sess_link);
1953
1954 list_remove(&exch->sess_link);
1955 list_remove(&exch->global_link);
1956 async_hangup_internal(exch->phone);
1957 free(exch);
1958 }
[4c50c8d]1959
1960 free(sess);
[36e2b55]1961
1962 fibril_mutex_unlock(&async_sess_mutex);
1963
[79ae36dd]1964 return rc;
[64d2b10]1965}
1966
1967/** Interrupt one thread of this task from waiting for IPC. */
1968void async_poke(void)
1969{
1970 ipc_poke();
1971}
1972
[79ae36dd]1973/** Start new exchange in a session.
1974 *
1975 * @param session Session.
1976 *
1977 * @return New exchange or NULL on error.
1978 *
1979 */
1980async_exch_t *async_exchange_begin(async_sess_t *sess)
1981{
1982 if (sess == NULL)
1983 return NULL;
1984
1985 async_exch_t *exch;
1986
1987 fibril_mutex_lock(&async_sess_mutex);
1988
1989 if (!list_empty(&sess->exch_list)) {
1990 /*
1991 * There are inactive exchanges in the session.
1992 */
1993 exch = (async_exch_t *)
[b72efe8]1994 list_get_instance(list_first(&sess->exch_list),
1995 async_exch_t, sess_link);
1996
[79ae36dd]1997 list_remove(&exch->sess_link);
1998 list_remove(&exch->global_link);
1999 } else {
2000 /*
2001 * There are no available exchanges in the session.
2002 */
2003
2004 if ((sess->mgmt == EXCHANGE_ATOMIC) ||
2005 (sess->mgmt == EXCHANGE_SERIALIZE)) {
2006 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2007 if (exch != NULL) {
[b72efe8]2008 link_initialize(&exch->sess_link);
2009 link_initialize(&exch->global_link);
[79ae36dd]2010 exch->sess = sess;
2011 exch->phone = sess->phone;
2012 }
2013 } else { /* EXCHANGE_PARALLEL */
2014 /*
2015 * Make a one-time attempt to connect a new data phone.
2016 */
2017
2018 int phone;
2019
2020retry:
2021 phone = async_connect_me_to_internal(sess->phone, sess->arg1,
2022 sess->arg2, sess->arg3, 0);
2023 if (phone >= 0) {
2024 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
2025 if (exch != NULL) {
[b72efe8]2026 link_initialize(&exch->sess_link);
2027 link_initialize(&exch->global_link);
[79ae36dd]2028 exch->sess = sess;
2029 exch->phone = phone;
2030 } else
2031 async_hangup_internal(phone);
2032 } else if (!list_empty(&inactive_exch_list)) {
2033 /*
2034 * We did not manage to connect a new phone. But we
2035 * can try to close some of the currently inactive
2036 * connections in other sessions and try again.
2037 */
2038 exch = (async_exch_t *)
[b72efe8]2039 list_get_instance(list_first(&inactive_exch_list),
2040 async_exch_t, global_link);
2041
[79ae36dd]2042 list_remove(&exch->sess_link);
2043 list_remove(&exch->global_link);
2044 async_hangup_internal(exch->phone);
2045 free(exch);
2046 goto retry;
2047 } else {
2048 /*
2049 * Wait for a phone to become available.
2050 */
2051 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
2052 goto retry;
2053 }
2054 }
2055 }
2056
2057 fibril_mutex_unlock(&async_sess_mutex);
2058
2059 if (exch != NULL) {
2060 atomic_inc(&sess->refcnt);
2061
2062 if (sess->mgmt == EXCHANGE_SERIALIZE)
2063 fibril_mutex_lock(&sess->mutex);
2064 }
2065
2066 return exch;
2067}
2068
2069/** Finish an exchange.
2070 *
2071 * @param exch Exchange to finish.
2072 *
2073 */
2074void async_exchange_end(async_exch_t *exch)
2075{
2076 if (exch == NULL)
2077 return;
2078
2079 async_sess_t *sess = exch->sess;
2080
[1c6436a]2081 atomic_dec(&sess->refcnt);
2082
[79ae36dd]2083 if (sess->mgmt == EXCHANGE_SERIALIZE)
2084 fibril_mutex_unlock(&sess->mutex);
2085
2086 fibril_mutex_lock(&async_sess_mutex);
2087
2088 list_append(&exch->sess_link, &sess->exch_list);
2089 list_append(&exch->global_link, &inactive_exch_list);
2090 fibril_condvar_signal(&avail_phone_cv);
2091
2092 fibril_mutex_unlock(&async_sess_mutex);
2093}
2094
[47b7006]2095/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
2096 *
[79ae36dd]2097 * @param exch Exchange for sending the message.
2098 * @param size Size of the destination address space area.
2099 * @param arg User defined argument.
2100 * @param flags Storage for the received flags. Can be NULL.
[fbcdeb8]2101 * @param dst Destination address space area base. Cannot be NULL.
[0da4e41]2102 *
[47b7006]2103 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2104 *
2105 */
[fbcdeb8]2106int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
2107 unsigned int *flags, void **dst)
[0da4e41]2108{
[79ae36dd]2109 if (exch == NULL)
2110 return ENOENT;
2111
[fbcdeb8]2112 sysarg_t _flags = 0;
2113 sysarg_t _dst = (sysarg_t) -1;
2114 int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
2115 arg, NULL, &_flags, NULL, &_dst);
[47b7006]2116
[0da4e41]2117 if (flags)
[fbcdeb8]2118 *flags = (unsigned int) _flags;
[47b7006]2119
[fbcdeb8]2120 *dst = (void *) _dst;
[0da4e41]2121 return res;
2122}
2123
2124/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
2125 *
[47b7006]2126 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
2127 * calls so that the user doesn't have to remember the meaning of each IPC
2128 * argument.
[0da4e41]2129 *
2130 * So far, this wrapper is to be used from within a connection fibril.
2131 *
[47b7006]2132 * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
2133 * @param size Destination address space area size.
2134 *
2135 * @return True on success, false on failure.
[0da4e41]2136 *
2137 */
[47b7006]2138bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
[0da4e41]2139{
2140 assert(callid);
2141 assert(size);
[47b7006]2142
2143 ipc_call_t data;
[0da4e41]2144 *callid = async_get_call(&data);
[47b7006]2145
[228e490]2146 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
[47b7006]2147 return false;
2148
[fbcdeb8]2149 *size = (size_t) IPC_GET_ARG1(data);
[47b7006]2150 return true;
[0da4e41]2151}
2152
2153/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
2154 *
[fbcdeb8]2155 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
[47b7006]2156 * calls so that the user doesn't have to remember the meaning of each IPC
2157 * argument.
[0da4e41]2158 *
[47b7006]2159 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2160 * @param src Source address space base.
2161 * @param flags Flags to be used for sharing. Bits can be only cleared.
2162 *
2163 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2164 *
2165 */
[47b7006]2166int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
[0da4e41]2167{
2168 return ipc_share_in_finalize(callid, src, flags);
2169}
2170
[47b7006]2171/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
[0da4e41]2172 *
[79ae36dd]2173 * @param exch Exchange for sending the message.
2174 * @param src Source address space area base address.
2175 * @param flags Flags to be used for sharing. Bits can be only cleared.
[47b7006]2176 *
2177 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2178 *
2179 */
[79ae36dd]2180int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
[0da4e41]2181{
[79ae36dd]2182 if (exch == NULL)
2183 return ENOENT;
2184
2185 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
[96b02eb9]2186 (sysarg_t) flags);
[0da4e41]2187}
2188
2189/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
2190 *
[47b7006]2191 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
2192 * calls so that the user doesn't have to remember the meaning of each IPC
2193 * argument.
[0da4e41]2194 *
2195 * So far, this wrapper is to be used from within a connection fibril.
2196 *
[47b7006]2197 * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
2198 * @param size Storage for the source address space area size.
2199 * @param flags Storage for the sharing flags.
2200 *
2201 * @return True on success, false on failure.
[0da4e41]2202 *
2203 */
[47b7006]2204bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
[0da4e41]2205{
2206 assert(callid);
2207 assert(size);
2208 assert(flags);
[47b7006]2209
2210 ipc_call_t data;
[0da4e41]2211 *callid = async_get_call(&data);
[47b7006]2212
[228e490]2213 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
[47b7006]2214 return false;
2215
[0da4e41]2216 *size = (size_t) IPC_GET_ARG2(data);
[47b7006]2217 *flags = (unsigned int) IPC_GET_ARG3(data);
2218 return true;
[0da4e41]2219}
2220
2221/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2222 *
[47b7006]2223 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2224 * calls so that the user doesn't have to remember the meaning of each IPC
2225 * argument.
[0da4e41]2226 *
[47b7006]2227 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2228 * @param dst Destination address space area base address.
2229 *
2230 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2231 *
2232 */
[fbcdeb8]2233int async_share_out_finalize(ipc_callid_t callid, void **dst)
[0da4e41]2234{
2235 return ipc_share_out_finalize(callid, dst);
2236}
2237
[8bf1eeb]2238/** Start IPC_M_DATA_READ using the async framework.
2239 *
[79ae36dd]2240 * @param exch Exchange for sending the message.
2241 * @param dst Address of the beginning of the destination buffer.
2242 * @param size Size of the destination buffer (in bytes).
[8bf1eeb]2243 * @param dataptr Storage of call data (arg 2 holds actual data size).
[79ae36dd]2244 *
[8bf1eeb]2245 * @return Hash of the sent message or 0 on error.
[79ae36dd]2246 *
[8bf1eeb]2247 */
[79ae36dd]2248aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2249 ipc_call_t *dataptr)
[8bf1eeb]2250{
[79ae36dd]2251 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
[8bf1eeb]2252 (sysarg_t) size, dataptr);
2253}
2254
[47b7006]2255/** Wrapper for IPC_M_DATA_READ calls using the async framework.
[0da4e41]2256 *
[79ae36dd]2257 * @param exch Exchange for sending the message.
2258 * @param dst Address of the beginning of the destination buffer.
2259 * @param size Size of the destination buffer.
[47b7006]2260 *
2261 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2262 *
2263 */
[79ae36dd]2264int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
[0da4e41]2265{
[79ae36dd]2266 if (exch == NULL)
2267 return ENOENT;
2268
2269 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2270 (sysarg_t) size);
[0da4e41]2271}
2272
2273/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2274 *
[47b7006]2275 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2276 * calls so that the user doesn't have to remember the meaning of each IPC
2277 * argument.
[0da4e41]2278 *
2279 * So far, this wrapper is to be used from within a connection fibril.
2280 *
[47b7006]2281 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2282 * @param size Storage for the maximum size. Can be NULL.
2283 *
2284 * @return True on success, false on failure.
[0da4e41]2285 *
2286 */
[47b7006]2287bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
[0da4e41]2288{
2289 assert(callid);
[47b7006]2290
2291 ipc_call_t data;
[0da4e41]2292 *callid = async_get_call(&data);
[47b7006]2293
[228e490]2294 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
[47b7006]2295 return false;
2296
[0da4e41]2297 if (size)
2298 *size = (size_t) IPC_GET_ARG2(data);
[47b7006]2299
2300 return true;
[0da4e41]2301}
2302
2303/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2304 *
[47b7006]2305 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2306 * calls so that the user doesn't have to remember the meaning of each IPC
2307 * argument.
[0da4e41]2308 *
[47b7006]2309 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2310 * @param src Source address for the IPC_M_DATA_READ call.
2311 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2312 * the maximum size announced by the sender.
2313 *
2314 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2315 *
2316 */
2317int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
2318{
2319 return ipc_data_read_finalize(callid, src, size);
2320}
2321
[b4cbef1]2322/** Wrapper for forwarding any read request
2323 *
2324 */
[79ae36dd]2325int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2326 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2327 ipc_call_t *dataptr)
[b4cbef1]2328{
[79ae36dd]2329 if (exch == NULL)
2330 return ENOENT;
2331
[b4cbef1]2332 ipc_callid_t callid;
2333 if (!async_data_read_receive(&callid, NULL)) {
2334 ipc_answer_0(callid, EINVAL);
2335 return EINVAL;
2336 }
2337
[79ae36dd]2338 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]2339 dataptr);
2340 if (msg == 0) {
2341 ipc_answer_0(callid, EINVAL);
2342 return EINVAL;
2343 }
2344
[79ae36dd]2345 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
[b4cbef1]2346 IPC_FF_ROUTE_FROM_ME);
2347 if (retval != EOK) {
[ab9f443]2348 async_forget(msg);
[b4cbef1]2349 ipc_answer_0(callid, retval);
2350 return retval;
2351 }
2352
[96b02eb9]2353 sysarg_t rc;
[b4cbef1]2354 async_wait_for(msg, &rc);
2355
2356 return (int) rc;
2357}
2358
[47b7006]2359/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
[0da4e41]2360 *
[79ae36dd]2361 * @param exch Exchange for sending the message.
2362 * @param src Address of the beginning of the source buffer.
2363 * @param size Size of the source buffer.
[b4cbef1]2364 *
2365 * @return Zero on success or a negative error code from errno.h.
[0da4e41]2366 *
2367 */
[79ae36dd]2368int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
[0da4e41]2369{
[79ae36dd]2370 if (exch == NULL)
2371 return ENOENT;
2372
2373 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2374 (sysarg_t) size);
[0da4e41]2375}
2376
2377/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2378 *
[47b7006]2379 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2380 * calls so that the user doesn't have to remember the meaning of each IPC
2381 * argument.
[0da4e41]2382 *
2383 * So far, this wrapper is to be used from within a connection fibril.
2384 *
[47b7006]2385 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2386 * @param size Storage for the suggested size. May be NULL.
[b4cbef1]2387 *
[47b7006]2388 * @return True on success, false on failure.
[0da4e41]2389 *
2390 */
[47b7006]2391bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
[0da4e41]2392{
2393 assert(callid);
[b4cbef1]2394
[47b7006]2395 ipc_call_t data;
[0da4e41]2396 *callid = async_get_call(&data);
[47b7006]2397
[228e490]2398 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
[47b7006]2399 return false;
[b4cbef1]2400
[0da4e41]2401 if (size)
2402 *size = (size_t) IPC_GET_ARG2(data);
[b4cbef1]2403
[47b7006]2404 return true;
[0da4e41]2405}
2406
2407/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2408 *
[47b7006]2409 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2410 * calls so that the user doesn't have to remember the meaning of each IPC
2411 * argument.
[0da4e41]2412 *
[b4cbef1]2413 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2414 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2415 * @param size Final size for the IPC_M_DATA_WRITE call.
2416 *
2417 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]2418 *
2419 */
2420int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
2421{
2422 return ipc_data_write_finalize(callid, dst, size);
2423}
2424
[eda925a]2425/** Wrapper for receiving binary data or strings
[8aa42e3]2426 *
2427 * This wrapper only makes it more comfortable to use async_data_write_*
[eda925a]2428 * functions to receive binary data or strings.
[8aa42e3]2429 *
[472c09d]2430 * @param data Pointer to data pointer (which should be later disposed
2431 * by free()). If the operation fails, the pointer is not
2432 * touched.
[eda925a]2433 * @param nullterm If true then the received data is always zero terminated.
2434 * This also causes to allocate one extra byte beyond the
2435 * raw transmitted data.
[b4cbef1]2436 * @param min_size Minimum size (in bytes) of the data to receive.
[472c09d]2437 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2438 * no limit.
[eda925a]2439 * @param granulariy If non-zero then the size of the received data has to
[472c09d]2440 * be divisible by this value.
2441 * @param received If not NULL, the size of the received data is stored here.
[8aa42e3]2442 *
2443 * @return Zero on success or a value from @ref errno.h on failure.
2444 *
2445 */
[eda925a]2446int async_data_write_accept(void **data, const bool nullterm,
2447 const size_t min_size, const size_t max_size, const size_t granularity,
2448 size_t *received)
[8aa42e3]2449{
[79ae36dd]2450 assert(data);
2451
[8aa42e3]2452 ipc_callid_t callid;
2453 size_t size;
2454 if (!async_data_write_receive(&callid, &size)) {
2455 ipc_answer_0(callid, EINVAL);
2456 return EINVAL;
2457 }
2458
[b4cbef1]2459 if (size < min_size) {
2460 ipc_answer_0(callid, EINVAL);
2461 return EINVAL;
2462 }
2463
[8aa42e3]2464 if ((max_size > 0) && (size > max_size)) {
2465 ipc_answer_0(callid, EINVAL);
2466 return EINVAL;
2467 }
2468
[472c09d]2469 if ((granularity > 0) && ((size % granularity) != 0)) {
2470 ipc_answer_0(callid, EINVAL);
2471 return EINVAL;
2472 }
2473
[eda925a]2474 void *_data;
2475
2476 if (nullterm)
2477 _data = malloc(size + 1);
2478 else
2479 _data = malloc(size);
2480
[472c09d]2481 if (_data == NULL) {
[8aa42e3]2482 ipc_answer_0(callid, ENOMEM);
2483 return ENOMEM;
2484 }
2485
[472c09d]2486 int rc = async_data_write_finalize(callid, _data, size);
[8aa42e3]2487 if (rc != EOK) {
[472c09d]2488 free(_data);
[8aa42e3]2489 return rc;
2490 }
2491
[eda925a]2492 if (nullterm)
2493 ((char *) _data)[size] = 0;
[8aa42e3]2494
[eda925a]2495 *data = _data;
[472c09d]2496 if (received != NULL)
2497 *received = size;
2498
[8aa42e3]2499 return EOK;
2500}
2501
[b4cbef1]2502/** Wrapper for voiding any data that is about to be received
2503 *
2504 * This wrapper can be used to void any pending data
2505 *
2506 * @param retval Error value from @ref errno.h to be returned to the caller.
2507 *
2508 */
[47b7006]2509void async_data_write_void(sysarg_t retval)
[b4cbef1]2510{
2511 ipc_callid_t callid;
2512 async_data_write_receive(&callid, NULL);
2513 ipc_answer_0(callid, retval);
2514}
2515
2516/** Wrapper for forwarding any data that is about to be received
2517 *
2518 */
[79ae36dd]2519int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
2520 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2521 ipc_call_t *dataptr)
[b4cbef1]2522{
[79ae36dd]2523 if (exch == NULL)
2524 return ENOENT;
2525
[b4cbef1]2526 ipc_callid_t callid;
2527 if (!async_data_write_receive(&callid, NULL)) {
2528 ipc_answer_0(callid, EINVAL);
2529 return EINVAL;
2530 }
2531
[79ae36dd]2532 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
[b4cbef1]2533 dataptr);
2534 if (msg == 0) {
2535 ipc_answer_0(callid, EINVAL);
2536 return EINVAL;
2537 }
2538
[79ae36dd]2539 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
[b4cbef1]2540 IPC_FF_ROUTE_FROM_ME);
2541 if (retval != EOK) {
[ab9f443]2542 async_forget(msg);
[b4cbef1]2543 ipc_answer_0(callid, retval);
2544 return retval;
2545 }
2546
[96b02eb9]2547 sysarg_t rc;
[b4cbef1]2548 async_wait_for(msg, &rc);
2549
2550 return (int) rc;
2551}
2552
[79ae36dd]2553/** Wrapper for sending an exchange over different exchange for cloning
2554 *
2555 * @param exch Exchange to be used for sending.
2556 * @param clone_exch Exchange to be cloned.
2557 *
2558 */
2559int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
2560{
2561 return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
2562}
2563
2564/** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
2565 *
2566 * If the current call is IPC_M_CONNECTION_CLONE then a new
2567 * async session is created for the accepted phone.
2568 *
2569 * @param mgmt Exchange management style.
2570 *
2571 * @return New async session or NULL on failure.
2572 *
2573 */
2574async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
2575{
2576 /* Accept the phone */
2577 ipc_call_t call;
2578 ipc_callid_t callid = async_get_call(&call);
2579 int phone = (int) IPC_GET_ARG1(call);
2580
2581 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
2582 (phone < 0)) {
2583 async_answer_0(callid, EINVAL);
2584 return NULL;
2585 }
2586
2587 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2588 if (sess == NULL) {
2589 async_answer_0(callid, ENOMEM);
2590 return NULL;
2591 }
2592
2593 sess->mgmt = mgmt;
2594 sess->phone = phone;
2595 sess->arg1 = 0;
2596 sess->arg2 = 0;
2597 sess->arg3 = 0;
2598
[58cbf8d5]2599 fibril_mutex_initialize(&sess->remote_state_mtx);
2600 sess->remote_state_data = NULL;
2601
[79ae36dd]2602 list_initialize(&sess->exch_list);
2603 fibril_mutex_initialize(&sess->mutex);
2604 atomic_set(&sess->refcnt, 0);
2605
2606 /* Acknowledge the cloned phone */
2607 async_answer_0(callid, EOK);
2608
2609 return sess;
2610}
2611
2612/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2613 *
2614 * If the current call is IPC_M_CONNECT_TO_ME then a new
2615 * async session is created for the accepted phone.
2616 *
2617 * @param mgmt Exchange management style.
2618 *
[8869f7b]2619 * @return New async session.
2620 * @return NULL on failure.
[79ae36dd]2621 *
2622 */
2623async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
2624{
2625 /* Accept the phone */
2626 ipc_call_t call;
2627 ipc_callid_t callid = async_get_call(&call);
2628 int phone = (int) IPC_GET_ARG5(call);
2629
2630 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
2631 (phone < 0)) {
2632 async_answer_0(callid, EINVAL);
2633 return NULL;
2634 }
2635
2636 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2637 if (sess == NULL) {
2638 async_answer_0(callid, ENOMEM);
2639 return NULL;
2640 }
2641
2642 sess->mgmt = mgmt;
2643 sess->phone = phone;
2644 sess->arg1 = 0;
2645 sess->arg2 = 0;
2646 sess->arg3 = 0;
2647
[58cbf8d5]2648 fibril_mutex_initialize(&sess->remote_state_mtx);
2649 sess->remote_state_data = NULL;
2650
[79ae36dd]2651 list_initialize(&sess->exch_list);
2652 fibril_mutex_initialize(&sess->mutex);
2653 atomic_set(&sess->refcnt, 0);
2654
2655 /* Acknowledge the connected phone */
2656 async_answer_0(callid, EOK);
2657
2658 return sess;
2659}
2660
[8869f7b]2661/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2662 *
2663 * If the call is IPC_M_CONNECT_TO_ME then a new
2664 * async session is created. However, the phone is
2665 * not accepted automatically.
2666 *
2667 * @param mgmt Exchange management style.
2668 * @param call Call data.
2669 *
2670 * @return New async session.
2671 * @return NULL on failure.
2672 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
2673 *
2674 */
2675async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
2676{
2677 int phone = (int) IPC_GET_ARG5(*call);
2678
2679 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
2680 (phone < 0))
2681 return NULL;
2682
2683 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2684 if (sess == NULL)
2685 return NULL;
2686
2687 sess->mgmt = mgmt;
2688 sess->phone = phone;
2689 sess->arg1 = 0;
2690 sess->arg2 = 0;
2691 sess->arg3 = 0;
2692
[58cbf8d5]2693 fibril_mutex_initialize(&sess->remote_state_mtx);
2694 sess->remote_state_data = NULL;
2695
[8869f7b]2696 list_initialize(&sess->exch_list);
2697 fibril_mutex_initialize(&sess->mutex);
2698 atomic_set(&sess->refcnt, 0);
2699
2700 return sess;
2701}
2702
[2c4aa39]2703int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
2704 sysarg_t arg3, async_exch_t *other_exch)
2705{
2706 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
2707 arg1, arg2, arg3, 0, other_exch->phone);
2708}
2709
2710bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
2711 sysarg_t *arg2, sysarg_t *arg3)
2712{
2713 assert(callid);
2714
2715 ipc_call_t call;
2716 *callid = async_get_call(&call);
2717
2718 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
2719 return false;
2720
2721 if (arg1)
2722 *arg1 = IPC_GET_ARG1(call);
2723 if (arg2)
2724 *arg2 = IPC_GET_ARG2(call);
2725 if (arg3)
2726 *arg3 = IPC_GET_ARG3(call);
2727
2728 return true;
2729}
2730
2731int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
2732{
2733 return ipc_answer_1(callid, EOK, other_exch->phone);
2734}
2735
[58cbf8d5]2736/** Lock and get session remote state
2737 *
2738 * Lock and get the local replica of the remote state
2739 * in stateful sessions. The call should be paired
2740 * with async_remote_state_release*().
2741 *
2742 * @param[in] sess Stateful session.
2743 *
2744 * @return Local replica of the remote state.
2745 *
2746 */
2747void *async_remote_state_acquire(async_sess_t *sess)
2748{
2749 fibril_mutex_lock(&sess->remote_state_mtx);
2750 return sess->remote_state_data;
2751}
2752
2753/** Update the session remote state
2754 *
2755 * Update the local replica of the remote state
2756 * in stateful sessions. The remote state must
2757 * be already locked.
2758 *
2759 * @param[in] sess Stateful session.
2760 * @param[in] state New local replica of the remote state.
2761 *
2762 */
2763void async_remote_state_update(async_sess_t *sess, void *state)
2764{
2765 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2766 sess->remote_state_data = state;
2767}
2768
2769/** Release the session remote state
2770 *
2771 * Unlock the local replica of the remote state
2772 * in stateful sessions.
2773 *
2774 * @param[in] sess Stateful session.
2775 *
2776 */
2777void async_remote_state_release(async_sess_t *sess)
2778{
2779 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2780
2781 fibril_mutex_unlock(&sess->remote_state_mtx);
2782}
2783
2784/** Release the session remote state and end an exchange
2785 *
2786 * Unlock the local replica of the remote state
2787 * in stateful sessions. This is convenience function
2788 * which gets the session pointer from the exchange
2789 * and also ends the exchange.
2790 *
2791 * @param[in] exch Stateful session's exchange.
2792 *
2793 */
2794void async_remote_state_release_exchange(async_exch_t *exch)
2795{
2796 if (exch == NULL)
2797 return;
2798
2799 async_sess_t *sess = exch->sess;
2800 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2801
2802 async_exchange_end(exch);
2803 fibril_mutex_unlock(&sess->remote_state_mtx);
2804}
2805
[a46da63]2806/** @}
[b2951e2]2807 */
Note: See TracBrowser for help on using the repository browser.