source: mainline/uspace/lib/c/generic/async.c@ 4db1fbf

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

make the libc private header really private (i.e. do not allow to be included from outside the library easily)

  • Property mode set to 100644
File size: 44.8 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 *
[9591265]42 * You should be able to write very simple multithreaded programs, the async
43 * framework will automatically take care of most synchronization problems.
[80649a91]44 *
45 * Default semantics:
[c07544d3]46 * - async_send_*(): Send asynchronously. If the kernel refuses to send
47 * more messages, [ try to get responses from kernel, if
48 * nothing found, might try synchronous ]
[80649a91]49 *
[9591265]50 * Example of use (pseudo C):
[c07544d3]51 *
[80649a91]52 * 1) Multithreaded client application
[9591265]53 *
[c07544d3]54 * fibril_create(fibril1, ...);
55 * fibril_create(fibril2, ...);
56 * ...
57 *
58 * int fibril1(void *arg)
59 * {
[007e6efa]60 * conn = async_connect_me_to();
[c07544d3]61 * c1 = async_send(conn);
62 * c2 = async_send(conn);
63 * async_wait_for(c1);
64 * async_wait_for(c2);
65 * ...
66 * }
[80649a91]67 *
68 *
69 * 2) Multithreaded server application
70 *
[c07544d3]71 * main()
72 * {
73 * async_manager();
74 * }
75 *
76 * my_client_connection(icallid, *icall)
77 * {
78 * if (want_refuse) {
79 * ipc_answer_0(icallid, ELIMIT);
80 * return;
81 * }
82 * ipc_answer_0(icallid, EOK);
[80649a91]83 *
[c07544d3]84 * callid = async_get_call(&call);
[0772aff]85 * somehow_handle_the_call(callid, call);
[c07544d3]86 * ipc_answer_2(callid, 1, 2, 3);
[53ca318]87 *
[c07544d3]88 * callid = async_get_call(&call);
89 * ...
90 * }
[a2cd194]91 *
[80649a91]92 */
[9591265]93
[80649a91]94#include <futex.h>
95#include <async.h>
[bc1f1c2]96#include <fibril.h>
[80649a91]97#include <stdio.h>
[d9c8c81]98#include <adt/hash_table.h>
99#include <adt/list.h>
[80649a91]100#include <ipc/ipc.h>
101#include <assert.h>
102#include <errno.h>
[daa90e8]103#include <sys/time.h>
[c042bdd]104#include <arch/barrier.h>
[0cc4313]105#include <bool.h>
[4db1fbf]106#include "priv/async.h"
[80649a91]107
[fc42b28]108atomic_t async_futex = FUTEX_INITIALIZER;
[80649a91]109
[8619f25]110/** Number of threads waiting for IPC in the kernel. */
111atomic_t threads_in_ipc_wait = { 0 };
112
[49d072e]113typedef struct {
114 awaiter_t wdata;
[e70bfa5]115
116 /** If reply was received. */
[c07544d3]117 bool done;
118
[e70bfa5]119 /** Pointer to where the answer data is stored. */
[c07544d3]120 ipc_call_t *dataptr;
121
[96b02eb9]122 sysarg_t retval;
[01ff41c]123} amsg_t;
124
[36c9234]125/**
126 * Structures of this type are used to group information about a call and a
127 * message queue link.
128 */
[80649a91]129typedef struct {
130 link_t link;
131 ipc_callid_t callid;
132 ipc_call_t call;
133} msg_t;
134
[c80fdd0]135typedef struct {
136 sysarg_t in_task_hash;
137 link_t link;
138 int refcnt;
139 void *data;
140} client_t;
141
[80649a91]142typedef struct {
[49d072e]143 awaiter_t wdata;
[c07544d3]144
[e70bfa5]145 /** Hash table link. */
146 link_t link;
[c07544d3]147
[3c22f70]148 /** Incoming client task hash. */
149 sysarg_t in_task_hash;
[e70bfa5]150 /** Incoming phone hash. */
[96b02eb9]151 sysarg_t in_phone_hash;
[c07544d3]152
[23882034]153 /** Link to the client tracking structure. */
154 client_t *client;
155
[e70bfa5]156 /** Messages that should be delivered to this fibril. */
[c07544d3]157 link_t msg_queue;
158
[e70bfa5]159 /** Identification of the opening call. */
[80649a91]160 ipc_callid_t callid;
[e70bfa5]161 /** Call data of the opening call. */
[80649a91]162 ipc_call_t call;
[c07544d3]163
[e70bfa5]164 /** Identification of the closing call. */
165 ipc_callid_t close_callid;
[c07544d3]166
[e70bfa5]167 /** Fibril function that will be used to handle the connection. */
[bc1f1c2]168 void (*cfibril)(ipc_callid_t, ipc_call_t *);
[80649a91]169} connection_t;
170
[bc1f1c2]171/** Identifier of the incoming connection handled by the current fibril. */
[26360f7]172fibril_local connection_t *FIBRIL_connection;
[e70bfa5]173
[46eec3b]174static void *default_client_data_constructor(void)
175{
176 return NULL;
177}
178
179static void default_client_data_destructor(void *data)
180{
181}
182
183static async_client_data_ctor_t async_client_data_create =
184 default_client_data_constructor;
185static async_client_data_dtor_t async_client_data_destroy =
186 default_client_data_destructor;
187
188void async_set_client_data_constructor(async_client_data_ctor_t ctor)
189{
190 async_client_data_create = ctor;
191}
192
193void async_set_client_data_destructor(async_client_data_dtor_t dtor)
194{
195 async_client_data_destroy = dtor;
196}
197
[23882034]198void *async_client_data_get(void)
199{
200 assert(FIBRIL_connection);
201
202 return FIBRIL_connection->client->data;
203}
204
[da0c91e7]205static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
[51dbadf3]206static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
[36c9234]207
208/**
209 * Pointer to a fibril function that will be used to handle connections.
210 */
[da0c91e7]211static async_client_conn_t client_connection = default_client_connection;
[c07544d3]212
[36c9234]213/**
214 * Pointer to a fibril function that will be used to handle interrupt
215 * notifications.
216 */
[51dbadf3]217static async_client_conn_t interrupt_received = default_interrupt_received;
[da0c91e7]218
[c80fdd0]219static hash_table_t client_hash_table;
[c07544d3]220static hash_table_t conn_hash_table;
221static LIST_INITIALIZE(timeout_list);
222
[c80fdd0]223#define CLIENT_HASH_TABLE_BUCKETS 32
224#define CONN_HASH_TABLE_BUCKETS 32
225
226static hash_index_t client_hash(unsigned long *key)
227{
228 assert(key);
229 return (((*key) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
230}
231
232static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
233{
234 client_t *cl = hash_table_get_instance(item, client_t, link);
235 return (key[0] == cl->in_task_hash);
236}
237
238static void client_remove(link_t *item)
239{
240}
241
242/** Operations for the client hash table. */
243static hash_table_operations_t client_hash_table_ops = {
244 .hash = client_hash,
245 .compare = client_compare,
246 .remove_callback = client_remove
247};
[80649a91]248
[e70bfa5]249/** Compute hash into the connection hash table based on the source phone hash.
250 *
[c07544d3]251 * @param key Pointer to source phone hash.
252 *
253 * @return Index into the connection hash table.
[e70bfa5]254 *
255 */
[80649a91]256static hash_index_t conn_hash(unsigned long *key)
[450cd3a]257{
[80649a91]258 assert(key);
[c80fdd0]259 return (((*key) >> 4) % CONN_HASH_TABLE_BUCKETS);
[450cd3a]260}
[06502f7d]261
[e70bfa5]262/** Compare hash table item with a key.
263 *
[c07544d3]264 * @param key Array containing the source phone hash as the only item.
265 * @param keys Expected 1 but ignored.
266 * @param item Connection hash table item.
267 *
268 * @return True on match, false otherwise.
[e70bfa5]269 *
270 */
[80649a91]271static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
[450cd3a]272{
[c07544d3]273 connection_t *hs = hash_table_get_instance(item, connection_t, link);
274 return (key[0] == hs->in_phone_hash);
[450cd3a]275}
[06502f7d]276
[e70bfa5]277/** Connection hash table removal callback function.
278 *
279 * This function is called whenever a connection is removed from the connection
280 * hash table.
281 *
[c07544d3]282 * @param item Connection hash table item being removed.
283 *
[e70bfa5]284 */
[80649a91]285static void conn_remove(link_t *item)
[450cd3a]286{
[80649a91]287 free(hash_table_get_instance(item, connection_t, link));
[450cd3a]288}
289
[80649a91]290
[e70bfa5]291/** Operations for the connection hash table. */
[80649a91]292static hash_table_operations_t conn_hash_table_ops = {
293 .hash = conn_hash,
294 .compare = conn_compare,
295 .remove_callback = conn_remove
296};
297
[e70bfa5]298/** Sort in current fibril's timeout request.
[49d072e]299 *
[c07544d3]300 * @param wd Wait data of the current fibril.
301 *
[49d072e]302 */
[b6ee5b1]303void async_insert_timeout(awaiter_t *wd)
[49d072e]304{
[f53cc81]305 wd->to_event.occurred = false;
306 wd->to_event.inlist = true;
[c07544d3]307
308 link_t *tmp = timeout_list.next;
[49d072e]309 while (tmp != &timeout_list) {
[f53cc81]310 awaiter_t *cur;
[c07544d3]311
[f53cc81]312 cur = list_get_instance(tmp, awaiter_t, to_event.link);
313 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
[49d072e]314 break;
315 tmp = tmp->next;
316 }
[c07544d3]317
[f53cc81]318 list_append(&wd->to_event.link, tmp);
[49d072e]319}
320
[e70bfa5]321/** Try to route a call to an appropriate connection fibril.
[80649a91]322 *
[36c9234]323 * If the proper connection fibril is found, a message with the call is added to
324 * its message queue. If the fibril was not active, it is activated and all
325 * timeouts are unregistered.
326 *
[c07544d3]327 * @param callid Hash of the incoming call.
328 * @param call Data of the incoming call.
329 *
330 * @return False if the call doesn't match any connection.
331 * True if the call was passed to the respective connection fibril.
[36c9234]332 *
[80649a91]333 */
[c07544d3]334static bool route_call(ipc_callid_t callid, ipc_call_t *call)
[450cd3a]335{
[01ff41c]336 futex_down(&async_futex);
[c07544d3]337
338 unsigned long key = call->in_phone_hash;
339 link_t *hlp = hash_table_find(&conn_hash_table, &key);
340
[80649a91]341 if (!hlp) {
[01ff41c]342 futex_up(&async_futex);
[c07544d3]343 return false;
[450cd3a]344 }
[c07544d3]345
346 connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
347
348 msg_t *msg = malloc(sizeof(*msg));
349 if (!msg) {
350 futex_up(&async_futex);
351 return false;
352 }
353
[80649a91]354 msg->callid = callid;
355 msg->call = *call;
356 list_append(&msg->link, &conn->msg_queue);
[c07544d3]357
[228e490]358 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
[41269bd]359 conn->close_callid = callid;
[80649a91]360
[36c9234]361 /* If the connection fibril is waiting for an event, activate it */
[49d072e]362 if (!conn->wdata.active) {
[c07544d3]363
[49d072e]364 /* If in timeout list, remove it */
[f53cc81]365 if (conn->wdata.to_event.inlist) {
366 conn->wdata.to_event.inlist = false;
367 list_remove(&conn->wdata.to_event.link);
[49d072e]368 }
[c07544d3]369
370 conn->wdata.active = true;
[bc1f1c2]371 fibril_add_ready(conn->wdata.fid);
[80649a91]372 }
[c07544d3]373
[01ff41c]374 futex_up(&async_futex);
[c07544d3]375 return true;
376}
[80649a91]377
[c07544d3]378/** Notification fibril.
379 *
380 * When a notification arrives, a fibril with this implementing function is
381 * created. It calls interrupt_received() and does the final cleanup.
382 *
383 * @param arg Message structure pointer.
384 *
385 * @return Always zero.
386 *
387 */
388static int notification_fibril(void *arg)
389{
390 msg_t *msg = (msg_t *) arg;
391 interrupt_received(msg->callid, &msg->call);
392
393 free(msg);
394 return 0;
395}
396
397/** Process interrupt notification.
398 *
399 * A new fibril is created which would process the notification.
400 *
401 * @param callid Hash of the incoming call.
402 * @param call Data of the incoming call.
403 *
404 * @return False if an error occured.
405 * True if the call was passed to the notification fibril.
406 *
407 */
408static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
409{
410 futex_down(&async_futex);
411
412 msg_t *msg = malloc(sizeof(*msg));
413 if (!msg) {
414 futex_up(&async_futex);
415 return false;
416 }
417
418 msg->callid = callid;
419 msg->call = *call;
420
421 fid_t fid = fibril_create(notification_fibril, msg);
422 fibril_add_ready(fid);
423
424 futex_up(&async_futex);
425 return true;
[80649a91]426}
427
[e70bfa5]428/** Return new incoming message for the current (fibril-local) connection.
429 *
[c07544d3]430 * @param call Storage where the incoming call data will be stored.
431 * @param usecs Timeout in microseconds. Zero denotes no timeout.
432 *
433 * @return If no timeout was specified, then a hash of the
434 * incoming call is returned. If a timeout is specified,
435 * then a hash of the incoming call is returned unless
436 * the timeout expires prior to receiving a message. In
437 * that case zero is returned.
[e70bfa5]438 *
439 */
[49d072e]440ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
[80649a91]441{
[bc1f1c2]442 assert(FIBRIL_connection);
[c07544d3]443
444 /* Why doing this?
445 * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
[6c46350]446 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
[c07544d3]447 * I would never expect to find so many errors in
448 * a compiler.
[6c46350]449 */
[c07544d3]450 connection_t *conn = FIBRIL_connection;
451
[01ff41c]452 futex_down(&async_futex);
[c07544d3]453
[49d072e]454 if (usecs) {
[f53cc81]455 gettimeofday(&conn->wdata.to_event.expires, NULL);
456 tv_add(&conn->wdata.to_event.expires, usecs);
[c07544d3]457 } else
[f53cc81]458 conn->wdata.to_event.inlist = false;
[c07544d3]459
[e70bfa5]460 /* If nothing in queue, wait until something arrives */
[6c46350]461 while (list_empty(&conn->msg_queue)) {
[8c8f8d6]462 if (conn->close_callid) {
463 /*
464 * Handle the case when the connection was already
465 * closed by the client but the server did not notice
466 * the first IPC_M_PHONE_HUNGUP call and continues to
467 * call async_get_call_timeout(). Repeat
468 * IPC_M_PHONE_HUNGUP until the caller notices.
469 */
470 memset(call, 0, sizeof(ipc_call_t));
[228e490]471 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
[8c8f8d6]472 futex_up(&async_futex);
473 return conn->close_callid;
474 }
475
[085bd54]476 if (usecs)
[b6ee5b1]477 async_insert_timeout(&conn->wdata);
[c07544d3]478
479 conn->wdata.active = false;
480
[c7509e5]481 /*
482 * Note: the current fibril will be rescheduled either due to a
483 * timeout or due to an arriving message destined to it. In the
484 * former case, handle_expired_timeouts() and, in the latter
485 * case, route_call() will perform the wakeup.
486 */
[116d3f6f]487 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]488
[e70bfa5]489 /*
[c07544d3]490 * Futex is up after getting back from async_manager.
491 * Get it again.
[c7509e5]492 */
[49d072e]493 futex_down(&async_futex);
[f53cc81]494 if ((usecs) && (conn->wdata.to_event.occurred)
[c07544d3]495 && (list_empty(&conn->msg_queue))) {
[e70bfa5]496 /* If we timed out -> exit */
[49d072e]497 futex_up(&async_futex);
498 return 0;
499 }
[450cd3a]500 }
501
[c07544d3]502 msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
[80649a91]503 list_remove(&msg->link);
[c07544d3]504
505 ipc_callid_t callid = msg->callid;
[80649a91]506 *call = msg->call;
507 free(msg);
508
[01ff41c]509 futex_up(&async_futex);
[80649a91]510 return callid;
511}
512
[36c9234]513/** Default fibril function that gets called to handle new connection.
[a2cd194]514 *
[e70bfa5]515 * This function is defined as a weak symbol - to be redefined in user code.
[36c9234]516 *
[c07544d3]517 * @param callid Hash of the incoming call.
518 * @param call Data of the incoming call.
519 *
[a2cd194]520 */
[da0c91e7]521static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
[80649a91]522{
[b74959bd]523 ipc_answer_0(callid, ENOENT);
[80649a91]524}
[36c9234]525
526/** Default fibril function that gets called to handle interrupt notifications.
527 *
[c07544d3]528 * This function is defined as a weak symbol - to be redefined in user code.
529 *
530 * @param callid Hash of the incoming call.
531 * @param call Data of the incoming call.
532 *
[36c9234]533 */
[51dbadf3]534static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
[44c6d88d]535{
536}
537
[f2f0392]538/** Wrapper for client connection fibril.
539 *
[36c9234]540 * When a new connection arrives, a fibril with this implementing function is
[f2f0392]541 * created. It calls client_connection() and does the final cleanup.
[a2cd194]542 *
[c07544d3]543 * @param arg Connection structure pointer.
544 *
545 * @return Always zero.
[a2cd194]546 *
547 */
[c07544d3]548static int connection_fibril(void *arg)
[80649a91]549{
[c80fdd0]550 unsigned long key;
551 client_t *cl;
552 link_t *lnk;
[8526e585]553 bool destroy = false;
[c80fdd0]554
[c07544d3]555 /*
[c80fdd0]556 * Setup fibril-local connection pointer.
[c07544d3]557 */
[bc1f1c2]558 FIBRIL_connection = (connection_t *) arg;
[c80fdd0]559
560 /*
561 * Add our reference for the current connection in the client task
562 * tracking structure. If this is the first reference, create and
563 * hash in a new tracking structure.
564 */
565 futex_down(&async_futex);
566 key = FIBRIL_connection->in_task_hash;
567 lnk = hash_table_find(&client_hash_table, &key);
568 if (lnk) {
569 cl = hash_table_get_instance(lnk, client_t, link);
570 cl->refcnt++;
571 } else {
572 cl = malloc(sizeof(client_t));
573 if (!cl) {
574 ipc_answer_0(FIBRIL_connection->callid, ENOMEM);
575 futex_up(&async_futex);
576 return 0;
577 }
578 cl->in_task_hash = FIBRIL_connection->in_task_hash;
[46eec3b]579 async_serialize_start();
580 cl->data = async_client_data_create();
581 async_serialize_end();
[c80fdd0]582 cl->refcnt = 1;
583 hash_table_insert(&client_hash_table, &key, &cl->link);
584 }
585 futex_up(&async_futex);
586
[23882034]587 FIBRIL_connection->client = cl;
588
[c80fdd0]589 /*
590 * Call the connection handler function.
591 */
[bc1f1c2]592 FIBRIL_connection->cfibril(FIBRIL_connection->callid,
593 &FIBRIL_connection->call);
[a46da63]594
[c80fdd0]595 /*
596 * Remove the reference for this client task connection.
597 */
[01ff41c]598 futex_down(&async_futex);
[c80fdd0]599 if (--cl->refcnt == 0) {
600 hash_table_remove(&client_hash_table, &key, 1);
[8526e585]601 destroy = true;
[c80fdd0]602 }
603 futex_up(&async_futex);
604
[8526e585]605 if (destroy) {
606 if (cl->data)
607 async_client_data_destroy(cl->data);
608 free(cl);
609 }
[46eec3b]610
[c80fdd0]611 /*
612 * Remove myself from the connection hash table.
613 */
614 futex_down(&async_futex);
615 key = FIBRIL_connection->in_phone_hash;
[a2cd194]616 hash_table_remove(&conn_hash_table, &key, 1);
[01ff41c]617 futex_up(&async_futex);
[a46da63]618
[c80fdd0]619 /*
620 * Answer all remaining messages with EHANGUP.
621 */
[bc1f1c2]622 while (!list_empty(&FIBRIL_connection->msg_queue)) {
[cc27c8c5]623 msg_t *msg;
[c07544d3]624
[cc27c8c5]625 msg = list_get_instance(FIBRIL_connection->msg_queue.next,
626 msg_t, link);
[a2cd194]627 list_remove(&msg->link);
[b74959bd]628 ipc_answer_0(msg->callid, EHANGUP);
[a2cd194]629 free(msg);
630 }
[c07544d3]631
[c80fdd0]632 /*
633 * If the connection was hung-up, answer the last call,
634 * i.e. IPC_M_PHONE_HUNGUP.
635 */
[bc1f1c2]636 if (FIBRIL_connection->close_callid)
[b74959bd]637 ipc_answer_0(FIBRIL_connection->close_callid, EOK);
[a46da63]638
639 return 0;
[80649a91]640}
641
[f2f0392]642/** Create a new fibril for a new connection.
[80649a91]643 *
[c07544d3]644 * Create new fibril for connection, fill in connection structures and inserts
[f2f0392]645 * it into the hash table, so that later we can easily do routing of messages to
646 * particular fibrils.
[53ca318]647 *
[3c22f70]648 * @param in_task_hash Identification of the incoming connection.
[c07544d3]649 * @param in_phone_hash Identification of the incoming connection.
650 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
651 * If callid is zero, the connection was opened by
652 * accepting the IPC_M_CONNECT_TO_ME call and this function
653 * is called directly by the server.
654 * @param call Call data of the opening call.
655 * @param cfibril Fibril function that should be called upon opening the
656 * connection.
657 *
658 * @return New fibril id or NULL on failure.
[36c9234]659 *
[80649a91]660 */
[3c22f70]661fid_t async_new_connection(sysarg_t in_task_hash, sysarg_t in_phone_hash,
662 ipc_callid_t callid, ipc_call_t *call,
663 void (*cfibril)(ipc_callid_t, ipc_call_t *))
[80649a91]664{
[c07544d3]665 connection_t *conn = malloc(sizeof(*conn));
[80649a91]666 if (!conn) {
[6675c70]667 if (callid)
[b74959bd]668 ipc_answer_0(callid, ENOMEM);
[0b4a67a]669 return (uintptr_t) NULL;
[80649a91]670 }
[c07544d3]671
[3c22f70]672 conn->in_task_hash = in_task_hash;
[44c6d88d]673 conn->in_phone_hash = in_phone_hash;
[80649a91]674 list_initialize(&conn->msg_queue);
675 conn->callid = callid;
[c4702804]676 conn->close_callid = 0;
[c07544d3]677
[eaf34f7]678 if (call)
679 conn->call = *call;
[6b21292]680
[c07544d3]681 /* We will activate the fibril ASAP */
682 conn->wdata.active = true;
683 conn->cfibril = cfibril;
[bc1f1c2]684 conn->wdata.fid = fibril_create(connection_fibril, conn);
[c07544d3]685
[bc1f1c2]686 if (!conn->wdata.fid) {
[80649a91]687 free(conn);
[6675c70]688 if (callid)
[b74959bd]689 ipc_answer_0(callid, ENOMEM);
[0b4a67a]690 return (uintptr_t) NULL;
[80649a91]691 }
[6b21292]692
[36c9234]693 /* Add connection to the connection hash table */
[9db9b10]694 unsigned long key = conn->in_phone_hash;
[c07544d3]695
[01ff41c]696 futex_down(&async_futex);
[80649a91]697 hash_table_insert(&conn_hash_table, &key, &conn->link);
[01ff41c]698 futex_up(&async_futex);
[6b21292]699
[bc1f1c2]700 fibril_add_ready(conn->wdata.fid);
[6b21292]701
[bc1f1c2]702 return conn->wdata.fid;
[80649a91]703}
704
[36c9234]705/** Handle a call that was received.
706 *
707 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
708 * Otherwise the call is routed to its connection fibril.
709 *
[c07544d3]710 * @param callid Hash of the incoming call.
711 * @param call Data of the incoming call.
[6b21292]712 *
[36c9234]713 */
[80649a91]714static void handle_call(ipc_callid_t callid, ipc_call_t *call)
715{
[44c6d88d]716 /* Unrouted call - do some default behaviour */
[15039b67]717 if ((callid & IPC_CALLID_NOTIFICATION)) {
[c07544d3]718 process_notification(callid, call);
[9db9b10]719 goto out;
[6b21292]720 }
721
[228e490]722 switch (IPC_GET_IMETHOD(*call)) {
[2c0e5d2]723 case IPC_M_CONNECT_ME:
[80649a91]724 case IPC_M_CONNECT_ME_TO:
[f2f0392]725 /* Open new connection with fibril etc. */
[3c22f70]726 async_new_connection(call->in_task_hash, IPC_GET_ARG5(*call),
727 callid, call, client_connection);
[9db9b10]728 goto out;
[80649a91]729 }
[6b21292]730
[36c9234]731 /* Try to route the call through the connection hash table */
[44c6d88d]732 if (route_call(callid, call))
[9db9b10]733 goto out;
[6b21292]734
[44c6d88d]735 /* Unknown call from unknown phone - hang it up */
[b74959bd]736 ipc_answer_0(callid, EHANGUP);
[9db9b10]737 return;
738
739out:
[953769f]740 ;
[450cd3a]741}
742
[f2f0392]743/** Fire all timeouts that expired. */
[c042bdd]744static void handle_expired_timeouts(void)
745{
746 struct timeval tv;
[36c9234]747 gettimeofday(&tv, NULL);
[c07544d3]748
[c042bdd]749 futex_down(&async_futex);
[c07544d3]750
751 link_t *cur = timeout_list.next;
[c042bdd]752 while (cur != &timeout_list) {
[f53cc81]753 awaiter_t *waiter;
[c07544d3]754
[f53cc81]755 waiter = list_get_instance(cur, awaiter_t, to_event.link);
756 if (tv_gt(&waiter->to_event.expires, &tv))
[c042bdd]757 break;
[f53cc81]758
[c042bdd]759 cur = cur->next;
[f53cc81]760
761 list_remove(&waiter->to_event.link);
762 waiter->to_event.inlist = false;
763 waiter->to_event.occurred = true;
[c07544d3]764
[36c9234]765 /*
[c07544d3]766 * Redundant condition?
767 * The fibril should not be active when it gets here.
[c042bdd]768 */
[49d072e]769 if (!waiter->active) {
[c07544d3]770 waiter->active = true;
[bc1f1c2]771 fibril_add_ready(waiter->fid);
[c042bdd]772 }
773 }
[c07544d3]774
[c042bdd]775 futex_up(&async_futex);
776}
777
[36c9234]778/** Endless loop dispatching incoming calls and answers.
779 *
[c07544d3]780 * @return Never returns.
781 *
[36c9234]782 */
[085bd54]783static int async_manager_worker(void)
[80649a91]784{
[c07544d3]785 while (true) {
[116d3f6f]786 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
[a46da63]787 futex_up(&async_futex);
[36c9234]788 /*
789 * async_futex is always held when entering a manager
790 * fibril.
[a46da63]791 */
[80649a91]792 continue;
793 }
[c07544d3]794
[c042bdd]795 futex_down(&async_futex);
[c07544d3]796
797 suseconds_t timeout;
[c042bdd]798 if (!list_empty(&timeout_list)) {
[cc27c8c5]799 awaiter_t *waiter = list_get_instance(timeout_list.next,
[f53cc81]800 awaiter_t, to_event.link);
[c07544d3]801
802 struct timeval tv;
[bc1f1c2]803 gettimeofday(&tv, NULL);
[c07544d3]804
[f53cc81]805 if (tv_gteq(&tv, &waiter->to_event.expires)) {
[6c46350]806 futex_up(&async_futex);
[c042bdd]807 handle_expired_timeouts();
808 continue;
809 } else
[f53cc81]810 timeout = tv_sub(&waiter->to_event.expires,
811 &tv);
[c042bdd]812 } else
[0b99e40]813 timeout = SYNCH_NO_TIMEOUT;
[c07544d3]814
[c042bdd]815 futex_up(&async_futex);
[8619f25]816
817 atomic_inc(&threads_in_ipc_wait);
[c07544d3]818
819 ipc_call_t call;
[cc27c8c5]820 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
821 SYNCH_FLAGS_NONE);
[c07544d3]822
[8619f25]823 atomic_dec(&threads_in_ipc_wait);
824
[0b99e40]825 if (!callid) {
[c042bdd]826 handle_expired_timeouts();
[0b99e40]827 continue;
828 }
[c07544d3]829
830 if (callid & IPC_CALLID_ANSWERED)
[80649a91]831 continue;
[c07544d3]832
[80649a91]833 handle_call(callid, &call);
834 }
[a46da63]835
836 return 0;
[80649a91]837}
838
[36c9234]839/** Function to start async_manager as a standalone fibril.
[c07544d3]840 *
[36c9234]841 * When more kernel threads are used, one async manager should exist per thread.
842 *
[c07544d3]843 * @param arg Unused.
844 * @return Never returns.
[36c9234]845 *
[a2cd194]846 */
[9591265]847static int async_manager_fibril(void *arg)
[80649a91]848{
[a46da63]849 futex_up(&async_futex);
[c07544d3]850
[36c9234]851 /*
852 * async_futex is always locked when entering manager
853 */
[085bd54]854 async_manager_worker();
[a46da63]855
856 return 0;
[80649a91]857}
[450cd3a]858
[36c9234]859/** Add one manager to manager list. */
[80649a91]860void async_create_manager(void)
[450cd3a]861{
[c07544d3]862 fid_t fid = fibril_create(async_manager_fibril, NULL);
[bc1f1c2]863 fibril_add_manager(fid);
[80649a91]864}
865
866/** Remove one manager from manager list */
867void async_destroy_manager(void)
868{
[bc1f1c2]869 fibril_remove_manager();
[80649a91]870}
871
[36c9234]872/** Initialize the async framework.
873 *
[c07544d3]874 * @return Zero on success or an error code.
[36c9234]875 */
[db24058]876int __async_init(void)
[80649a91]877{
[c80fdd0]878 if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS, 1,
879 &client_hash_table_ops) || !hash_table_create(&conn_hash_table,
880 CONN_HASH_TABLE_BUCKETS, 1, &conn_hash_table_ops)) {
[80649a91]881 return ENOMEM;
882 }
[c1c0184]883
884 _async_sess_init();
[80649a91]885
[a46da63]886 return 0;
[450cd3a]887}
[01ff41c]888
[36c9234]889/** Reply received callback.
[01ff41c]890 *
[36c9234]891 * This function is called whenever a reply for an asynchronous message sent out
892 * by the asynchronous framework is received.
893 *
894 * Notify the fibril which is waiting for this message that it has arrived.
895 *
[c07544d3]896 * @param arg Pointer to the asynchronous message record.
897 * @param retval Value returned in the answer.
898 * @param data Call data of the answer.
[01ff41c]899 */
[c07544d3]900static void reply_received(void *arg, int retval, ipc_call_t *data)
[01ff41c]901{
[9db9b10]902 futex_down(&async_futex);
903
[c07544d3]904 amsg_t *msg = (amsg_t *) arg;
[01ff41c]905 msg->retval = retval;
[c07544d3]906
[36c9234]907 /* Copy data after futex_down, just in case the call was detached */
[9db9b10]908 if ((msg->dataptr) && (data))
[c07544d3]909 *msg->dataptr = *data;
910
[c042bdd]911 write_barrier();
[c07544d3]912
[c042bdd]913 /* Remove message from timeout list */
[f53cc81]914 if (msg->wdata.to_event.inlist)
915 list_remove(&msg->wdata.to_event.link);
[c07544d3]916
917 msg->done = true;
[36c9234]918 if (!msg->wdata.active) {
[c07544d3]919 msg->wdata.active = true;
[bc1f1c2]920 fibril_add_ready(msg->wdata.fid);
[01ff41c]921 }
[c07544d3]922
[01ff41c]923 futex_up(&async_futex);
924}
925
[36c9234]926/** Send message and return id of the sent message.
927 *
928 * The return value can be used as input for async_wait() to wait for
929 * completion.
[01ff41c]930 *
[c07544d3]931 * @param phoneid Handle of the phone that will be used for the send.
932 * @param method Service-defined method.
933 * @param arg1 Service-defined payload argument.
934 * @param arg2 Service-defined payload argument.
935 * @param arg3 Service-defined payload argument.
936 * @param arg4 Service-defined payload argument.
937 * @param dataptr If non-NULL, storage where the reply data will be
938 * stored.
939 *
940 * @return Hash of the sent message or 0 on error.
[36c9234]941 *
[01ff41c]942 */
[96b02eb9]943aid_t async_send_fast(int phoneid, sysarg_t method, sysarg_t arg1,
944 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[01ff41c]945{
[c07544d3]946 amsg_t *msg = malloc(sizeof(*msg));
947
948 if (!msg)
949 return 0;
[6b21292]950
[c07544d3]951 msg->done = false;
[01ff41c]952 msg->dataptr = dataptr;
[6b21292]953
[f53cc81]954 msg->wdata.to_event.inlist = false;
[36c9234]955 /* We may sleep in the next method, but it will use its own mechanism */
[c07544d3]956 msg->wdata.active = true;
957
[0cc4313]958 ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
[c07544d3]959 reply_received, true);
[6b21292]960
[01ff41c]961 return (aid_t) msg;
962}
963
[90f5d64]964/** Send message and return id of the sent message
965 *
[36c9234]966 * The return value can be used as input for async_wait() to wait for
967 * completion.
968 *
[c07544d3]969 * @param phoneid Handle of the phone that will be used for the send.
970 * @param method Service-defined method.
971 * @param arg1 Service-defined payload argument.
972 * @param arg2 Service-defined payload argument.
973 * @param arg3 Service-defined payload argument.
974 * @param arg4 Service-defined payload argument.
975 * @param arg5 Service-defined payload argument.
976 * @param dataptr If non-NULL, storage where the reply data will be
977 * stored.
978 *
979 * @return Hash of the sent message or 0 on error.
[36c9234]980 *
[90f5d64]981 */
[96b02eb9]982aid_t async_send_slow(int phoneid, sysarg_t method, sysarg_t arg1,
983 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[0cc4313]984 ipc_call_t *dataptr)
[90f5d64]985{
[c07544d3]986 amsg_t *msg = malloc(sizeof(*msg));
[6b21292]987
[c07544d3]988 if (!msg)
989 return 0;
990
991 msg->done = false;
[90f5d64]992 msg->dataptr = dataptr;
[6b21292]993
[f53cc81]994 msg->wdata.to_event.inlist = false;
[36c9234]995 /* We may sleep in next method, but it will use its own mechanism */
[c07544d3]996 msg->wdata.active = true;
[6b21292]997
[0cc4313]998 ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
[c07544d3]999 reply_received, true);
[6b21292]1000
[90f5d64]1001 return (aid_t) msg;
1002}
1003
[36c9234]1004/** Wait for a message sent by the async framework.
[01ff41c]1005 *
[c07544d3]1006 * @param amsgid Hash of the message to wait for.
1007 * @param retval Pointer to storage where the retval of the answer will
1008 * be stored.
1009 *
[01ff41c]1010 */
[96b02eb9]1011void async_wait_for(aid_t amsgid, sysarg_t *retval)
[01ff41c]1012{
1013 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]1014
[01ff41c]1015 futex_down(&async_futex);
1016 if (msg->done) {
1017 futex_up(&async_futex);
1018 goto done;
1019 }
[c07544d3]1020
[bc1f1c2]1021 msg->wdata.fid = fibril_get_id();
[c07544d3]1022 msg->wdata.active = false;
[f53cc81]1023 msg->wdata.to_event.inlist = false;
[c07544d3]1024
[36c9234]1025 /* Leave the async_futex locked when entering this function */
[116d3f6f]1026 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1027
1028 /* Futex is up automatically after fibril_switch */
1029
[01ff41c]1030done:
1031 if (retval)
1032 *retval = msg->retval;
[c07544d3]1033
[01ff41c]1034 free(msg);
1035}
[0b99e40]1036
[36c9234]1037/** Wait for a message sent by the async framework, timeout variant.
[c042bdd]1038 *
[c07544d3]1039 * @param amsgid Hash of the message to wait for.
1040 * @param retval Pointer to storage where the retval of the answer will
1041 * be stored.
1042 * @param timeout Timeout in microseconds.
1043 *
1044 * @return Zero on success, ETIMEOUT if the timeout has expired.
[c042bdd]1045 *
1046 */
[96b02eb9]1047int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
[c042bdd]1048{
1049 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]1050
[86029498]1051 /* TODO: Let it go through the event read at least once */
1052 if (timeout < 0)
1053 return ETIMEOUT;
[c07544d3]1054
[c042bdd]1055 futex_down(&async_futex);
1056 if (msg->done) {
1057 futex_up(&async_futex);
1058 goto done;
1059 }
[c07544d3]1060
[f53cc81]1061 gettimeofday(&msg->wdata.to_event.expires, NULL);
1062 tv_add(&msg->wdata.to_event.expires, timeout);
[c07544d3]1063
[bc1f1c2]1064 msg->wdata.fid = fibril_get_id();
[c07544d3]1065 msg->wdata.active = false;
[b6ee5b1]1066 async_insert_timeout(&msg->wdata);
[c07544d3]1067
[36c9234]1068 /* Leave the async_futex locked when entering this function */
[116d3f6f]1069 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1070
1071 /* Futex is up automatically after fibril_switch */
1072
[c042bdd]1073 if (!msg->done)
1074 return ETIMEOUT;
[c07544d3]1075
[c042bdd]1076done:
1077 if (retval)
1078 *retval = msg->retval;
[c07544d3]1079
[c042bdd]1080 free(msg);
[c07544d3]1081
[c042bdd]1082 return 0;
1083}
[0b99e40]1084
[36c9234]1085/** Wait for specified time.
[44c6d88d]1086 *
[36c9234]1087 * The current fibril is suspended but the thread continues to execute.
1088 *
[c07544d3]1089 * @param timeout Duration of the wait in microseconds.
1090 *
[44c6d88d]1091 */
1092void async_usleep(suseconds_t timeout)
1093{
[c07544d3]1094 amsg_t *msg = malloc(sizeof(*msg));
[44c6d88d]1095
1096 if (!msg)
1097 return;
[6b21292]1098
[bc1f1c2]1099 msg->wdata.fid = fibril_get_id();
[c07544d3]1100 msg->wdata.active = false;
[6b21292]1101
[f53cc81]1102 gettimeofday(&msg->wdata.to_event.expires, NULL);
1103 tv_add(&msg->wdata.to_event.expires, timeout);
[6b21292]1104
[44c6d88d]1105 futex_down(&async_futex);
[c07544d3]1106
[b6ee5b1]1107 async_insert_timeout(&msg->wdata);
[c07544d3]1108
[36c9234]1109 /* Leave the async_futex locked when entering this function */
[116d3f6f]1110 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]1111
1112 /* Futex is up automatically after fibril_switch() */
1113
[44c6d88d]1114 free(msg);
1115}
[da0c91e7]1116
[36c9234]1117/** Setter for client_connection function pointer.
[da0c91e7]1118 *
[c07544d3]1119 * @param conn Function that will implement a new connection fibril.
1120 *
[da0c91e7]1121 */
1122void async_set_client_connection(async_client_conn_t conn)
1123{
1124 client_connection = conn;
1125}
[36c9234]1126
1127/** Setter for interrupt_received function pointer.
1128 *
[c07544d3]1129 * @param intr Function that will implement a new interrupt
1130 * notification fibril.
[36c9234]1131 */
[c07544d3]1132void async_set_interrupt_received(async_client_conn_t intr)
[51dbadf3]1133{
[c07544d3]1134 interrupt_received = intr;
[51dbadf3]1135}
[085bd54]1136
[0cc4313]1137/** Pseudo-synchronous message sending - fast version.
1138 *
1139 * Send message asynchronously and return only after the reply arrives.
1140 *
1141 * This function can only transfer 4 register payload arguments. For
1142 * transferring more arguments, see the slower async_req_slow().
1143 *
[c07544d3]1144 * @param phoneid Hash of the phone through which to make the call.
1145 * @param method Method of the call.
1146 * @param arg1 Service-defined payload argument.
1147 * @param arg2 Service-defined payload argument.
1148 * @param arg3 Service-defined payload argument.
1149 * @param arg4 Service-defined payload argument.
1150 * @param r1 If non-NULL, storage for the 1st reply argument.
1151 * @param r2 If non-NULL, storage for the 2nd reply argument.
1152 * @param r3 If non-NULL, storage for the 3rd reply argument.
1153 * @param r4 If non-NULL, storage for the 4th reply argument.
1154 * @param r5 If non-NULL, storage for the 5th reply argument.
1155 *
1156 * @return Return code of the reply or a negative error code.
1157 *
[0cc4313]1158 */
[96b02eb9]1159sysarg_t async_req_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1160 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1161 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1162{
[0cc4313]1163 ipc_call_t result;
1164 aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1165 &result);
[c07544d3]1166
[96b02eb9]1167 sysarg_t rc;
[0cc4313]1168 async_wait_for(eid, &rc);
[c07544d3]1169
1170 if (r1)
[0cc4313]1171 *r1 = IPC_GET_ARG1(result);
[c07544d3]1172
[0cc4313]1173 if (r2)
1174 *r2 = IPC_GET_ARG2(result);
[c07544d3]1175
[0cc4313]1176 if (r3)
1177 *r3 = IPC_GET_ARG3(result);
[c07544d3]1178
[0cc4313]1179 if (r4)
1180 *r4 = IPC_GET_ARG4(result);
[c07544d3]1181
[0cc4313]1182 if (r5)
1183 *r5 = IPC_GET_ARG5(result);
[c07544d3]1184
[0cc4313]1185 return rc;
[085bd54]1186}
1187
[0cc4313]1188/** Pseudo-synchronous message sending - slow version.
1189 *
1190 * Send message asynchronously and return only after the reply arrives.
1191 *
[c07544d3]1192 * @param phoneid Hash of the phone through which to make the call.
1193 * @param method Method of the call.
1194 * @param arg1 Service-defined payload argument.
1195 * @param arg2 Service-defined payload argument.
1196 * @param arg3 Service-defined payload argument.
1197 * @param arg4 Service-defined payload argument.
1198 * @param arg5 Service-defined payload argument.
1199 * @param r1 If non-NULL, storage for the 1st reply argument.
1200 * @param r2 If non-NULL, storage for the 2nd reply argument.
1201 * @param r3 If non-NULL, storage for the 3rd reply argument.
1202 * @param r4 If non-NULL, storage for the 4th reply argument.
1203 * @param r5 If non-NULL, storage for the 5th reply argument.
1204 *
1205 * @return Return code of the reply or a negative error code.
1206 *
[0cc4313]1207 */
[96b02eb9]1208sysarg_t async_req_slow(int phoneid, sysarg_t method, sysarg_t arg1,
1209 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1210 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1211{
[0cc4313]1212 ipc_call_t result;
1213 aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1214 &result);
[c07544d3]1215
[96b02eb9]1216 sysarg_t rc;
[0cc4313]1217 async_wait_for(eid, &rc);
[c07544d3]1218
1219 if (r1)
[0cc4313]1220 *r1 = IPC_GET_ARG1(result);
[c07544d3]1221
[0cc4313]1222 if (r2)
1223 *r2 = IPC_GET_ARG2(result);
[c07544d3]1224
[0cc4313]1225 if (r3)
1226 *r3 = IPC_GET_ARG3(result);
[c07544d3]1227
[0cc4313]1228 if (r4)
1229 *r4 = IPC_GET_ARG4(result);
[c07544d3]1230
[0cc4313]1231 if (r5)
1232 *r5 = IPC_GET_ARG5(result);
[c07544d3]1233
[0cc4313]1234 return rc;
[085bd54]1235}
[b2951e2]1236
[007e6efa]1237/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1238 *
1239 * Ask through phone for a new connection to some service.
1240 *
1241 * @param phone Phone handle used for contacting the other side.
1242 * @param arg1 User defined argument.
1243 * @param arg2 User defined argument.
1244 * @param arg3 User defined argument.
1245 * @param client_receiver Connection handing routine.
1246 *
1247 * @return New phone handle on success or a negative error code.
1248 *
1249 */
1250int async_connect_to_me(int phone, sysarg_t arg1, sysarg_t arg2,
1251 sysarg_t arg3, async_client_conn_t client_receiver)
1252{
1253 sysarg_t task_hash;
1254 sysarg_t phone_hash;
1255 int rc = async_req_3_5(phone, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1256 NULL, NULL, NULL, &task_hash, &phone_hash);
1257 if (rc != EOK)
1258 return rc;
1259
1260 if (client_receiver != NULL)
1261 async_new_connection(task_hash, phone_hash, 0, NULL,
1262 client_receiver);
1263
1264 return EOK;
1265}
1266
[f74392f]1267/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
[007e6efa]1268 *
[f74392f]1269 * Ask through phone for a new connection to some service.
1270 *
[007e6efa]1271 * @param phone Phone handle used for contacting the other side.
1272 * @param arg1 User defined argument.
1273 * @param arg2 User defined argument.
1274 * @param arg3 User defined argument.
1275 *
1276 * @return New phone handle on success or a negative error code.
[f74392f]1277 *
1278 */
[007e6efa]1279int async_connect_me_to(int phone, sysarg_t arg1, sysarg_t arg2,
1280 sysarg_t arg3)
[f74392f]1281{
[96b02eb9]1282 sysarg_t newphid;
[007e6efa]1283 int rc = async_req_3_5(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
1284 NULL, NULL, NULL, NULL, &newphid);
[f74392f]1285
[007e6efa]1286 if (rc != EOK)
[f74392f]1287 return rc;
[007e6efa]1288
[f74392f]1289 return newphid;
1290}
1291
1292/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
[007e6efa]1293 *
[f74392f]1294 * Ask through phone for a new connection to some service and block until
1295 * success.
1296 *
[007e6efa]1297 * @param phoneid Phone handle used for contacting the other side.
1298 * @param arg1 User defined argument.
1299 * @param arg2 User defined argument.
1300 * @param arg3 User defined argument.
1301 *
1302 * @return New phone handle on success or a negative error code.
[f74392f]1303 *
1304 */
[007e6efa]1305int async_connect_me_to_blocking(int phoneid, sysarg_t arg1, sysarg_t arg2,
[96b02eb9]1306 sysarg_t arg3)
[f74392f]1307{
[96b02eb9]1308 sysarg_t newphid;
[007e6efa]1309 int rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
[f74392f]1310 IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
1311
[007e6efa]1312 if (rc != EOK)
[f74392f]1313 return rc;
[007e6efa]1314
[f74392f]1315 return newphid;
1316}
1317
[0da4e41]1318/** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
1319 *
1320 * @param phoneid Phone that will be used to contact the receiving side.
1321 * @param dst Destination address space area base.
1322 * @param size Size of the destination address space area.
1323 * @param arg User defined argument.
1324 * @param flags Storage where the received flags will be stored. Can be
1325 * NULL.
1326 *
1327 * @return Zero on success or a negative error code from errno.h.
1328 */
[96b02eb9]1329int async_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
[0da4e41]1330 int *flags)
1331{
1332 int res;
1333 sysarg_t tmp_flags;
[96b02eb9]1334 res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
1335 (sysarg_t) size, arg, NULL, &tmp_flags);
[0da4e41]1336 if (flags)
1337 *flags = tmp_flags;
1338 return res;
1339}
1340
1341/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1342 *
1343 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN calls
1344 * so that the user doesn't have to remember the meaning of each IPC argument.
1345 *
1346 * So far, this wrapper is to be used from within a connection fibril.
1347 *
1348 * @param callid Storage where the hash of the IPC_M_SHARE_IN call will
1349 * be stored.
1350 * @param size Destination address space area size.
1351 *
1352 * @return Non-zero on success, zero on failure.
1353 */
1354int async_share_in_receive(ipc_callid_t *callid, size_t *size)
1355{
1356 ipc_call_t data;
1357
1358 assert(callid);
1359 assert(size);
1360
1361 *callid = async_get_call(&data);
[228e490]1362 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
[0da4e41]1363 return 0;
1364 *size = (size_t) IPC_GET_ARG2(data);
1365 return 1;
1366}
1367
1368/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1369 *
1370 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1371 * so that the user doesn't have to remember the meaning of each IPC argument.
1372 *
1373 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1374 * @param src Source address space base.
1375 * @param flags Flags to be used for sharing. Bits can be only cleared.
1376 *
1377 * @return Zero on success or a value from @ref errno.h on failure.
1378 */
1379int async_share_in_finalize(ipc_callid_t callid, void *src, int flags)
1380{
1381 return ipc_share_in_finalize(callid, src, flags);
1382}
1383
1384/** Wrapper for making IPC_M_SHARE_OUT calls using the async framework.
1385 *
1386 * @param phoneid Phone that will be used to contact the receiving side.
1387 * @param src Source address space area base address.
1388 * @param flags Flags to be used for sharing. Bits can be only cleared.
1389 *
1390 * @return Zero on success or a negative error code from errno.h.
1391 */
1392int async_share_out_start(int phoneid, void *src, int flags)
1393{
[96b02eb9]1394 return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
1395 (sysarg_t) flags);
[0da4e41]1396}
1397
1398/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1399 *
1400 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT calls
1401 * so that the user doesn't have to remember the meaning of each IPC argument.
1402 *
1403 * So far, this wrapper is to be used from within a connection fibril.
1404 *
1405 * @param callid Storage where the hash of the IPC_M_SHARE_OUT call will
1406 * be stored.
1407 * @param size Storage where the source address space area size will be
1408 * stored.
1409 * @param flags Storage where the sharing flags will be stored.
1410 *
1411 * @return Non-zero on success, zero on failure.
1412 */
1413int async_share_out_receive(ipc_callid_t *callid, size_t *size, int *flags)
1414{
1415 ipc_call_t data;
1416
1417 assert(callid);
1418 assert(size);
1419 assert(flags);
1420
1421 *callid = async_get_call(&data);
[228e490]1422 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
[0da4e41]1423 return 0;
1424 *size = (size_t) IPC_GET_ARG2(data);
1425 *flags = (int) IPC_GET_ARG3(data);
1426 return 1;
1427}
1428
1429/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
1430 *
1431 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT calls
1432 * so that the user doesn't have to remember the meaning of each IPC argument.
1433 *
1434 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1435 * @param dst Destination address space area base address.
1436 *
1437 * @return Zero on success or a value from @ref errno.h on failure.
1438 */
1439int async_share_out_finalize(ipc_callid_t callid, void *dst)
1440{
1441 return ipc_share_out_finalize(callid, dst);
1442}
1443
1444
1445/** Wrapper for making IPC_M_DATA_READ calls using the async framework.
1446 *
1447 * @param phoneid Phone that will be used to contact the receiving side.
1448 * @param dst Address of the beginning of the destination buffer.
1449 * @param size Size of the destination buffer.
1450 *
1451 * @return Zero on success or a negative error code from errno.h.
1452 */
1453int async_data_read_start(int phoneid, void *dst, size_t size)
1454{
[96b02eb9]1455 return async_req_2_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
1456 (sysarg_t) size);
[0da4e41]1457}
1458
1459/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
1460 *
1461 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ calls
1462 * so that the user doesn't have to remember the meaning of each IPC argument.
1463 *
1464 * So far, this wrapper is to be used from within a connection fibril.
1465 *
1466 * @param callid Storage where the hash of the IPC_M_DATA_READ call will
1467 * be stored.
1468 * @param size Storage where the maximum size will be stored. Can be
1469 * NULL.
1470 *
1471 * @return Non-zero on success, zero on failure.
1472 */
1473int async_data_read_receive(ipc_callid_t *callid, size_t *size)
1474{
1475 ipc_call_t data;
1476
1477 assert(callid);
1478
1479 *callid = async_get_call(&data);
[228e490]1480 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
[0da4e41]1481 return 0;
1482 if (size)
1483 *size = (size_t) IPC_GET_ARG2(data);
1484 return 1;
1485}
1486
1487/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
1488 *
1489 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1490 * so that the user doesn't have to remember the meaning of each IPC argument.
1491 *
1492 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1493 * @param src Source address for the IPC_M_DATA_READ call.
1494 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
1495 * the maximum size announced by the sender.
1496 *
1497 * @return Zero on success or a value from @ref errno.h on failure.
1498 */
1499int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
1500{
1501 return ipc_data_read_finalize(callid, src, size);
1502}
1503
[b4cbef1]1504/** Wrapper for forwarding any read request
1505 *
1506 *
1507 */
[96b02eb9]1508int async_data_read_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1509 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[b4cbef1]1510{
1511 ipc_callid_t callid;
1512 if (!async_data_read_receive(&callid, NULL)) {
1513 ipc_answer_0(callid, EINVAL);
1514 return EINVAL;
1515 }
1516
1517 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1518 dataptr);
1519 if (msg == 0) {
1520 ipc_answer_0(callid, EINVAL);
1521 return EINVAL;
1522 }
1523
1524 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1525 IPC_FF_ROUTE_FROM_ME);
1526 if (retval != EOK) {
[a281fc82]1527 async_wait_for(msg, NULL);
[b4cbef1]1528 ipc_answer_0(callid, retval);
1529 return retval;
1530 }
1531
[96b02eb9]1532 sysarg_t rc;
[b4cbef1]1533 async_wait_for(msg, &rc);
1534
1535 return (int) rc;
1536}
1537
[0da4e41]1538/** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
1539 *
[b4cbef1]1540 * @param phoneid Phone that will be used to contact the receiving side.
1541 * @param src Address of the beginning of the source buffer.
1542 * @param size Size of the source buffer.
1543 *
1544 * @return Zero on success or a negative error code from errno.h.
[0da4e41]1545 *
1546 */
1547int async_data_write_start(int phoneid, const void *src, size_t size)
1548{
[96b02eb9]1549 return async_req_2_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
1550 (sysarg_t) size);
[0da4e41]1551}
1552
1553/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
1554 *
1555 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE calls
1556 * so that the user doesn't have to remember the meaning of each IPC argument.
1557 *
1558 * So far, this wrapper is to be used from within a connection fibril.
1559 *
[b4cbef1]1560 * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
1561 * be stored.
1562 * @param size Storage where the suggested size will be stored. May be
1563 * NULL
1564 *
1565 * @return Non-zero on success, zero on failure.
[0da4e41]1566 *
1567 */
1568int async_data_write_receive(ipc_callid_t *callid, size_t *size)
1569{
1570 ipc_call_t data;
1571
1572 assert(callid);
[b4cbef1]1573
[0da4e41]1574 *callid = async_get_call(&data);
[228e490]1575 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
[0da4e41]1576 return 0;
[b4cbef1]1577
[0da4e41]1578 if (size)
1579 *size = (size_t) IPC_GET_ARG2(data);
[b4cbef1]1580
[0da4e41]1581 return 1;
1582}
1583
1584/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
1585 *
1586 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
1587 * so that the user doesn't have to remember the meaning of each IPC argument.
1588 *
[b4cbef1]1589 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1590 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
1591 * @param size Final size for the IPC_M_DATA_WRITE call.
1592 *
1593 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]1594 *
1595 */
1596int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
1597{
1598 return ipc_data_write_finalize(callid, dst, size);
1599}
1600
[eda925a]1601/** Wrapper for receiving binary data or strings
[8aa42e3]1602 *
1603 * This wrapper only makes it more comfortable to use async_data_write_*
[eda925a]1604 * functions to receive binary data or strings.
[8aa42e3]1605 *
[472c09d]1606 * @param data Pointer to data pointer (which should be later disposed
1607 * by free()). If the operation fails, the pointer is not
1608 * touched.
[eda925a]1609 * @param nullterm If true then the received data is always zero terminated.
1610 * This also causes to allocate one extra byte beyond the
1611 * raw transmitted data.
[b4cbef1]1612 * @param min_size Minimum size (in bytes) of the data to receive.
[472c09d]1613 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
1614 * no limit.
[eda925a]1615 * @param granulariy If non-zero then the size of the received data has to
[472c09d]1616 * be divisible by this value.
1617 * @param received If not NULL, the size of the received data is stored here.
[8aa42e3]1618 *
1619 * @return Zero on success or a value from @ref errno.h on failure.
1620 *
1621 */
[eda925a]1622int async_data_write_accept(void **data, const bool nullterm,
1623 const size_t min_size, const size_t max_size, const size_t granularity,
1624 size_t *received)
[8aa42e3]1625{
1626 ipc_callid_t callid;
1627 size_t size;
1628 if (!async_data_write_receive(&callid, &size)) {
1629 ipc_answer_0(callid, EINVAL);
1630 return EINVAL;
1631 }
1632
[b4cbef1]1633 if (size < min_size) {
1634 ipc_answer_0(callid, EINVAL);
1635 return EINVAL;
1636 }
1637
[8aa42e3]1638 if ((max_size > 0) && (size > max_size)) {
1639 ipc_answer_0(callid, EINVAL);
1640 return EINVAL;
1641 }
1642
[472c09d]1643 if ((granularity > 0) && ((size % granularity) != 0)) {
1644 ipc_answer_0(callid, EINVAL);
1645 return EINVAL;
1646 }
1647
[eda925a]1648 void *_data;
1649
1650 if (nullterm)
1651 _data = malloc(size + 1);
1652 else
1653 _data = malloc(size);
1654
[472c09d]1655 if (_data == NULL) {
[8aa42e3]1656 ipc_answer_0(callid, ENOMEM);
1657 return ENOMEM;
1658 }
1659
[472c09d]1660 int rc = async_data_write_finalize(callid, _data, size);
[8aa42e3]1661 if (rc != EOK) {
[472c09d]1662 free(_data);
[8aa42e3]1663 return rc;
1664 }
1665
[eda925a]1666 if (nullterm)
1667 ((char *) _data)[size] = 0;
[8aa42e3]1668
[eda925a]1669 *data = _data;
[472c09d]1670 if (received != NULL)
1671 *received = size;
1672
[8aa42e3]1673 return EOK;
1674}
1675
[b4cbef1]1676/** Wrapper for voiding any data that is about to be received
1677 *
1678 * This wrapper can be used to void any pending data
1679 *
1680 * @param retval Error value from @ref errno.h to be returned to the caller.
1681 *
1682 */
[eda925a]1683void async_data_write_void(const int retval)
[b4cbef1]1684{
1685 ipc_callid_t callid;
1686 async_data_write_receive(&callid, NULL);
1687 ipc_answer_0(callid, retval);
1688}
1689
1690/** Wrapper for forwarding any data that is about to be received
1691 *
1692 *
1693 */
[96b02eb9]1694int async_data_write_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1695 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[b4cbef1]1696{
1697 ipc_callid_t callid;
1698 if (!async_data_write_receive(&callid, NULL)) {
1699 ipc_answer_0(callid, EINVAL);
1700 return EINVAL;
1701 }
1702
1703 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1704 dataptr);
1705 if (msg == 0) {
1706 ipc_answer_0(callid, EINVAL);
1707 return EINVAL;
1708 }
1709
1710 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1711 IPC_FF_ROUTE_FROM_ME);
1712 if (retval != EOK) {
[a281fc82]1713 async_wait_for(msg, NULL);
[b4cbef1]1714 ipc_answer_0(callid, retval);
1715 return retval;
1716 }
1717
[96b02eb9]1718 sysarg_t rc;
[b4cbef1]1719 async_wait_for(msg, &rc);
1720
1721 return (int) rc;
1722}
1723
[a46da63]1724/** @}
[b2951e2]1725 */
Note: See TracBrowser for help on using the repository browser.