source: mainline/uspace/lib/c/generic/async.c@ 7f9d97f3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f9d97f3 was 7f9d97f3, checked in by Jakub Jermar <jakub@…>, 10 years ago

Make gettimeofday() return the actual microseconds.

Enhance struct tm to also have a field to hold microseconds and make
sure that this information propagates from the RTC driver.

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