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

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

Have async_poke() check whether the poke is necessary.

This was originally done in fibril_synch.c and the previous commit accidentally
removed it.

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