source: mainline/uspace/lib/c/generic/async.c@ c80fdd0

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

Track and reference count connections from the same client task.

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