source: mainline/uspace/lib/c/generic/async.c@ 64d2b10

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

libc: do not intermix low-level IPC methods with async framework methods

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