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

Last change on this file since 205f1add was 205f1add, checked in by Jakub Jermar <jakub@…>, 7 years ago

Get rid of sys/time.h

This commit moves the POSIX-like time functionality from libc's
sys/time.h to libposix and introduces C99-like or HelenOS-specific
interfaces to libc.

Specifically, use of sys/time.h, struct timeval, suseconds_t and
gettimeofday is replaced by time.h (C99), struct timespec (C99),
usec_t (HelenOS) and getuptime / getrealtime (HelenOS).

  • Property mode set to 100644
File size: 40.6 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(ipc_call_t *icall)
80 * {
81 * if (want_refuse) {
82 * async_answer_0(icall, ELIMIT);
83 * return;
84 * }
85 *
86 * async_answer_0(icall, EOK);
87 *
88 * async_get_call(&call);
89 * somehow_handle_the_call(&call);
90 * async_answer_2(&call, 1, 2, 3);
91 *
92 * async_get_call(&call);
93 * ...
94 * }
95 *
96 */
97
98#define LIBC_ASYNC_C_
99#include <ipc/ipc.h>
100#include <async.h>
101#include "../private/async.h"
102#undef LIBC_ASYNC_C_
103
104#include <ipc/irq.h>
105#include <ipc/event.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 <time.h>
113#include <stdbool.h>
114#include <stdlib.h>
115#include <mem.h>
116#include <stdlib.h>
117#include <macros.h>
118#include <str_error.h>
119#include <as.h>
120#include <abi/mm/as.h>
121#include "../private/libc.h"
122#include "../private/fibril.h"
123
124#define DPRINTF(...) ((void) 0)
125
126/* Client connection data */
127typedef struct {
128 ht_link_t link;
129
130 task_id_t in_task_id;
131 int refcnt;
132 void *data;
133} client_t;
134
135/* Server connection data */
136typedef struct {
137 /** Fibril handling the connection. */
138 fid_t fid;
139
140 /** Hash table link. */
141 ht_link_t link;
142
143 /** Incoming client task ID. */
144 task_id_t in_task_id;
145
146 /** Incoming phone hash. */
147 sysarg_t in_phone_hash;
148
149 /** Link to the client tracking structure. */
150 client_t *client;
151
152 /** Channel for messages that should be delivered to this fibril. */
153 mpsc_t *msg_channel;
154
155 /** Call data of the opening call. */
156 ipc_call_t call;
157
158 /** Fibril function that will be used to handle the connection. */
159 async_port_handler_t handler;
160
161 /** Client data */
162 void *data;
163} connection_t;
164
165/* Member of notification_t::msg_list. */
166typedef struct {
167 link_t link;
168 ipc_call_t calldata;
169} notification_msg_t;
170
171/* Notification data */
172typedef struct {
173 /** notification_hash_table link */
174 ht_link_t htlink;
175
176 /** notification_queue link */
177 link_t qlink;
178
179 /** Notification method */
180 sysarg_t imethod;
181
182 /** Notification handler */
183 async_notification_handler_t handler;
184
185 /** Notification handler argument */
186 void *arg;
187
188 /** List of arrived notifications. */
189 list_t msg_list;
190} notification_t;
191
192/** Identifier of the incoming connection handled by the current fibril. */
193static fibril_local connection_t *fibril_connection;
194
195static void *default_client_data_constructor(void)
196{
197 return NULL;
198}
199
200static void default_client_data_destructor(void *data)
201{
202}
203
204static async_client_data_ctor_t async_client_data_create =
205 default_client_data_constructor;
206static async_client_data_dtor_t async_client_data_destroy =
207 default_client_data_destructor;
208
209void async_set_client_data_constructor(async_client_data_ctor_t ctor)
210{
211 assert(async_client_data_create == default_client_data_constructor);
212 async_client_data_create = ctor;
213}
214
215void async_set_client_data_destructor(async_client_data_dtor_t dtor)
216{
217 assert(async_client_data_destroy == default_client_data_destructor);
218 async_client_data_destroy = dtor;
219}
220
221static FIBRIL_RMUTEX_INITIALIZE(client_mutex);
222static hash_table_t client_hash_table;
223
224// TODO: lockfree notification_queue?
225static FIBRIL_RMUTEX_INITIALIZE(notification_mutex);
226static hash_table_t notification_hash_table;
227static LIST_INITIALIZE(notification_queue);
228static FIBRIL_SEMAPHORE_INITIALIZE(notification_semaphore, 0);
229
230static LIST_INITIALIZE(notification_freelist);
231static long notification_freelist_total = 0;
232static long notification_freelist_used = 0;
233
234static sysarg_t notification_avail = 0;
235
236static FIBRIL_RMUTEX_INITIALIZE(conn_mutex);
237static hash_table_t conn_hash_table;
238
239static size_t client_key_hash(void *key)
240{
241 task_id_t in_task_id = *(task_id_t *) key;
242 return in_task_id;
243}
244
245static size_t client_hash(const ht_link_t *item)
246{
247 client_t *client = hash_table_get_inst(item, client_t, link);
248 return client_key_hash(&client->in_task_id);
249}
250
251static bool client_key_equal(void *key, const ht_link_t *item)
252{
253 task_id_t in_task_id = *(task_id_t *) key;
254 client_t *client = hash_table_get_inst(item, client_t, link);
255 return in_task_id == client->in_task_id;
256}
257
258/** Operations for the client hash table. */
259static hash_table_ops_t client_hash_table_ops = {
260 .hash = client_hash,
261 .key_hash = client_key_hash,
262 .key_equal = client_key_equal,
263 .equal = NULL,
264 .remove_callback = NULL
265};
266
267typedef struct {
268 task_id_t task_id;
269 sysarg_t phone_hash;
270} conn_key_t;
271
272/** Compute hash into the connection hash table
273 *
274 * The hash is based on the source task ID and the source phone hash. The task
275 * ID is included in the hash because a phone hash alone might not be unique
276 * while we still track connections for killed tasks due to kernel's recycling
277 * of phone structures.
278 *
279 * @param key Pointer to the connection key structure.
280 *
281 * @return Index into the connection hash table.
282 *
283 */
284static size_t conn_key_hash(void *key)
285{
286 conn_key_t *ck = (conn_key_t *) key;
287
288 size_t hash = 0;
289 hash = hash_combine(hash, LOWER32(ck->task_id));
290 hash = hash_combine(hash, UPPER32(ck->task_id));
291 hash = hash_combine(hash, ck->phone_hash);
292 return hash;
293}
294
295static size_t conn_hash(const ht_link_t *item)
296{
297 connection_t *conn = hash_table_get_inst(item, connection_t, link);
298 return conn_key_hash(&(conn_key_t){
299 .task_id = conn->in_task_id,
300 .phone_hash = conn->in_phone_hash
301 });
302}
303
304static bool conn_key_equal(void *key, const ht_link_t *item)
305{
306 conn_key_t *ck = (conn_key_t *) key;
307 connection_t *conn = hash_table_get_inst(item, connection_t, link);
308 return ((ck->task_id == conn->in_task_id) &&
309 (ck->phone_hash == conn->in_phone_hash));
310}
311
312/** Operations for the connection hash table. */
313static hash_table_ops_t conn_hash_table_ops = {
314 .hash = conn_hash,
315 .key_hash = conn_key_hash,
316 .key_equal = conn_key_equal,
317 .equal = NULL,
318 .remove_callback = NULL
319};
320
321static client_t *async_client_get(task_id_t client_id, bool create)
322{
323 client_t *client = NULL;
324
325 fibril_rmutex_lock(&client_mutex);
326 ht_link_t *link = hash_table_find(&client_hash_table, &client_id);
327 if (link) {
328 client = hash_table_get_inst(link, client_t, link);
329 client->refcnt++;
330 } else if (create) {
331 // TODO: move the malloc out of critical section
332 /* malloc() is rmutex safe. */
333 client = malloc(sizeof(client_t));
334 if (client) {
335 client->in_task_id = client_id;
336 client->data = async_client_data_create();
337
338 client->refcnt = 1;
339 hash_table_insert(&client_hash_table, &client->link);
340 }
341 }
342
343 fibril_rmutex_unlock(&client_mutex);
344 return client;
345}
346
347static void async_client_put(client_t *client)
348{
349 bool destroy;
350
351 fibril_rmutex_lock(&client_mutex);
352
353 if (--client->refcnt == 0) {
354 hash_table_remove(&client_hash_table, &client->in_task_id);
355 destroy = true;
356 } else
357 destroy = false;
358
359 fibril_rmutex_unlock(&client_mutex);
360
361 if (destroy) {
362 if (client->data)
363 async_client_data_destroy(client->data);
364
365 free(client);
366 }
367}
368
369/** Wrapper for client connection fibril.
370 *
371 * When a new connection arrives, a fibril with this implementing
372 * function is created.
373 *
374 * @param arg Connection structure pointer.
375 *
376 * @return Always zero.
377 *
378 */
379static errno_t connection_fibril(void *arg)
380{
381 assert(arg);
382
383 /*
384 * Setup fibril-local connection pointer.
385 */
386 fibril_connection = (connection_t *) arg;
387
388 /*
389 * Add our reference for the current connection in the client task
390 * tracking structure. If this is the first reference, create and
391 * hash in a new tracking structure.
392 */
393
394 client_t *client = async_client_get(fibril_connection->in_task_id, true);
395 if (!client) {
396 ipc_answer_0(fibril_connection->call.cap_handle, ENOMEM);
397 return 0;
398 }
399
400 fibril_connection->client = client;
401
402 /*
403 * Call the connection handler function.
404 */
405 fibril_connection->handler(&fibril_connection->call,
406 fibril_connection->data);
407
408 /*
409 * Remove the reference for this client task connection.
410 */
411 async_client_put(client);
412
413 fibril_rmutex_lock(&conn_mutex);
414
415 /*
416 * Remove myself from the connection hash table.
417 */
418 hash_table_remove(&conn_hash_table, &(conn_key_t){
419 .task_id = fibril_connection->in_task_id,
420 .phone_hash = fibril_connection->in_phone_hash
421 });
422
423 /*
424 * Close the channel, if it isn't closed already.
425 */
426 mpsc_t *c = fibril_connection->msg_channel;
427 mpsc_close(c);
428
429 fibril_rmutex_unlock(&conn_mutex);
430
431 /*
432 * Answer all remaining messages with EHANGUP.
433 */
434 ipc_call_t call;
435 while (mpsc_receive(c, &call, NULL) == EOK)
436 ipc_answer_0(call.cap_handle, EHANGUP);
437
438 /*
439 * Clean up memory.
440 */
441 mpsc_destroy(c);
442 free(fibril_connection);
443 return EOK;
444}
445
446/** Create a new fibril for a new connection.
447 *
448 * Create new fibril for connection, fill in connection structures and insert it
449 * into the hash table, so that later we can easily do routing of messages to
450 * particular fibrils.
451 *
452 * @param in_task_id Identification of the incoming connection.
453 * @param in_phone_hash Identification of the incoming connection.
454 * @param call Call data of the opening call. If call is NULL,
455 * the connection was opened by accepting the
456 * IPC_M_CONNECT_TO_ME call and this function is
457 * called directly by the server.
458 * @param handler Connection handler.
459 * @param data Client argument to pass to the connection handler.
460 *
461 * @return New fibril id or NULL on failure.
462 *
463 */
464static fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
465 ipc_call_t *call, async_port_handler_t handler, void *data)
466{
467 connection_t *conn = malloc(sizeof(*conn));
468 if (!conn) {
469 if (call)
470 ipc_answer_0(call->cap_handle, ENOMEM);
471
472 return (fid_t) NULL;
473 }
474
475 conn->in_task_id = in_task_id;
476 conn->in_phone_hash = in_phone_hash;
477 conn->msg_channel = mpsc_create(sizeof(ipc_call_t));
478 conn->handler = handler;
479 conn->data = data;
480
481 if (call)
482 conn->call = *call;
483 else
484 conn->call.cap_handle = CAP_NIL;
485
486 /* We will activate the fibril ASAP */
487 conn->fid = fibril_create(connection_fibril, conn);
488
489 if (conn->fid == 0) {
490 mpsc_destroy(conn->msg_channel);
491 free(conn);
492
493 if (call)
494 ipc_answer_0(call->cap_handle, ENOMEM);
495
496 return (fid_t) NULL;
497 }
498
499 /* Add connection to the connection hash table */
500
501 fibril_rmutex_lock(&conn_mutex);
502 hash_table_insert(&conn_hash_table, &conn->link);
503 fibril_rmutex_unlock(&conn_mutex);
504
505 fibril_start(conn->fid);
506
507 return conn->fid;
508}
509
510/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
511 *
512 * Ask through phone for a new connection to some service.
513 *
514 * @param exch Exchange for sending the message.
515 * @param iface Callback interface.
516 * @param arg1 User defined argument.
517 * @param arg2 User defined argument.
518 * @param handler Callback handler.
519 * @param data Handler data.
520 * @param port_id ID of the newly created port.
521 *
522 * @return Zero on success or an error code.
523 *
524 */
525errno_t async_create_callback_port(async_exch_t *exch, iface_t iface, sysarg_t arg1,
526 sysarg_t arg2, async_port_handler_t handler, void *data, port_id_t *port_id)
527{
528 if ((iface & IFACE_MOD_CALLBACK) != IFACE_MOD_CALLBACK)
529 return EINVAL;
530
531 if (exch == NULL)
532 return ENOENT;
533
534 ipc_call_t answer;
535 aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, iface, arg1, arg2,
536 &answer);
537
538 errno_t rc;
539 async_wait_for(req, &rc);
540 if (rc != EOK)
541 return rc;
542
543 rc = async_create_port_internal(iface, handler, data, port_id);
544 if (rc != EOK)
545 return rc;
546
547 sysarg_t phone_hash = IPC_GET_ARG5(answer);
548 fid_t fid = async_new_connection(answer.in_task_id, phone_hash,
549 NULL, handler, data);
550 if (fid == (fid_t) NULL)
551 return ENOMEM;
552
553 return EOK;
554}
555
556static size_t notification_key_hash(void *key)
557{
558 sysarg_t id = *(sysarg_t *) key;
559 return id;
560}
561
562static size_t notification_hash(const ht_link_t *item)
563{
564 notification_t *notification =
565 hash_table_get_inst(item, notification_t, htlink);
566 return notification_key_hash(&notification->imethod);
567}
568
569static bool notification_key_equal(void *key, const ht_link_t *item)
570{
571 sysarg_t id = *(sysarg_t *) key;
572 notification_t *notification =
573 hash_table_get_inst(item, notification_t, htlink);
574 return id == notification->imethod;
575}
576
577/** Operations for the notification hash table. */
578static hash_table_ops_t notification_hash_table_ops = {
579 .hash = notification_hash,
580 .key_hash = notification_key_hash,
581 .key_equal = notification_key_equal,
582 .equal = NULL,
583 .remove_callback = NULL
584};
585
586/** Try to route a call to an appropriate connection fibril.
587 *
588 * If the proper connection fibril is found, a message with the call is added to
589 * its message queue. If the fibril was not active, it is activated and all
590 * timeouts are unregistered.
591 *
592 * @param call Data of the incoming call.
593 *
594 * @return EOK if the call was successfully passed to the respective fibril.
595 * @return ENOENT if the call doesn't match any connection.
596 * @return Other error code if routing failed for other reasons.
597 *
598 */
599static errno_t route_call(ipc_call_t *call)
600{
601 assert(call);
602
603 fibril_rmutex_lock(&conn_mutex);
604
605 ht_link_t *link = hash_table_find(&conn_hash_table, &(conn_key_t){
606 .task_id = call->in_task_id,
607 .phone_hash = call->in_phone_hash
608 });
609 if (!link) {
610 fibril_rmutex_unlock(&conn_mutex);
611 return ENOENT;
612 }
613
614 connection_t *conn = hash_table_get_inst(link, connection_t, link);
615
616 errno_t rc = mpsc_send(conn->msg_channel, call);
617
618 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP) {
619 /* Close the channel, but let the connection fibril answer. */
620 mpsc_close(conn->msg_channel);
621 // FIXME: Ideally, we should be able to discard/answer the
622 // hungup message here and just close the channel without
623 // passing it out. Unfortunatelly, somehow that breaks
624 // handling of CPU exceptions.
625 }
626
627 fibril_rmutex_unlock(&conn_mutex);
628 return rc;
629}
630
631/** Function implementing the notification handler fibril. Never returns. */
632static errno_t notification_fibril_func(void *arg)
633{
634 (void) arg;
635
636 while (true) {
637 fibril_semaphore_down(&notification_semaphore);
638
639 fibril_rmutex_lock(&notification_mutex);
640
641 /*
642 * The semaphore ensures that if we get this far,
643 * the queue must be non-empty.
644 */
645 assert(!list_empty(&notification_queue));
646
647 notification_t *notification = list_get_instance(
648 list_first(&notification_queue), notification_t, qlink);
649
650 async_notification_handler_t handler = notification->handler;
651 void *arg = notification->arg;
652
653 notification_msg_t *m = list_pop(&notification->msg_list,
654 notification_msg_t, link);
655 assert(m);
656 ipc_call_t calldata = m->calldata;
657
658 notification_freelist_used--;
659
660 if (notification_freelist_total > 64 &&
661 notification_freelist_total > 2 * notification_freelist_used) {
662 /* Going to free the structure if we have too much. */
663 notification_freelist_total--;
664 } else {
665 /* Otherwise add to freelist. */
666 list_append(&m->link, &notification_freelist);
667 m = NULL;
668 }
669
670 if (list_empty(&notification->msg_list))
671 list_remove(&notification->qlink);
672
673 fibril_rmutex_unlock(&notification_mutex);
674
675 if (handler)
676 handler(&calldata, arg);
677
678 free(m);
679 }
680
681 /* Not reached. */
682 return EOK;
683}
684
685/**
686 * Creates a new dedicated fibril for handling notifications.
687 * By default, there is one such fibril. This function can be used to
688 * create more in order to increase the number of notification that can
689 * be processed concurrently.
690 *
691 * Currently, there is no way to destroy those fibrils after they are created.
692 */
693errno_t async_spawn_notification_handler(void)
694{
695 fid_t f = fibril_create(notification_fibril_func, NULL);
696 if (f == 0)
697 return ENOMEM;
698
699 fibril_add_ready(f);
700 return EOK;
701}
702
703/** Queue notification.
704 *
705 * @param call Data of the incoming call.
706 *
707 */
708static void queue_notification(ipc_call_t *call)
709{
710 assert(call);
711
712 fibril_rmutex_lock(&notification_mutex);
713
714 notification_msg_t *m = list_pop(&notification_freelist,
715 notification_msg_t, link);
716
717 if (!m) {
718 fibril_rmutex_unlock(&notification_mutex);
719 m = malloc(sizeof(notification_msg_t));
720 if (!m) {
721 DPRINTF("Out of memory.\n");
722 abort();
723 }
724
725 fibril_rmutex_lock(&notification_mutex);
726 notification_freelist_total++;
727 }
728
729 ht_link_t *link = hash_table_find(&notification_hash_table,
730 &IPC_GET_IMETHOD(*call));
731 if (!link) {
732 /* Invalid notification. */
733 // TODO: Make sure this can't happen and turn it into assert.
734 notification_freelist_total--;
735 fibril_rmutex_unlock(&notification_mutex);
736 free(m);
737 return;
738 }
739
740 notification_t *notification =
741 hash_table_get_inst(link, notification_t, htlink);
742
743 notification_freelist_used++;
744 m->calldata = *call;
745 list_append(&m->link, &notification->msg_list);
746
747 if (!link_in_use(&notification->qlink))
748 list_append(&notification->qlink, &notification_queue);
749
750 fibril_rmutex_unlock(&notification_mutex);
751
752 fibril_semaphore_up(&notification_semaphore);
753}
754
755/**
756 * Creates a new notification structure and inserts it into the hash table.
757 *
758 * @param handler Function to call when notification is received.
759 * @param arg Argument for the handler function.
760 * @return The newly created notification structure.
761 */
762static notification_t *notification_create(async_notification_handler_t handler, void *arg)
763{
764 notification_t *notification = calloc(1, sizeof(notification_t));
765 if (!notification)
766 return NULL;
767
768 notification->handler = handler;
769 notification->arg = arg;
770
771 list_initialize(&notification->msg_list);
772
773 fid_t fib = 0;
774
775 fibril_rmutex_lock(&notification_mutex);
776
777 if (notification_avail == 0) {
778 /* Attempt to create the first handler fibril. */
779 fib = fibril_create(notification_fibril_func, NULL);
780 if (fib == 0) {
781 fibril_rmutex_unlock(&notification_mutex);
782 free(notification);
783 return NULL;
784 }
785 }
786
787 sysarg_t imethod = notification_avail;
788 notification_avail++;
789
790 notification->imethod = imethod;
791 hash_table_insert(&notification_hash_table, &notification->htlink);
792
793 fibril_rmutex_unlock(&notification_mutex);
794
795 if (imethod == 0) {
796 assert(fib);
797 fibril_add_ready(fib);
798 }
799
800 return notification;
801}
802
803/** Subscribe to IRQ notification.
804 *
805 * @param inr IRQ number.
806 * @param handler Notification handler.
807 * @param data Notification handler client data.
808 * @param ucode Top-half pseudocode handler.
809 *
810 * @param[out] handle IRQ capability handle on success.
811 *
812 * @return An error code.
813 *
814 */
815errno_t async_irq_subscribe(int inr, async_notification_handler_t handler,
816 void *data, const irq_code_t *ucode, cap_irq_handle_t *handle)
817{
818 notification_t *notification = notification_create(handler, data);
819 if (!notification)
820 return ENOMEM;
821
822 cap_irq_handle_t ihandle;
823 errno_t rc = ipc_irq_subscribe(inr, notification->imethod, ucode,
824 &ihandle);
825 if (rc == EOK && handle != NULL) {
826 *handle = ihandle;
827 }
828 return rc;
829}
830
831/** Unsubscribe from IRQ notification.
832 *
833 * @param handle IRQ capability handle.
834 *
835 * @return Zero on success or an error code.
836 *
837 */
838errno_t async_irq_unsubscribe(cap_irq_handle_t ihandle)
839{
840 // TODO: Remove entry from hash table
841 // to avoid memory leak
842
843 return ipc_irq_unsubscribe(ihandle);
844}
845
846/** Subscribe to event notifications.
847 *
848 * @param evno Event type to subscribe.
849 * @param handler Notification handler.
850 * @param data Notification handler client data.
851 *
852 * @return Zero on success or an error code.
853 *
854 */
855errno_t async_event_subscribe(event_type_t evno,
856 async_notification_handler_t handler, void *data)
857{
858 notification_t *notification = notification_create(handler, data);
859 if (!notification)
860 return ENOMEM;
861
862 return ipc_event_subscribe(evno, notification->imethod);
863}
864
865/** Subscribe to task event notifications.
866 *
867 * @param evno Event type to subscribe.
868 * @param handler Notification handler.
869 * @param data Notification handler client data.
870 *
871 * @return Zero on success or an error code.
872 *
873 */
874errno_t async_event_task_subscribe(event_task_type_t evno,
875 async_notification_handler_t handler, void *data)
876{
877 notification_t *notification = notification_create(handler, data);
878 if (!notification)
879 return ENOMEM;
880
881 return ipc_event_task_subscribe(evno, notification->imethod);
882}
883
884/** Unmask event notifications.
885 *
886 * @param evno Event type to unmask.
887 *
888 * @return Value returned by the kernel.
889 *
890 */
891errno_t async_event_unmask(event_type_t evno)
892{
893 return ipc_event_unmask(evno);
894}
895
896/** Unmask task event notifications.
897 *
898 * @param evno Event type to unmask.
899 *
900 * @return Value returned by the kernel.
901 *
902 */
903errno_t async_event_task_unmask(event_task_type_t evno)
904{
905 return ipc_event_task_unmask(evno);
906}
907
908/** Return new incoming message for the current (fibril-local) connection.
909 *
910 * @param call Storage where the incoming call data will be stored.
911 * @param usecs Timeout in microseconds. Zero denotes no timeout.
912 *
913 * @return If no timeout was specified, then true is returned.
914 * @return If a timeout is specified, then true is returned unless
915 * the timeout expires prior to receiving a message.
916 *
917 */
918bool async_get_call_timeout(ipc_call_t *call, usec_t usecs)
919{
920 assert(call);
921 assert(fibril_connection);
922
923 struct timespec ts;
924 struct timespec *expires = NULL;
925 if (usecs) {
926 getuptime(&ts);
927 ts_add_diff(&ts, USEC2NSEC(usecs));
928 expires = &ts;
929 }
930
931 errno_t rc = mpsc_receive(fibril_connection->msg_channel,
932 call, expires);
933
934 if (rc == ETIMEOUT)
935 return false;
936
937 if (rc != EOK) {
938 /*
939 * The async_get_call_timeout() interface doesn't support
940 * propagating errors. Return a null call instead.
941 */
942
943 memset(call, 0, sizeof(ipc_call_t));
944 }
945
946 return true;
947}
948
949void *async_get_client_data(void)
950{
951 assert(fibril_connection);
952 return fibril_connection->client->data;
953}
954
955void *async_get_client_data_by_id(task_id_t client_id)
956{
957 client_t *client = async_client_get(client_id, false);
958 if (!client)
959 return NULL;
960
961 if (!client->data) {
962 async_client_put(client);
963 return NULL;
964 }
965
966 return client->data;
967}
968
969void async_put_client_data_by_id(task_id_t client_id)
970{
971 client_t *client = async_client_get(client_id, false);
972
973 assert(client);
974 assert(client->data);
975
976 /* Drop the reference we got in async_get_client_data_by_hash(). */
977 async_client_put(client);
978
979 /* Drop our own reference we got at the beginning of this function. */
980 async_client_put(client);
981}
982
983/** Handle a call that was received.
984 *
985 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
986 * Otherwise the call is routed to its connection fibril.
987 *
988 * @param call Data of the incoming call.
989 *
990 */
991static void handle_call(ipc_call_t *call)
992{
993 assert(call);
994
995 if (call->flags & IPC_CALL_ANSWERED) {
996 /* Answer to a call made by us. */
997 async_reply_received(call);
998 return;
999 }
1000
1001 if (call->cap_handle == CAP_NIL) {
1002 if (call->flags & IPC_CALL_NOTIF) {
1003 /* Kernel notification */
1004 queue_notification(call);
1005 }
1006 return;
1007 }
1008
1009 /* New connection */
1010 if (IPC_GET_IMETHOD(*call) == IPC_M_CONNECT_ME_TO) {
1011 iface_t iface = (iface_t) IPC_GET_ARG1(*call);
1012 sysarg_t in_phone_hash = IPC_GET_ARG5(*call);
1013
1014 // TODO: Currently ignores all ports but the first one.
1015 void *data;
1016 async_port_handler_t handler =
1017 async_get_port_handler(iface, 0, &data);
1018
1019 async_new_connection(call->in_task_id, in_phone_hash, call,
1020 handler, data);
1021 return;
1022 }
1023
1024 /* Try to route the call through the connection hash table */
1025 errno_t rc = route_call(call);
1026 if (rc == EOK)
1027 return;
1028
1029 // TODO: Log the error.
1030
1031 if (call->cap_handle != CAP_NIL)
1032 /* Unknown call from unknown phone - hang it up */
1033 ipc_answer_0(call->cap_handle, EHANGUP);
1034}
1035
1036/** Endless loop dispatching incoming calls and answers.
1037 *
1038 * @return Never returns.
1039 *
1040 */
1041static errno_t async_manager_worker(void)
1042{
1043 ipc_call_t call;
1044 errno_t rc;
1045
1046 while (true) {
1047 rc = fibril_ipc_wait(&call, NULL);
1048 if (rc == EOK)
1049 handle_call(&call);
1050 }
1051
1052 return 0;
1053}
1054
1055/** Function to start async_manager as a standalone fibril.
1056 *
1057 * When more kernel threads are used, one async manager should exist per thread.
1058 *
1059 * @param arg Unused.
1060 * @return Never returns.
1061 *
1062 */
1063static errno_t async_manager_fibril(void *arg)
1064{
1065 async_manager_worker();
1066 return 0;
1067}
1068
1069/** Add one manager to manager list. */
1070fid_t async_create_manager(void)
1071{
1072 fid_t fid = fibril_create_generic(async_manager_fibril, NULL, PAGE_SIZE);
1073 fibril_start(fid);
1074 return fid;
1075}
1076
1077/** Initialize the async framework.
1078 *
1079 */
1080void __async_server_init(void)
1081{
1082 if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
1083 abort();
1084
1085 if (!hash_table_create(&conn_hash_table, 0, 0, &conn_hash_table_ops))
1086 abort();
1087
1088 if (!hash_table_create(&notification_hash_table, 0, 0,
1089 &notification_hash_table_ops))
1090 abort();
1091
1092 async_create_manager();
1093}
1094
1095errno_t async_answer_0(ipc_call_t *call, errno_t retval)
1096{
1097 return ipc_answer_0(call->cap_handle, retval);
1098}
1099
1100errno_t async_answer_1(ipc_call_t *call, errno_t retval, sysarg_t arg1)
1101{
1102 return ipc_answer_1(call->cap_handle, retval, arg1);
1103}
1104
1105errno_t async_answer_2(ipc_call_t *call, errno_t retval, sysarg_t arg1,
1106 sysarg_t arg2)
1107{
1108 return ipc_answer_2(call->cap_handle, retval, arg1, arg2);
1109}
1110
1111errno_t async_answer_3(ipc_call_t *call, errno_t retval, sysarg_t arg1,
1112 sysarg_t arg2, sysarg_t arg3)
1113{
1114 return ipc_answer_3(call->cap_handle, retval, arg1, arg2, arg3);
1115}
1116
1117errno_t async_answer_4(ipc_call_t *call, errno_t retval, sysarg_t arg1,
1118 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1119{
1120 return ipc_answer_4(call->cap_handle, retval, arg1, arg2, arg3, arg4);
1121}
1122
1123errno_t async_answer_5(ipc_call_t *call, errno_t retval, sysarg_t arg1,
1124 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1125{
1126 return ipc_answer_5(call->cap_handle, retval, arg1, arg2, arg3, arg4,
1127 arg5);
1128}
1129
1130errno_t async_forward_fast(ipc_call_t *call, async_exch_t *exch,
1131 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
1132{
1133 assert(call);
1134
1135 if (exch == NULL)
1136 return ENOENT;
1137
1138 return ipc_forward_fast(call->cap_handle, exch->phone, imethod, arg1,
1139 arg2, mode);
1140}
1141
1142errno_t async_forward_slow(ipc_call_t *call, async_exch_t *exch,
1143 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
1144 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
1145{
1146 assert(call);
1147
1148 if (exch == NULL)
1149 return ENOENT;
1150
1151 return ipc_forward_slow(call->cap_handle, exch->phone, imethod, arg1,
1152 arg2, arg3, arg4, arg5, mode);
1153}
1154
1155/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1156 *
1157 * Ask through phone for a new connection to some service.
1158 *
1159 * @param exch Exchange for sending the message.
1160 * @param iface Callback interface.
1161 * @param arg2 User defined argument.
1162 * @param arg3 User defined argument.
1163 *
1164 * @return Zero on success or an error code.
1165 *
1166 */
1167errno_t async_connect_to_me(async_exch_t *exch, iface_t iface, sysarg_t arg2,
1168 sysarg_t arg3)
1169{
1170 if (exch == NULL)
1171 return ENOENT;
1172
1173 ipc_call_t answer;
1174 aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, iface, arg2, arg3,
1175 &answer);
1176
1177 errno_t rc;
1178 async_wait_for(req, &rc);
1179 if (rc != EOK)
1180 return (errno_t) rc;
1181
1182 return EOK;
1183}
1184
1185/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
1186 *
1187 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
1188 * calls so that the user doesn't have to remember the meaning of each IPC
1189 * argument.
1190 *
1191 * So far, this wrapper is to be used from within a connection fibril.
1192 *
1193 * @param call Storage for the data of the IPC_M_SHARE_IN call.
1194 * @param size Destination address space area size.
1195 *
1196 * @return True on success, false on failure.
1197 *
1198 */
1199bool async_share_in_receive(ipc_call_t *call, size_t *size)
1200{
1201 assert(call);
1202 assert(size);
1203
1204 async_get_call(call);
1205
1206 if (IPC_GET_IMETHOD(*call) != IPC_M_SHARE_IN)
1207 return false;
1208
1209 *size = (size_t) IPC_GET_ARG1(*call);
1210 return true;
1211}
1212
1213/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
1214 *
1215 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
1216 * calls so that the user doesn't have to remember the meaning of each IPC
1217 * argument.
1218 *
1219 * @param call IPC_M_DATA_READ call to answer.
1220 * @param src Source address space base.
1221 * @param flags Flags to be used for sharing. Bits can be only cleared.
1222 *
1223 * @return Zero on success or a value from @ref errno.h on failure.
1224 *
1225 */
1226errno_t async_share_in_finalize(ipc_call_t *call, void *src, unsigned int flags)
1227{
1228 assert(call);
1229
1230 return ipc_answer_2(call->cap_handle, EOK, (sysarg_t) src, (sysarg_t) flags);
1231}
1232
1233/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
1234 *
1235 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
1236 * calls so that the user doesn't have to remember the meaning of each IPC
1237 * argument.
1238 *
1239 * So far, this wrapper is to be used from within a connection fibril.
1240 *
1241 * @param call Storage for the data of the IPC_M_SHARE_OUT call.
1242 * @param size Storage for the source address space area size.
1243 * @param flags Storage for the sharing flags.
1244 *
1245 * @return True on success, false on failure.
1246 *
1247 */
1248bool async_share_out_receive(ipc_call_t *call, size_t *size,
1249 unsigned int *flags)
1250{
1251 assert(call);
1252 assert(size);
1253 assert(flags);
1254
1255 async_get_call(call);
1256
1257 if (IPC_GET_IMETHOD(*call) != IPC_M_SHARE_OUT)
1258 return false;
1259
1260 *size = (size_t) IPC_GET_ARG2(*call);
1261 *flags = (unsigned int) IPC_GET_ARG3(*call);
1262 return true;
1263}
1264
1265/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
1266 *
1267 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
1268 * calls so that the user doesn't have to remember the meaning of each IPC
1269 * argument.
1270 *
1271 * @param call IPC_M_DATA_WRITE call to answer.
1272 * @param dst Address of the storage for the destination address space area
1273 * base address.
1274 *
1275 * @return Zero on success or a value from @ref errno.h on failure.
1276 *
1277 */
1278errno_t async_share_out_finalize(ipc_call_t *call, void **dst)
1279{
1280 assert(call);
1281
1282 return ipc_answer_2(call->cap_handle, EOK, (sysarg_t) __progsymbols.end,
1283 (sysarg_t) dst);
1284}
1285
1286/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
1287 *
1288 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
1289 * calls so that the user doesn't have to remember the meaning of each IPC
1290 * argument.
1291 *
1292 * So far, this wrapper is to be used from within a connection fibril.
1293 *
1294 * @param call Storage for the data of the IPC_M_DATA_READ.
1295 * @param size Storage for the maximum size. Can be NULL.
1296 *
1297 * @return True on success, false on failure.
1298 *
1299 */
1300bool async_data_read_receive(ipc_call_t *call, size_t *size)
1301{
1302 assert(call);
1303
1304 async_get_call(call);
1305
1306 if (IPC_GET_IMETHOD(*call) != IPC_M_DATA_READ)
1307 return false;
1308
1309 if (size)
1310 *size = (size_t) IPC_GET_ARG2(*call);
1311
1312 return true;
1313}
1314
1315/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
1316 *
1317 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
1318 * calls so that the user doesn't have to remember the meaning of each IPC
1319 * argument.
1320 *
1321 * @param call IPC_M_DATA_READ call to answer.
1322 * @param src Source address for the IPC_M_DATA_READ call.
1323 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
1324 * the maximum size announced by the sender.
1325 *
1326 * @return Zero on success or a value from @ref errno.h on failure.
1327 *
1328 */
1329errno_t async_data_read_finalize(ipc_call_t *call, const void *src, size_t size)
1330{
1331 assert(call);
1332
1333 return ipc_answer_2(call->cap_handle, EOK, (sysarg_t) src,
1334 (sysarg_t) size);
1335}
1336
1337/** Wrapper for forwarding any read request
1338 *
1339 */
1340errno_t async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
1341 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
1342 ipc_call_t *dataptr)
1343{
1344 if (exch == NULL)
1345 return ENOENT;
1346
1347 ipc_call_t call;
1348 if (!async_data_read_receive(&call, NULL)) {
1349 async_answer_0(&call, EINVAL);
1350 return EINVAL;
1351 }
1352
1353 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
1354 dataptr);
1355 if (msg == 0) {
1356 async_answer_0(&call, EINVAL);
1357 return EINVAL;
1358 }
1359
1360 errno_t retval = ipc_forward_fast(call.cap_handle, exch->phone, 0, 0, 0,
1361 IPC_FF_ROUTE_FROM_ME);
1362 if (retval != EOK) {
1363 async_forget(msg);
1364 async_answer_0(&call, retval);
1365 return retval;
1366 }
1367
1368 errno_t rc;
1369 async_wait_for(msg, &rc);
1370
1371 return (errno_t) rc;
1372}
1373
1374/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
1375 *
1376 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
1377 * calls so that the user doesn't have to remember the meaning of each IPC
1378 * argument.
1379 *
1380 * So far, this wrapper is to be used from within a connection fibril.
1381 *
1382 * @param call Storage for the data of the IPC_M_DATA_WRITE.
1383 * @param size Storage for the suggested size. May be NULL.
1384 *
1385 * @return True on success, false on failure.
1386 *
1387 */
1388bool async_data_write_receive(ipc_call_t *call, size_t *size)
1389{
1390 assert(call);
1391
1392 async_get_call(call);
1393
1394 if (IPC_GET_IMETHOD(*call) != IPC_M_DATA_WRITE)
1395 return false;
1396
1397 if (size)
1398 *size = (size_t) IPC_GET_ARG2(*call);
1399
1400 return true;
1401}
1402
1403/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
1404 *
1405 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
1406 * calls so that the user doesn't have to remember the meaning of each IPC
1407 * argument.
1408 *
1409 * @param call IPC_M_DATA_WRITE call to answer.
1410 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
1411 * @param size Final size for the IPC_M_DATA_WRITE call.
1412 *
1413 * @return Zero on success or a value from @ref errno.h on failure.
1414 *
1415 */
1416errno_t async_data_write_finalize(ipc_call_t *call, void *dst, size_t size)
1417{
1418 assert(call);
1419
1420 return async_answer_2(call, EOK, (sysarg_t) dst, (sysarg_t) size);
1421}
1422
1423/** Wrapper for receiving binary data or strings
1424 *
1425 * This wrapper only makes it more comfortable to use async_data_write_*
1426 * functions to receive binary data or strings.
1427 *
1428 * @param data Pointer to data pointer (which should be later disposed
1429 * by free()). If the operation fails, the pointer is not
1430 * touched.
1431 * @param nullterm If true then the received data is always zero terminated.
1432 * This also causes to allocate one extra byte beyond the
1433 * raw transmitted data.
1434 * @param min_size Minimum size (in bytes) of the data to receive.
1435 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
1436 * no limit.
1437 * @param granulariy If non-zero then the size of the received data has to
1438 * be divisible by this value.
1439 * @param received If not NULL, the size of the received data is stored here.
1440 *
1441 * @return Zero on success or a value from @ref errno.h on failure.
1442 *
1443 */
1444errno_t async_data_write_accept(void **data, const bool nullterm,
1445 const size_t min_size, const size_t max_size, const size_t granularity,
1446 size_t *received)
1447{
1448 assert(data);
1449
1450 ipc_call_t call;
1451 size_t size;
1452 if (!async_data_write_receive(&call, &size)) {
1453 async_answer_0(&call, EINVAL);
1454 return EINVAL;
1455 }
1456
1457 if (size < min_size) {
1458 async_answer_0(&call, EINVAL);
1459 return EINVAL;
1460 }
1461
1462 if ((max_size > 0) && (size > max_size)) {
1463 async_answer_0(&call, EINVAL);
1464 return EINVAL;
1465 }
1466
1467 if ((granularity > 0) && ((size % granularity) != 0)) {
1468 async_answer_0(&call, EINVAL);
1469 return EINVAL;
1470 }
1471
1472 void *arg_data;
1473
1474 if (nullterm)
1475 arg_data = malloc(size + 1);
1476 else
1477 arg_data = malloc(size);
1478
1479 if (arg_data == NULL) {
1480 async_answer_0(&call, ENOMEM);
1481 return ENOMEM;
1482 }
1483
1484 errno_t rc = async_data_write_finalize(&call, arg_data, size);
1485 if (rc != EOK) {
1486 free(arg_data);
1487 return rc;
1488 }
1489
1490 if (nullterm)
1491 ((char *) arg_data)[size] = 0;
1492
1493 *data = arg_data;
1494 if (received != NULL)
1495 *received = size;
1496
1497 return EOK;
1498}
1499
1500/** Wrapper for voiding any data that is about to be received
1501 *
1502 * This wrapper can be used to void any pending data
1503 *
1504 * @param retval Error value from @ref errno.h to be returned to the caller.
1505 *
1506 */
1507void async_data_write_void(errno_t retval)
1508{
1509 ipc_call_t call;
1510 async_data_write_receive(&call, NULL);
1511 async_answer_0(&call, retval);
1512}
1513
1514/** Wrapper for forwarding any data that is about to be received
1515 *
1516 */
1517errno_t async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
1518 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
1519 ipc_call_t *dataptr)
1520{
1521 if (exch == NULL)
1522 return ENOENT;
1523
1524 ipc_call_t call;
1525 if (!async_data_write_receive(&call, NULL)) {
1526 async_answer_0(&call, EINVAL);
1527 return EINVAL;
1528 }
1529
1530 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
1531 dataptr);
1532 if (msg == 0) {
1533 async_answer_0(&call, EINVAL);
1534 return EINVAL;
1535 }
1536
1537 errno_t retval = ipc_forward_fast(call.cap_handle, exch->phone, 0, 0, 0,
1538 IPC_FF_ROUTE_FROM_ME);
1539 if (retval != EOK) {
1540 async_forget(msg);
1541 async_answer_0(&call, retval);
1542 return retval;
1543 }
1544
1545 errno_t rc;
1546 async_wait_for(msg, &rc);
1547
1548 return (errno_t) rc;
1549}
1550
1551/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
1552 *
1553 * If the current call is IPC_M_CONNECT_TO_ME then a new
1554 * async session is created for the accepted phone.
1555 *
1556 * @param mgmt Exchange management style.
1557 *
1558 * @return New async session.
1559 * @return NULL on failure.
1560 *
1561 */
1562async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
1563{
1564 /* Accept the phone */
1565 ipc_call_t call;
1566 async_get_call(&call);
1567
1568 cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(call);
1569
1570 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
1571 !CAP_HANDLE_VALID((phandle))) {
1572 async_answer_0(&call, EINVAL);
1573 return NULL;
1574 }
1575
1576 async_sess_t *sess = calloc(1, sizeof(async_sess_t));
1577 if (sess == NULL) {
1578 async_answer_0(&call, ENOMEM);
1579 return NULL;
1580 }
1581
1582 sess->iface = 0;
1583 sess->mgmt = mgmt;
1584 sess->phone = phandle;
1585
1586 fibril_mutex_initialize(&sess->remote_state_mtx);
1587 list_initialize(&sess->exch_list);
1588 fibril_mutex_initialize(&sess->mutex);
1589
1590 /* Acknowledge the connected phone */
1591 async_answer_0(&call, EOK);
1592
1593 return sess;
1594}
1595
1596/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
1597 *
1598 * If the call is IPC_M_CONNECT_TO_ME then a new
1599 * async session is created. However, the phone is
1600 * not accepted automatically.
1601 *
1602 * @param mgmt Exchange management style.
1603 * @param call Call data.
1604 *
1605 * @return New async session.
1606 * @return NULL on failure.
1607 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
1608 *
1609 */
1610async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
1611{
1612 cap_phone_handle_t phandle = (cap_handle_t) IPC_GET_ARG5(*call);
1613
1614 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
1615 !CAP_HANDLE_VALID((phandle)))
1616 return NULL;
1617
1618 async_sess_t *sess = calloc(1, sizeof(async_sess_t));
1619 if (sess == NULL)
1620 return NULL;
1621
1622 sess->iface = 0;
1623 sess->mgmt = mgmt;
1624 sess->phone = phandle;
1625
1626 fibril_mutex_initialize(&sess->remote_state_mtx);
1627 list_initialize(&sess->exch_list);
1628 fibril_mutex_initialize(&sess->mutex);
1629
1630 return sess;
1631}
1632
1633bool async_state_change_receive(ipc_call_t *call)
1634{
1635 assert(call);
1636
1637 async_get_call(call);
1638
1639 if (IPC_GET_IMETHOD(*call) != IPC_M_STATE_CHANGE_AUTHORIZE)
1640 return false;
1641
1642 return true;
1643}
1644
1645errno_t async_state_change_finalize(ipc_call_t *call, async_exch_t *other_exch)
1646{
1647 assert(call);
1648
1649 return async_answer_1(call, EOK, CAP_HANDLE_RAW(other_exch->phone));
1650}
1651
1652__noreturn void async_manager(void)
1653{
1654 fibril_event_t ever = FIBRIL_EVENT_INIT;
1655 fibril_wait_for(&ever);
1656 __builtin_unreachable();
1657}
1658
1659/** @}
1660 */
Note: See TracBrowser for help on using the repository browser.