source: mainline/uspace/lib/c/generic/async/server.c@ 0a0dff8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0a0dff8 was 3bd1d7d4, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

async: Use a dedicated futex for client hash table.

  • Property mode set to 100644
File size: 47.4 KB
Line 
1/*
2 * Copyright (c) 2006 Ondrej Palkovsky
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.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 */
34
35/**
36 * Asynchronous library
37 *
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.
41 *
42 * You should be able to write very simple multithreaded programs. The async
43 * framework will automatically take care of most of the synchronization
44 * problems.
45 *
46 * Example of use (pseudo C):
47 *
48 * 1) Multithreaded client application
49 *
50 * fibril_create(fibril1, ...);
51 * fibril_create(fibril2, ...);
52 * ...
53 *
54 * int fibril1(void *arg)
55 * {
56 * conn = async_connect_me_to(...);
57 *
58 * exch = async_exchange_begin(conn);
59 * c1 = async_send(exch);
60 * async_exchange_end(exch);
61 *
62 * exch = async_exchange_begin(conn);
63 * c2 = async_send(exch);
64 * async_exchange_end(exch);
65 *
66 * async_wait_for(c1);
67 * async_wait_for(c2);
68 * ...
69 * }
70 *
71 *
72 * 2) Multithreaded server application
73 *
74 * main()
75 * {
76 * async_manager();
77 * }
78 *
79 * port_handler(ichandle, *icall)
80 * {
81 * if (want_refuse) {
82 * async_answer_0(ichandle, ELIMIT);
83 * return;
84 * }
85 * async_answer_0(ichandle, EOK);
86 *
87 * chandle = async_get_call(&call);
88 * somehow_handle_the_call(chandle, call);
89 * async_answer_2(chandle, 1, 2, 3);
90 *
91 * chandle = async_get_call(&call);
92 * ...
93 * }
94 *
95 */
96
97#define LIBC_ASYNC_C_
98#include <ipc/ipc.h>
99#include <async.h>
100#include "../private/async.h"
101#undef LIBC_ASYNC_C_
102
103#include <ipc/irq.h>
104#include <ipc/event.h>
105#include <futex.h>
106#include <fibril.h>
107#include <adt/hash_table.h>
108#include <adt/hash.h>
109#include <adt/list.h>
110#include <assert.h>
111#include <errno.h>
112#include <sys/time.h>
113#include <libarch/barrier.h>
114#include <stdbool.h>
115#include <stdlib.h>
116#include <mem.h>
117#include <stdlib.h>
118#include <macros.h>
119#include <as.h>
120#include <abi/mm/as.h>
121#include "../private/libc.h"
122
123/** Async framework global futex */
124futex_t async_futex = FUTEX_INITIALIZER;
125
126/** Number of threads waiting for IPC in the kernel. */
127static atomic_t threads_in_ipc_wait = { 0 };
128
129/** Call data */
130typedef struct {
131 link_t link;
132
133 cap_call_handle_t chandle;
134 ipc_call_t call;
135} msg_t;
136
137/* Client connection data */
138typedef struct {
139 ht_link_t link;
140
141 task_id_t in_task_id;
142 atomic_t refcnt;
143 void *data;
144} client_t;
145
146/* Server connection data */
147typedef struct {
148 awaiter_t wdata;
149
150 /** Hash table link. */
151 ht_link_t link;
152
153 /** Incoming client task ID. */
154 task_id_t in_task_id;
155
156 /** Incoming phone hash. */
157 sysarg_t in_phone_hash;
158
159 /** Link to the client tracking structure. */
160 client_t *client;
161
162 /** Messages that should be delivered to this fibril. */
163 list_t msg_queue;
164
165 /** Identification of the opening call. */
166 cap_call_handle_t chandle;
167
168 /** Call data of the opening call. */
169 ipc_call_t call;
170
171 /** Identification of the closing call. */
172 cap_call_handle_t close_chandle;
173
174 /** Fibril function that will be used to handle the connection. */
175 async_port_handler_t handler;
176
177 /** Client data */
178 void *data;
179} connection_t;
180
181/* Notification data */
182typedef struct {
183 /** notification_hash_table link */
184 ht_link_t htlink;
185
186 /** notification_queue link */
187 link_t qlink;
188
189 /** Notification method */
190 sysarg_t imethod;
191
192 /** Notification handler */
193 async_notification_handler_t handler;
194
195 /** Notification handler argument */
196 void *arg;
197
198 /** Data of the most recent notification. */
199 ipc_call_t calldata;
200
201 /**
202 * How many notifications with this `imethod` arrived since it was last
203 * handled. If `count` > 1, `calldata` only holds the data for the most
204 * recent such notification, all the older data being lost.
205 *
206 * `async_spawn_notification_handler()` can be used to increase the
207 * number of notifications that can be processed simultaneously,
208 * reducing the likelihood of losing them when the handler blocks.
209 */
210 long count;
211} notification_t;
212
213/** Identifier of the incoming connection handled by the current fibril. */
214static fibril_local connection_t *fibril_connection;
215
216static void *default_client_data_constructor(void)
217{
218 return NULL;
219}
220
221static void default_client_data_destructor(void *data)
222{
223}
224
225static async_client_data_ctor_t async_client_data_create =
226 default_client_data_constructor;
227static async_client_data_dtor_t async_client_data_destroy =
228 default_client_data_destructor;
229
230void async_set_client_data_constructor(async_client_data_ctor_t ctor)
231{
232 assert(async_client_data_create == default_client_data_constructor);
233 async_client_data_create = ctor;
234}
235
236void async_set_client_data_destructor(async_client_data_dtor_t dtor)
237{
238 assert(async_client_data_destroy == default_client_data_destructor);
239 async_client_data_destroy = dtor;
240}
241
242static futex_t client_futex = FUTEX_INITIALIZER;
243static hash_table_t client_hash_table;
244
245// TODO: lockfree notification_queue?
246static futex_t notification_futex = FUTEX_INITIALIZER;
247static hash_table_t notification_hash_table;
248static LIST_INITIALIZE(notification_queue);
249static FIBRIL_SEMAPHORE_INITIALIZE(notification_semaphore, 0);
250
251static sysarg_t notification_avail = 0;
252
253/* The remaining structures are guarded by async_futex. */
254static hash_table_t conn_hash_table;
255static LIST_INITIALIZE(timeout_list);
256
257static size_t client_key_hash(void *key)
258{
259 task_id_t in_task_id = *(task_id_t *) key;
260 return in_task_id;
261}
262
263static size_t client_hash(const ht_link_t *item)
264{
265 client_t *client = hash_table_get_inst(item, client_t, link);
266 return client_key_hash(&client->in_task_id);
267}
268
269static bool client_key_equal(void *key, const ht_link_t *item)
270{
271 task_id_t in_task_id = *(task_id_t *) key;
272 client_t *client = hash_table_get_inst(item, client_t, link);
273 return in_task_id == client->in_task_id;
274}
275
276/** Operations for the client hash table. */
277static hash_table_ops_t client_hash_table_ops = {
278 .hash = client_hash,
279 .key_hash = client_key_hash,
280 .key_equal = client_key_equal,
281 .equal = NULL,
282 .remove_callback = NULL
283};
284
285typedef struct {
286 task_id_t task_id;
287 sysarg_t phone_hash;
288} conn_key_t;
289
290/** Compute hash into the connection hash table
291 *
292 * The hash is based on the source task ID and the source phone hash. The task
293 * ID is included in the hash because a phone hash alone might not be unique
294 * while we still track connections for killed tasks due to kernel's recycling
295 * of phone structures.
296 *
297 * @param key Pointer to the connection key structure.
298 *
299 * @return Index into the connection hash table.
300 *
301 */
302static size_t conn_key_hash(void *key)
303{
304 conn_key_t *ck = (conn_key_t *) key;
305
306 size_t hash = 0;
307 hash = hash_combine(hash, LOWER32(ck->task_id));
308 hash = hash_combine(hash, UPPER32(ck->task_id));
309 hash = hash_combine(hash, ck->phone_hash);
310 return hash;
311}
312
313static size_t conn_hash(const ht_link_t *item)
314{
315 connection_t *conn = hash_table_get_inst(item, connection_t, link);
316 return conn_key_hash(&(conn_key_t){
317 .task_id = conn->in_task_id,
318 .phone_hash = conn->in_phone_hash
319 });
320}
321
322static bool conn_key_equal(void *key, const ht_link_t *item)
323{
324 conn_key_t *ck = (conn_key_t *) key;
325 connection_t *conn = hash_table_get_inst(item, connection_t, link);
326 return ((ck->task_id == conn->in_task_id) &&
327 (ck->phone_hash == conn->in_phone_hash));
328}
329
330/** Operations for the connection hash table. */
331static hash_table_ops_t conn_hash_table_ops = {
332 .hash = conn_hash,
333 .key_hash = conn_key_hash,
334 .key_equal = conn_key_equal,
335 .equal = NULL,
336 .remove_callback = NULL
337};
338
339static client_t *async_client_get(task_id_t client_id, bool create)
340{
341 client_t *client = NULL;
342
343 futex_lock(&client_futex);
344 ht_link_t *link = hash_table_find(&client_hash_table, &client_id);
345 if (link) {
346 client = hash_table_get_inst(link, client_t, link);
347 atomic_inc(&client->refcnt);
348 } else if (create) {
349 // TODO: move the malloc out of critical section
350 client = malloc(sizeof(client_t));
351 if (client) {
352 client->in_task_id = client_id;
353 client->data = async_client_data_create();
354
355 atomic_set(&client->refcnt, 1);
356 hash_table_insert(&client_hash_table, &client->link);
357 }
358 }
359
360 futex_unlock(&client_futex);
361 return client;
362}
363
364static void async_client_put(client_t *client)
365{
366 bool destroy;
367
368 futex_lock(&client_futex);
369
370 if (atomic_predec(&client->refcnt) == 0) {
371 hash_table_remove(&client_hash_table, &client->in_task_id);
372 destroy = true;
373 } else
374 destroy = false;
375
376 futex_unlock(&client_futex);
377
378 if (destroy) {
379 if (client->data)
380 async_client_data_destroy(client->data);
381
382 free(client);
383 }
384}
385
386/** Wrapper for client connection fibril.
387 *
388 * When a new connection arrives, a fibril with this implementing
389 * function is created.
390 *
391 * @param arg Connection structure pointer.
392 *
393 * @return Always zero.
394 *
395 */
396static errno_t connection_fibril(void *arg)
397{
398 assert(arg);
399
400 /*
401 * Setup fibril-local connection pointer.
402 */
403 fibril_connection = (connection_t *) arg;
404
405 /*
406 * Add our reference for the current connection in the client task
407 * tracking structure. If this is the first reference, create and
408 * hash in a new tracking structure.
409 */
410
411 client_t *client = async_client_get(fibril_connection->in_task_id, true);
412 if (!client) {
413 ipc_answer_0(fibril_connection->chandle, ENOMEM);
414 return 0;
415 }
416
417 fibril_connection->client = client;
418
419 /*
420 * Call the connection handler function.
421 */
422 fibril_connection->handler(fibril_connection->chandle,
423 &fibril_connection->call, fibril_connection->data);
424
425 /*
426 * Remove the reference for this client task connection.
427 */
428 async_client_put(client);
429
430 /*
431 * Remove myself from the connection hash table.
432 */
433 futex_down(&async_futex);
434 hash_table_remove(&conn_hash_table, &(conn_key_t){
435 .task_id = fibril_connection->in_task_id,
436 .phone_hash = fibril_connection->in_phone_hash
437 });
438 futex_up(&async_futex);
439
440 /*
441 * Answer all remaining messages with EHANGUP.
442 */
443 while (!list_empty(&fibril_connection->msg_queue)) {
444 msg_t *msg =
445 list_get_instance(list_first(&fibril_connection->msg_queue),
446 msg_t, link);
447
448 list_remove(&msg->link);
449 ipc_answer_0(msg->chandle, EHANGUP);
450 free(msg);
451 }
452
453 /*
454 * If the connection was hung-up, answer the last call,
455 * i.e. IPC_M_PHONE_HUNGUP.
456 */
457 if (fibril_connection->close_chandle)
458 ipc_answer_0(fibril_connection->close_chandle, EOK);
459
460 free(fibril_connection);
461 return EOK;
462}
463
464/** Create a new fibril for a new connection.
465 *
466 * Create new fibril for connection, fill in connection structures and insert it
467 * into the hash table, so that later we can easily do routing of messages to
468 * particular fibrils.
469 *
470 * @param in_task_id Identification of the incoming connection.
471 * @param in_phone_hash Identification of the incoming connection.
472 * @param chandle Handle of the opening IPC_M_CONNECT_ME_TO call.
473 * If chandle is CAP_NIL, the connection was opened by
474 * accepting the IPC_M_CONNECT_TO_ME call and this
475 * function is called directly by the server.
476 * @param call Call data of the opening call.
477 * @param handler Connection handler.
478 * @param data Client argument to pass to the connection handler.
479 *
480 * @return New fibril id or NULL on failure.
481 *
482 */
483static fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
484 cap_call_handle_t chandle, ipc_call_t *call, async_port_handler_t handler,
485 void *data)
486{
487 connection_t *conn = malloc(sizeof(*conn));
488 if (!conn) {
489 if (chandle != CAP_NIL)
490 ipc_answer_0(chandle, ENOMEM);
491
492 return (uintptr_t) NULL;
493 }
494
495 conn->in_task_id = in_task_id;
496 conn->in_phone_hash = in_phone_hash;
497 list_initialize(&conn->msg_queue);
498 conn->chandle = chandle;
499 conn->close_chandle = CAP_NIL;
500 conn->handler = handler;
501 conn->data = data;
502
503 if (call)
504 conn->call = *call;
505
506 /* We will activate the fibril ASAP */
507 conn->wdata.active = true;
508 conn->wdata.fid = fibril_create(connection_fibril, conn);
509
510 if (conn->wdata.fid == 0) {
511 free(conn);
512
513 if (chandle != CAP_NIL)
514 ipc_answer_0(chandle, ENOMEM);
515
516 return (uintptr_t) NULL;
517 }
518
519 /* Add connection to the connection hash table */
520
521 futex_down(&async_futex);
522 hash_table_insert(&conn_hash_table, &conn->link);
523 futex_up(&async_futex);
524
525 fibril_add_ready(conn->wdata.fid);
526
527 return conn->wdata.fid;
528}
529
530/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
531 *
532 * Ask through phone for a new connection to some service.
533 *
534 * @param exch Exchange for sending the message.
535 * @param iface Callback interface.
536 * @param arg1 User defined argument.
537 * @param arg2 User defined argument.
538 * @param handler Callback handler.
539 * @param data Handler data.
540 * @param port_id ID of the newly created port.
541 *
542 * @return Zero on success or an error code.
543 *
544 */
545errno_t async_create_callback_port(async_exch_t *exch, iface_t iface, sysarg_t arg1,
546 sysarg_t arg2, async_port_handler_t handler, void *data, port_id_t *port_id)
547{
548 if ((iface & IFACE_MOD_CALLBACK) != IFACE_MOD_CALLBACK)
549 return EINVAL;
550
551 if (exch == NULL)
552 return ENOENT;
553
554 ipc_call_t answer;
555 aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, iface, arg1, arg2,
556 &answer);
557
558 errno_t rc;
559 async_wait_for(req, &rc);
560 if (rc != EOK)
561 return rc;
562
563 rc = async_create_port_internal(iface, handler, data, port_id);
564 if (rc != EOK)
565 return rc;
566
567 sysarg_t phone_hash = IPC_GET_ARG5(answer);
568 fid_t fid = async_new_connection(answer.in_task_id, phone_hash,
569 CAP_NIL, NULL, handler, data);
570 if (fid == (uintptr_t) NULL)
571 return ENOMEM;
572
573 return EOK;
574}
575
576static size_t notification_key_hash(void *key)
577{
578 sysarg_t id = *(sysarg_t *) key;
579 return id;
580}
581
582static size_t notification_hash(const ht_link_t *item)
583{
584 notification_t *notification =
585 hash_table_get_inst(item, notification_t, htlink);
586 return notification_key_hash(&notification->imethod);
587}
588
589static bool notification_key_equal(void *key, const ht_link_t *item)
590{
591 sysarg_t id = *(sysarg_t *) key;
592 notification_t *notification =
593 hash_table_get_inst(item, notification_t, htlink);
594 return id == notification->imethod;
595}
596
597/** Operations for the notification hash table. */
598static hash_table_ops_t notification_hash_table_ops = {
599 .hash = notification_hash,
600 .key_hash = notification_key_hash,
601 .key_equal = notification_key_equal,
602 .equal = NULL,
603 .remove_callback = NULL
604};
605
606/** Sort in current fibril's timeout request.
607 *
608 * @param wd Wait data of the current fibril.
609 *
610 */
611void async_insert_timeout(awaiter_t *wd)
612{
613 assert(wd);
614
615 wd->to_event.occurred = false;
616 wd->to_event.inlist = true;
617
618 link_t *tmp = timeout_list.head.next;
619 while (tmp != &timeout_list.head) {
620 awaiter_t *cur =
621 list_get_instance(tmp, awaiter_t, to_event.link);
622
623 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
624 break;
625
626 tmp = tmp->next;
627 }
628
629 list_insert_before(&wd->to_event.link, tmp);
630}
631
632/** Try to route a call to an appropriate connection fibril.
633 *
634 * If the proper connection fibril is found, a message with the call is added to
635 * its message queue. If the fibril was not active, it is activated and all
636 * timeouts are unregistered.
637 *
638 * @param chandle Handle of the incoming call.
639 * @param call Data of the incoming call.
640 *
641 * @return False if the call doesn't match any connection.
642 * @return True if the call was passed to the respective connection fibril.
643 *
644 */
645static bool route_call(cap_call_handle_t chandle, ipc_call_t *call)
646{
647 assert(call);
648
649 futex_down(&async_futex);
650
651 ht_link_t *link = hash_table_find(&conn_hash_table, &(conn_key_t){
652 .task_id = call->in_task_id,
653 .phone_hash = call->in_phone_hash
654 });
655 if (!link) {
656 futex_up(&async_futex);
657 return false;
658 }
659
660 connection_t *conn = hash_table_get_inst(link, connection_t, link);
661
662 msg_t *msg = malloc(sizeof(*msg));
663 if (!msg) {
664 futex_up(&async_futex);
665 return false;
666 }
667
668 msg->chandle = chandle;
669 msg->call = *call;
670 list_append(&msg->link, &conn->msg_queue);
671
672 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
673 conn->close_chandle = chandle;
674
675 /* If the connection fibril is waiting for an event, activate it */
676 if (!conn->wdata.active) {
677
678 /* If in timeout list, remove it */
679 if (conn->wdata.to_event.inlist) {
680 conn->wdata.to_event.inlist = false;
681 list_remove(&conn->wdata.to_event.link);
682 }
683
684 conn->wdata.active = true;
685 fibril_add_ready(conn->wdata.fid);
686 }
687
688 futex_up(&async_futex);
689 return true;
690}
691
692/** Function implementing the notification handler fibril. Never returns. */
693static errno_t notification_fibril_func(void *arg)
694{
695 (void) arg;
696
697 while (true) {
698 fibril_semaphore_down(&notification_semaphore);
699
700 futex_lock(&notification_futex);
701
702 /*
703 * The semaphore ensures that if we get this far,
704 * the queue must be non-empty.
705 */
706 assert(!list_empty(&notification_queue));
707
708 notification_t *notification = list_get_instance(
709 list_first(&notification_queue), notification_t, qlink);
710 list_remove(&notification->qlink);
711
712 async_notification_handler_t handler = notification->handler;
713 void *arg = notification->arg;
714 ipc_call_t calldata = notification->calldata;
715 long count = notification->count;
716
717 notification->count = 0;
718
719 futex_unlock(&notification_futex);
720
721 // FIXME: Pass count to the handler. It might be important.
722 (void) count;
723
724 if (handler)
725 handler(&calldata, arg);
726 }
727
728 /* Not reached. */
729 return EOK;
730}
731
732/**
733 * Creates a new dedicated fibril for handling notifications.
734 * By default, there is one such fibril. This function can be used to
735 * create more in order to increase the number of notification that can
736 * be processed concurrently.
737 *
738 * Currently, there is no way to destroy those fibrils after they are created.
739 */
740errno_t async_spawn_notification_handler(void)
741{
742 fid_t f = fibril_create(notification_fibril_func, NULL);
743 if (f == 0)
744 return ENOMEM;
745
746 fibril_add_ready(f);
747 return EOK;
748}
749
750/** Queue notification.
751 *
752 * @param call Data of the incoming call.
753 *
754 */
755static void queue_notification(ipc_call_t *call)
756{
757 assert(call);
758
759 futex_lock(&notification_futex);
760
761 ht_link_t *link = hash_table_find(&notification_hash_table,
762 &IPC_GET_IMETHOD(*call));
763 if (!link) {
764 /* Invalid notification. */
765 // TODO: Make sure this can't happen and turn it into assert.
766 futex_unlock(&notification_futex);
767 return;
768 }
769
770 notification_t *notification =
771 hash_table_get_inst(link, notification_t, htlink);
772
773 notification->count++;
774 notification->calldata = *call;
775
776 if (link_in_use(&notification->qlink)) {
777 /* Notification already queued. */
778 futex_unlock(&notification_futex);
779 return;
780 }
781
782 list_append(&notification->qlink, &notification_queue);
783 futex_unlock(&notification_futex);
784
785 fibril_semaphore_up(&notification_semaphore);
786}
787
788/**
789 * Creates a new notification structure and inserts it into the hash table.
790 *
791 * @param handler Function to call when notification is received.
792 * @param arg Argument for the handler function.
793 * @return The newly created notification structure.
794 */
795static notification_t *notification_create(async_notification_handler_t handler, void *arg)
796{
797 notification_t *notification = calloc(1, sizeof(notification_t));
798 if (!notification)
799 return NULL;
800
801 notification->handler = handler;
802 notification->arg = arg;
803
804 fid_t fib = 0;
805
806 futex_lock(&notification_futex);
807
808 if (notification_avail == 0) {
809 /* Attempt to create the first handler fibril. */
810 fib = fibril_create(notification_fibril_func, NULL);
811 if (fib == 0) {
812 futex_unlock(&notification_futex);
813 free(notification);
814 return NULL;
815 }
816 }
817
818 sysarg_t imethod = notification_avail;
819 notification_avail++;
820
821 notification->imethod = imethod;
822 hash_table_insert(&notification_hash_table, &notification->htlink);
823
824 futex_unlock(&notification_futex);
825
826 if (imethod == 0) {
827 assert(fib);
828 fibril_add_ready(fib);
829 }
830
831 return notification;
832}
833
834/** Subscribe to IRQ notification.
835 *
836 * @param inr IRQ number.
837 * @param handler Notification handler.
838 * @param data Notification handler client data.
839 * @param ucode Top-half pseudocode handler.
840 *
841 * @param[out] handle IRQ capability handle on success.
842 *
843 * @return An error code.
844 *
845 */
846errno_t async_irq_subscribe(int inr, async_notification_handler_t handler,
847 void *data, const irq_code_t *ucode, cap_irq_handle_t *handle)
848{
849 notification_t *notification = notification_create(handler, data);
850 if (!notification)
851 return ENOMEM;
852
853 cap_irq_handle_t ihandle;
854 errno_t rc = ipc_irq_subscribe(inr, notification->imethod, ucode,
855 &ihandle);
856 if (rc == EOK && handle != NULL) {
857 *handle = ihandle;
858 }
859 return rc;
860}
861
862/** Unsubscribe from IRQ notification.
863 *
864 * @param handle IRQ capability handle.
865 *
866 * @return Zero on success or an error code.
867 *
868 */
869errno_t async_irq_unsubscribe(cap_irq_handle_t ihandle)
870{
871 // TODO: Remove entry from hash table
872 // to avoid memory leak
873
874 return ipc_irq_unsubscribe(ihandle);
875}
876
877/** Subscribe to event notifications.
878 *
879 * @param evno Event type to subscribe.
880 * @param handler Notification handler.
881 * @param data Notification handler client data.
882 *
883 * @return Zero on success or an error code.
884 *
885 */
886errno_t async_event_subscribe(event_type_t evno,
887 async_notification_handler_t handler, void *data)
888{
889 notification_t *notification = notification_create(handler, data);
890 if (!notification)
891 return ENOMEM;
892
893 return ipc_event_subscribe(evno, notification->imethod);
894}
895
896/** Subscribe to task event notifications.
897 *
898 * @param evno Event type to subscribe.
899 * @param handler Notification handler.
900 * @param data Notification handler client data.
901 *
902 * @return Zero on success or an error code.
903 *
904 */
905errno_t async_event_task_subscribe(event_task_type_t evno,
906 async_notification_handler_t handler, void *data)
907{
908 notification_t *notification = notification_create(handler, data);
909 if (!notification)
910 return ENOMEM;
911
912 return ipc_event_task_subscribe(evno, notification->imethod);
913}
914
915/** Unmask event notifications.
916 *
917 * @param evno Event type to unmask.
918 *
919 * @return Value returned by the kernel.
920 *
921 */
922errno_t async_event_unmask(event_type_t evno)
923{
924 return ipc_event_unmask(evno);
925}
926
927/** Unmask task event notifications.
928 *
929 * @param evno Event type to unmask.
930 *
931 * @return Value returned by the kernel.
932 *
933 */
934errno_t async_event_task_unmask(event_task_type_t evno)
935{
936 return ipc_event_task_unmask(evno);
937}
938
939/** Return new incoming message for the current (fibril-local) connection.
940 *
941 * @param call Storage where the incoming call data will be stored.
942 * @param usecs Timeout in microseconds. Zero denotes no timeout.
943 *
944 * @return If no timeout was specified, then a handle of the incoming call is
945 * returned. If a timeout is specified, then a handle of the incoming
946 * call is returned unless the timeout expires prior to receiving a
947 * message. In that case zero CAP_NIL is returned.
948 */
949cap_call_handle_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
950{
951 assert(call);
952 assert(fibril_connection);
953
954 /*
955 * Why doing this?
956 * GCC 4.1.0 coughs on fibril_connection-> dereference.
957 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
958 * I would never expect to find so many errors in
959 * a compiler.
960 */
961 connection_t *conn = fibril_connection;
962
963 futex_down(&async_futex);
964
965 if (usecs) {
966 getuptime(&conn->wdata.to_event.expires);
967 tv_add_diff(&conn->wdata.to_event.expires, usecs);
968 } else
969 conn->wdata.to_event.inlist = false;
970
971 /* If nothing in queue, wait until something arrives */
972 while (list_empty(&conn->msg_queue)) {
973 if (conn->close_chandle) {
974 /*
975 * Handle the case when the connection was already
976 * closed by the client but the server did not notice
977 * the first IPC_M_PHONE_HUNGUP call and continues to
978 * call async_get_call_timeout(). Repeat
979 * IPC_M_PHONE_HUNGUP until the caller notices.
980 */
981 memset(call, 0, sizeof(ipc_call_t));
982 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
983 futex_up(&async_futex);
984 return conn->close_chandle;
985 }
986
987 if (usecs)
988 async_insert_timeout(&conn->wdata);
989
990 conn->wdata.active = false;
991
992 /*
993 * Note: the current fibril will be rescheduled either due to a
994 * timeout or due to an arriving message destined to it. In the
995 * former case, handle_expired_timeouts() and, in the latter
996 * case, route_call() will perform the wakeup.
997 */
998 fibril_switch(FIBRIL_TO_MANAGER);
999
1000 /*
1001 * Futex is up after getting back from async_manager.
1002 * Get it again.
1003 */
1004 futex_down(&async_futex);
1005 if ((usecs) && (conn->wdata.to_event.occurred) &&
1006 (list_empty(&conn->msg_queue))) {
1007 /* If we timed out -> exit */
1008 futex_up(&async_futex);
1009 return CAP_NIL;
1010 }
1011 }
1012
1013 msg_t *msg = list_get_instance(list_first(&conn->msg_queue),
1014 msg_t, link);
1015 list_remove(&msg->link);
1016
1017 cap_call_handle_t chandle = msg->chandle;
1018 *call = msg->call;
1019 free(msg);
1020
1021 futex_up(&async_futex);
1022 return chandle;
1023}
1024
1025void *async_get_client_data(void)
1026{
1027 assert(fibril_connection);
1028 return fibril_connection->client->data;
1029}
1030
1031void *async_get_client_data_by_id(task_id_t client_id)
1032{
1033 client_t *client = async_client_get(client_id, false);
1034 if (!client)
1035 return NULL;
1036
1037 if (!client->data) {
1038 async_client_put(client);
1039 return NULL;
1040 }
1041
1042 return client->data;
1043}
1044
1045void async_put_client_data_by_id(task_id_t client_id)
1046{
1047 client_t *client = async_client_get(client_id, false);
1048
1049 assert(client);
1050 assert(client->data);
1051
1052 /* Drop the reference we got in async_get_client_data_by_hash(). */
1053 async_client_put(client);
1054
1055 /* Drop our own reference we got at the beginning of this function. */
1056 async_client_put(client);
1057}
1058
1059/** Handle a call that was received.
1060 *
1061 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
1062 * Otherwise the call is routed to its connection fibril.
1063 *
1064 * @param chandle Handle of the incoming call.
1065 * @param call Data of the incoming call.
1066 *
1067 */
1068static void handle_call(cap_call_handle_t chandle, ipc_call_t *call)
1069{
1070 assert(call);
1071
1072 /* Kernel notification */
1073 if ((chandle == CAP_NIL) && (call->flags & IPC_CALL_NOTIF)) {
1074 queue_notification(call);
1075 return;
1076 }
1077
1078 /* New connection */
1079 if (IPC_GET_IMETHOD(*call) == IPC_M_CONNECT_ME_TO) {
1080 iface_t iface = (iface_t) IPC_GET_ARG1(*call);
1081 sysarg_t in_phone_hash = IPC_GET_ARG5(*call);
1082
1083 // TODO: Currently ignores all ports but the first one.
1084 void *data;
1085 async_port_handler_t handler =
1086 async_get_port_handler(iface, 0, &data);
1087
1088 async_new_connection(call->in_task_id, in_phone_hash, chandle,
1089 call, handler, data);
1090 return;
1091 }
1092
1093 /* Try to route the call through the connection hash table */
1094 if (route_call(chandle, call))
1095 return;
1096
1097 /* Unknown call from unknown phone - hang it up */
1098 ipc_answer_0(chandle, EHANGUP);
1099}
1100
1101/** Fire all timeouts that expired. */
1102static void handle_expired_timeouts(void)
1103{
1104 struct timeval tv;
1105 getuptime(&tv);
1106
1107 futex_down(&async_futex);
1108
1109 link_t *cur = list_first(&timeout_list);
1110 while (cur != NULL) {
1111 awaiter_t *waiter =
1112 list_get_instance(cur, awaiter_t, to_event.link);
1113
1114 if (tv_gt(&waiter->to_event.expires, &tv))
1115 break;
1116
1117 list_remove(&waiter->to_event.link);
1118 waiter->to_event.inlist = false;
1119 waiter->to_event.occurred = true;
1120
1121 /*
1122 * Redundant condition?
1123 * The fibril should not be active when it gets here.
1124 */
1125 if (!waiter->active) {
1126 waiter->active = true;
1127 fibril_add_ready(waiter->fid);
1128 }
1129
1130 cur = list_first(&timeout_list);
1131 }
1132
1133 futex_up(&async_futex);
1134}
1135
1136/** Endless loop dispatching incoming calls and answers.
1137 *
1138 * @return Never returns.
1139 *
1140 */
1141static errno_t async_manager_worker(void)
1142{
1143 while (true) {
1144 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
1145 futex_up(&async_futex);
1146 /*
1147 * async_futex is always held when entering a manager
1148 * fibril.
1149 */
1150 continue;
1151 }
1152
1153 futex_down(&async_futex);
1154
1155 suseconds_t timeout;
1156 unsigned int flags = SYNCH_FLAGS_NONE;
1157 if (!list_empty(&timeout_list)) {
1158 awaiter_t *waiter = list_get_instance(
1159 list_first(&timeout_list), awaiter_t, to_event.link);
1160
1161 struct timeval tv;
1162 getuptime(&tv);
1163
1164 if (tv_gteq(&tv, &waiter->to_event.expires)) {
1165 futex_up(&async_futex);
1166 handle_expired_timeouts();
1167 /*
1168 * Notice that even if the event(s) already
1169 * expired (and thus the other fibril was
1170 * supposed to be running already),
1171 * we check for incoming IPC.
1172 *
1173 * Otherwise, a fibril that continuously
1174 * creates (almost) expired events could
1175 * prevent IPC retrieval from the kernel.
1176 */
1177 timeout = 0;
1178 flags = SYNCH_FLAGS_NON_BLOCKING;
1179
1180 } else {
1181 timeout = tv_sub_diff(&waiter->to_event.expires,
1182 &tv);
1183 futex_up(&async_futex);
1184 }
1185 } else {
1186 futex_up(&async_futex);
1187 timeout = SYNCH_NO_TIMEOUT;
1188 }
1189
1190 atomic_inc(&threads_in_ipc_wait);
1191
1192 ipc_call_t call;
1193 errno_t rc = ipc_wait_cycle(&call, timeout, flags);
1194
1195 atomic_dec(&threads_in_ipc_wait);
1196
1197 assert(rc == EOK);
1198
1199 if (call.cap_handle == CAP_NIL) {
1200 if ((call.flags &
1201 (IPC_CALL_NOTIF | IPC_CALL_ANSWERED)) == 0) {
1202 /* Neither a notification nor an answer. */
1203 handle_expired_timeouts();
1204 continue;
1205 }
1206 }
1207
1208 if (call.flags & IPC_CALL_ANSWERED)
1209 continue;
1210
1211 handle_call(call.cap_handle, &call);
1212 }
1213
1214 return 0;
1215}
1216
1217/** Function to start async_manager as a standalone fibril.
1218 *
1219 * When more kernel threads are used, one async manager should exist per thread.
1220 *
1221 * @param arg Unused.
1222 * @return Never returns.
1223 *
1224 */
1225static errno_t async_manager_fibril(void *arg)
1226{
1227 futex_up(&async_futex);
1228
1229 /*
1230 * async_futex is always locked when entering manager
1231 */
1232 async_manager_worker();
1233
1234 return 0;
1235}
1236
1237/** Add one manager to manager list. */
1238void async_create_manager(void)
1239{
1240 fid_t fid = fibril_create_generic(async_manager_fibril, NULL, PAGE_SIZE);
1241 if (fid != 0)
1242 fibril_add_manager(fid);
1243}
1244
1245/** Remove one manager from manager list */
1246void async_destroy_manager(void)
1247{
1248 fibril_remove_manager();
1249}
1250
1251/** Initialize the async framework.
1252 *
1253 */
1254void __async_server_init(void)
1255{
1256 if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
1257 abort();
1258
1259 if (!hash_table_create(&conn_hash_table, 0, 0, &conn_hash_table_ops))
1260 abort();
1261
1262 if (!hash_table_create(&notification_hash_table, 0, 0,
1263 &notification_hash_table_ops))
1264 abort();
1265}
1266
1267errno_t async_answer_0(cap_call_handle_t chandle, errno_t retval)
1268{
1269 return ipc_answer_0(chandle, retval);
1270}
1271
1272errno_t async_answer_1(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1)
1273{
1274 return ipc_answer_1(chandle, retval, arg1);
1275}
1276
1277errno_t async_answer_2(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
1278 sysarg_t arg2)
1279{
1280 return ipc_answer_2(chandle, retval, arg1, arg2);
1281}
1282
1283errno_t async_answer_3(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
1284 sysarg_t arg2, sysarg_t arg3)
1285{
1286 return ipc_answer_3(chandle, retval, arg1, arg2, arg3);
1287}
1288
1289errno_t async_answer_4(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
1290 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1291{
1292 return ipc_answer_4(chandle, retval, arg1, arg2, arg3, arg4);
1293}
1294
1295errno_t async_answer_5(cap_call_handle_t chandle, errno_t retval, sysarg_t arg1,
1296 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1297{
1298 return ipc_answer_5(chandle, retval, arg1, arg2, arg3, arg4, arg5);
1299}
1300
1301errno_t async_forward_fast(cap_call_handle_t chandle, async_exch_t *exch,
1302 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
1303{
1304 if (exch == NULL)
1305 return ENOENT;
1306
1307 return ipc_forward_fast(chandle, exch->phone, imethod, arg1, arg2, mode);
1308}
1309
1310errno_t async_forward_slow(cap_call_handle_t chandle, async_exch_t *exch,
1311 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
1312 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
1313{
1314 if (exch == NULL)
1315 return ENOENT;
1316
1317 return ipc_forward_slow(chandle, exch->phone, imethod, arg1, arg2, arg3,
1318 arg4, arg5, mode);
1319}
1320
1321/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1322 *
1323 * Ask through phone for a new connection to some service.
1324 *
1325 * @param exch Exchange for sending the message.
1326 * @param arg1 User defined argument.
1327 * @param arg2 User defined argument.
1328 * @param arg3 User defined argument.
1329 *
1330 * @return Zero on success or an error code.
1331 *
1332 */
1333errno_t async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
1334 sysarg_t arg3)
1335{
1336 if (exch == NULL)
1337 return ENOENT;
1338
1339 ipc_call_t answer;
1340 aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1341 &answer);
1342
1343 errno_t rc;
1344 async_wait_for(req, &rc);
1345 if (rc != EOK)
1346 return (errno_t) rc;
1347
1348 return EOK;
1349}
1350
1351/** Interrupt one thread of this task from waiting for IPC. */
1352void async_poke(void)
1353{
1354 if (atomic_get(&threads_in_ipc_wait) > 0)
1355 ipc_poke();
1356}
1357
1358/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1359 *
1360 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
1361 * calls so that the user doesn't have to remember the meaning of each IPC
1362 * argument.
1363 *
1364 * So far, this wrapper is to be used from within a connection fibril.
1365 *
1366 * @param chandle Storage for the handle of the IPC_M_SHARE_IN call.
1367 * @param size Destination address space area size.
1368 *
1369 * @return True on success, false on failure.
1370 *
1371 */
1372bool async_share_in_receive(cap_call_handle_t *chandle, size_t *size)
1373{
1374 assert(chandle);
1375 assert(size);
1376
1377 ipc_call_t data;
1378 *chandle = async_get_call(&data);
1379
1380 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
1381 return false;
1382
1383 *size = (size_t) IPC_GET_ARG1(data);
1384 return true;
1385}
1386
1387/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1388 *
1389 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
1390 * calls so that the user doesn't have to remember the meaning of each IPC
1391 * argument.
1392 *
1393 * @param chandle Handle of the IPC_M_DATA_READ call to answer.
1394 * @param src Source address space base.
1395 * @param flags Flags to be used for sharing. Bits can be only cleared.
1396 *
1397 * @return Zero on success or a value from @ref errno.h on failure.
1398 *
1399 */
1400errno_t async_share_in_finalize(cap_call_handle_t chandle, void *src,
1401 unsigned int flags)
1402{
1403 // FIXME: The source has no business deciding destination address.
1404 return ipc_answer_3(chandle, EOK, (sysarg_t) src, (sysarg_t) flags,
1405 (sysarg_t) _end);
1406}
1407
1408/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1409 *
1410 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
1411 * calls so that the user doesn't have to remember the meaning of each IPC
1412 * argument.
1413 *
1414 * So far, this wrapper is to be used from within a connection fibril.
1415 *
1416 * @param chandle Storage for the hash of the IPC_M_SHARE_OUT call.
1417 * @param size Storage for the source address space area size.
1418 * @param flags Storage for the sharing flags.
1419 *
1420 * @return True on success, false on failure.
1421 *
1422 */
1423bool async_share_out_receive(cap_call_handle_t *chandle, size_t *size,
1424 unsigned int *flags)
1425{
1426 assert(chandle);
1427 assert(size);
1428 assert(flags);
1429
1430 ipc_call_t data;
1431 *chandle = async_get_call(&data);
1432
1433 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
1434 return false;
1435
1436 *size = (size_t) IPC_GET_ARG2(data);
1437 *flags = (unsigned int) IPC_GET_ARG3(data);
1438 return true;
1439}
1440
1441/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
1442 *
1443 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
1444 * calls so that the user doesn't have to remember the meaning of each IPC
1445 * argument.
1446 *
1447 * @param chandle Handle of the IPC_M_DATA_WRITE call to answer.
1448 * @param dst Address of the storage for the destination address space area
1449 * base address.
1450 *
1451 * @return Zero on success or a value from @ref errno.h on failure.
1452 *
1453 */
1454errno_t async_share_out_finalize(cap_call_handle_t chandle, void **dst)
1455{
1456 return ipc_answer_2(chandle, EOK, (sysarg_t) _end, (sysarg_t) dst);
1457}
1458
1459/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
1460 *
1461 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
1462 * calls so that the user doesn't have to remember the meaning of each IPC
1463 * argument.
1464 *
1465 * So far, this wrapper is to be used from within a connection fibril.
1466 *
1467 * @param chandle Storage for the handle of the IPC_M_DATA_READ.
1468 * @param size Storage for the maximum size. Can be NULL.
1469 *
1470 * @return True on success, false on failure.
1471 *
1472 */
1473bool async_data_read_receive(cap_call_handle_t *chandle, size_t *size)
1474{
1475 ipc_call_t data;
1476 return async_data_read_receive_call(chandle, &data, size);
1477}
1478
1479/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
1480 *
1481 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
1482 * calls so that the user doesn't have to remember the meaning of each IPC
1483 * argument.
1484 *
1485 * So far, this wrapper is to be used from within a connection fibril.
1486 *
1487 * @param chandle Storage for the handle of the IPC_M_DATA_READ.
1488 * @param size Storage for the maximum size. Can be NULL.
1489 *
1490 * @return True on success, false on failure.
1491 *
1492 */
1493bool async_data_read_receive_call(cap_call_handle_t *chandle, ipc_call_t *data,
1494 size_t *size)
1495{
1496 assert(chandle);
1497 assert(data);
1498
1499 *chandle = async_get_call(data);
1500
1501 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_READ)
1502 return false;
1503
1504 if (size)
1505 *size = (size_t) IPC_GET_ARG2(*data);
1506
1507 return true;
1508}
1509
1510/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
1511 *
1512 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
1513 * calls so that the user doesn't have to remember the meaning of each IPC
1514 * argument.
1515 *
1516 * @param chandle Handle of the IPC_M_DATA_READ call to answer.
1517 * @param src Source address for the IPC_M_DATA_READ call.
1518 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
1519 * the maximum size announced by the sender.
1520 *
1521 * @return Zero on success or a value from @ref errno.h on failure.
1522 *
1523 */
1524errno_t async_data_read_finalize(cap_call_handle_t chandle, const void *src,
1525 size_t size)
1526{
1527 return ipc_answer_2(chandle, EOK, (sysarg_t) src, (sysarg_t) size);
1528}
1529
1530/** Wrapper for forwarding any read request
1531 *
1532 */
1533errno_t async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
1534 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
1535 ipc_call_t *dataptr)
1536{
1537 if (exch == NULL)
1538 return ENOENT;
1539
1540 cap_call_handle_t chandle;
1541 if (!async_data_read_receive(&chandle, NULL)) {
1542 ipc_answer_0(chandle, EINVAL);
1543 return EINVAL;
1544 }
1545
1546 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
1547 dataptr);
1548 if (msg == 0) {
1549 ipc_answer_0(chandle, EINVAL);
1550 return EINVAL;
1551 }
1552
1553 errno_t retval = ipc_forward_fast(chandle, exch->phone, 0, 0, 0,
1554 IPC_FF_ROUTE_FROM_ME);
1555 if (retval != EOK) {
1556 async_forget(msg);
1557 ipc_answer_0(chandle, retval);
1558 return retval;
1559 }
1560
1561 errno_t rc;
1562 async_wait_for(msg, &rc);
1563
1564 return (errno_t) rc;
1565}
1566
1567/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
1568 *
1569 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
1570 * calls so that the user doesn't have to remember the meaning of each IPC
1571 * argument.
1572 *
1573 * So far, this wrapper is to be used from within a connection fibril.
1574 *
1575 * @param chandle Storage for the handle of the IPC_M_DATA_WRITE.
1576 * @param size Storage for the suggested size. May be NULL.
1577 *
1578 * @return True on success, false on failure.
1579 *
1580 */
1581bool async_data_write_receive(cap_call_handle_t *chandle, size_t *size)
1582{
1583 ipc_call_t data;
1584 return async_data_write_receive_call(chandle, &data, size);
1585}
1586
1587/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
1588 *
1589 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
1590 * calls so that the user doesn't have to remember the meaning of each IPC
1591 * argument.
1592 *
1593 * So far, this wrapper is to be used from within a connection fibril.
1594 *
1595 * @param chandle Storage for the handle of the IPC_M_DATA_WRITE.
1596 * @param data Storage for the ipc call data.
1597 * @param size Storage for the suggested size. May be NULL.
1598 *
1599 * @return True on success, false on failure.
1600 *
1601 */
1602bool async_data_write_receive_call(cap_call_handle_t *chandle, ipc_call_t *data,
1603 size_t *size)
1604{
1605 assert(chandle);
1606 assert(data);
1607
1608 *chandle = async_get_call(data);
1609
1610 if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_WRITE)
1611 return false;
1612
1613 if (size)
1614 *size = (size_t) IPC_GET_ARG2(*data);
1615
1616 return true;
1617}
1618
1619/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
1620 *
1621 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
1622 * calls so that the user doesn't have to remember the meaning of each IPC
1623 * argument.
1624 *
1625 * @param chandle Handle of the IPC_M_DATA_WRITE call to answer.
1626 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
1627 * @param size Final size for the IPC_M_DATA_WRITE call.
1628 *
1629 * @return Zero on success or a value from @ref errno.h on failure.
1630 *
1631 */
1632errno_t async_data_write_finalize(cap_call_handle_t chandle, void *dst,
1633 size_t size)
1634{
1635 return ipc_answer_2(chandle, EOK, (sysarg_t) dst, (sysarg_t) size);
1636}
1637
1638/** Wrapper for receiving binary data or strings
1639 *
1640 * This wrapper only makes it more comfortable to use async_data_write_*
1641 * functions to receive binary data or strings.
1642 *
1643 * @param data Pointer to data pointer (which should be later disposed
1644 * by free()). If the operation fails, the pointer is not
1645 * touched.
1646 * @param nullterm If true then the received data is always zero terminated.
1647 * This also causes to allocate one extra byte beyond the
1648 * raw transmitted data.
1649 * @param min_size Minimum size (in bytes) of the data to receive.
1650 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
1651 * no limit.
1652 * @param granulariy If non-zero then the size of the received data has to
1653 * be divisible by this value.
1654 * @param received If not NULL, the size of the received data is stored here.
1655 *
1656 * @return Zero on success or a value from @ref errno.h on failure.
1657 *
1658 */
1659errno_t async_data_write_accept(void **data, const bool nullterm,
1660 const size_t min_size, const size_t max_size, const size_t granularity,
1661 size_t *received)
1662{
1663 assert(data);
1664
1665 cap_call_handle_t chandle;
1666 size_t size;
1667 if (!async_data_write_receive(&chandle, &size)) {
1668 ipc_answer_0(chandle, EINVAL);
1669 return EINVAL;
1670 }
1671
1672 if (size < min_size) {
1673 ipc_answer_0(chandle, EINVAL);
1674 return EINVAL;
1675 }
1676
1677 if ((max_size > 0) && (size > max_size)) {
1678 ipc_answer_0(chandle, EINVAL);
1679 return EINVAL;
1680 }
1681
1682 if ((granularity > 0) && ((size % granularity) != 0)) {
1683 ipc_answer_0(chandle, EINVAL);
1684 return EINVAL;
1685 }
1686
1687 void *arg_data;
1688
1689 if (nullterm)
1690 arg_data = malloc(size + 1);
1691 else
1692 arg_data = malloc(size);
1693
1694 if (arg_data == NULL) {
1695 ipc_answer_0(chandle, ENOMEM);
1696 return ENOMEM;
1697 }
1698
1699 errno_t rc = async_data_write_finalize(chandle, arg_data, size);
1700 if (rc != EOK) {
1701 free(arg_data);
1702 return rc;
1703 }
1704
1705 if (nullterm)
1706 ((char *) arg_data)[size] = 0;
1707
1708 *data = arg_data;
1709 if (received != NULL)
1710 *received = size;
1711
1712 return EOK;
1713}
1714
1715/** Wrapper for voiding any data that is about to be received
1716 *
1717 * This wrapper can be used to void any pending data
1718 *
1719 * @param retval Error value from @ref errno.h to be returned to the caller.
1720 *
1721 */
1722void async_data_write_void(errno_t retval)
1723{
1724 cap_call_handle_t chandle;
1725 async_data_write_receive(&chandle, NULL);
1726 ipc_answer_0(chandle, retval);
1727}
1728
1729/** Wrapper for forwarding any data that is about to be received
1730 *
1731 */
1732errno_t async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
1733 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
1734 ipc_call_t *dataptr)
1735{
1736 if (exch == NULL)
1737 return ENOENT;
1738
1739 cap_call_handle_t chandle;
1740 if (!async_data_write_receive(&chandle, NULL)) {
1741 ipc_answer_0(chandle, EINVAL);
1742 return EINVAL;
1743 }
1744
1745 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
1746 dataptr);
1747 if (msg == 0) {
1748 ipc_answer_0(chandle, EINVAL);
1749 return EINVAL;
1750 }
1751
1752 errno_t retval = ipc_forward_fast(chandle, exch->phone, 0, 0, 0,
1753 IPC_FF_ROUTE_FROM_ME);
1754 if (retval != EOK) {
1755 async_forget(msg);
1756 ipc_answer_0(chandle, retval);
1757 return retval;
1758 }
1759
1760 errno_t rc;
1761 async_wait_for(msg, &rc);
1762
1763 return (errno_t) rc;
1764}
1765
1766/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
1767 *
1768 * If the current call is IPC_M_CONNECT_TO_ME then a new
1769 * async session is created for the accepted phone.
1770 *
1771 * @param mgmt Exchange management style.
1772 *
1773 * @return New async session.
1774 * @return NULL on failure.
1775 *
1776 */
1777async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
1778{
1779 /* Accept the phone */
1780 ipc_call_t call;
1781 cap_call_handle_t chandle = async_get_call(&call);
1782 cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(call);
1783
1784 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
1785 !CAP_HANDLE_VALID((phandle))) {
1786 async_answer_0(chandle, EINVAL);
1787 return NULL;
1788 }
1789
1790 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1791 if (sess == NULL) {
1792 async_answer_0(chandle, ENOMEM);
1793 return NULL;
1794 }
1795
1796 sess->iface = 0;
1797 sess->mgmt = mgmt;
1798 sess->phone = phandle;
1799 sess->arg1 = 0;
1800 sess->arg2 = 0;
1801 sess->arg3 = 0;
1802
1803 fibril_mutex_initialize(&sess->remote_state_mtx);
1804 sess->remote_state_data = NULL;
1805
1806 list_initialize(&sess->exch_list);
1807 fibril_mutex_initialize(&sess->mutex);
1808 atomic_set(&sess->refcnt, 0);
1809
1810 /* Acknowledge the connected phone */
1811 async_answer_0(chandle, EOK);
1812
1813 return sess;
1814}
1815
1816/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
1817 *
1818 * If the call is IPC_M_CONNECT_TO_ME then a new
1819 * async session is created. However, the phone is
1820 * not accepted automatically.
1821 *
1822 * @param mgmt Exchange management style.
1823 * @param call Call data.
1824 *
1825 * @return New async session.
1826 * @return NULL on failure.
1827 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
1828 *
1829 */
1830async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
1831{
1832 cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(*call);
1833
1834 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
1835 !CAP_HANDLE_VALID((phandle)))
1836 return NULL;
1837
1838 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1839 if (sess == NULL)
1840 return NULL;
1841
1842 sess->iface = 0;
1843 sess->mgmt = mgmt;
1844 sess->phone = phandle;
1845 sess->arg1 = 0;
1846 sess->arg2 = 0;
1847 sess->arg3 = 0;
1848
1849 fibril_mutex_initialize(&sess->remote_state_mtx);
1850 sess->remote_state_data = NULL;
1851
1852 list_initialize(&sess->exch_list);
1853 fibril_mutex_initialize(&sess->mutex);
1854 atomic_set(&sess->refcnt, 0);
1855
1856 return sess;
1857}
1858
1859bool async_state_change_receive(cap_call_handle_t *chandle, sysarg_t *arg1,
1860 sysarg_t *arg2, sysarg_t *arg3)
1861{
1862 assert(chandle);
1863
1864 ipc_call_t call;
1865 *chandle = async_get_call(&call);
1866
1867 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
1868 return false;
1869
1870 if (arg1)
1871 *arg1 = IPC_GET_ARG1(call);
1872 if (arg2)
1873 *arg2 = IPC_GET_ARG2(call);
1874 if (arg3)
1875 *arg3 = IPC_GET_ARG3(call);
1876
1877 return true;
1878}
1879
1880errno_t async_state_change_finalize(cap_call_handle_t chandle,
1881 async_exch_t *other_exch)
1882{
1883 return ipc_answer_1(chandle, EOK, CAP_HANDLE_RAW(other_exch->phone));
1884}
1885
1886/** @}
1887 */
Note: See TracBrowser for help on using the repository browser.