source: mainline/uspace/lib/c/generic/async.c@ 6d8455d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6d8455d was fbcdeb8, checked in by Martin Decky <martin@…>, 14 years ago

Remove the two-phase way of creating virtual memory areas (first asking for a mappable address and then mapping it) which was prone to race conditions when two or more calls to as_get_mappable_page() and as_area_create() were interleaved. This for example caused the e1k driver to randomly fail.

The memory area related syscalls and IPC calls have all been altered to accept a special value (void *) -1, representing a demand to atomically search for a mappable address space "hole" and map to it.

Individual changes:

  • IPC_M_SHARE_OUT: the destination address space area is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the callee via a pointer in an IPC reply argument)

  • IPC_M_SHARE_IN: the destination address space ares is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the caller as usual via an IPC argument)

  • SYS_AS_GET_UNMAPPED_AREA was removed
  • dummy implementations of SYS_PHYSMEM_UNMAP and SYS_IOSPACE_DISABLE were added for the sake of symmetry (they do nothing yet)
  • SYS_PHYSMEM_MAP and SYS_DMAMEM_MAP were altered to accept (void *) -1 as address space area base and a lower bound
  • kernel as_area_create() and as_area_share() were altered to accept (void *) -1 as address space area base and a lower bound
  • uspace libraries and programs were altered to reflect the new API
  • Property mode set to 100644
