source: mainline/uspace/lib/c/generic/async.c@ 1e9f8ab

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1e9f8ab was c1c0184, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Make session management explicit.

  • Property mode set to 100644
File size: 40.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
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. */
[96b02eb9]142 sysarg_t in_phone_hash;
[c07544d3]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
[228e490]290 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
[41269bd]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)) {
[8c8f8d6]394 if (conn->close_callid) {
395 /*
396 * Handle the case when the connection was already
397 * closed by the client but the server did not notice
398 * the first IPC_M_PHONE_HUNGUP call and continues to
399 * call async_get_call_timeout(). Repeat
400 * IPC_M_PHONE_HUNGUP until the caller notices.
401 */
402 memset(call, 0, sizeof(ipc_call_t));
[228e490]403 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
[8c8f8d6]404 futex_up(&async_futex);
405 return conn->close_callid;
406 }
407
[085bd54]408 if (usecs)
[b6ee5b1]409 async_insert_timeout(&conn->wdata);
[c07544d3]410
411 conn->wdata.active = false;
412
[c7509e5]413 /*
414 * Note: the current fibril will be rescheduled either due to a
415 * timeout or due to an arriving message destined to it. In the
416 * former case, handle_expired_timeouts() and, in the latter
417 * case, route_call() will perform the wakeup.
418 */
[116d3f6f]419 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]420
[e70bfa5]421 /*
[c07544d3]422 * Futex is up after getting back from async_manager.
423 * Get it again.
[c7509e5]424 */
[49d072e]425 futex_down(&async_futex);
[f53cc81]426 if ((usecs) && (conn->wdata.to_event.occurred)
[c07544d3]427 && (list_empty(&conn->msg_queue))) {
[e70bfa5]428 /* If we timed out -> exit */
[49d072e]429 futex_up(&async_futex);
430 return 0;
431 }
[450cd3a]432 }
433
[c07544d3]434 msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
[80649a91]435 list_remove(&msg->link);
[c07544d3]436
437 ipc_callid_t callid = msg->callid;
[80649a91]438 *call = msg->call;
439 free(msg);
440
[01ff41c]441 futex_up(&async_futex);
[80649a91]442 return callid;
443}
444
[36c9234]445/** Default fibril function that gets called to handle new connection.
[a2cd194]446 *
[e70bfa5]447 * This function is defined as a weak symbol - to be redefined in user code.
[36c9234]448 *
[c07544d3]449 * @param callid Hash of the incoming call.
450 * @param call Data of the incoming call.
451 *
[a2cd194]452 */
[da0c91e7]453static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
[80649a91]454{
[b74959bd]455 ipc_answer_0(callid, ENOENT);
[80649a91]456}
[36c9234]457
458/** Default fibril function that gets called to handle interrupt notifications.
459 *
[c07544d3]460 * This function is defined as a weak symbol - to be redefined in user code.
461 *
462 * @param callid Hash of the incoming call.
463 * @param call Data of the incoming call.
464 *
[36c9234]465 */
[51dbadf3]466static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
[44c6d88d]467{
468}
469
[f2f0392]470/** Wrapper for client connection fibril.
471 *
[36c9234]472 * When a new connection arrives, a fibril with this implementing function is
[f2f0392]473 * created. It calls client_connection() and does the final cleanup.
[a2cd194]474 *
[c07544d3]475 * @param arg Connection structure pointer.
476 *
477 * @return Always zero.
[a2cd194]478 *
479 */
[c07544d3]480static int connection_fibril(void *arg)
[80649a91]481{
[c07544d3]482 /*
483 * Setup fibril-local connection pointer and call client_connection().
484 *
485 */
[bc1f1c2]486 FIBRIL_connection = (connection_t *) arg;
487 FIBRIL_connection->cfibril(FIBRIL_connection->callid,
488 &FIBRIL_connection->call);
[a46da63]489
[36c9234]490 /* Remove myself from the connection hash table */
[01ff41c]491 futex_down(&async_futex);
[c07544d3]492 unsigned long key = FIBRIL_connection->in_phone_hash;
[a2cd194]493 hash_table_remove(&conn_hash_table, &key, 1);
[01ff41c]494 futex_up(&async_futex);
[a46da63]495
[36c9234]496 /* Answer all remaining messages with EHANGUP */
[bc1f1c2]497 while (!list_empty(&FIBRIL_connection->msg_queue)) {
[cc27c8c5]498 msg_t *msg;
[c07544d3]499
[cc27c8c5]500 msg = list_get_instance(FIBRIL_connection->msg_queue.next,
501 msg_t, link);
[a2cd194]502 list_remove(&msg->link);
[b74959bd]503 ipc_answer_0(msg->callid, EHANGUP);
[a2cd194]504 free(msg);
505 }
[c07544d3]506
[bc1f1c2]507 if (FIBRIL_connection->close_callid)
[b74959bd]508 ipc_answer_0(FIBRIL_connection->close_callid, EOK);
[a46da63]509
510 return 0;
[80649a91]511}
512
[f2f0392]513/** Create a new fibril for a new connection.
[80649a91]514 *
[c07544d3]515 * Create new fibril for connection, fill in connection structures and inserts
[f2f0392]516 * it into the hash table, so that later we can easily do routing of messages to
517 * particular fibrils.
[53ca318]518 *
[c07544d3]519 * @param in_phone_hash Identification of the incoming connection.
520 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
521 * If callid is zero, the connection was opened by
522 * accepting the IPC_M_CONNECT_TO_ME call and this function
523 * is called directly by the server.
524 * @param call Call data of the opening call.
525 * @param cfibril Fibril function that should be called upon opening the
526 * connection.
527 *
528 * @return New fibril id or NULL on failure.
[36c9234]529 *
[80649a91]530 */
[96b02eb9]531fid_t async_new_connection(sysarg_t in_phone_hash, ipc_callid_t callid,
[bc1f1c2]532 ipc_call_t *call, void (*cfibril)(ipc_callid_t, ipc_call_t *))
[80649a91]533{
[c07544d3]534 connection_t *conn = malloc(sizeof(*conn));
[80649a91]535 if (!conn) {
[6675c70]536 if (callid)
[b74959bd]537 ipc_answer_0(callid, ENOMEM);
[0b4a67a]538 return (uintptr_t) NULL;
[80649a91]539 }
[c07544d3]540
[44c6d88d]541 conn->in_phone_hash = in_phone_hash;
[80649a91]542 list_initialize(&conn->msg_queue);
543 conn->callid = callid;
[c4702804]544 conn->close_callid = 0;
[c07544d3]545
[eaf34f7]546 if (call)
547 conn->call = *call;
[6b21292]548
[c07544d3]549 /* We will activate the fibril ASAP */
550 conn->wdata.active = true;
551 conn->cfibril = cfibril;
[bc1f1c2]552 conn->wdata.fid = fibril_create(connection_fibril, conn);
[c07544d3]553
[bc1f1c2]554 if (!conn->wdata.fid) {
[80649a91]555 free(conn);
[6675c70]556 if (callid)
[b74959bd]557 ipc_answer_0(callid, ENOMEM);
[0b4a67a]558 return (uintptr_t) NULL;
[80649a91]559 }
[6b21292]560
[36c9234]561 /* Add connection to the connection hash table */
[9db9b10]562 unsigned long key = conn->in_phone_hash;
[c07544d3]563
[01ff41c]564 futex_down(&async_futex);
[80649a91]565 hash_table_insert(&conn_hash_table, &key, &conn->link);
[01ff41c]566 futex_up(&async_futex);
[6b21292]567
[bc1f1c2]568 fibril_add_ready(conn->wdata.fid);
[6b21292]569
[bc1f1c2]570 return conn->wdata.fid;
[80649a91]571}
572
[36c9234]573/** Handle a call that was received.
574 *
575 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
576 * Otherwise the call is routed to its connection fibril.
577 *
[c07544d3]578 * @param callid Hash of the incoming call.
579 * @param call Data of the incoming call.
[6b21292]580 *
[36c9234]581 */
[80649a91]582static void handle_call(ipc_callid_t callid, ipc_call_t *call)
583{
[44c6d88d]584 /* Unrouted call - do some default behaviour */
[15039b67]585 if ((callid & IPC_CALLID_NOTIFICATION)) {
[c07544d3]586 process_notification(callid, call);
[9db9b10]587 goto out;
[6b21292]588 }
589
[228e490]590 switch (IPC_GET_IMETHOD(*call)) {
[2c0e5d2]591 case IPC_M_CONNECT_ME:
[80649a91]592 case IPC_M_CONNECT_ME_TO:
[f2f0392]593 /* Open new connection with fibril etc. */
[b61d47d]594 async_new_connection(IPC_GET_ARG5(*call), callid, call,
[bc1f1c2]595 client_connection);
[9db9b10]596 goto out;
[80649a91]597 }
[6b21292]598
[36c9234]599 /* Try to route the call through the connection hash table */
[44c6d88d]600 if (route_call(callid, call))
[9db9b10]601 goto out;
[6b21292]602
[44c6d88d]603 /* Unknown call from unknown phone - hang it up */
[b74959bd]604 ipc_answer_0(callid, EHANGUP);
[9db9b10]605 return;
606
607out:
[953769f]608 ;
[450cd3a]609}
610
[f2f0392]611/** Fire all timeouts that expired. */
[c042bdd]612static void handle_expired_timeouts(void)
613{
614 struct timeval tv;
[36c9234]615 gettimeofday(&tv, NULL);
[c07544d3]616
[c042bdd]617 futex_down(&async_futex);
[c07544d3]618
619 link_t *cur = timeout_list.next;
[c042bdd]620 while (cur != &timeout_list) {
[f53cc81]621 awaiter_t *waiter;
[c07544d3]622
[f53cc81]623 waiter = list_get_instance(cur, awaiter_t, to_event.link);
624 if (tv_gt(&waiter->to_event.expires, &tv))
[c042bdd]625 break;
[f53cc81]626
[c042bdd]627 cur = cur->next;
[f53cc81]628
629 list_remove(&waiter->to_event.link);
630 waiter->to_event.inlist = false;
631 waiter->to_event.occurred = true;
[c07544d3]632
[36c9234]633 /*
[c07544d3]634 * Redundant condition?
635 * The fibril should not be active when it gets here.
[c042bdd]636 */
[49d072e]637 if (!waiter->active) {
[c07544d3]638 waiter->active = true;
[bc1f1c2]639 fibril_add_ready(waiter->fid);
[c042bdd]640 }
641 }
[c07544d3]642
[c042bdd]643 futex_up(&async_futex);
644}
645
[36c9234]646/** Endless loop dispatching incoming calls and answers.
647 *
[c07544d3]648 * @return Never returns.
649 *
[36c9234]650 */
[085bd54]651static int async_manager_worker(void)
[80649a91]652{
[c07544d3]653 while (true) {
[116d3f6f]654 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
[a46da63]655 futex_up(&async_futex);
[36c9234]656 /*
657 * async_futex is always held when entering a manager
658 * fibril.
[a46da63]659 */
[80649a91]660 continue;
661 }
[c07544d3]662
[c042bdd]663 futex_down(&async_futex);
[c07544d3]664
665 suseconds_t timeout;
[c042bdd]666 if (!list_empty(&timeout_list)) {
[cc27c8c5]667 awaiter_t *waiter = list_get_instance(timeout_list.next,
[f53cc81]668 awaiter_t, to_event.link);
[c07544d3]669
670 struct timeval tv;
[bc1f1c2]671 gettimeofday(&tv, NULL);
[c07544d3]672
[f53cc81]673 if (tv_gteq(&tv, &waiter->to_event.expires)) {
[6c46350]674 futex_up(&async_futex);
[c042bdd]675 handle_expired_timeouts();
676 continue;
677 } else
[f53cc81]678 timeout = tv_sub(&waiter->to_event.expires,
679 &tv);
[c042bdd]680 } else
[0b99e40]681 timeout = SYNCH_NO_TIMEOUT;
[c07544d3]682
[c042bdd]683 futex_up(&async_futex);
[8619f25]684
685 atomic_inc(&threads_in_ipc_wait);
[c07544d3]686
687 ipc_call_t call;
[cc27c8c5]688 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
689 SYNCH_FLAGS_NONE);
[c07544d3]690
[8619f25]691 atomic_dec(&threads_in_ipc_wait);
692
[0b99e40]693 if (!callid) {
[c042bdd]694 handle_expired_timeouts();
[0b99e40]695 continue;
696 }
[c07544d3]697
698 if (callid & IPC_CALLID_ANSWERED)
[80649a91]699 continue;
[c07544d3]700
[80649a91]701 handle_call(callid, &call);
702 }
[a46da63]703
704 return 0;
[80649a91]705}
706
[36c9234]707/** Function to start async_manager as a standalone fibril.
[c07544d3]708 *
[36c9234]709 * When more kernel threads are used, one async manager should exist per thread.
710 *
[c07544d3]711 * @param arg Unused.
712 * @return Never returns.
[36c9234]713 *
[a2cd194]714 */
[9591265]715static int async_manager_fibril(void *arg)
[80649a91]716{
[a46da63]717 futex_up(&async_futex);
[c07544d3]718
[36c9234]719 /*
720 * async_futex is always locked when entering manager
721 */
[085bd54]722 async_manager_worker();
[a46da63]723
724 return 0;
[80649a91]725}
[450cd3a]726
[36c9234]727/** Add one manager to manager list. */
[80649a91]728void async_create_manager(void)
[450cd3a]729{
[c07544d3]730 fid_t fid = fibril_create(async_manager_fibril, NULL);
[bc1f1c2]731 fibril_add_manager(fid);
[80649a91]732}
733
734/** Remove one manager from manager list */
735void async_destroy_manager(void)
736{
[bc1f1c2]737 fibril_remove_manager();
[80649a91]738}
739
[36c9234]740/** Initialize the async framework.
741 *
[c07544d3]742 * @return Zero on success or an error code.
[36c9234]743 */
[db24058]744int __async_init(void)
[80649a91]745{
[bc1f1c2]746 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
747 &conn_hash_table_ops)) {
[e35bf88]748 printf("%s: Cannot create async hash table\n", "libc");
[80649a91]749 return ENOMEM;
750 }
[c1c0184]751
752 _async_sess_init();
[80649a91]753
[a46da63]754 return 0;
[450cd3a]755}
[01ff41c]756
[36c9234]757/** Reply received callback.
[01ff41c]758 *
[36c9234]759 * This function is called whenever a reply for an asynchronous message sent out
760 * by the asynchronous framework is received.
761 *
762 * Notify the fibril which is waiting for this message that it has arrived.
763 *
[c07544d3]764 * @param arg Pointer to the asynchronous message record.
765 * @param retval Value returned in the answer.
766 * @param data Call data of the answer.
[01ff41c]767 */
[c07544d3]768static void reply_received(void *arg, int retval, ipc_call_t *data)
[01ff41c]769{
[9db9b10]770 futex_down(&async_futex);
771
[c07544d3]772 amsg_t *msg = (amsg_t *) arg;
[01ff41c]773 msg->retval = retval;
[c07544d3]774
[36c9234]775 /* Copy data after futex_down, just in case the call was detached */
[9db9b10]776 if ((msg->dataptr) && (data))
[c07544d3]777 *msg->dataptr = *data;
778
[c042bdd]779 write_barrier();
[c07544d3]780
[c042bdd]781 /* Remove message from timeout list */
[f53cc81]782 if (msg->wdata.to_event.inlist)
783 list_remove(&msg->wdata.to_event.link);
[c07544d3]784
785 msg->done = true;
[36c9234]786 if (!msg->wdata.active) {
[c07544d3]787 msg->wdata.active = true;
[bc1f1c2]788 fibril_add_ready(msg->wdata.fid);
[01ff41c]789 }
[c07544d3]790
[01ff41c]791 futex_up(&async_futex);
792}
793
[36c9234]794/** Send message and return id of the sent message.
795 *
796 * The return value can be used as input for async_wait() to wait for
797 * completion.
[01ff41c]798 *
[c07544d3]799 * @param phoneid Handle of the phone that will be used for the send.
800 * @param method Service-defined method.
801 * @param arg1 Service-defined payload argument.
802 * @param arg2 Service-defined payload argument.
803 * @param arg3 Service-defined payload argument.
804 * @param arg4 Service-defined payload argument.
805 * @param dataptr If non-NULL, storage where the reply data will be
806 * stored.
807 *
808 * @return Hash of the sent message or 0 on error.
[36c9234]809 *
[01ff41c]810 */
[96b02eb9]811aid_t async_send_fast(int phoneid, sysarg_t method, sysarg_t arg1,
812 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[01ff41c]813{
[c07544d3]814 amsg_t *msg = malloc(sizeof(*msg));
815
816 if (!msg)
817 return 0;
[6b21292]818
[c07544d3]819 msg->done = false;
[01ff41c]820 msg->dataptr = dataptr;
[6b21292]821
[f53cc81]822 msg->wdata.to_event.inlist = false;
[36c9234]823 /* We may sleep in the next method, but it will use its own mechanism */
[c07544d3]824 msg->wdata.active = true;
825
[0cc4313]826 ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
[c07544d3]827 reply_received, true);
[6b21292]828
[01ff41c]829 return (aid_t) msg;
830}
831
[90f5d64]832/** Send message and return id of the sent message
833 *
[36c9234]834 * The return value can be used as input for async_wait() to wait for
835 * completion.
836 *
[c07544d3]837 * @param phoneid Handle of the phone that will be used for the send.
838 * @param method Service-defined method.
839 * @param arg1 Service-defined payload argument.
840 * @param arg2 Service-defined payload argument.
841 * @param arg3 Service-defined payload argument.
842 * @param arg4 Service-defined payload argument.
843 * @param arg5 Service-defined payload argument.
844 * @param dataptr If non-NULL, storage where the reply data will be
845 * stored.
846 *
847 * @return Hash of the sent message or 0 on error.
[36c9234]848 *
[90f5d64]849 */
[96b02eb9]850aid_t async_send_slow(int phoneid, sysarg_t method, sysarg_t arg1,
851 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
[0cc4313]852 ipc_call_t *dataptr)
[90f5d64]853{
[c07544d3]854 amsg_t *msg = malloc(sizeof(*msg));
[6b21292]855
[c07544d3]856 if (!msg)
857 return 0;
858
859 msg->done = false;
[90f5d64]860 msg->dataptr = dataptr;
[6b21292]861
[f53cc81]862 msg->wdata.to_event.inlist = false;
[36c9234]863 /* We may sleep in next method, but it will use its own mechanism */
[c07544d3]864 msg->wdata.active = true;
[6b21292]865
[0cc4313]866 ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
[c07544d3]867 reply_received, true);
[6b21292]868
[90f5d64]869 return (aid_t) msg;
870}
871
[36c9234]872/** Wait for a message sent by the async framework.
[01ff41c]873 *
[c07544d3]874 * @param amsgid Hash of the message to wait for.
875 * @param retval Pointer to storage where the retval of the answer will
876 * be stored.
877 *
[01ff41c]878 */
[96b02eb9]879void async_wait_for(aid_t amsgid, sysarg_t *retval)
[01ff41c]880{
881 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]882
[01ff41c]883 futex_down(&async_futex);
884 if (msg->done) {
885 futex_up(&async_futex);
886 goto done;
887 }
[c07544d3]888
[bc1f1c2]889 msg->wdata.fid = fibril_get_id();
[c07544d3]890 msg->wdata.active = false;
[f53cc81]891 msg->wdata.to_event.inlist = false;
[c07544d3]892
[36c9234]893 /* Leave the async_futex locked when entering this function */
[116d3f6f]894 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]895
896 /* Futex is up automatically after fibril_switch */
897
[01ff41c]898done:
899 if (retval)
900 *retval = msg->retval;
[c07544d3]901
[01ff41c]902 free(msg);
903}
[0b99e40]904
[36c9234]905/** Wait for a message sent by the async framework, timeout variant.
[c042bdd]906 *
[c07544d3]907 * @param amsgid Hash of the message to wait for.
908 * @param retval Pointer to storage where the retval of the answer will
909 * be stored.
910 * @param timeout Timeout in microseconds.
911 *
912 * @return Zero on success, ETIMEOUT if the timeout has expired.
[c042bdd]913 *
914 */
[96b02eb9]915int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
[c042bdd]916{
917 amsg_t *msg = (amsg_t *) amsgid;
[c07544d3]918
[86029498]919 /* TODO: Let it go through the event read at least once */
920 if (timeout < 0)
921 return ETIMEOUT;
[c07544d3]922
[c042bdd]923 futex_down(&async_futex);
924 if (msg->done) {
925 futex_up(&async_futex);
926 goto done;
927 }
[c07544d3]928
[f53cc81]929 gettimeofday(&msg->wdata.to_event.expires, NULL);
930 tv_add(&msg->wdata.to_event.expires, timeout);
[c07544d3]931
[bc1f1c2]932 msg->wdata.fid = fibril_get_id();
[c07544d3]933 msg->wdata.active = false;
[b6ee5b1]934 async_insert_timeout(&msg->wdata);
[c07544d3]935
[36c9234]936 /* Leave the async_futex locked when entering this function */
[116d3f6f]937 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]938
939 /* Futex is up automatically after fibril_switch */
940
[c042bdd]941 if (!msg->done)
942 return ETIMEOUT;
[c07544d3]943
[c042bdd]944done:
945 if (retval)
946 *retval = msg->retval;
[c07544d3]947
[c042bdd]948 free(msg);
[c07544d3]949
[c042bdd]950 return 0;
951}
[0b99e40]952
[36c9234]953/** Wait for specified time.
[44c6d88d]954 *
[36c9234]955 * The current fibril is suspended but the thread continues to execute.
956 *
[c07544d3]957 * @param timeout Duration of the wait in microseconds.
958 *
[44c6d88d]959 */
960void async_usleep(suseconds_t timeout)
961{
[c07544d3]962 amsg_t *msg = malloc(sizeof(*msg));
[44c6d88d]963
964 if (!msg)
965 return;
[6b21292]966
[bc1f1c2]967 msg->wdata.fid = fibril_get_id();
[c07544d3]968 msg->wdata.active = false;
[6b21292]969
[f53cc81]970 gettimeofday(&msg->wdata.to_event.expires, NULL);
971 tv_add(&msg->wdata.to_event.expires, timeout);
[6b21292]972
[44c6d88d]973 futex_down(&async_futex);
[c07544d3]974
[b6ee5b1]975 async_insert_timeout(&msg->wdata);
[c07544d3]976
[36c9234]977 /* Leave the async_futex locked when entering this function */
[116d3f6f]978 fibril_switch(FIBRIL_TO_MANAGER);
[c07544d3]979
980 /* Futex is up automatically after fibril_switch() */
981
[44c6d88d]982 free(msg);
983}
[da0c91e7]984
[36c9234]985/** Setter for client_connection function pointer.
[da0c91e7]986 *
[c07544d3]987 * @param conn Function that will implement a new connection fibril.
988 *
[da0c91e7]989 */
990void async_set_client_connection(async_client_conn_t conn)
991{
992 client_connection = conn;
993}
[36c9234]994
995/** Setter for interrupt_received function pointer.
996 *
[c07544d3]997 * @param intr Function that will implement a new interrupt
998 * notification fibril.
[36c9234]999 */
[c07544d3]1000void async_set_interrupt_received(async_client_conn_t intr)
[51dbadf3]1001{
[c07544d3]1002 interrupt_received = intr;
[51dbadf3]1003}
[085bd54]1004
[0cc4313]1005/** Pseudo-synchronous message sending - fast version.
1006 *
1007 * Send message asynchronously and return only after the reply arrives.
1008 *
1009 * This function can only transfer 4 register payload arguments. For
1010 * transferring more arguments, see the slower async_req_slow().
1011 *
[c07544d3]1012 * @param phoneid Hash of the phone through which to make the call.
1013 * @param method Method of the call.
1014 * @param arg1 Service-defined payload argument.
1015 * @param arg2 Service-defined payload argument.
1016 * @param arg3 Service-defined payload argument.
1017 * @param arg4 Service-defined payload argument.
1018 * @param r1 If non-NULL, storage for the 1st reply argument.
1019 * @param r2 If non-NULL, storage for the 2nd reply argument.
1020 * @param r3 If non-NULL, storage for the 3rd reply argument.
1021 * @param r4 If non-NULL, storage for the 4th reply argument.
1022 * @param r5 If non-NULL, storage for the 5th reply argument.
1023 *
1024 * @return Return code of the reply or a negative error code.
1025 *
[0cc4313]1026 */
[96b02eb9]1027sysarg_t async_req_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1028 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1029 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1030{
[0cc4313]1031 ipc_call_t result;
1032 aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
1033 &result);
[c07544d3]1034
[96b02eb9]1035 sysarg_t rc;
[0cc4313]1036 async_wait_for(eid, &rc);
[c07544d3]1037
1038 if (r1)
[0cc4313]1039 *r1 = IPC_GET_ARG1(result);
[c07544d3]1040
[0cc4313]1041 if (r2)
1042 *r2 = IPC_GET_ARG2(result);
[c07544d3]1043
[0cc4313]1044 if (r3)
1045 *r3 = IPC_GET_ARG3(result);
[c07544d3]1046
[0cc4313]1047 if (r4)
1048 *r4 = IPC_GET_ARG4(result);
[c07544d3]1049
[0cc4313]1050 if (r5)
1051 *r5 = IPC_GET_ARG5(result);
[c07544d3]1052
[0cc4313]1053 return rc;
[085bd54]1054}
1055
[0cc4313]1056/** Pseudo-synchronous message sending - slow version.
1057 *
1058 * Send message asynchronously and return only after the reply arrives.
1059 *
[c07544d3]1060 * @param phoneid Hash of the phone through which to make the call.
1061 * @param method Method of the call.
1062 * @param arg1 Service-defined payload argument.
1063 * @param arg2 Service-defined payload argument.
1064 * @param arg3 Service-defined payload argument.
1065 * @param arg4 Service-defined payload argument.
1066 * @param arg5 Service-defined payload argument.
1067 * @param r1 If non-NULL, storage for the 1st reply argument.
1068 * @param r2 If non-NULL, storage for the 2nd reply argument.
1069 * @param r3 If non-NULL, storage for the 3rd reply argument.
1070 * @param r4 If non-NULL, storage for the 4th reply argument.
1071 * @param r5 If non-NULL, storage for the 5th reply argument.
1072 *
1073 * @return Return code of the reply or a negative error code.
1074 *
[0cc4313]1075 */
[96b02eb9]1076sysarg_t async_req_slow(int phoneid, sysarg_t method, sysarg_t arg1,
1077 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1078 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
[085bd54]1079{
[0cc4313]1080 ipc_call_t result;
1081 aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
1082 &result);
[c07544d3]1083
[96b02eb9]1084 sysarg_t rc;
[0cc4313]1085 async_wait_for(eid, &rc);
[c07544d3]1086
1087 if (r1)
[0cc4313]1088 *r1 = IPC_GET_ARG1(result);
[c07544d3]1089
[0cc4313]1090 if (r2)
1091 *r2 = IPC_GET_ARG2(result);
[c07544d3]1092
[0cc4313]1093 if (r3)
1094 *r3 = IPC_GET_ARG3(result);
[c07544d3]1095
[0cc4313]1096 if (r4)
1097 *r4 = IPC_GET_ARG4(result);
[c07544d3]1098
[0cc4313]1099 if (r5)
1100 *r5 = IPC_GET_ARG5(result);
[c07544d3]1101
[0cc4313]1102 return rc;
[085bd54]1103}
[b2951e2]1104
[f74392f]1105/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1106 *
1107 * Ask through phone for a new connection to some service.
1108 *
1109 * @param phoneid Phone handle used for contacting the other side.
1110 * @param arg1 User defined argument.
1111 * @param arg2 User defined argument.
1112 * @param arg3 User defined argument.
1113 *
1114 * @return New phone handle on success or a negative error code.
1115 */
1116int
[96b02eb9]1117async_connect_me_to(int phoneid, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
[f74392f]1118{
1119 int rc;
[96b02eb9]1120 sysarg_t newphid;
[f74392f]1121
1122 rc = async_req_3_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, NULL,
1123 NULL, NULL, NULL, &newphid);
1124
1125 if (rc != EOK)
1126 return rc;
1127
1128 return newphid;
1129}
1130
1131/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1132 *
1133 * Ask through phone for a new connection to some service and block until
1134 * success.
1135 *
1136 * @param phoneid Phone handle used for contacting the other side.
1137 * @param arg1 User defined argument.
1138 * @param arg2 User defined argument.
1139 * @param arg3 User defined argument.
1140 *
1141 * @return New phone handle on success or a negative error code.
1142 */
1143int
[96b02eb9]1144async_connect_me_to_blocking(int phoneid, sysarg_t arg1, sysarg_t arg2,
1145 sysarg_t arg3)
[f74392f]1146{
1147 int rc;
[96b02eb9]1148 sysarg_t newphid;
[f74392f]1149
1150 rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
1151 IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
1152
1153 if (rc != EOK)
1154 return rc;
1155
1156 return newphid;
1157}
1158
[0da4e41]1159/** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
1160 *
1161 * @param phoneid Phone that will be used to contact the receiving side.
1162 * @param dst Destination address space area base.
1163 * @param size Size of the destination address space area.
1164 * @param arg User defined argument.
1165 * @param flags Storage where the received flags will be stored. Can be
1166 * NULL.
1167 *
1168 * @return Zero on success or a negative error code from errno.h.
1169 */
[96b02eb9]1170int async_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
[0da4e41]1171 int *flags)
1172{
1173 int res;
1174 sysarg_t tmp_flags;
[96b02eb9]1175 res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
1176 (sysarg_t) size, arg, NULL, &tmp_flags);
[0da4e41]1177 if (flags)
1178 *flags = tmp_flags;
1179 return res;
1180}
1181
1182/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1183 *
1184 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN calls
1185 * so that the user doesn't have to remember the meaning of each IPC argument.
1186 *
1187 * So far, this wrapper is to be used from within a connection fibril.
1188 *
1189 * @param callid Storage where the hash of the IPC_M_SHARE_IN call will
1190 * be stored.
1191 * @param size Destination address space area size.
1192 *
1193 * @return Non-zero on success, zero on failure.
1194 */
1195int async_share_in_receive(ipc_callid_t *callid, size_t *size)
1196{
1197 ipc_call_t data;
1198
1199 assert(callid);
1200 assert(size);
1201
1202 *callid = async_get_call(&data);
[228e490]1203 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
[0da4e41]1204 return 0;
1205 *size = (size_t) IPC_GET_ARG2(data);
1206 return 1;
1207}
1208
1209/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1210 *
1211 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1212 * so that the user doesn't have to remember the meaning of each IPC argument.
1213 *
1214 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1215 * @param src Source address space base.
1216 * @param flags Flags to be used for sharing. Bits can be only cleared.
1217 *
1218 * @return Zero on success or a value from @ref errno.h on failure.
1219 */
1220int async_share_in_finalize(ipc_callid_t callid, void *src, int flags)
1221{
1222 return ipc_share_in_finalize(callid, src, flags);
1223}
1224
1225/** Wrapper for making IPC_M_SHARE_OUT calls using the async framework.
1226 *
1227 * @param phoneid Phone that will be used to contact the receiving side.
1228 * @param src Source address space area base address.
1229 * @param flags Flags to be used for sharing. Bits can be only cleared.
1230 *
1231 * @return Zero on success or a negative error code from errno.h.
1232 */
1233int async_share_out_start(int phoneid, void *src, int flags)
1234{
[96b02eb9]1235 return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
1236 (sysarg_t) flags);
[0da4e41]1237}
1238
1239/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1240 *
1241 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT calls
1242 * so that the user doesn't have to remember the meaning of each IPC argument.
1243 *
1244 * So far, this wrapper is to be used from within a connection fibril.
1245 *
1246 * @param callid Storage where the hash of the IPC_M_SHARE_OUT call will
1247 * be stored.
1248 * @param size Storage where the source address space area size will be
1249 * stored.
1250 * @param flags Storage where the sharing flags will be stored.
1251 *
1252 * @return Non-zero on success, zero on failure.
1253 */
1254int async_share_out_receive(ipc_callid_t *callid, size_t *size, int *flags)
1255{
1256 ipc_call_t data;
1257
1258 assert(callid);
1259 assert(size);
1260 assert(flags);
1261
1262 *callid = async_get_call(&data);
[228e490]1263 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
[0da4e41]1264 return 0;
1265 *size = (size_t) IPC_GET_ARG2(data);
1266 *flags = (int) IPC_GET_ARG3(data);
1267 return 1;
1268}
1269
1270/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
1271 *
1272 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT calls
1273 * so that the user doesn't have to remember the meaning of each IPC argument.
1274 *
1275 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1276 * @param dst Destination address space area base address.
1277 *
1278 * @return Zero on success or a value from @ref errno.h on failure.
1279 */
1280int async_share_out_finalize(ipc_callid_t callid, void *dst)
1281{
1282 return ipc_share_out_finalize(callid, dst);
1283}
1284
1285
1286/** Wrapper for making IPC_M_DATA_READ calls using the async framework.
1287 *
1288 * @param phoneid Phone that will be used to contact the receiving side.
1289 * @param dst Address of the beginning of the destination buffer.
1290 * @param size Size of the destination buffer.
1291 *
1292 * @return Zero on success or a negative error code from errno.h.
1293 */
1294int async_data_read_start(int phoneid, void *dst, size_t size)
1295{
[96b02eb9]1296 return async_req_2_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
1297 (sysarg_t) size);
[0da4e41]1298}
1299
1300/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
1301 *
1302 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ calls
1303 * so that the user doesn't have to remember the meaning of each IPC argument.
1304 *
1305 * So far, this wrapper is to be used from within a connection fibril.
1306 *
1307 * @param callid Storage where the hash of the IPC_M_DATA_READ call will
1308 * be stored.
1309 * @param size Storage where the maximum size will be stored. Can be
1310 * NULL.
1311 *
1312 * @return Non-zero on success, zero on failure.
1313 */
1314int async_data_read_receive(ipc_callid_t *callid, size_t *size)
1315{
1316 ipc_call_t data;
1317
1318 assert(callid);
1319
1320 *callid = async_get_call(&data);
[228e490]1321 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
[0da4e41]1322 return 0;
1323 if (size)
1324 *size = (size_t) IPC_GET_ARG2(data);
1325 return 1;
1326}
1327
1328/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
1329 *
1330 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
1331 * so that the user doesn't have to remember the meaning of each IPC argument.
1332 *
1333 * @param callid Hash of the IPC_M_DATA_READ call to answer.
1334 * @param src Source address for the IPC_M_DATA_READ call.
1335 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
1336 * the maximum size announced by the sender.
1337 *
1338 * @return Zero on success or a value from @ref errno.h on failure.
1339 */
1340int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
1341{
1342 return ipc_data_read_finalize(callid, src, size);
1343}
1344
[b4cbef1]1345/** Wrapper for forwarding any read request
1346 *
1347 *
1348 */
[96b02eb9]1349int async_data_read_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1350 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[b4cbef1]1351{
1352 ipc_callid_t callid;
1353 if (!async_data_read_receive(&callid, NULL)) {
1354 ipc_answer_0(callid, EINVAL);
1355 return EINVAL;
1356 }
1357
1358 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1359 dataptr);
1360 if (msg == 0) {
1361 ipc_answer_0(callid, EINVAL);
1362 return EINVAL;
1363 }
1364
1365 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1366 IPC_FF_ROUTE_FROM_ME);
1367 if (retval != EOK) {
[a281fc82]1368 async_wait_for(msg, NULL);
[b4cbef1]1369 ipc_answer_0(callid, retval);
1370 return retval;
1371 }
1372
[96b02eb9]1373 sysarg_t rc;
[b4cbef1]1374 async_wait_for(msg, &rc);
1375
1376 return (int) rc;
1377}
1378
[0da4e41]1379/** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
1380 *
[b4cbef1]1381 * @param phoneid Phone that will be used to contact the receiving side.
1382 * @param src Address of the beginning of the source buffer.
1383 * @param size Size of the source buffer.
1384 *
1385 * @return Zero on success or a negative error code from errno.h.
[0da4e41]1386 *
1387 */
1388int async_data_write_start(int phoneid, const void *src, size_t size)
1389{
[96b02eb9]1390 return async_req_2_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
1391 (sysarg_t) size);
[0da4e41]1392}
1393
1394/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
1395 *
1396 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE calls
1397 * so that the user doesn't have to remember the meaning of each IPC argument.
1398 *
1399 * So far, this wrapper is to be used from within a connection fibril.
1400 *
[b4cbef1]1401 * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
1402 * be stored.
1403 * @param size Storage where the suggested size will be stored. May be
1404 * NULL
1405 *
1406 * @return Non-zero on success, zero on failure.
[0da4e41]1407 *
1408 */
1409int async_data_write_receive(ipc_callid_t *callid, size_t *size)
1410{
1411 ipc_call_t data;
1412
1413 assert(callid);
[b4cbef1]1414
[0da4e41]1415 *callid = async_get_call(&data);
[228e490]1416 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
[0da4e41]1417 return 0;
[b4cbef1]1418
[0da4e41]1419 if (size)
1420 *size = (size_t) IPC_GET_ARG2(data);
[b4cbef1]1421
[0da4e41]1422 return 1;
1423}
1424
1425/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
1426 *
1427 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
1428 * so that the user doesn't have to remember the meaning of each IPC argument.
1429 *
[b4cbef1]1430 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
1431 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
1432 * @param size Final size for the IPC_M_DATA_WRITE call.
1433 *
1434 * @return Zero on success or a value from @ref errno.h on failure.
[0da4e41]1435 *
1436 */
1437int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
1438{
1439 return ipc_data_write_finalize(callid, dst, size);
1440}
1441
[eda925a]1442/** Wrapper for receiving binary data or strings
[8aa42e3]1443 *
1444 * This wrapper only makes it more comfortable to use async_data_write_*
[eda925a]1445 * functions to receive binary data or strings.
[8aa42e3]1446 *
[472c09d]1447 * @param data Pointer to data pointer (which should be later disposed
1448 * by free()). If the operation fails, the pointer is not
1449 * touched.
[eda925a]1450 * @param nullterm If true then the received data is always zero terminated.
1451 * This also causes to allocate one extra byte beyond the
1452 * raw transmitted data.
[b4cbef1]1453 * @param min_size Minimum size (in bytes) of the data to receive.
[472c09d]1454 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
1455 * no limit.
[eda925a]1456 * @param granulariy If non-zero then the size of the received data has to
[472c09d]1457 * be divisible by this value.
1458 * @param received If not NULL, the size of the received data is stored here.
[8aa42e3]1459 *
1460 * @return Zero on success or a value from @ref errno.h on failure.
1461 *
1462 */
[eda925a]1463int async_data_write_accept(void **data, const bool nullterm,
1464 const size_t min_size, const size_t max_size, const size_t granularity,
1465 size_t *received)
[8aa42e3]1466{
1467 ipc_callid_t callid;
1468 size_t size;
1469 if (!async_data_write_receive(&callid, &size)) {
1470 ipc_answer_0(callid, EINVAL);
1471 return EINVAL;
1472 }
1473
[b4cbef1]1474 if (size < min_size) {
1475 ipc_answer_0(callid, EINVAL);
1476 return EINVAL;
1477 }
1478
[8aa42e3]1479 if ((max_size > 0) && (size > max_size)) {
1480 ipc_answer_0(callid, EINVAL);
1481 return EINVAL;
1482 }
1483
[472c09d]1484 if ((granularity > 0) && ((size % granularity) != 0)) {
1485 ipc_answer_0(callid, EINVAL);
1486 return EINVAL;
1487 }
1488
[eda925a]1489 void *_data;
1490
1491 if (nullterm)
1492 _data = malloc(size + 1);
1493 else
1494 _data = malloc(size);
1495
[472c09d]1496 if (_data == NULL) {
[8aa42e3]1497 ipc_answer_0(callid, ENOMEM);
1498 return ENOMEM;
1499 }
1500
[472c09d]1501 int rc = async_data_write_finalize(callid, _data, size);
[8aa42e3]1502 if (rc != EOK) {
[472c09d]1503 free(_data);
[8aa42e3]1504 return rc;
1505 }
1506
[eda925a]1507 if (nullterm)
1508 ((char *) _data)[size] = 0;
[8aa42e3]1509
[eda925a]1510 *data = _data;
[472c09d]1511 if (received != NULL)
1512 *received = size;
1513
[8aa42e3]1514 return EOK;
1515}
1516
[b4cbef1]1517/** Wrapper for voiding any data that is about to be received
1518 *
1519 * This wrapper can be used to void any pending data
1520 *
1521 * @param retval Error value from @ref errno.h to be returned to the caller.
1522 *
1523 */
[eda925a]1524void async_data_write_void(const int retval)
[b4cbef1]1525{
1526 ipc_callid_t callid;
1527 async_data_write_receive(&callid, NULL);
1528 ipc_answer_0(callid, retval);
1529}
1530
1531/** Wrapper for forwarding any data that is about to be received
1532 *
1533 *
1534 */
[96b02eb9]1535int async_data_write_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
1536 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
[b4cbef1]1537{
1538 ipc_callid_t callid;
1539 if (!async_data_write_receive(&callid, NULL)) {
1540 ipc_answer_0(callid, EINVAL);
1541 return EINVAL;
1542 }
1543
1544 aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
1545 dataptr);
1546 if (msg == 0) {
1547 ipc_answer_0(callid, EINVAL);
1548 return EINVAL;
1549 }
1550
1551 int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
1552 IPC_FF_ROUTE_FROM_ME);
1553 if (retval != EOK) {
[a281fc82]1554 async_wait_for(msg, NULL);
[b4cbef1]1555 ipc_answer_0(callid, retval);
1556 return retval;
1557 }
1558
[96b02eb9]1559 sysarg_t rc;
[b4cbef1]1560 async_wait_for(msg, &rc);
1561
1562 return (int) rc;
1563}
1564
[a46da63]1565/** @}
[b2951e2]1566 */
Note: See TracBrowser for help on using the repository browser.