source: mainline/uspace/lib/libc/generic/async.c@ c4702804

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

close_callid is not bool.

  • Property mode set to 100644
File size: 35.3 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
[01ff41c]122 ipcarg_t retval;
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
135typedef struct {
[49d072e]136 awaiter_t wdata;
[c07544d3]137
[e70bfa5]138 /** Hash table link. */
139 link_t link;
[c07544d3]140
[e70bfa5]141 /** Incoming phone hash. */
[c07544d3]142 ipcarg_t in_phone_hash;
143
[e70bfa5]144 /** Messages that should be delivered to this fibril. */
[c07544d3]145 link_t msg_queue;
146
[e70bfa5]147 /** Identification of the opening call. */
[80649a91]148 ipc_callid_t callid;
[e70bfa5]149 /** Call data of the opening call. */
[80649a91]150 ipc_call_t call;
[c07544d3]151
[e70bfa5]152 /** Identification of the closing call. */
153 ipc_callid_t close_callid;
[c07544d3]154
[e70bfa5]155 /** Fibril function that will be used to handle the connection. */
[bc1f1c2]156 void (*cfibril)(ipc_callid_t, ipc_call_t *);
[80649a91]157} connection_t;
158
[bc1f1c2]159/** Identifier of the incoming connection handled by the current fibril. */
[26360f7]160fibril_local connection_t *FIBRIL_connection;
[e70bfa5]161
[da0c91e7]162static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
[51dbadf3]163static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
[36c9234]164
165/**
166 * Pointer to a fibril function that will be used to handle connections.
167 */
[da0c91e7]168static async_client_conn_t client_connection = default_client_connection;
[c07544d3]169
[36c9234]170/**
171 * Pointer to a fibril function that will be used to handle interrupt
172 * notifications.
173 */
[51dbadf3]174static async_client_conn_t interrupt_received = default_interrupt_received;
[da0c91e7]175
[c07544d3]176static hash_table_t conn_hash_table;
177static LIST_INITIALIZE(timeout_list);
178
179#define CONN_HASH_TABLE_CHAINS 32
[80649a91]180
[e70bfa5]181/** Compute hash into the connection hash table based on the source phone hash.
182 *
[c07544d3]183 * @param key Pointer to source phone hash.
184 *
185 * @return Index into the connection hash table.
[e70bfa5]186 *
187 */
[80649a91]188static hash_index_t conn_hash(unsigned long *key)
[450cd3a]189{
[80649a91]190 assert(key);
[c07544d3]191 return (((*key) >> 4) % CONN_HASH_TABLE_CHAINS);
[450cd3a]192}
[06502f7d]193
[e70bfa5]194/** Compare hash table item with a key.
195 *
[c07544d3]196 * @param key Array containing the source phone hash as the only item.
197 * @param keys Expected 1 but ignored.
198 * @param item Connection hash table item.
199 *
200 * @return True on match, false otherwise.
[e70bfa5]201 *
202 */
[80649a91]203static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
[450cd3a]204{
[c07544d3]205 connection_t *hs = hash_table_get_instance(item, connection_t, link);
206 return (key[0] == hs->in_phone_hash);
[450cd3a]207}
[06502f7d]208
[e70bfa5]209/** Connection hash table removal callback function.
210 *
211 * This function is called whenever a connection is removed from the connection
212 * hash table.
213 *
[c07544d3]214 * @param item Connection hash table item being removed.
215 *
[e70bfa5]216 */
[80649a91]217static void conn_remove(link_t *item)
[450cd3a]218{
[80649a91]219 free(hash_table_get_instance(item, connection_t, link));
[450cd3a]220}
221
[80649a91]222
[e70bfa5]223/** Operations for the connection hash table. */
[80649a91]224static hash_table_operations_t conn_hash_table_ops = {
225 .hash = conn_hash,
226 .compare = conn_compare,
227 .remove_callback = conn_remove
228};
229
[e70bfa5]230/** Sort in current fibril's timeout request.
[49d072e]231 *
[c07544d3]232 * @param wd Wait data of the current fibril.
233 *
[49d072e]234 */
[b6ee5b1]235void async_insert_timeout(awaiter_t *wd)
[49d072e]236{
[f53cc81]237 wd->to_event.occurred = false;
238 wd->to_event.inlist = true;
[c07544d3]239
240 link_t *tmp = timeout_list.next;
[49d072e]241 while (tmp != &timeout_list) {
[f53cc81]242 awaiter_t *cur;
[c07544d3]243
[f53cc81]244 cur = list_get_instance(tmp, awaiter_t, to_event.link);
245 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
[49d072e]246 break;
247 tmp = tmp->next;
248 }
[c07544d3]249
[f53cc81]250 list_append(&wd->to_event.link, tmp);
[49d072e]251}
252
[e70bfa5]253/** Try to route a call to an appropriate connection fibril.
[80649a91]254 *
[36c9234]255 * If the proper connection fibril is found, a message with the call is added to
256 * its message queue. If the fibril was not active, it is activated and all
257 * timeouts are unregistered.
258 *
[c07544d3]259 * @param callid Hash of the incoming call.
260 * @param call Data of the incoming call.
261 *
262 * @return False if the call doesn't match any connection.
263 * True if the call was passed to the respective connection fibril.
[36c9234]264 *
[80649a91]265 */
[c07544d3]266static bool route_call(ipc_callid_t callid, ipc_call_t *call)
[450cd3a]267{
[01ff41c]268 futex_down(&async_futex);
[c07544d3]269
270 unsigned long key = call->in_phone_hash;
271 link_t *hlp = hash_table_find(&conn_hash_table, &key);
272
[80649a91]273 if (!hlp) {
[01ff41c]274 futex_up(&async_futex);
[c07544d3]275 return false;
[450cd3a]276 }
[c07544d3]277
278 connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
279
280 msg_t *msg = malloc(sizeof(*msg));
281 if (!msg) {
282 futex_up(&async_futex);
283 return false;
284 }
285
[80649a91]286 msg->callid = callid;
287 msg->call = *call;
288 list_append(&msg->link, &conn->msg_queue);
[c07544d3]289
[41269bd]290 if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP)
291 conn->close_callid = callid;
[80649a91]292
[36c9234]293 /* If the connection fibril is waiting for an event, activate it */
[49d072e]294 if (!conn->wdata.active) {
[c07544d3]295
[49d072e]296 /* If in timeout list, remove it */
[f53cc81]297 if (conn->wdata.to_event.inlist) {
298 conn->wdata.to_event.inlist = false;
299 list_remove(&conn->wdata.to_event.link);
[49d072e]300 }
[c07544d3]301
302 conn->wdata.active = true;
[bc1f1c2]303 fibril_add_ready(conn->wdata.fid);
[80649a91]304 }
[c07544d3]305
[01ff41c]306 futex_up(&async_futex);
[c07544d3]307 return true;
308}
[80649a91]309
[c07544d3]310/** Notification fibril.
311 *
312 * When a notification arrives, a fibril with this implementing function is
313 * created. It calls interrupt_received() and does the final cleanup.
314 *
315 * @param arg Message structure pointer.
316 *
317 * @return Always zero.
318 *
319 */
320static int notification_fibril(void *arg)
321{
322 msg_t *msg = (msg_t *) arg;
323 interrupt_received(msg->callid, &msg->call);
324
325 free(msg);
326 return 0;
327}
328
329/** Process interrupt notification.
330 *
331 * A new fibril is created which would process the notification.
332 *
333 * @param callid Hash of the incoming call.
334 * @param call Data of the incoming call.
335 *
336 * @return False if an error occured.
337 * True if the call was passed to the notification fibril.
338 *
339 */
340static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
341{
342 futex_down(&async_futex);
343
344 msg_t *msg = malloc(sizeof(*msg));
345 if (!msg) {
346 futex_up(&async_futex);
347 return false;
348 }
349
350 msg->callid = callid;
351 msg->call = *call;
352
353 fid_t fid = fibril_create(notification_fibril, msg);
354 fibril_add_ready(fid);
355
356 futex_up(&async_futex);
357 return true;
[80649a91]358}
359
[e70bfa5]360/** Return new incoming message for the current (fibril-local) connection.
361 *
[c07544d3]362 * @param call Storage where the incoming call data will be stored.
363 * @param usecs Timeout in microseconds. Zero denotes no timeout.
364 *
365 * @return If no timeout was specified, then a hash of the
366 * incoming call is returned. If a timeout is specified,
367 * then a hash of the incoming call is returned unless
368 * the timeout expires prior to receiving a message. In
369 * that case zero is returned.
[e70bfa5]370 *
371 */
[49d072e]372ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
[80649a91]373{
[bc1f1c2]374 assert(FIBRIL_connection);
[c07544d3]375
376 /* Why doing this?
377 * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
[6c46350]378 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
[c07544d3]379 * I would never expect to find so many errors in
380 * a compiler.
[6c46350]381 */
[c07544d3]382 connection_t *conn = FIBRIL_connection;
383
[01ff41c]384 futex_down(&async_futex);
[c07544d3]385
[49d072e]386 if (usecs) {
[f53cc81]387 gettimeofday(&conn->wdata.to_event.expires, NULL);
388 tv_add(&conn->wdata.to_event.expires, usecs);
[c07544d3]389 } else
[f53cc81]390 conn->wdata.to_event.inlist = false;
[c07544d3]391
[e70bfa5]392 /* If nothing in queue, wait until something arrives */
[6c46350]393 while (list_empty(&conn->msg_queue)) {
[085bd54]394 if (usecs)
[b6ee5b1]395 async_insert_timeout(&conn->wdata);
[c07544d3]396
397 conn->wdata.active = false;
398
[c7509e5]399 /*
400 * Note: the current fibril will be rescheduled either due to a
401 * timeout or due to an arriving message destined to it. In the
402 * former case, handle_expired_timeouts() and, in the latter
403 * case, route_call() will perform the wakeup.
404 */
[116d3f6f]405 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]406
[e70bfa5]407 /*
[c07544d3]408 * Futex is up after getting back from async_manager.
409 * Get it again.
[c7509e5]410 */
[49d072e]411 futex_down(&async_futex);
[f53cc81]412 if ((usecs) && (conn->wdata.to_event.occurred)
[c07544d3]413 && (list_empty(&conn->msg_queue))) {
[e70bfa5]414 /* If we timed out -> exit */
[49d072e]415 futex_up(&async_futex);
416 return 0;
417 }
[450cd3a]418 }
419
[c07544d3]420 msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
[80649a91]421 list_remove(&msg->link);
[c07544d3]422
423 ipc_callid_t callid = msg->callid;
[80649a91]424 *call = msg->call;
425 free(msg);
426
[01ff41c]427 futex_up(&async_futex);
[80649a91]428 return callid;
429}
430
[36c9234]431/** Default fibril function that gets called to handle new connection.
[a2cd194]432 *
[e70bfa5]433 * This function is defined as a weak symbol - to be redefined in user code.
[36c9234]434 *
[c07544d3]435 * @param callid Hash of the incoming call.
436 * @param call Data of the incoming call.
437 *
[a2cd194]438 */
[da0c91e7]439static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
[80649a91]440{
[b74959bd]441 ipc_answer_0(callid, ENOENT);
[80649a91]442}
[36c9234]443
444/** Default fibril function that gets called to handle interrupt notifications.
445 *
[c07544d3]446 * This function is defined as a weak symbol - to be redefined in user code.
447 *
448 * @param callid Hash of the incoming call.
449 * @param call Data of the incoming call.
450 *
[36c9234]451 */
[51dbadf3]452static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
[44c6d88d]453{
454}
455
[f2f0392]456/** Wrapper for client connection fibril.
457 *
[36c9234]458 * When a new connection arrives, a fibril with this implementing function is
[f2f0392]459 * created. It calls client_connection() and does the final cleanup.
[a2cd194]460 *
[c07544d3]461 * @param arg Connection structure pointer.
462 *
463 * @return Always zero.
[a2cd194]464 *
465 */
[c07544d3]466static int connection_fibril(void *arg)
[80649a91]467{
[c07544d3]468 /*
469 * Setup fibril-local connection pointer and call client_connection().
470 *
471 */
[bc1f1c2]472 FIBRIL_connection = (connection_t *) arg;
473 FIBRIL_connection->cfibril(FIBRIL_connection->callid,
474 &FIBRIL_connection->call);
[a46da63]475
[36c9234]476 /* Remove myself from the connection hash table */
[01ff41c]477 futex_down(&async_futex);
[c07544d3]478 unsigned long key = FIBRIL_connection->in_phone_hash;
[a2cd194]479 hash_table_remove(&conn_hash_table, &key, 1);
[01ff41c]480 futex_up(&async_futex);
[a46da63]481
[36c9234]482 /* Answer all remaining messages with EHANGUP */
[bc1f1c2]483 while (!list_empty(&FIBRIL_connection->msg_queue)) {
[cc27c8c5]484 msg_t *msg;
[c07544d3]485
[cc27c8c5]486 msg = list_get_instance(FIBRIL_connection->msg_queue.next,
487 msg_t, link);
[a2cd194]488 list_remove(&msg->link);
[b74959bd]489 ipc_answer_0(msg->callid, EHANGUP);
[a2cd194]490 free(msg);
491 }
[c07544d3]492
[bc1f1c2]493 if (FIBRIL_connection->close_callid)
[b74959bd]494 ipc_answer_0(FIBRIL_connection->close_callid, EOK);
[a46da63]495
496 return 0;
[80649a91]497}
498
[f2f0392]499/** Create a new fibril for a new connection.
[80649a91]500 *
[c07544d3]501 * Create new fibril for connection, fill in connection structures and inserts
[f2f0392]502 * it into the hash table, so that later we can easily do routing of messages to
503 * particular fibrils.
[53ca318]504 *
[c07544d3]505 * @param in_phone_hash Identification of the incoming connection.
506 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
507 * If callid is zero, the connection was opened by
508 * accepting the IPC_M_CONNECT_TO_ME call and this function
509 * is called directly by the server.
510 * @param call Call data of the opening call.
511 * @param cfibril Fibril function that should be called upon opening the
512 * connection.
513 *
514 * @return New fibril id or NULL on failure.
[36c9234]515 *
[80649a91]516 */
[bc1f1c2]517fid_t async_new_connection(ipcarg_t in_phone_hash, ipc_callid_t callid,
518 ipc_call_t *call, void (*cfibril)(ipc_callid_t, ipc_call_t *))
[80649a91]519{
[c07544d3]520 connection_t *conn = malloc(sizeof(*conn));
[80649a91]521 if (!conn) {
[6675c70]522 if (callid)
[b74959bd]523 ipc_answer_0(callid, ENOMEM);
[53ca318]524 return NULL;
[80649a91]525 }
[c07544d3]526
[44c6d88d]527 conn->in_phone_hash = in_phone_hash;
[80649a91]528 list_initialize(&conn->msg_queue);
529 conn->callid = callid;
[c4702804]530 conn->close_callid = 0;
[c07544d3]531
[eaf34f7]532 if (call)
533 conn->call = *call;
[6b21292]534
[c07544d3]535 /* We will activate the fibril ASAP */
536 conn->wdata.active = true;
537 conn->cfibril = cfibril;
[bc1f1c2]538 conn->wdata.fid = fibril_create(connection_fibril, conn);
[c07544d3]539
[bc1f1c2]540 if (!conn->wdata.fid) {
[80649a91]541 free(conn);
[6675c70]542 if (callid)
[b74959bd]543 ipc_answer_0(callid, ENOMEM);
[53ca318]544 return NULL;
[80649a91]545 }
[6b21292]546
[36c9234]547 /* Add connection to the connection hash table */
[9db9b10]548 unsigned long key = conn->in_phone_hash;
[c07544d3]549
[01ff41c]550 futex_down(&async_futex);
[80649a91]551 hash_table_insert(&conn_hash_table, &key, &conn->link);
[01ff41c]552 futex_up(&async_futex);
[6b21292]553
[bc1f1c2]554 fibril_add_ready(conn->wdata.fid);
[6b21292]555
[bc1f1c2]556 return conn->wdata.fid;
[80649a91]557}
558
[36c9234]559/** Handle a call that was received.
560 *
561 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
562 * Otherwise the call is routed to its connection fibril.
563 *
[c07544d3]564 * @param callid Hash of the incoming call.
565 * @param call Data of the incoming call.
[6b21292]566 *
[36c9234]567 */
[80649a91]568static void handle_call(ipc_callid_t callid, ipc_call_t *call)
569{
[44c6d88d]570 /* Unrouted call - do some default behaviour */
[15039b67]571 if ((callid & IPC_CALLID_NOTIFICATION)) {
[c07544d3]572 process_notification(callid, call);
[9db9b10]573 goto out;
[6b21292]574 }
575
[15039b67]576 switch (IPC_GET_METHOD(*call)) {
[2c0e5d2]577 case IPC_M_CONNECT_ME:
[80649a91]578 case IPC_M_CONNECT_ME_TO:
[f2f0392]579 /* Open new connection with fibril etc. */
[b61d47d]580 async_new_connection(IPC_GET_ARG5(*call), callid, call,
[bc1f1c2]581 client_connection);
[9db9b10]582 goto out;
[80649a91]583 }
[6b21292]584
[36c9234]585 /* Try to route the call through the connection hash table */
[44c6d88d]586 if (route_call(callid, call))
[9db9b10]587 goto out;
[6b21292]588
[44c6d88d]589 /* Unknown call from unknown phone - hang it up */
[b74959bd]590 ipc_answer_0(callid, EHANGUP);
[9db9b10]591 return;
592
593out:
[953769f]594 ;
[450cd3a]595}
596
[f2f0392]597/** Fire all timeouts that expired. */
[c042bdd]598static void handle_expired_timeouts(void)
599{
600 struct timeval tv;
[36c9234]601 gettimeofday(&tv, NULL);
[c07544d3]602
[c042bdd]603 futex_down(&async_futex);
[c07544d3]604
605 link_t *cur = timeout_list.next;
[c042bdd]606 while (cur != &timeout_list) {
[f53cc81]607 awaiter_t *waiter;
[c07544d3]608
[f53cc81]609 waiter = list_get_instance(cur, awaiter_t, to_event.link);
610 if (tv_gt(&waiter->to_event.expires, &tv))
[c042bdd]611 break;
[f53cc81]612
[c042bdd]613 cur = cur->next;
[f53cc81]614
615 list_remove(&waiter->to_event.link);
616 waiter->to_event.inlist = false;
617 waiter->to_event.occurred = true;
[c07544d3]618
[36c9234]619 /*
[c07544d3]620 * Redundant condition?
621 * The fibril should not be active when it gets here.
[c042bdd]622 */
[49d072e]623 if (!waiter->active) {
[c07544d3]624 waiter->active = true;
[bc1f1c2]625 fibril_add_ready(waiter->fid);
[c042bdd]626 }
627 }
[c07544d3]628
[c042bdd]629 futex_up(&async_futex);
630}
631
[36c9234]632/** Endless loop dispatching incoming calls and answers.
633 *
[c07544d3]634 * @return Never returns.
635 *
[36c9234]636 */
[085bd54]637static int async_manager_worker(void)
[80649a91]638{
[c07544d3]639 while (true) {
[116d3f6f]640 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
[a46da63]641 futex_up(&async_futex);
[36c9234]642 /*
643 * async_futex is always held when entering a manager
644 * fibril.
[a46da63]645 */
[80649a91]646 continue;
647 }
[c07544d3]648
[c042bdd]649 futex_down(&async_futex);
[c07544d3]650
651 suseconds_t timeout;
[c042bdd]652 if (!list_empty(&timeout_list)) {
[cc27c8c5]653 awaiter_t *waiter = list_get_instance(timeout_list.next,
[f53cc81]654 awaiter_t, to_event.link);
[c07544d3]655
656 struct timeval tv;
[bc1f1c2]657 gettimeofday(&tv, NULL);
[c07544d3]658
[f53cc81]659 if (tv_gteq(&tv, &waiter->to_event.expires)) {
[6c46350]660 futex_up(&async_futex);
[c042bdd]661 handle_expired_timeouts();
662 continue;
663 } else
[f53cc81]664 timeout = tv_sub(&waiter->to_event.expires,
665 &tv);
[c042bdd]666 } else
[0b99e40]667 timeout = SYNCH_NO_TIMEOUT;
[c07544d3]668
[c042bdd]669 futex_up(&async_futex);
[8619f25]670
671 atomic_inc(&threads_in_ipc_wait);
[c07544d3]672
673 ipc_call_t call;
[cc27c8c5]674 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
675 SYNCH_FLAGS_NONE);
[c07544d3]676
[8619f25]677 atomic_dec(&threads_in_ipc_wait);
678
[0b99e40]679 if (!callid) {
[c042bdd]680 handle_expired_timeouts();
[0b99e40]681 continue;
682 }
[c07544d3]683
684 if (callid & IPC_CALLID_ANSWERED)
[80649a91]685 continue;
[c07544d3]686
[80649a91]687 handle_call(callid, &call);
688 }
[a46da63]689
690 return 0;
[80649a91]691}
692
[36c9234]693/** Function to start async_manager as a standalone fibril.
[c07544d3]694 *
[36c9234]695 * When more kernel threads are used, one async manager should exist per thread.
696 *
[c07544d3]697 * @param arg Unused.
698 * @return Never returns.
[36c9234]699 *
[a2cd194]700 */
[9591265]701static int async_manager_fibril(void *arg)
[80649a91]702{
[a46da63]703 futex_up(&async_futex);
[c07544d3]704
[36c9234]705 /*
706 * async_futex is always locked when entering manager
707 */
[085bd54]708 async_manager_worker();
[a46da63]709
710 return 0;
[80649a91]711}
[450cd3a]712
[36c9234]713/** Add one manager to manager list. */
[80649a91]714void async_create_manager(void)
[450cd3a]715{
[c07544d3]716 fid_t fid = fibril_create(async_manager_fibril, NULL);
[bc1f1c2]717 fibril_add_manager(fid);
[80649a91]718}
719
720/** Remove one manager from manager list */
721void async_destroy_manager(void)
722{
[bc1f1c2]723 fibril_remove_manager();
[80649a91]724}
725
[36c9234]726/** Initialize the async framework.
727 *
[c07544d3]728 * @return Zero on success or an error code.
[36c9234]729 */
[db24058]730int __async_init(void)
[80649a91]731{
[bc1f1c2]732 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
733 &conn_hash_table_ops)) {
[80649a91]734 printf("%s: cannot create hash table\n", "async");
735 return ENOMEM;
736 }
737
[a46da63]738 return 0;
[450cd3a]739}
[01ff41c]740
[36c9234]741/** Reply received callback.
[01ff41c]742 *
[36c9234]743 * This function is called whenever a reply for an asynchronous message sent out
744 * by the asynchronous framework is received.
745 *
746 * Notify the fibril which is waiting for this message that it has arrived.
747 *
[c07544d3]748 * @param arg Pointer to the asynchronous message record.
749 * @param retval Value returned in the answer.
750 * @param data Call data of the answer.
[01ff41c]751 */
[c07544d3]752static void reply_received(void *arg, int retval, ipc_call_t *data)
[01ff41c]753{
[9db9b10]754 futex_down(&async_futex);
755
[c07544d3]756 amsg_t *msg = (amsg_t *) arg;
[01ff41c]757 msg->retval = retval;
[c07544d3]758
[36c9234]759 /* Copy data after futex_down, just in case the call was detached */
[9db9b10]760 if ((msg->dataptr) && (data))
[c07544d3]761 *msg->dataptr = *data;
762
[c042bdd]763 write_barrier();
[c07544d3]764
[c042bdd]765 /* Remove message from timeout list */
[f53cc81]766 if (msg->wdata.to_event.inlist)
767 list_remove(&msg->wdata.to_event.link);
[c07544d3]768
769 msg->done = true;
[36c9234]770 if (!msg->wdata.active) {
[c07544d3]771 msg->wdata.active = true;
[bc1f1c2]772 fibril_add_ready(msg->wdata.fid);
[01ff41c]773 }
[c07544d3]774
[01ff41c]775 futex_up(&async_futex);
776}
777
[36c9234]778/** Send message and return id of the sent message.
779 *
780 * The return value can be used as input for async_wait() to wait for
781 * completion.
[01ff41c]782 *
[c07544d3]783 * @param phoneid Handle of the phone that will be used for the send.
784 * @param method Service-defined method.
785 * @param arg1 Service-defined payload argument.
786 * @param arg2 Service-defined payload argument.
787 * @param arg3 Service-defined payload argument.
788 * @param arg4 Service-defined payload argument.
789 * @param dataptr If non-NULL, storage where the reply data will be
790 * stored.
791 *
792 * @return Hash of the sent message or 0 on error.
[36c9234]793 *
[01ff41c]794 */
[0cc4313]795aid_t async_send_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
796 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
[01ff41c]797{
[c07544d3]798 amsg_t *msg = malloc(sizeof(*msg));
799
800 if (!msg)
801 return 0;
[6b21292]802
[c07544d3]803 msg->done = false;
[01ff41c]804 msg->dataptr = dataptr;
[6b21292]805
[f53cc81]806 msg->wdata.to_event.inlist = false;
[36c9234]807 /* We may sleep in the next method, but it will use its own mechanism */
[c07544d3]808 msg->wdata.active = true;
809
[0cc4313]810 ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
[c07544d3]811 reply_received, true);
[6b21292]812
[01ff41c]813 return (aid_t) msg;
814}
815
[90f5d64]816/** Send message and return id of the sent message
817 *
[36c9234]818 * The return value can be used as input for async_wait() to wait for
819 * completion.
820 *
[c07544d3]821 * @param phoneid Handle of the phone that will be used for the send.
822 * @param method Service-defined method.
823 * @param arg1 Service-defined payload argument.
824 * @param arg2 Service-defined payload argument.
825 * @param arg3 Service-defined payload argument.
826 * @param arg4 Service-defined payload argument.
827 * @param arg5 Service-defined payload argument.
828 * @param dataptr If non-NULL, storage where the reply data will be
829 * stored.
830 *
831 * @return Hash of the sent message or 0 on error.
[36c9234]832 *
[90f5d64]833 */
[0cc4313]834aid_t async_send_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
835 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5,
836 ipc_call_t *dataptr)
[90f5d64]837{
[c07544d3]838 amsg_t *msg = malloc(sizeof(*msg));
[6b21292]839
[c07544d3]840 if (!msg)
841 return 0;
842
843 msg->done = false;
[90f5d64]844 msg->dataptr = dataptr;
[6b21292]845
[f53cc81]846 msg->wdata.to_event.inlist = false;
[36c9234]847 /* We may sleep in next method, but it will use its own mechanism */
[c07544d3]848 msg->wdata.active = true;
[6b21292]849
[0cc4313]850 ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
[c07544d3]851 reply_received, true);
[6b21292]852
[90f5d64]853 return (aid_t) msg;
854}
855
[36c9234]856/** Wait for a message sent by the async framework.
[01ff41c]857 *
[c07544d3]858 * @param amsgid Hash of the message to wait for.
859 * @param retval Pointer to storage where the retval of the answer will
860 * be stored.
861 *
[01ff41c]862 */
863void async_wait_for(aid_t amsgid, ipcarg_t *retval)
864{
865 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]866
[01ff41c]867 futex_down(&async_futex);
868 if (msg->done) {
869 futex_up(&async_futex);
870 goto done;
871 }
[c07544d3]872
[bc1f1c2]873 msg->wdata.fid = fibril_get_id();
[c07544d3]874 msg->wdata.active = false;
[f53cc81]875 msg->wdata.to_event.inlist = false;
[c07544d3]876
[36c9234]877 /* Leave the async_futex locked when entering this function */
[116d3f6f]878 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]879
880 /* Futex is up automatically after fibril_switch */
881
[01ff41c]882done:
883 if (retval)
884 *retval = msg->retval;
[c07544d3]885
[01ff41c]886 free(msg);
887}
[0b99e40]888
[36c9234]889/** Wait for a message sent by the async framework, timeout variant.
[c042bdd]890 *
[c07544d3]891 * @param amsgid Hash of the message to wait for.
892 * @param retval Pointer to storage where the retval of the answer will
893 * be stored.
894 * @param timeout Timeout in microseconds.
895 *
896 * @return Zero on success, ETIMEOUT if the timeout has expired.
[c042bdd]897 *
898 */
899int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
900{
901 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]902
[86029498]903 /* TODO: Let it go through the event read at least once */
904 if (timeout < 0)
905 return ETIMEOUT;
[c07544d3]906
[c042bdd]907 futex_down(&async_futex);
908 if (msg->done) {
909 futex_up(&async_futex);
910 goto done;
911 }
[c07544d3]912
[f53cc81]913 gettimeofday(&msg->wdata.to_event.expires, NULL);
914 tv_add(&msg->wdata.to_event.expires, timeout);
[c07544d3]915
[bc1f1c2]916 msg->wdata.fid = fibril_get_id();
[c07544d3]917 msg->wdata.active = false;
[b6ee5b1]918 async_insert_timeout(&msg->wdata);
[c07544d3]919
[36c9234]920 /* Leave the async_futex locked when entering this function */
[116d3f6f]921 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]922
923 /* Futex is up automatically after fibril_switch */
924
[c042bdd]925 if (!msg->done)
926 return ETIMEOUT;
[c07544d3]927
[c042bdd]928done:
929 if (retval)
930 *retval = msg->retval;
[c07544d3]931
[c042bdd]932 free(msg);
[c07544d3]933
[c042bdd]934 return 0;
935}
[0b99e40]936
[36c9234]937/** Wait for specified time.
[44c6d88d]938 *
[36c9234]939 * The current fibril is suspended but the thread continues to execute.
940 *
[c07544d3]941 * @param timeout Duration of the wait in microseconds.
942 *
[44c6d88d]943 */
944void async_usleep(suseconds_t timeout)
945{
[c07544d3]946 amsg_t *msg = malloc(sizeof(*msg));
[44c6d88d]947
948 if (!msg)
949 return;
[6b21292]950
[bc1f1c2]951 msg->wdata.fid = fibril_get_id();
[c07544d3]952 msg->wdata.active = false;
[6b21292]953
[f53cc81]954 gettimeofday(&msg->wdata.to_event.expires, NULL);
955 tv_add(&msg->wdata.to_event.expires, timeout);
[6b21292]956
[44c6d88d]957 futex_down(&async_futex);
[c07544d3]958
[b6ee5b1]959 async_insert_timeout(&msg->wdata);
[c07544d3]960
[36c9234]961 /* Leave the async_futex locked when entering this function */
[116d3f6f]962 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]963
964 /* Futex is up automatically after fibril_switch() */
965
[44c6d88d]966 free(msg);
967}
[da0c91e7]968
[36c9234]969/** Setter for client_connection function pointer.
[da0c91e7]970 *
[c07544d3]971 * @param conn Function that will implement a new connection fibril.
972 *
[da0c91e7]973 */
974void async_set_client_connection(async_client_conn_t conn)
975{
976 client_connection = conn;
977}
[36c9234]978
979/** Setter for interrupt_received function pointer.
980 *
[c07544d3]981 * @param intr Function that will implement a new interrupt
982 * notification fibril.
[36c9234]983 */
[c07544d3]984void async_set_interrupt_received(async_client_conn_t intr)
[51dbadf3]985{
[c07544d3]986 interrupt_received = intr;
[51dbadf3]987}
[085bd54]988
[0cc4313]989/** Pseudo-synchronous message sending - fast version.
990 *
991 * Send message asynchronously and return only after the reply arrives.
992 *
993 * This function can only transfer 4 register payload arguments. For
994 * transferring more arguments, see the slower async_req_slow().
995 *
[c07544d3]996 * @param phoneid Hash of the phone through which to make the call.
997 * @param method Method of the call.
998 * @param arg1 Service-defined payload argument.
999 * @param arg2 Service-defined payload argument.
1000 * @param arg3 Service-defined payload argument.
1001 * @param arg4 Service-defined payload argument.
1002 * @param r1 If non-NULL, storage for the 1st reply argument.
1003 * @param r2 If non-NULL, storage for the 2nd reply argument.
1004 * @param r3 If non-NULL, storage for the 3rd reply argument.
1005 * @param r4 If non-NULL, storage for the 4th reply argument.
1006 * @param r5 If non-NULL, storage for the 5th reply argument.
1007 *
1008 * @return Return code of the reply or a negative error code.
1009 *
[0cc4313]1010 */
1011ipcarg_t async_req_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
1012 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t *r1, ipcarg_t *r2,
1013 ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
[085bd54]1014{
[0cc4313]1015 ipc_call_t result;
1016 aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1017 &result);
[c07544d3]1018
1019 ipcarg_t rc;
[0cc4313]1020 async_wait_for(eid, &rc);
[c07544d3]1021
1022 if (r1)
[0cc4313]1023 *r1 = IPC_GET_ARG1(result);
[c07544d3]1024
[0cc4313]1025 if (r2)
1026 *r2 = IPC_GET_ARG2(result);
[c07544d3]1027
[0cc4313]1028 if (r3)
1029 *r3 = IPC_GET_ARG3(result);
[c07544d3]1030
[0cc4313]1031 if (r4)
1032 *r4 = IPC_GET_ARG4(result);
[c07544d3]1033
[0cc4313]1034 if (r5)
1035 *r5 = IPC_GET_ARG5(result);
[c07544d3]1036
[0cc4313]1037 return rc;
[085bd54]1038}
1039
[0cc4313]1040/** Pseudo-synchronous message sending - slow version.
1041 *
1042 * Send message asynchronously and return only after the reply arrives.
1043 *
[c07544d3]1044 * @param phoneid Hash of the phone through which to make the call.
1045 * @param method Method of the call.
1046 * @param arg1 Service-defined payload argument.
1047 * @param arg2 Service-defined payload argument.
1048 * @param arg3 Service-defined payload argument.
1049 * @param arg4 Service-defined payload argument.
1050 * @param arg5 Service-defined payload argument.
1051 * @param r1 If non-NULL, storage for the 1st reply argument.
1052 * @param r2 If non-NULL, storage for the 2nd reply argument.
1053 * @param r3 If non-NULL, storage for the 3rd reply argument.
1054 * @param r4 If non-NULL, storage for the 4th reply argument.
1055 * @param r5 If non-NULL, storage for the 5th reply argument.
1056 *
1057 * @return Return code of the reply or a negative error code.
1058 *
[0cc4313]1059 */
1060ipcarg_t async_req_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
1061 ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *r1,
1062 ipcarg_t *r2, ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
[085bd54]1063{
[0cc4313]1064 ipc_call_t result;
1065 aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1066 &result);
[c07544d3]1067
1068 ipcarg_t rc;
[0cc4313]1069 async_wait_for(eid, &rc);
[c07544d3]1070
1071 if (r1)
[0cc4313]1072 *r1 = IPC_GET_ARG1(result);
[c07544d3]1073
[0cc4313]1074 if (r2)
1075 *r2 = IPC_GET_ARG2(result);
[c07544d3]1076
[0cc4313]1077 if (r3)
1078 *r3 = IPC_GET_ARG3(result);
[c07544d3]1079
[0cc4313]1080 if (r4)
1081 *r4 = IPC_GET_ARG4(result);
[c07544d3]1082
[0cc4313]1083 if (r5)
1084 *r5 = IPC_GET_ARG5(result);
[c07544d3]1085
[0cc4313]1086 return rc;
[085bd54]1087}
[b2951e2]1088
[0da4e41]1089/** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
1090 *
1091 * @param phoneid Phone that will be used to contact the receiving side.
1092 * @param dst Destination address space area base.
1093 * @param size Size of the destination address space area.
1094 * @param arg User defined argument.
1095 * @param flags Storage where the received flags will be stored. Can be
1096 * NULL.
1097 *
1098 * @return Zero on success or a negative error code from errno.h.
1099 */
1100int async_share_in_start(int phoneid, void *dst, size_t size, ipcarg_t arg,
1101 int *flags)
1102{
1103 int res;
1104 sysarg_t tmp_flags;
1105 res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (ipcarg_t) dst,
1106 (ipcarg_t) size, arg, NULL, &tmp_flags);
1107 if (flags)
1108 *flags = tmp_flags;
1109 return res;
1110}
1111
1112/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1113 *
1114 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN calls
1115 * so that the user doesn't have to remember the meaning of each IPC argument.
1116 *
1117 * So far, this wrapper is to be used from within a connection fibril.
1118 *
1119 * @param callid Storage where the hash of the IPC_M_SHARE_IN call will
1120 * be stored.
1121 * @param size Destination address space area size.
1122 *
1123 * @return Non-zero on success, zero on failure.
1124 */
1125int async_share_in_receive(ipc_callid_t *callid, size_t *size)
1126{
1127 ipc_call_t data;
1128
1129 assert(callid);
1130 assert(size);
1131
1132 *callid = async_get_call(&data);
1133 if (IPC_GET_METHOD(data) != IPC_M_SHARE_IN)
1134 return 0;
1135 *size = (size_t) IPC_GET_ARG2(data);
1136 return 1;
1137}
1138
1139/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1140 *
1141 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1142 * so that the user doesn't have to remember the meaning of each IPC argument.
1143 *
1144 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1145 * @param src Source address space base.
1146 * @param flags Flags to be used for sharing. Bits can be only cleared.
1147 *
1148 * @return Zero on success or a value from @ref errno.h on failure.
1149 */
1150int async_share_in_finalize(ipc_callid_t callid, void *src, int flags)
1151{
1152 return ipc_share_in_finalize(callid, src, flags);
1153}
1154
1155/** Wrapper for making IPC_M_SHARE_OUT calls using the async framework.
1156 *
1157 * @param phoneid Phone that will be used to contact the receiving side.
1158 * @param src Source address space area base address.
1159 * @param flags Flags to be used for sharing. Bits can be only cleared.
1160 *
1161 * @return Zero on success or a negative error code from errno.h.
1162 */
1163int async_share_out_start(int phoneid, void *src, int flags)
1164{
1165 return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (ipcarg_t) src, 0,
1166 (ipcarg_t) flags);
1167}
1168
1169/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1170 *
1171 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT calls
1172 * so that the user doesn't have to remember the meaning of each IPC argument.
1173 *
1174 * So far, this wrapper is to be used from within a connection fibril.
1175 *
1176 * @param callid Storage where the hash of the IPC_M_SHARE_OUT call will
1177 * be stored.
1178 * @param size Storage where the source address space area size will be
1179 * stored.
1180 * @param flags Storage where the sharing flags will be stored.
1181 *
1182 * @return Non-zero on success, zero on failure.
1183 */
1184int async_share_out_receive(ipc_callid_t *callid, size_t *size, int *flags)
1185{
1186 ipc_call_t data;
1187
1188 assert(callid);
1189 assert(size);
1190 assert(flags);
1191
1192 *callid = async_get_call(&data);
1193 if (IPC_GET_METHOD(data) != IPC_M_SHARE_OUT)
1194 return 0;
1195 *size = (size_t) IPC_GET_ARG2(data);
1196 *flags = (int) IPC_GET_ARG3(data);
1197 return 1;
1198}
1199
1200/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
1201 *
1202 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT calls
1203 * so that the user doesn't have to remember the meaning of each IPC argument.
1204 *
1205 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1206 * @param dst Destination address space area base address.
1207 *
1208 * @return Zero on success or a value from @ref errno.h on failure.
1209 */
1210int async_share_out_finalize(ipc_callid_t callid, void *dst)
1211{
1212 return ipc_share_out_finalize(callid, dst);
1213}
1214
1215
1216/** Wrapper for making IPC_M_DATA_READ calls using the async framework.
1217 *
1218 * @param phoneid Phone that will be used to contact the receiving side.
1219 * @param dst Address of the beginning of the destination buffer.
1220 * @param size Size of the destination buffer.
1221 *
1222 * @return Zero on success or a negative error code from errno.h.
1223 */
1224int async_data_read_start(int phoneid, void *dst, size_t size)
1225{
1226 return async_req_2_0(phoneid, IPC_M_DATA_READ, (ipcarg_t) dst,
1227 (ipcarg_t) size);
1228}
1229
1230/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
1231 *
1232 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ calls
1233 * so that the user doesn't have to remember the meaning of each IPC argument.
1234 *
1235 * So far, this wrapper is to be used from within a connection fibril.
1236 *
1237 * @param callid Storage where the hash of the IPC_M_DATA_READ call will
1238 * be stored.
1239 * @param size Storage where the maximum size will be stored. Can be
1240 * NULL.
1241 *
1242 * @return Non-zero on success, zero on failure.
1243 */
1244int async_data_read_receive(ipc_callid_t *callid, size_t *size)
1245{
1246 ipc_call_t data;
1247
1248 assert(callid);
1249
1250 *callid = async_get_call(&data);
1251 if (IPC_GET_METHOD(data) != IPC_M_DATA_READ)
1252 return 0;
1253 if (size)
1254 *size = (size_t) IPC_GET_ARG2(data);
1255 return 1;
1256}
1257
1258/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
1259 *
1260 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1261 * so that the user doesn't have to remember the meaning of each IPC argument.
1262 *
1263 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1264 * @param src Source address for the IPC_M_DATA_READ call.
1265 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
1266 * the maximum size announced by the sender.
1267 *
1268 * @return Zero on success or a value from @ref errno.h on failure.
1269 */
1270int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
1271{
1272 return ipc_data_read_finalize(callid, src, size);
1273}
1274
1275/** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
1276 *
1277 * @param phoneid Phone that will be used to contact the receiving side.
1278 * @param src Address of the beginning of the source buffer.
1279 * @param size Size of the source buffer.
1280 *
1281 * @return Zero on success or a negative error code from errno.h.
1282 */
1283int async_data_write_start(int phoneid, const void *src, size_t size)
1284{
1285 return async_req_2_0(phoneid, IPC_M_DATA_WRITE, (ipcarg_t) src,
1286 (ipcarg_t) size);
1287}
1288
1289/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
1290 *
1291 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE calls
1292 * so that the user doesn't have to remember the meaning of each IPC argument.
1293 *
1294 * So far, this wrapper is to be used from within a connection fibril.
1295 *
1296 * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
1297 * be stored.
1298 * @param size Storage where the suggested size will be stored. May be
1299 * NULL
1300 *
1301 * @return Non-zero on success, zero on failure.
1302 */
1303int async_data_write_receive(ipc_callid_t *callid, size_t *size)
1304{
1305 ipc_call_t data;
1306
1307 assert(callid);
1308
1309 *callid = async_get_call(&data);
1310 if (IPC_GET_METHOD(data) != IPC_M_DATA_WRITE)
1311 return 0;
1312 if (size)
1313 *size = (size_t) IPC_GET_ARG2(data);
1314 return 1;
1315}
1316
1317/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
1318 *
1319 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
1320 * so that the user doesn't have to remember the meaning of each IPC argument.
1321 *
1322 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1323 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
1324 * @param size Final size for the IPC_M_DATA_WRITE call.
1325 *
1326 * @return Zero on success or a value from @ref errno.h on failure.
1327 */
1328int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
1329{
1330 return ipc_data_write_finalize(callid, dst, size);
1331}
1332
[a46da63]1333/** @}
[b2951e2]1334 */
Note: See TracBrowser for help on using the repository browser.