File size: 65.0 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 * my_client_connection(icallid, *icall)
80 * {
81 * if (want_refuse) {
82 * async_answer_0(icallid, ELIMIT);
83 * return;
84 * }
85 * async_answer_0(icallid, EOK);
86 *
87 * callid = async_get_call(&call);
88 * somehow_handle_the_call(callid, call);
89 * async_answer_2(callid, 1, 2, 3);
90 *
91 * callid = 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 <futex.h>
104#include <fibril.h>
105#include <adt/hash_table.h>
106#include <adt/list.h>
107#include <assert.h>
108#include <errno.h>
109#include <sys/time.h>
110#include <libarch/barrier.h>
111#include <bool.h>
112#include <malloc.h>
113#include <mem.h>
114#include <stdlib.h>
115#include <macros.h>
116
117#define CLIENT_HASH_TABLE_BUCKETS 32
118#define CONN_HASH_TABLE_BUCKETS 32
119
120/** Session data */
121struct async_sess {
122 /** List of inactive exchanges */
123 list_t exch_list;
124
125 /** Exchange management style */
126 exch_mgmt_t mgmt;
127
128 /** Session identification */
129 int phone;
130
131 /** First clone connection argument */
132 sysarg_t arg1;
133
134 /** Second clone connection argument */
135 sysarg_t arg2;
136
137 /** Third clone connection argument */
138 sysarg_t arg3;
139
140 /** Exchange mutex */
141 fibril_mutex_t mutex;
142
143 /** Number of opened exchanges */
144 atomic_t refcnt;
145
146 /** Mutex for stateful connections */
147 fibril_mutex_t remote_state_mtx;
148
149 /** Data for stateful connections */
150 void *remote_state_data;
151};
152
153/** Exchange data */
154struct async_exch {
155 /** Link into list of inactive exchanges */
156 link_t sess_link;
157
158 /** Link into global list of inactive exchanges */
159 link_t global_link;
160
161 /** Session pointer */
162 async_sess_t *sess;
163
164 /** Exchange identification */
165 int phone;
166};
167
168/** Async framework global futex */
169atomic_t async_futex = FUTEX_INITIALIZER;
170
171/** Number of threads waiting for IPC in the kernel. */
172atomic_t threads_in_ipc_wait = { 0 };
173
174/** Naming service session */
175async_sess_t *session_ns;
176
177/** Call data */
178typedef struct {
179 link_t link;
180
181 ipc_callid_t callid;
182 ipc_call_t call;
183} msg_t;
184
185/** Message data */
186typedef struct {
187 awaiter_t wdata;
188
189 /** If reply was received. */
190 bool done;
191
192 /** Pointer to where the answer data is stored. */
193 ipc_call_t *dataptr;
194
195 sysarg_t retval;
196} amsg_t;
197
198/* Client connection data */
199typedef struct {
200 link_t link;
201
202 task_id_t in_task_id;
203 atomic_t refcnt;
204 void *data;
205} client_t;
206
207/* Server connection data */
208typedef struct {
209 awaiter_t wdata;
210
211 /** Hash table link. */
212 link_t link;
213
214 /** Incoming client task ID. */
215 task_id_t in_task_id;
216
217 /** Incoming phone hash. */
218 sysarg_t in_phone_hash;
219
220 /** Link to the client tracking structure. */
221 client_t *client;
222
223 /** Messages that should be delivered to this fibril. */
224 list_t msg_queue;
225
226 /** Identification of the opening call. */
227 ipc_callid_t callid;
228 /** Call data of the opening call. */
229 ipc_call_t call;
230 /** Local argument or NULL if none. */
231 void *carg;
232
233 /** Identification of the closing call. */
234 ipc_callid_t close_callid;
235
236 /** Fibril function that will be used to handle the connection. */
237 async_client_conn_t cfibril;
238} connection_t;
239
240/** Identifier of the incoming connection handled by the current fibril. */
241static fibril_local connection_t *fibril_connection;
242
243static void *default_client_data_constructor(void)
244{
245 return NULL;
246}
247
248static void default_client_data_destructor(void *data)
249{
250}
251
252static async_client_data_ctor_t async_client_data_create =
253 default_client_data_constructor;
254static async_client_data_dtor_t async_client_data_destroy =
255 default_client_data_destructor;
256
257void async_set_client_data_constructor(async_client_data_ctor_t ctor)
258{
259 async_client_data_create = ctor;
260}
261
262void async_set_client_data_destructor(async_client_data_dtor_t dtor)
263{
264 async_client_data_destroy = dtor;
265}
266
267/** Default fibril function that gets called to handle new connection.
268 *
269 * This function is defined as a weak symbol - to be redefined in user code.
270 *
271 * @param callid Hash of the incoming call.
272 * @param call Data of the incoming call.
273 * @param arg Local argument
274 *
275 */
276static void default_client_connection(ipc_callid_t callid, ipc_call_t *call,
277 void *arg)
278{
279 ipc_answer_0(callid, ENOENT);
280}
281
282/** Default fibril function that gets called to handle interrupt notifications.
283 *
284 * This function is defined as a weak symbol - to be redefined in user code.
285 *
286 * @param callid Hash of the incoming call.
287 * @param call Data of the incoming call.
288 * @param arg Local argument.
289 *
290 */
291static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
292{
293}
294
295static async_client_conn_t client_connection = default_client_connection;
296static async_interrupt_handler_t interrupt_received = default_interrupt_received;
297
298/** Setter for client_connection function pointer.
299 *
300 * @param conn Function that will implement a new connection fibril.
301 *
302 */
303void async_set_client_connection(async_client_conn_t conn)
304{
305 client_connection = conn;
306}
307
308/** Setter for interrupt_received function pointer.
309 *
310 * @param intr Function that will implement a new interrupt
311 * notification fibril.
312 */
313void async_set_interrupt_received(async_interrupt_handler_t intr)
314{
315 interrupt_received = intr;
316}
317
318/** Mutex protecting inactive_exch_list and avail_phone_cv.
319 *
320 */
321static FIBRIL_MUTEX_INITIALIZE(async_sess_mutex);
322
323/** List of all currently inactive exchanges.
324 *
325 */
326static LIST_INITIALIZE(inactive_exch_list);
327
328/** Condition variable to wait for a phone to become available.
329 *
330 */
331static FIBRIL_CONDVAR_INITIALIZE(avail_phone_cv);
332
333static hash_table_t client_hash_table;
334static hash_table_t conn_hash_table;
335static LIST_INITIALIZE(timeout_list);
336
337static hash_index_t client_hash(unsigned long key[])
338{
339 assert(key);
340
341 return (((key[0]) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
342}
343
344static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
345{
346 assert(key);
347 assert(keys == 2);
348 assert(item);
349
350 client_t *client = hash_table_get_instance(item, client_t, link);
351 return (key[0] == LOWER32(client->in_task_id) &&
352 (key[1] == UPPER32(client->in_task_id)));
353}
354
355static void client_remove(link_t *item)
356{
357}
358
359/** Operations for the client hash table. */
360static hash_table_operations_t client_hash_table_ops = {
361 .hash = client_hash,
362 .compare = client_compare,
363 .remove_callback = client_remove
364};
365
366/** Compute hash into the connection hash table based on the source phone hash.
367 *
368 * @param key Pointer to source phone hash.
369 *
370 * @return Index into the connection hash table.
371 *
372 */
373static hash_index_t conn_hash(unsigned long key[])
374{
375 assert(key);
376
377 return (((key[0]) >> 4) % CONN_HASH_TABLE_BUCKETS);
378}
379
380/** Compare hash table item with a key.
381 *
382 * @param key Array containing the source phone hash as the only item.
383 * @param keys Expected 1 but ignored.
384 * @param item Connection hash table item.
385 *
386 * @return True on match, false otherwise.
387 *
388 */
389static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
390{
391 assert(key);
392 assert(item);
393
394 connection_t *conn = hash_table_get_instance(item, connection_t, link);
395 return (key[0] == conn->in_phone_hash);
396}
397
398static void conn_remove(link_t *item)
399{
400}
401
402/** Operations for the connection hash table. */
403static hash_table_operations_t conn_hash_table_ops = {
404 .hash = conn_hash,
405 .compare = conn_compare,
406 .remove_callback = conn_remove
407};
408
409/** Sort in current fibril's timeout request.
410 *
411 * @param wd Wait data of the current fibril.
412 *
413 */
414void async_insert_timeout(awaiter_t *wd)
415{
416 assert(wd);
417
418 wd->to_event.occurred = false;
419 wd->to_event.inlist = true;
420
421 link_t *tmp = timeout_list.head.next;
422 while (tmp != &timeout_list.head) {
423 awaiter_t *cur
424 = list_get_instance(tmp, awaiter_t, to_event.link);
425
426 if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
427 break;
428
429 tmp = tmp->next;
430 }
431
432 list_insert_before(&wd->to_event.link, tmp);
433}
434
435/** Try to route a call to an appropriate connection fibril.
436 *
437 * If the proper connection fibril is found, a message with the call is added to
438 * its message queue. If the fibril was not active, it is activated and all
439 * timeouts are unregistered.
440 *
441 * @param callid Hash of the incoming call.
442 * @param call Data of the incoming call.
443 *
444 * @return False if the call doesn't match any connection.
445 * @return True if the call was passed to the respective connection fibril.
446 *
447 */
448static bool route_call(ipc_callid_t callid, ipc_call_t *call)
449{
450 assert(call);
451
452 futex_down(&async_futex);
453
454 unsigned long key = call->in_phone_hash;
455 link_t *hlp = hash_table_find(&conn_hash_table, &key);
456
457 if (!hlp) {
458 futex_up(&async_futex);
459 return false;
460 }
461
462 connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
463
464 msg_t *msg = malloc(sizeof(*msg));
465 if (!msg) {
466 futex_up(&async_futex);
467 return false;
468 }
469
470 msg->callid = callid;
471 msg->call = *call;
472 list_append(&msg->link, &conn->msg_queue);
473
474 if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
475 conn->close_callid = callid;
476
477 /* If the connection fibril is waiting for an event, activate it */
478 if (!conn->wdata.active) {
479
480 /* If in timeout list, remove it */
481 if (conn->wdata.to_event.inlist) {
482 conn->wdata.to_event.inlist = false;
483 list_remove(&conn->wdata.to_event.link);
484 }
485
486 conn->wdata.active = true;
487 fibril_add_ready(conn->wdata.fid);
488 }
489
490 futex_up(&async_futex);
491 return true;
492}
493
494/** Notification fibril.
495 *
496 * When a notification arrives, a fibril with this implementing function is
497 * created. It calls interrupt_received() and does the final cleanup.
498 *
499 * @param arg Message structure pointer.
500 *
501 * @return Always zero.
502 *
503 */
504static int notification_fibril(void *arg)
505{
506 assert(arg);
507
508 msg_t *msg = (msg_t *) arg;
509 interrupt_received(msg->callid, &msg->call);
510
511 free(msg);
512 return 0;
513}
514
515/** Process interrupt notification.
516 *
517 * A new fibril is created which would process the notification.
518 *
519 * @param callid Hash of the incoming call.
520 * @param call Data of the incoming call.
521 *
522 * @return False if an error occured.
523 * True if the call was passed to the notification fibril.
524 *
525 */
526static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
527{
528 assert(call);
529
530 futex_down(&async_futex);
531
532 msg_t *msg = malloc(sizeof(*msg));
533 if (!msg) {
534 futex_up(&async_futex);
535 return false;
536 }
537
538 msg->callid = callid;
539 msg->call = *call;
540
541 fid_t fid = fibril_create(notification_fibril, msg);
542 if (fid == 0) {
543 free(msg);
544 futex_up(&async_futex);
545 return false;
546 }
547
548 fibril_add_ready(fid);
549
550 futex_up(&async_futex);
551 return true;
552}
553
554/** Return new incoming message for the current (fibril-local) connection.
555 *
556 * @param call Storage where the incoming call data will be stored.
557 * @param usecs Timeout in microseconds. Zero denotes no timeout.
558 *
559 * @return If no timeout was specified, then a hash of the
560 * incoming call is returned. If a timeout is specified,
561 * then a hash of the incoming call is returned unless
562 * the timeout expires prior to receiving a message. In
563 * that case zero is returned.
564 *
565 */
566ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
567{
568 assert(call);
569 assert(fibril_connection);
570
571 /* Why doing this?
572 * GCC 4.1.0 coughs on fibril_connection-> dereference.
573 * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
574 * I would never expect to find so many errors in
575 * a compiler.
576 */
577 connection_t *conn = fibril_connection;
578
579 futex_down(&async_futex);
580
581 if (usecs) {
582 gettimeofday(&conn->wdata.to_event.expires, NULL);
583 tv_add(&conn->wdata.to_event.expires, usecs);
584 } else
585 conn->wdata.to_event.inlist = false;
586
587 /* If nothing in queue, wait until something arrives */
588 while (list_empty(&conn->msg_queue)) {
589 if (conn->close_callid) {
590 /*
591 * Handle the case when the connection was already
592 * closed by the client but the server did not notice
593 * the first IPC_M_PHONE_HUNGUP call and continues to
594 * call async_get_call_timeout(). Repeat
595 * IPC_M_PHONE_HUNGUP until the caller notices.
596 */
597 memset(call, 0, sizeof(ipc_call_t));
598 IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
599 futex_up(&async_futex);
600 return conn->close_callid;
601 }
602
603 if (usecs)
604 async_insert_timeout(&conn->wdata);
605
606 conn->wdata.active = false;
607
608 /*
609 * Note: the current fibril will be rescheduled either due to a
610 * timeout or due to an arriving message destined to it. In the
611 * former case, handle_expired_timeouts() and, in the latter
612 * case, route_call() will perform the wakeup.
613 */
614 fibril_switch(FIBRIL_TO_MANAGER);
615
616 /*
617 * Futex is up after getting back from async_manager.
618 * Get it again.
619 */
620 futex_down(&async_futex);
621 if ((usecs) && (conn->wdata.to_event.occurred)
622 && (list_empty(&conn->msg_queue))) {
623 /* If we timed out -> exit */
624 futex_up(&async_futex);
625 return 0;
626 }
627 }
628
629 msg_t *msg = list_get_instance(list_first(&conn->msg_queue), msg_t, link);
630 list_remove(&msg->link);
631
632 ipc_callid_t callid = msg->callid;
633 *call = msg->call;
634 free(msg);
635
636 futex_up(&async_futex);
637 return callid;
638}
639
640static client_t *async_client_get(task_id_t client_id, bool create)
641{
642 unsigned long key[2] = {
643 LOWER32(client_id),
644 UPPER32(client_id),
645 };
646 client_t *client = NULL;
647
648 futex_down(&async_futex);
649 link_t *lnk = hash_table_find(&client_hash_table, key);
650 if (lnk) {
651 client = hash_table_get_instance(lnk, client_t, link);
652 atomic_inc(&client->refcnt);
653 } else if (create) {
654 client = malloc(sizeof(client_t));
655 if (client) {
656 client->in_task_id = client_id;
657 client->data = async_client_data_create();
658
659 atomic_set(&client->refcnt, 1);
660 hash_table_insert(&client_hash_table, key, &client->link);
661 }
662 }
663
664 futex_up(&async_futex);
665 return client;
666}
667
668static void async_client_put(client_t *client)
669{
670 bool destroy;
671 unsigned long key[2] = {
672 LOWER32(client->in_task_id),
673 UPPER32(client->in_task_id)
674 };
675
676 futex_down(&async_futex);
677
678 if (atomic_predec(&client->refcnt) == 0) {
679 hash_table_remove(&client_hash_table, key, 2);
680 destroy = true;
681 } else
682 destroy = false;
683
684 futex_up(&async_futex);
685
686 if (destroy) {
687 if (client->data)
688 async_client_data_destroy(client->data);
689
690 free(client);
691 }
692}
693
694void *async_get_client_data(void)
695{
696 assert(fibril_connection);
697 return fibril_connection->client->data;
698}
699
700void *async_get_client_data_by_id(task_id_t client_id)
701{
702 client_t *client = async_client_get(client_id, false);
703 if (!client)
704 return NULL;
705 if (!client->data) {
706 async_client_put(client);
707 return NULL;
708 }
709
710 return client->data;
711}
712
713void async_put_client_data_by_id(task_id_t client_id)
714{
715 client_t *client = async_client_get(client_id, false);
716
717 assert(client);
718 assert(client->data);
719
720 /* Drop the reference we got in async_get_client_data_by_hash(). */
721 async_client_put(client);
722
723 /* Drop our own reference we got at the beginning of this function. */
724 async_client_put(client);
725}
726
727/** Wrapper for client connection fibril.
728 *
729 * When a new connection arrives, a fibril with this implementing function is
730 * created. It calls client_connection() and does the final cleanup.
731 *
732 * @param arg Connection structure pointer.
733 *
734 * @return Always zero.
735 *
736 */
737static int connection_fibril(void *arg)
738{
739 assert(arg);
740
741 /*
742 * Setup fibril-local connection pointer.
743 */
744 fibril_connection = (connection_t *) arg;
745
746 /*
747 * Add our reference for the current connection in the client task
748 * tracking structure. If this is the first reference, create and
749 * hash in a new tracking structure.
750 */
751
752 client_t *client = async_client_get(fibril_connection->in_task_id, true);
753 if (!client) {
754 ipc_answer_0(fibril_connection->callid, ENOMEM);
755 return 0;
756 }
757
758 fibril_connection->client = client;
759
760 /*
761 * Call the connection handler function.
762 */
763 fibril_connection->cfibril(fibril_connection->callid,
764 &fibril_connection->call, fibril_connection->carg);
765
766 /*
767 * Remove the reference for this client task connection.
768 */
769 async_client_put(client);
770
771 /*
772 * Remove myself from the connection hash table.
773 */
774 futex_down(&async_futex);
775 unsigned long key = fibril_connection->in_phone_hash;
776 hash_table_remove(&conn_hash_table, &key, 1);
777 futex_up(&async_futex);
778
779 /*
780 * Answer all remaining messages with EHANGUP.
781 */
782 while (!list_empty(&fibril_connection->msg_queue)) {
783 msg_t *msg =
784 list_get_instance(list_first(&fibril_connection->msg_queue),
785 msg_t, link);
786
787 list_remove(&msg->link);
788 ipc_answer_0(msg->callid, EHANGUP);
789 free(msg);
790 }
791
792 /*
793 * If the connection was hung-up, answer the last call,
794 * i.e. IPC_M_PHONE_HUNGUP.
795 */
796 if (fibril_connection->close_callid)
797 ipc_answer_0(fibril_connection->close_callid, EOK);
798
799 free(fibril_connection);
800 return 0;
801}
802
803/** Create a new fibril for a new connection.
804 *
805 * Create new fibril for connection, fill in connection structures and insert
806 * it into the hash table, so that later we can easily do routing of messages to
807 * particular fibrils.
808 *
809 * @param in_task_id Identification of the incoming connection.
810 * @param in_phone_hash Identification of the incoming connection.
811 * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
812 * If callid is zero, the connection was opened by
813 * accepting the IPC_M_CONNECT_TO_ME call and this function
814 * is called directly by the server.
815 * @param call Call data of the opening call.
816 * @param cfibril Fibril function that should be called upon opening the
817 * connection.
818 * @param carg Extra argument to pass to the connection fibril
819 *
820 * @return New fibril id or NULL on failure.
821 *
822 */
823fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
824 ipc_callid_t callid, ipc_call_t *call,
825 async_client_conn_t cfibril, void *carg)
826{
827 connection_t *conn = malloc(sizeof(*conn));
828 if (!conn) {
829 if (callid)
830 ipc_answer_0(callid, ENOMEM);
831
832 return (uintptr_t) NULL;
833 }
834
835 conn->in_task_id = in_task_id;
836 conn->in_phone_hash = in_phone_hash;
837 list_initialize(&conn->msg_queue);
838 conn->callid = callid;
839 conn->close_callid = 0;
840 conn->carg = carg;
841
842 if (call)
843 conn->call = *call;
844
845 /* We will activate the fibril ASAP */
846 conn->wdata.active = true;
847 conn->cfibril = cfibril;
848 conn->wdata.fid = fibril_create(connection_fibril, conn);
849
850 if (conn->wdata.fid == 0) {
851 free(conn);
852
853 if (callid)
854 ipc_answer_0(callid, ENOMEM);
855
856 return (uintptr_t) NULL;
857 }
858
859 /* Add connection to the connection hash table */
860 unsigned long key = conn->in_phone_hash;
861
862 futex_down(&async_futex);
863 hash_table_insert(&conn_hash_table, &key, &conn->link);
864 futex_up(&async_futex);
865
866 fibril_add_ready(conn->wdata.fid);
867
868 return conn->wdata.fid;
869}
870
871/** Handle a call that was received.
872 *
873 * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
874 * Otherwise the call is routed to its connection fibril.
875 *
876 * @param callid Hash of the incoming call.
877 * @param call Data of the incoming call.
878 *
879 */
880static void handle_call(ipc_callid_t callid, ipc_call_t *call)
881{
882 assert(call);
883
884 /* Unrouted call - take some default action */
885 if ((callid & IPC_CALLID_NOTIFICATION)) {
886 process_notification(callid, call);
887 return;
888 }
889
890 switch (IPC_GET_IMETHOD(*call)) {
891 case IPC_M_CONNECT_ME:
892 case IPC_M_CONNECT_ME_TO:
893 /* Open new connection with fibril, etc. */
894 async_new_connection(call->in_task_id, IPC_GET_ARG5(*call),
895 callid, call, client_connection, NULL);
896 return;
897 }
898
899 /* Try to route the call through the connection hash table */
900 if (route_call(callid, call))
901 return;
902
903 /* Unknown call from unknown phone - hang it up */
904 ipc_answer_0(callid, EHANGUP);
905}
906
907/** Fire all timeouts that expired. */
908static void handle_expired_timeouts(void)
909{
910 struct timeval tv;
911 gettimeofday(&tv, NULL);
912
913 futex_down(&async_futex);
914
915 link_t *cur = list_first(&timeout_list);
916 while (cur != NULL) {
917 awaiter_t *waiter =
918 list_get_instance(cur, awaiter_t, to_event.link);
919
920 if (tv_gt(&waiter->to_event.expires, &tv))
921 break;
922
923 list_remove(&waiter->to_event.link);
924 waiter->to_event.inlist = false;
925 waiter->to_event.occurred = true;
926
927 /*
928 * Redundant condition?
929 * The fibril should not be active when it gets here.
930 */
931 if (!waiter->active) {
932 waiter->active = true;
933 fibril_add_ready(waiter->fid);
934 }
935
936 cur = list_first(&timeout_list);
937 }
938
939 futex_up(&async_futex);
940}
941
942/** Endless loop dispatching incoming calls and answers.
943 *
944 * @return Never returns.
945 *
946 */
947static int async_manager_worker(void)
948{
949 while (true) {
950 if (fibril_switch(FIBRIL_FROM_MANAGER)) {
951 futex_up(&async_futex);
952 /*
953 * async_futex is always held when entering a manager
954 * fibril.
955 */
956 continue;
957 }
958
959 futex_down(&async_futex);
960
961 suseconds_t timeout;
962 if (!list_empty(&timeout_list)) {
963 awaiter_t *waiter = list_get_instance(
964 list_first(&timeout_list), awaiter_t, to_event.link);
965
966 struct timeval tv;
967 gettimeofday(&tv, NULL);
968
969 if (tv_gteq(&tv, &waiter->to_event.expires)) {
970 futex_up(&async_futex);
971 handle_expired_timeouts();
972 continue;
973 } else
974 timeout = tv_sub(&waiter->to_event.expires, &tv);
975 } else
976 timeout = SYNCH_NO_TIMEOUT;
977
978 futex_up(&async_futex);
979
980 atomic_inc(&threads_in_ipc_wait);
981
982 ipc_call_t call;
983 ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
984 SYNCH_FLAGS_NONE);
985
986 atomic_dec(&threads_in_ipc_wait);
987
988 if (!callid) {
989 handle_expired_timeouts();
990 continue;
991 }
992
993 if (callid & IPC_CALLID_ANSWERED)
994 continue;
995
996 handle_call(callid, &call);
997 }
998
999 return 0;
1000}
1001
1002/** Function to start async_manager as a standalone fibril.
1003 *
1004 * When more kernel threads are used, one async manager should exist per thread.
1005 *
1006 * @param arg Unused.
1007 * @return Never returns.
1008 *
1009 */
1010static int async_manager_fibril(void *arg)
1011{
1012 futex_up(&async_futex);
1013
1014 /*
1015 * async_futex is always locked when entering manager
1016 */
1017 async_manager_worker();
1018
1019 return 0;
1020}
1021
1022/** Add one manager to manager list. */
1023void async_create_manager(void)
1024{
1025 fid_t fid = fibril_create(async_manager_fibril, NULL);
1026 if (fid != 0)
1027 fibril_add_manager(fid);
1028}
1029
1030/** Remove one manager from manager list */
1031void async_destroy_manager(void)
1032{
1033 fibril_remove_manager();
1034}
1035
1036/** Initialize the async framework.
1037 *
1038 */
1039void __async_init(void)
1040{
1041 if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS,
1042 2, &client_hash_table_ops))
1043 abort();
1044
1045 if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_BUCKETS,
1046 1, &conn_hash_table_ops))
1047 abort();
1048
1049 session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
1050 if (session_ns == NULL)
1051 abort();
1052
1053 session_ns->mgmt = EXCHANGE_ATOMIC;
1054 session_ns->phone = PHONE_NS;
1055 session_ns->arg1 = 0;
1056 session_ns->arg2 = 0;
1057 session_ns->arg3 = 0;
1058
1059 fibril_mutex_initialize(&session_ns->remote_state_mtx);
1060 session_ns->remote_state_data = NULL;
1061
1062 list_initialize(&session_ns->exch_list);
1063 fibril_mutex_initialize(&session_ns->mutex);
1064 atomic_set(&session_ns->refcnt, 0);
1065}
1066
1067/** Reply received callback.
1068 *
1069 * This function is called whenever a reply for an asynchronous message sent out
1070 * by the asynchronous framework is received.
1071 *
1072 * Notify the fibril which is waiting for this message that it has arrived.
1073 *
1074 * @param arg Pointer to the asynchronous message record.
1075 * @param retval Value returned in the answer.
1076 * @param data Call data of the answer.
1077 *
1078 */
1079void reply_received(void *arg, int retval, ipc_call_t *data)
1080{
1081 assert(arg);
1082
1083 futex_down(&async_futex);
1084
1085 amsg_t *msg = (amsg_t *) arg;
1086 msg->retval = retval;
1087
1088 /* Copy data after futex_down, just in case the call was detached */
1089 if ((msg->dataptr) && (data))
1090 *msg->dataptr = *data;
1091
1092 write_barrier();
1093
1094 /* Remove message from timeout list */
1095 if (msg->wdata.to_event.inlist)
1096 list_remove(&msg->wdata.to_event.link);
1097
1098 msg->done = true;
1099 if (!msg->wdata.active) {
1100 msg->wdata.active = true;
1101 fibril_add_ready(msg->wdata.fid);
1102 }
1103
1104 futex_up(&async_futex);
1105}
1106
1107/** Send message and return id of the sent message.
1108 *
1109 * The return value can be used as input for async_wait() to wait for
1110 * completion.
1111 *
1112 * @param exch Exchange for sending the message.
1113 * @param imethod Service-defined interface and method.
1114 * @param arg1 Service-defined payload argument.
1115 * @param arg2 Service-defined payload argument.
1116 * @param arg3 Service-defined payload argument.
1117 * @param arg4 Service-defined payload argument.
1118 * @param dataptr If non-NULL, storage where the reply data will be
1119 * stored.
1120 *
1121 * @return Hash of the sent message or 0 on error.
1122 *
1123 */
1124aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1125 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
1126{
1127 if (exch == NULL)
1128 return 0;
1129
1130 amsg_t *msg = malloc(sizeof(amsg_t));
1131 if (msg == NULL)
1132 return 0;
1133
1134 msg->done = false;
1135 msg->dataptr = dataptr;
1136
1137 msg->wdata.to_event.inlist = false;
1138
1139 /*
1140 * We may sleep in the next method,
1141 * but it will use its own means
1142 */
1143 msg->wdata.active = true;
1144
1145 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
1146 reply_received, true);
1147
1148 return (aid_t) msg;
1149}
1150
1151/** Send message and return id of the sent message
1152 *
1153 * The return value can be used as input for async_wait() to wait for
1154 * completion.
1155 *
1156 * @param exch Exchange for sending the message.
1157 * @param imethod Service-defined interface and method.
1158 * @param arg1 Service-defined payload argument.
1159 * @param arg2 Service-defined payload argument.
1160 * @param arg3 Service-defined payload argument.
1161 * @param arg4 Service-defined payload argument.
1162 * @param arg5 Service-defined payload argument.
1163 * @param dataptr If non-NULL, storage where the reply data will be
1164 * stored.
1165 *
1166 * @return Hash of the sent message or 0 on error.
1167 *
1168 */
1169aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1170 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
1171 ipc_call_t *dataptr)
1172{
1173 if (exch == NULL)
1174 return 0;
1175
1176 amsg_t *msg = malloc(sizeof(amsg_t));
1177
1178 if (msg == NULL)
1179 return 0;
1180
1181 msg->done = false;
1182 msg->dataptr = dataptr;
1183
1184 msg->wdata.to_event.inlist = false;
1185
1186 /*
1187 * We may sleep in the next method,
1188 * but it will use its own means
1189 */
1190 msg->wdata.active = true;
1191
1192 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
1193 msg, reply_received, true);
1194
1195 return (aid_t) msg;
1196}
1197
1198/** Wait for a message sent by the async framework.
1199 *
1200 * @param amsgid Hash of the message to wait for.
1201 * @param retval Pointer to storage where the retval of the answer will
1202 * be stored.
1203 *
1204 */
1205void async_wait_for(aid_t amsgid, sysarg_t *retval)
1206{
1207 assert(amsgid);
1208
1209 amsg_t *msg = (amsg_t *) amsgid;
1210
1211 futex_down(&async_futex);
1212 if (msg->done) {
1213 futex_up(&async_futex);
1214 goto done;
1215 }
1216
1217 msg->wdata.fid = fibril_get_id();
1218 msg->wdata.active = false;
1219 msg->wdata.to_event.inlist = false;
1220
1221 /* Leave the async_futex locked when entering this function */
1222 fibril_switch(FIBRIL_TO_MANAGER);
1223
1224 /* Futex is up automatically after fibril_switch */
1225
1226done:
1227 if (retval)
1228 *retval = msg->retval;
1229
1230 free(msg);
1231}
1232
1233/** Wait for a message sent by the async framework, timeout variant.
1234 *
1235 * @param amsgid Hash of the message to wait for.
1236 * @param retval Pointer to storage where the retval of the answer will
1237 * be stored.
1238 * @param timeout Timeout in microseconds.
1239 *
1240 * @return Zero on success, ETIMEOUT if the timeout has expired.
1241 *
1242 */
1243int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
1244{
1245 assert(amsgid);
1246
1247 amsg_t *msg = (amsg_t *) amsgid;
1248
1249 /* TODO: Let it go through the event read at least once */
1250 if (timeout < 0)
1251 return ETIMEOUT;
1252
1253 futex_down(&async_futex);
1254 if (msg->done) {
1255 futex_up(&async_futex);
1256 goto done;
1257 }
1258
1259 gettimeofday(&msg->wdata.to_event.expires, NULL);
1260 tv_add(&msg->wdata.to_event.expires, timeout);
1261
1262 msg->wdata.fid = fibril_get_id();
1263 msg->wdata.active = false;
1264 async_insert_timeout(&msg->wdata);
1265
1266 /* Leave the async_futex locked when entering this function */
1267 fibril_switch(FIBRIL_TO_MANAGER);
1268
1269 /* Futex is up automatically after fibril_switch */
1270
1271 if (!msg->done)
1272 return ETIMEOUT;
1273
1274done:
1275 if (retval)
1276 *retval = msg->retval;
1277
1278 free(msg);
1279
1280 return 0;
1281}
1282
1283/** Wait for specified time.
1284 *
1285 * The current fibril is suspended but the thread continues to execute.
1286 *
1287 * @param timeout Duration of the wait in microseconds.
1288 *
1289 */
1290void async_usleep(suseconds_t timeout)
1291{
1292 amsg_t *msg = malloc(sizeof(amsg_t));
1293
1294 if (!msg)
1295 return;
1296
1297 msg->wdata.fid = fibril_get_id();
1298 msg->wdata.active = false;
1299
1300 gettimeofday(&msg->wdata.to_event.expires, NULL);
1301 tv_add(&msg->wdata.to_event.expires, timeout);
1302
1303 futex_down(&async_futex);
1304
1305 async_insert_timeout(&msg->wdata);
1306
1307 /* Leave the async_futex locked when entering this function */
1308 fibril_switch(FIBRIL_TO_MANAGER);
1309
1310 /* Futex is up automatically after fibril_switch() */
1311
1312 free(msg);
1313}
1314
1315/** Pseudo-synchronous message sending - fast version.
1316 *
1317 * Send message asynchronously and return only after the reply arrives.
1318 *
1319 * This function can only transfer 4 register payload arguments. For
1320 * transferring more arguments, see the slower async_req_slow().
1321 *
1322 * @param exch Exchange for sending the message.
1323 * @param imethod Interface and method of the call.
1324 * @param arg1 Service-defined payload argument.
1325 * @param arg2 Service-defined payload argument.
1326 * @param arg3 Service-defined payload argument.
1327 * @param arg4 Service-defined payload argument.
1328 * @param r1 If non-NULL, storage for the 1st reply argument.
1329 * @param r2 If non-NULL, storage for the 2nd reply argument.
1330 * @param r3 If non-NULL, storage for the 3rd reply argument.
1331 * @param r4 If non-NULL, storage for the 4th reply argument.
1332 * @param r5 If non-NULL, storage for the 5th reply argument.
1333 *
1334 * @return Return code of the reply or a negative error code.
1335 *
1336 */
1337sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1338 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
1339 sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1340{
1341 if (exch == NULL)
1342 return ENOENT;
1343
1344 ipc_call_t result;
1345 aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
1346 &result);
1347
1348 sysarg_t rc;
1349 async_wait_for(aid, &rc);
1350
1351 if (r1)
1352 *r1 = IPC_GET_ARG1(result);
1353
1354 if (r2)
1355 *r2 = IPC_GET_ARG2(result);
1356
1357 if (r3)
1358 *r3 = IPC_GET_ARG3(result);
1359
1360 if (r4)
1361 *r4 = IPC_GET_ARG4(result);
1362
1363 if (r5)
1364 *r5 = IPC_GET_ARG5(result);
1365
1366 return rc;
1367}
1368
1369/** Pseudo-synchronous message sending - slow version.
1370 *
1371 * Send message asynchronously and return only after the reply arrives.
1372 *
1373 * @param exch Exchange for sending the message.
1374 * @param imethod Interface and method of the call.
1375 * @param arg1 Service-defined payload argument.
1376 * @param arg2 Service-defined payload argument.
1377 * @param arg3 Service-defined payload argument.
1378 * @param arg4 Service-defined payload argument.
1379 * @param arg5 Service-defined payload argument.
1380 * @param r1 If non-NULL, storage for the 1st reply argument.
1381 * @param r2 If non-NULL, storage for the 2nd reply argument.
1382 * @param r3 If non-NULL, storage for the 3rd reply argument.
1383 * @param r4 If non-NULL, storage for the 4th reply argument.
1384 * @param r5 If non-NULL, storage for the 5th reply argument.
1385 *
1386 * @return Return code of the reply or a negative error code.
1387 *
1388 */
1389sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1390 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
1391 sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
1392{
1393 if (exch == NULL)
1394 return ENOENT;
1395
1396 ipc_call_t result;
1397 aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
1398 &result);
1399
1400 sysarg_t rc;
1401 async_wait_for(aid, &rc);
1402
1403 if (r1)
1404 *r1 = IPC_GET_ARG1(result);
1405
1406 if (r2)
1407 *r2 = IPC_GET_ARG2(result);
1408
1409 if (r3)
1410 *r3 = IPC_GET_ARG3(result);
1411
1412 if (r4)
1413 *r4 = IPC_GET_ARG4(result);
1414
1415 if (r5)
1416 *r5 = IPC_GET_ARG5(result);
1417
1418 return rc;
1419}
1420
1421void async_msg_0(async_exch_t *exch, sysarg_t imethod)
1422{
1423 if (exch != NULL)
1424 ipc_call_async_0(exch->phone, imethod, NULL, NULL, true);
1425}
1426
1427void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
1428{
1429 if (exch != NULL)
1430 ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL, true);
1431}
1432
1433void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1434 sysarg_t arg2)
1435{
1436 if (exch != NULL)
1437 ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL,
1438 true);
1439}
1440
1441void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1442 sysarg_t arg2, sysarg_t arg3)
1443{
1444 if (exch != NULL)
1445 ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
1446 NULL, true);
1447}
1448
1449void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1450 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1451{
1452 if (exch != NULL)
1453 ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
1454 NULL, NULL, true);
1455}
1456
1457void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
1458 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1459{
1460 if (exch != NULL)
1461 ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
1462 arg5, NULL, NULL, true);
1463}
1464
1465sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
1466{
1467 return ipc_answer_0(callid, retval);
1468}
1469
1470sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
1471{
1472 return ipc_answer_1(callid, retval, arg1);
1473}
1474
1475sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1476 sysarg_t arg2)
1477{
1478 return ipc_answer_2(callid, retval, arg1, arg2);
1479}
1480
1481sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1482 sysarg_t arg2, sysarg_t arg3)
1483{
1484 return ipc_answer_3(callid, retval, arg1, arg2, arg3);
1485}
1486
1487sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1488 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
1489{
1490 return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
1491}
1492
1493sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
1494 sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
1495{
1496 return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
1497}
1498
1499int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
1500 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
1501{
1502 if (exch == NULL)
1503 return ENOENT;
1504
1505 return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
1506}
1507
1508int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
1509 sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
1510 sysarg_t arg4, sysarg_t arg5, unsigned int mode)
1511{
1512 if (exch == NULL)
1513 return ENOENT;
1514
1515 return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
1516 arg4, arg5, mode);
1517}
1518
1519/** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
1520 *
1521 * Ask through phone for a new connection to some service.
1522 *
1523 * @param exch Exchange for sending the message.
1524 * @param arg1 User defined argument.
1525 * @param arg2 User defined argument.
1526 * @param arg3 User defined argument.
1527 * @param client_receiver Connection handing routine.
1528 *
1529 * @return Zero on success or a negative error code.
1530 *
1531 */
1532int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
1533 sysarg_t arg3, async_client_conn_t client_receiver, void *carg)
1534{
1535 if (exch == NULL)
1536 return ENOENT;
1537
1538 sysarg_t phone_hash;
1539 sysarg_t rc;
1540
1541 aid_t req;
1542 ipc_call_t answer;
1543 req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
1544 &answer);
1545 async_wait_for(req, &rc);
1546 if (rc != EOK)
1547 return (int) rc;
1548
1549 phone_hash = IPC_GET_ARG5(answer);
1550
1551 if (client_receiver != NULL)
1552 async_new_connection(answer.in_task_id, phone_hash, 0, NULL,
1553 client_receiver, carg);
1554
1555 return EOK;
1556}
1557
1558/** Wrapper for making IPC_M_CONNECT_ME calls using the async framework.
1559 *
1560 * Ask through for a cloned connection to some service.
1561 *
1562 * @param mgmt Exchange management style.
1563 * @param exch Exchange for sending the message.
1564 *
1565 * @return New session on success or NULL on error.
1566 *
1567 */
1568async_sess_t *async_connect_me(exch_mgmt_t mgmt, async_exch_t *exch)
1569{
1570 if (exch == NULL) {
1571 errno = ENOENT;
1572 return NULL;
1573 }
1574
1575 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1576 if (sess == NULL) {
1577 errno = ENOMEM;
1578 return NULL;
1579 }
1580
1581 ipc_call_t result;
1582
1583 amsg_t *msg = malloc(sizeof(amsg_t));
1584 if (msg == NULL) {
1585 free(sess);
1586 errno = ENOMEM;
1587 return NULL;
1588 }
1589
1590 msg->done = false;
1591 msg->dataptr = &result;
1592
1593 msg->wdata.to_event.inlist = false;
1594
1595 /*
1596 * We may sleep in the next method,
1597 * but it will use its own means
1598 */
1599 msg->wdata.active = true;
1600
1601 ipc_call_async_0(exch->phone, IPC_M_CONNECT_ME, msg,
1602 reply_received, true);
1603
1604 sysarg_t rc;
1605 async_wait_for((aid_t) msg, &rc);
1606
1607 if (rc != EOK) {
1608 errno = rc;
1609 free(sess);
1610 return NULL;
1611 }
1612
1613 int phone = (int) IPC_GET_ARG5(result);
1614
1615 if (phone < 0) {
1616 errno = phone;
1617 free(sess);
1618 return NULL;
1619 }
1620
1621 sess->mgmt = mgmt;
1622 sess->phone = phone;
1623 sess->arg1 = 0;
1624 sess->arg2 = 0;
1625 sess->arg3 = 0;
1626
1627 fibril_mutex_initialize(&sess->remote_state_mtx);
1628 sess->remote_state_data = NULL;
1629
1630 list_initialize(&sess->exch_list);
1631 fibril_mutex_initialize(&sess->mutex);
1632 atomic_set(&sess->refcnt, 0);
1633
1634 return sess;
1635}
1636
1637static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
1638 sysarg_t arg3, sysarg_t arg4)
1639{
1640 ipc_call_t result;
1641
1642 amsg_t *msg = malloc(sizeof(amsg_t));
1643 if (msg == NULL)
1644 return ENOENT;
1645
1646 msg->done = false;
1647 msg->dataptr = &result;
1648
1649 msg->wdata.to_event.inlist = false;
1650
1651 /*
1652 * We may sleep in the next method,
1653 * but it will use its own means
1654 */
1655 msg->wdata.active = true;
1656
1657 ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
1658 msg, reply_received, true);
1659
1660 sysarg_t rc;
1661 async_wait_for((aid_t) msg, &rc);
1662
1663 if (rc != EOK)
1664 return rc;
1665
1666 return (int) IPC_GET_ARG5(result);
1667}
1668
1669/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1670 *
1671 * Ask through for a new connection to some service.
1672 *
1673 * @param mgmt Exchange management style.
1674 * @param exch Exchange for sending the message.
1675 * @param arg1 User defined argument.
1676 * @param arg2 User defined argument.
1677 * @param arg3 User defined argument.
1678 *
1679 * @return New session on success or NULL on error.
1680 *
1681 */
1682async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
1683 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1684{
1685 if (exch == NULL) {
1686 errno = ENOENT;
1687 return NULL;
1688 }
1689
1690 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1691 if (sess == NULL) {
1692 errno = ENOMEM;
1693 return NULL;
1694 }
1695
1696 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1697 0);
1698
1699 if (phone < 0) {
1700 errno = phone;
1701 free(sess);
1702 return NULL;
1703 }
1704
1705 sess->mgmt = mgmt;
1706 sess->phone = phone;
1707 sess->arg1 = arg1;
1708 sess->arg2 = arg2;
1709 sess->arg3 = arg3;
1710
1711 fibril_mutex_initialize(&sess->remote_state_mtx);
1712 sess->remote_state_data = NULL;
1713
1714 list_initialize(&sess->exch_list);
1715 fibril_mutex_initialize(&sess->mutex);
1716 atomic_set(&sess->refcnt, 0);
1717
1718 return sess;
1719}
1720
1721/** Set arguments for new connections.
1722 *
1723 * FIXME This is an ugly hack to work around the problem that parallel
1724 * exchanges are implemented using parallel connections. When we create
1725 * a callback session, the framework does not know arguments for the new
1726 * connections.
1727 *
1728 * The proper solution seems to be to implement parallel exchanges using
1729 * tagging.
1730 */
1731void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
1732 sysarg_t arg3)
1733{
1734 sess->arg1 = arg1;
1735 sess->arg2 = arg2;
1736 sess->arg3 = arg3;
1737}
1738
1739/** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
1740 *
1741 * Ask through phone for a new connection to some service and block until
1742 * success.
1743 *
1744 * @param mgmt Exchange management style.
1745 * @param exch Exchange for sending the message.
1746 * @param arg1 User defined argument.
1747 * @param arg2 User defined argument.
1748 * @param arg3 User defined argument.
1749 *
1750 * @return New session on success or NULL on error.
1751 *
1752 */
1753async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
1754 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
1755{
1756 if (exch == NULL) {
1757 errno = ENOENT;
1758 return NULL;
1759 }
1760
1761 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1762 if (sess == NULL) {
1763 errno = ENOMEM;
1764 return NULL;
1765 }
1766
1767 int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
1768 IPC_FLAG_BLOCKING);
1769
1770 if (phone < 0) {
1771 errno = phone;
1772 free(sess);
1773 return NULL;
1774 }
1775
1776 sess->mgmt = mgmt;
1777 sess->phone = phone;
1778 sess->arg1 = arg1;
1779 sess->arg2 = arg2;
1780 sess->arg3 = arg3;
1781
1782 fibril_mutex_initialize(&sess->remote_state_mtx);
1783 sess->remote_state_data = NULL;
1784
1785 list_initialize(&sess->exch_list);
1786 fibril_mutex_initialize(&sess->mutex);
1787 atomic_set(&sess->refcnt, 0);
1788
1789 return sess;
1790}
1791
1792/** Connect to a task specified by id.
1793 *
1794 */
1795async_sess_t *async_connect_kbox(task_id_t id)
1796{
1797 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
1798 if (sess == NULL) {
1799 errno = ENOMEM;
1800 return NULL;
1801 }
1802
1803 int phone = ipc_connect_kbox(id);
1804 if (phone < 0) {
1805 errno = phone;
1806 free(sess);
1807 return NULL;
1808 }
1809
1810 sess->mgmt = EXCHANGE_ATOMIC;
1811 sess->phone = phone;
1812 sess->arg1 = 0;
1813 sess->arg2 = 0;
1814 sess->arg3 = 0;
1815
1816 fibril_mutex_initialize(&sess->remote_state_mtx);
1817 sess->remote_state_data = NULL;
1818
1819 list_initialize(&sess->exch_list);
1820 fibril_mutex_initialize(&sess->mutex);
1821 atomic_set(&sess->refcnt, 0);
1822
1823 return sess;
1824}
1825
1826static int async_hangup_internal(int phone)
1827{
1828 return ipc_hangup(phone);
1829}
1830
1831/** Wrapper for ipc_hangup.
1832 *
1833 * @param sess Session to hung up.
1834 *
1835 * @return Zero on success or a negative error code.
1836 *
1837 */
1838int async_hangup(async_sess_t *sess)
1839{
1840 async_exch_t *exch;
1841
1842 assert(sess);
1843
1844 if (atomic_get(&sess->refcnt) > 0)
1845 return EBUSY;
1846
1847 fibril_mutex_lock(&async_sess_mutex);
1848
1849 int rc = async_hangup_internal(sess->phone);
1850
1851 while (!list_empty(&sess->exch_list)) {
1852 exch = (async_exch_t *)
1853 list_get_instance(list_first(&sess->exch_list),
1854 async_exch_t, sess_link);
1855
1856 list_remove(&exch->sess_link);
1857 list_remove(&exch->global_link);
1858 async_hangup_internal(exch->phone);
1859 free(exch);
1860 }
1861
1862 free(sess);
1863
1864 fibril_mutex_unlock(&async_sess_mutex);
1865
1866 return rc;
1867}
1868
1869/** Interrupt one thread of this task from waiting for IPC. */
1870void async_poke(void)
1871{
1872 ipc_poke();
1873}
1874
1875/** Start new exchange in a session.
1876 *
1877 * @param session Session.
1878 *
1879 * @return New exchange or NULL on error.
1880 *
1881 */
1882async_exch_t *async_exchange_begin(async_sess_t *sess)
1883{
1884 if (sess == NULL)
1885 return NULL;
1886
1887 async_exch_t *exch;
1888
1889 fibril_mutex_lock(&async_sess_mutex);
1890
1891 if (!list_empty(&sess->exch_list)) {
1892 /*
1893 * There are inactive exchanges in the session.
1894 */
1895 exch = (async_exch_t *)
1896 list_get_instance(list_first(&sess->exch_list),
1897 async_exch_t, sess_link);
1898
1899 list_remove(&exch->sess_link);
1900 list_remove(&exch->global_link);
1901 } else {
1902 /*
1903 * There are no available exchanges in the session.
1904 */
1905
1906 if ((sess->mgmt == EXCHANGE_ATOMIC) ||
1907 (sess->mgmt == EXCHANGE_SERIALIZE)) {
1908 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
1909 if (exch != NULL) {
1910 link_initialize(&exch->sess_link);
1911 link_initialize(&exch->global_link);
1912 exch->sess = sess;
1913 exch->phone = sess->phone;
1914 }
1915 } else { /* EXCHANGE_PARALLEL */
1916 /*
1917 * Make a one-time attempt to connect a new data phone.
1918 */
1919
1920 int phone;
1921
1922retry:
1923 phone = async_connect_me_to_internal(sess->phone, sess->arg1,
1924 sess->arg2, sess->arg3, 0);
1925 if (phone >= 0) {
1926 exch = (async_exch_t *) malloc(sizeof(async_exch_t));
1927 if (exch != NULL) {
1928 link_initialize(&exch->sess_link);
1929 link_initialize(&exch->global_link);
1930 exch->sess = sess;
1931 exch->phone = phone;
1932 } else
1933 async_hangup_internal(phone);
1934 } else if (!list_empty(&inactive_exch_list)) {
1935 /*
1936 * We did not manage to connect a new phone. But we
1937 * can try to close some of the currently inactive
1938 * connections in other sessions and try again.
1939 */
1940 exch = (async_exch_t *)
1941 list_get_instance(list_first(&inactive_exch_list),
1942 async_exch_t, global_link);
1943
1944 list_remove(&exch->sess_link);
1945 list_remove(&exch->global_link);
1946 async_hangup_internal(exch->phone);
1947 free(exch);
1948 goto retry;
1949 } else {
1950 /*
1951 * Wait for a phone to become available.
1952 */
1953 fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
1954 goto retry;
1955 }
1956 }
1957 }
1958
1959 fibril_mutex_unlock(&async_sess_mutex);
1960
1961 if (exch != NULL) {
1962 atomic_inc(&sess->refcnt);
1963
1964 if (sess->mgmt == EXCHANGE_SERIALIZE)
1965 fibril_mutex_lock(&sess->mutex);
1966 }
1967
1968 return exch;
1969}
1970
1971/** Finish an exchange.
1972 *
1973 * @param exch Exchange to finish.
1974 *
1975 */
1976void async_exchange_end(async_exch_t *exch)
1977{
1978 if (exch == NULL)
1979 return;
1980
1981 async_sess_t *sess = exch->sess;
1982
1983 atomic_dec(&sess->refcnt);
1984
1985 if (sess->mgmt == EXCHANGE_SERIALIZE)
1986 fibril_mutex_unlock(&sess->mutex);
1987
1988 fibril_mutex_lock(&async_sess_mutex);
1989
1990 list_append(&exch->sess_link, &sess->exch_list);
1991 list_append(&exch->global_link, &inactive_exch_list);
1992 fibril_condvar_signal(&avail_phone_cv);
1993
1994 fibril_mutex_unlock(&async_sess_mutex);
1995}
1996
1997/** Wrapper for IPC_M_SHARE_IN calls using the async framework.
1998 *
1999 * @param exch Exchange for sending the message.
2000 * @param size Size of the destination address space area.
2001 * @param arg User defined argument.
2002 * @param flags Storage for the received flags. Can be NULL.
2003 * @param dst Destination address space area base. Cannot be NULL.
2004 *
2005 * @return Zero on success or a negative error code from errno.h.
2006 *
2007 */
2008int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
2009 unsigned int *flags, void **dst)
2010{
2011 if (exch == NULL)
2012 return ENOENT;
2013
2014 sysarg_t _flags = 0;
2015 sysarg_t _dst = (sysarg_t) -1;
2016 int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
2017 arg, NULL, &_flags, NULL, &_dst);
2018
2019 if (flags)
2020 *flags = (unsigned int) _flags;
2021
2022 *dst = (void *) _dst;
2023 return res;
2024}
2025
2026/** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
2027 *
2028 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
2029 * calls so that the user doesn't have to remember the meaning of each IPC
2030 * argument.
2031 *
2032 * So far, this wrapper is to be used from within a connection fibril.
2033 *
2034 * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
2035 * @param size Destination address space area size.
2036 *
2037 * @return True on success, false on failure.
2038 *
2039 */
2040bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
2041{
2042 assert(callid);
2043 assert(size);
2044
2045 ipc_call_t data;
2046 *callid = async_get_call(&data);
2047
2048 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
2049 return false;
2050
2051 *size = (size_t) IPC_GET_ARG1(data);
2052 return true;
2053}
2054
2055/** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
2056 *
2057 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
2058 * calls so that the user doesn't have to remember the meaning of each IPC
2059 * argument.
2060 *
2061 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2062 * @param src Source address space base.
2063 * @param flags Flags to be used for sharing. Bits can be only cleared.
2064 *
2065 * @return Zero on success or a value from @ref errno.h on failure.
2066 *
2067 */
2068int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
2069{
2070 return ipc_share_in_finalize(callid, src, flags);
2071}
2072
2073/** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
2074 *
2075 * @param exch Exchange for sending the message.
2076 * @param src Source address space area base address.
2077 * @param flags Flags to be used for sharing. Bits can be only cleared.
2078 *
2079 * @return Zero on success or a negative error code from errno.h.
2080 *
2081 */
2082int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
2083{
2084 if (exch == NULL)
2085 return ENOENT;
2086
2087 return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
2088 (sysarg_t) flags);
2089}
2090
2091/** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
2092 *
2093 * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
2094 * calls so that the user doesn't have to remember the meaning of each IPC
2095 * argument.
2096 *
2097 * So far, this wrapper is to be used from within a connection fibril.
2098 *
2099 * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
2100 * @param size Storage for the source address space area size.
2101 * @param flags Storage for the sharing flags.
2102 *
2103 * @return True on success, false on failure.
2104 *
2105 */
2106bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
2107{
2108 assert(callid);
2109 assert(size);
2110 assert(flags);
2111
2112 ipc_call_t data;
2113 *callid = async_get_call(&data);
2114
2115 if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
2116 return false;
2117
2118 *size = (size_t) IPC_GET_ARG2(data);
2119 *flags = (unsigned int) IPC_GET_ARG3(data);
2120 return true;
2121}
2122
2123/** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
2124 *
2125 * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
2126 * calls so that the user doesn't have to remember the meaning of each IPC
2127 * argument.
2128 *
2129 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2130 * @param dst Destination address space area base address.
2131 *
2132 * @return Zero on success or a value from @ref errno.h on failure.
2133 *
2134 */
2135int async_share_out_finalize(ipc_callid_t callid, void **dst)
2136{
2137 return ipc_share_out_finalize(callid, dst);
2138}
2139
2140/** Start IPC_M_DATA_READ using the async framework.
2141 *
2142 * @param exch Exchange for sending the message.
2143 * @param dst Address of the beginning of the destination buffer.
2144 * @param size Size of the destination buffer (in bytes).
2145 * @param dataptr Storage of call data (arg 2 holds actual data size).
2146 *
2147 * @return Hash of the sent message or 0 on error.
2148 *
2149 */
2150aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
2151 ipc_call_t *dataptr)
2152{
2153 return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2154 (sysarg_t) size, dataptr);
2155}
2156
2157/** Wrapper for IPC_M_DATA_READ calls using the async framework.
2158 *
2159 * @param exch Exchange for sending the message.
2160 * @param dst Address of the beginning of the destination buffer.
2161 * @param size Size of the destination buffer.
2162 *
2163 * @return Zero on success or a negative error code from errno.h.
2164 *
2165 */
2166int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
2167{
2168 if (exch == NULL)
2169 return ENOENT;
2170
2171 return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
2172 (sysarg_t) size);
2173}
2174
2175/** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
2176 *
2177 * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
2178 * calls so that the user doesn't have to remember the meaning of each IPC
2179 * argument.
2180 *
2181 * So far, this wrapper is to be used from within a connection fibril.
2182 *
2183 * @param callid Storage for the hash of the IPC_M_DATA_READ.
2184 * @param size Storage for the maximum size. Can be NULL.
2185 *
2186 * @return True on success, false on failure.
2187 *
2188 */
2189bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
2190{
2191 assert(callid);
2192
2193 ipc_call_t data;
2194 *callid = async_get_call(&data);
2195
2196 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
2197 return false;
2198
2199 if (size)
2200 *size = (size_t) IPC_GET_ARG2(data);
2201
2202 return true;
2203}
2204
2205/** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
2206 *
2207 * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
2208 * calls so that the user doesn't have to remember the meaning of each IPC
2209 * argument.
2210 *
2211 * @param callid Hash of the IPC_M_DATA_READ call to answer.
2212 * @param src Source address for the IPC_M_DATA_READ call.
2213 * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
2214 * the maximum size announced by the sender.
2215 *
2216 * @return Zero on success or a value from @ref errno.h on failure.
2217 *
2218 */
2219int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
2220{
2221 return ipc_data_read_finalize(callid, src, size);
2222}
2223
2224/** Wrapper for forwarding any read request
2225 *
2226 */
2227int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
2228 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2229 ipc_call_t *dataptr)
2230{
2231 if (exch == NULL)
2232 return ENOENT;
2233
2234 ipc_callid_t callid;
2235 if (!async_data_read_receive(&callid, NULL)) {
2236 ipc_answer_0(callid, EINVAL);
2237 return EINVAL;
2238 }
2239
2240 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2241 dataptr);
2242 if (msg == 0) {
2243 ipc_answer_0(callid, EINVAL);
2244 return EINVAL;
2245 }
2246
2247 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
2248 IPC_FF_ROUTE_FROM_ME);
2249 if (retval != EOK) {
2250 async_wait_for(msg, NULL);
2251 ipc_answer_0(callid, retval);
2252 return retval;
2253 }
2254
2255 sysarg_t rc;
2256 async_wait_for(msg, &rc);
2257
2258 return (int) rc;
2259}
2260
2261/** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
2262 *
2263 * @param exch Exchange for sending the message.
2264 * @param src Address of the beginning of the source buffer.
2265 * @param size Size of the source buffer.
2266 *
2267 * @return Zero on success or a negative error code from errno.h.
2268 *
2269 */
2270int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
2271{
2272 if (exch == NULL)
2273 return ENOENT;
2274
2275 return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
2276 (sysarg_t) size);
2277}
2278
2279/** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
2280 *
2281 * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
2282 * calls so that the user doesn't have to remember the meaning of each IPC
2283 * argument.
2284 *
2285 * So far, this wrapper is to be used from within a connection fibril.
2286 *
2287 * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
2288 * @param size Storage for the suggested size. May be NULL.
2289 *
2290 * @return True on success, false on failure.
2291 *
2292 */
2293bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
2294{
2295 assert(callid);
2296
2297 ipc_call_t data;
2298 *callid = async_get_call(&data);
2299
2300 if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
2301 return false;
2302
2303 if (size)
2304 *size = (size_t) IPC_GET_ARG2(data);
2305
2306 return true;
2307}
2308
2309/** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
2310 *
2311 * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
2312 * calls so that the user doesn't have to remember the meaning of each IPC
2313 * argument.
2314 *
2315 * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
2316 * @param dst Final destination address for the IPC_M_DATA_WRITE call.
2317 * @param size Final size for the IPC_M_DATA_WRITE call.
2318 *
2319 * @return Zero on success or a value from @ref errno.h on failure.
2320 *
2321 */
2322int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
2323{
2324 return ipc_data_write_finalize(callid, dst, size);
2325}
2326
2327/** Wrapper for receiving binary data or strings
2328 *
2329 * This wrapper only makes it more comfortable to use async_data_write_*
2330 * functions to receive binary data or strings.
2331 *
2332 * @param data Pointer to data pointer (which should be later disposed
2333 * by free()). If the operation fails, the pointer is not
2334 * touched.
2335 * @param nullterm If true then the received data is always zero terminated.
2336 * This also causes to allocate one extra byte beyond the
2337 * raw transmitted data.
2338 * @param min_size Minimum size (in bytes) of the data to receive.
2339 * @param max_size Maximum size (in bytes) of the data to receive. 0 means
2340 * no limit.
2341 * @param granulariy If non-zero then the size of the received data has to
2342 * be divisible by this value.
2343 * @param received If not NULL, the size of the received data is stored here.
2344 *
2345 * @return Zero on success or a value from @ref errno.h on failure.
2346 *
2347 */
2348int async_data_write_accept(void **data, const bool nullterm,
2349 const size_t min_size, const size_t max_size, const size_t granularity,
2350 size_t *received)
2351{
2352 assert(data);
2353
2354 ipc_callid_t callid;
2355 size_t size;
2356 if (!async_data_write_receive(&callid, &size)) {
2357 ipc_answer_0(callid, EINVAL);
2358 return EINVAL;
2359 }
2360
2361 if (size < min_size) {
2362 ipc_answer_0(callid, EINVAL);
2363 return EINVAL;
2364 }
2365
2366 if ((max_size > 0) && (size > max_size)) {
2367 ipc_answer_0(callid, EINVAL);
2368 return EINVAL;
2369 }
2370
2371 if ((granularity > 0) && ((size % granularity) != 0)) {
2372 ipc_answer_0(callid, EINVAL);
2373 return EINVAL;
2374 }
2375
2376 void *_data;
2377
2378 if (nullterm)
2379 _data = malloc(size + 1);
2380 else
2381 _data = malloc(size);
2382
2383 if (_data == NULL) {
2384 ipc_answer_0(callid, ENOMEM);
2385 return ENOMEM;
2386 }
2387
2388 int rc = async_data_write_finalize(callid, _data, size);
2389 if (rc != EOK) {
2390 free(_data);
2391 return rc;
2392 }
2393
2394 if (nullterm)
2395 ((char *) _data)[size] = 0;
2396
2397 *data = _data;
2398 if (received != NULL)
2399 *received = size;
2400
2401 return EOK;
2402}
2403
2404/** Wrapper for voiding any data that is about to be received
2405 *
2406 * This wrapper can be used to void any pending data
2407 *
2408 * @param retval Error value from @ref errno.h to be returned to the caller.
2409 *
2410 */
2411void async_data_write_void(sysarg_t retval)
2412{
2413 ipc_callid_t callid;
2414 async_data_write_receive(&callid, NULL);
2415 ipc_answer_0(callid, retval);
2416}
2417
2418/** Wrapper for forwarding any data that is about to be received
2419 *
2420 */
2421int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
2422 sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
2423 ipc_call_t *dataptr)
2424{
2425 if (exch == NULL)
2426 return ENOENT;
2427
2428 ipc_callid_t callid;
2429 if (!async_data_write_receive(&callid, NULL)) {
2430 ipc_answer_0(callid, EINVAL);
2431 return EINVAL;
2432 }
2433
2434 aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
2435 dataptr);
2436 if (msg == 0) {
2437 ipc_answer_0(callid, EINVAL);
2438 return EINVAL;
2439 }
2440
2441 int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
2442 IPC_FF_ROUTE_FROM_ME);
2443 if (retval != EOK) {
2444 async_wait_for(msg, NULL);
2445 ipc_answer_0(callid, retval);
2446 return retval;
2447 }
2448
2449 sysarg_t rc;
2450 async_wait_for(msg, &rc);
2451
2452 return (int) rc;
2453}
2454
2455/** Wrapper for sending an exchange over different exchange for cloning
2456 *
2457 * @param exch Exchange to be used for sending.
2458 * @param clone_exch Exchange to be cloned.
2459 *
2460 */
2461int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
2462{
2463 return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
2464}
2465
2466/** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
2467 *
2468 * If the current call is IPC_M_CONNECTION_CLONE then a new
2469 * async session is created for the accepted phone.
2470 *
2471 * @param mgmt Exchange management style.
2472 *
2473 * @return New async session or NULL on failure.
2474 *
2475 */
2476async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
2477{
2478 /* Accept the phone */
2479 ipc_call_t call;
2480 ipc_callid_t callid = async_get_call(&call);
2481 int phone = (int) IPC_GET_ARG1(call);
2482
2483 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
2484 (phone < 0)) {
2485 async_answer_0(callid, EINVAL);
2486 return NULL;
2487 }
2488
2489 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2490 if (sess == NULL) {
2491 async_answer_0(callid, ENOMEM);
2492 return NULL;
2493 }
2494
2495 sess->mgmt = mgmt;
2496 sess->phone = phone;
2497 sess->arg1 = 0;
2498 sess->arg2 = 0;
2499 sess->arg3 = 0;
2500
2501 fibril_mutex_initialize(&sess->remote_state_mtx);
2502 sess->remote_state_data = NULL;
2503
2504 list_initialize(&sess->exch_list);
2505 fibril_mutex_initialize(&sess->mutex);
2506 atomic_set(&sess->refcnt, 0);
2507
2508 /* Acknowledge the cloned phone */
2509 async_answer_0(callid, EOK);
2510
2511 return sess;
2512}
2513
2514/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2515 *
2516 * If the current call is IPC_M_CONNECT_TO_ME then a new
2517 * async session is created for the accepted phone.
2518 *
2519 * @param mgmt Exchange management style.
2520 *
2521 * @return New async session.
2522 * @return NULL on failure.
2523 *
2524 */
2525async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
2526{
2527 /* Accept the phone */
2528 ipc_call_t call;
2529 ipc_callid_t callid = async_get_call(&call);
2530 int phone = (int) IPC_GET_ARG5(call);
2531
2532 if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
2533 (phone < 0)) {
2534 async_answer_0(callid, EINVAL);
2535 return NULL;
2536 }
2537
2538 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2539 if (sess == NULL) {
2540 async_answer_0(callid, ENOMEM);
2541 return NULL;
2542 }
2543
2544 sess->mgmt = mgmt;
2545 sess->phone = phone;
2546 sess->arg1 = 0;
2547 sess->arg2 = 0;
2548 sess->arg3 = 0;
2549
2550 fibril_mutex_initialize(&sess->remote_state_mtx);
2551 sess->remote_state_data = NULL;
2552
2553 list_initialize(&sess->exch_list);
2554 fibril_mutex_initialize(&sess->mutex);
2555 atomic_set(&sess->refcnt, 0);
2556
2557 /* Acknowledge the connected phone */
2558 async_answer_0(callid, EOK);
2559
2560 return sess;
2561}
2562
2563/** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
2564 *
2565 * If the call is IPC_M_CONNECT_TO_ME then a new
2566 * async session is created. However, the phone is
2567 * not accepted automatically.
2568 *
2569 * @param mgmt Exchange management style.
2570 * @param call Call data.
2571 *
2572 * @return New async session.
2573 * @return NULL on failure.
2574 * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
2575 *
2576 */
2577async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
2578{
2579 int phone = (int) IPC_GET_ARG5(*call);
2580
2581 if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
2582 (phone < 0))
2583 return NULL;
2584
2585 async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
2586 if (sess == NULL)
2587 return NULL;
2588
2589 sess->mgmt = mgmt;
2590 sess->phone = phone;
2591 sess->arg1 = 0;
2592 sess->arg2 = 0;
2593 sess->arg3 = 0;
2594
2595 fibril_mutex_initialize(&sess->remote_state_mtx);
2596 sess->remote_state_data = NULL;
2597
2598 list_initialize(&sess->exch_list);
2599 fibril_mutex_initialize(&sess->mutex);
2600 atomic_set(&sess->refcnt, 0);
2601
2602 return sess;
2603}
2604
2605int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
2606 sysarg_t arg3, async_exch_t *other_exch)
2607{
2608 return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
2609 arg1, arg2, arg3, 0, other_exch->phone);
2610}
2611
2612bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
2613 sysarg_t *arg2, sysarg_t *arg3)
2614{
2615 assert(callid);
2616
2617 ipc_call_t call;
2618 *callid = async_get_call(&call);
2619
2620 if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
2621 return false;
2622
2623 if (arg1)
2624 *arg1 = IPC_GET_ARG1(call);
2625 if (arg2)
2626 *arg2 = IPC_GET_ARG2(call);
2627 if (arg3)
2628 *arg3 = IPC_GET_ARG3(call);
2629
2630 return true;
2631}
2632
2633int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
2634{
2635 return ipc_answer_1(callid, EOK, other_exch->phone);
2636}
2637
2638/** Lock and get session remote state
2639 *
2640 * Lock and get the local replica of the remote state
2641 * in stateful sessions. The call should be paired
2642 * with async_remote_state_release*().
2643 *
2644 * @param[in] sess Stateful session.
2645 *
2646 * @return Local replica of the remote state.
2647 *
2648 */
2649void *async_remote_state_acquire(async_sess_t *sess)
2650{
2651 fibril_mutex_lock(&sess->remote_state_mtx);
2652 return sess->remote_state_data;
2653}
2654
2655/** Update the session remote state
2656 *
2657 * Update the local replica of the remote state
2658 * in stateful sessions. The remote state must
2659 * be already locked.
2660 *
2661 * @param[in] sess Stateful session.
2662 * @param[in] state New local replica of the remote state.
2663 *
2664 */
2665void async_remote_state_update(async_sess_t *sess, void *state)
2666{
2667 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2668 sess->remote_state_data = state;
2669}
2670
2671/** Release the session remote state
2672 *
2673 * Unlock the local replica of the remote state
2674 * in stateful sessions.
2675 *
2676 * @param[in] sess Stateful session.
2677 *
2678 */
2679void async_remote_state_release(async_sess_t *sess)
2680{
2681 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2682
2683 fibril_mutex_unlock(&sess->remote_state_mtx);
2684}
2685
2686/** Release the session remote state and end an exchange
2687 *
2688 * Unlock the local replica of the remote state
2689 * in stateful sessions. This is convenience function
2690 * which gets the session pointer from the exchange
2691 * and also ends the exchange.
2692 *
2693 * @param[in] exch Stateful session's exchange.
2694 *
2695 */
2696void async_remote_state_release_exchange(async_exch_t *exch)
2697{
2698 if (exch == NULL)
2699 return;
2700
2701 async_sess_t *sess = exch->sess;
2702 assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
2703
2704 async_exchange_end(exch);
2705 fibril_mutex_unlock(&sess->remote_state_mtx);
2706}
2707
2708/** @}
2709 */
Note: See TracBrowser for help on using the repository browser.