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(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 <ipc/irq.h>
|
---|
104 | #include <ipc/event.h>
|
---|
105 | #include <futex.h>
|
---|
106 | #include <fibril.h>
|
---|
107 | #include <adt/hash_table.h>
|
---|
108 | #include <adt/list.h>
|
---|
109 | #include <assert.h>
|
---|
110 | #include <errno.h>
|
---|
111 | #include <sys/time.h>
|
---|
112 | #include <libarch/barrier.h>
|
---|
113 | #include <stdbool.h>
|
---|
114 | #include <malloc.h>
|
---|
115 | #include <mem.h>
|
---|
116 | #include <stdlib.h>
|
---|
117 | #include <macros.h>
|
---|
118 | #include <as.h>
|
---|
119 | #include <abi/mm/as.h>
|
---|
120 | #include "private/libc.h"
|
---|
121 |
|
---|
122 | /** Session data */
|
---|
123 | struct async_sess {
|
---|
124 | /** List of inactive exchanges */
|
---|
125 | list_t exch_list;
|
---|
126 |
|
---|
127 | /** Session interface */
|
---|
128 | iface_t iface;
|
---|
129 |
|
---|
130 | /** Exchange management style */
|
---|
131 | exch_mgmt_t mgmt;
|
---|
132 |
|
---|
133 | /** Session identification */
|
---|
134 | int phone;
|
---|
135 |
|
---|
136 | /** First clone connection argument */
|
---|
137 | sysarg_t arg1;
|
---|
138 |
|
---|
139 | /** Second clone connection argument */
|
---|
140 | sysarg_t arg2;
|
---|
141 |
|
---|
142 | /** Third clone connection argument */
|
---|
143 | sysarg_t arg3;
|
---|
144 |
|
---|
145 | /** Exchange mutex */
|
---|
146 | fibril_mutex_t mutex;
|
---|
147 |
|
---|
148 | /** Number of opened exchanges */
|
---|
149 | atomic_t refcnt;
|
---|
150 |
|
---|
151 | /** Mutex for stateful connections */
|
---|
152 | fibril_mutex_t remote_state_mtx;
|
---|
153 |
|
---|
154 | /** Data for stateful connections */
|
---|
155 | void *remote_state_data;
|
---|
156 | };
|
---|
157 |
|
---|
158 | /** Exchange data */
|
---|
159 | struct async_exch {
|
---|
160 | /** Link into list of inactive exchanges */
|
---|
161 | link_t sess_link;
|
---|
162 |
|
---|
163 | /** Link into global list of inactive exchanges */
|
---|
164 | link_t global_link;
|
---|
165 |
|
---|
166 | /** Session pointer */
|
---|
167 | async_sess_t *sess;
|
---|
168 |
|
---|
169 | /** Exchange identification */
|
---|
170 | int phone;
|
---|
171 | };
|
---|
172 |
|
---|
173 | /** Async framework global futex */
|
---|
174 | futex_t async_futex = FUTEX_INITIALIZER;
|
---|
175 |
|
---|
176 | /** Number of threads waiting for IPC in the kernel. */
|
---|
177 | atomic_t threads_in_ipc_wait = { 0 };
|
---|
178 |
|
---|
179 | /** Naming service session */
|
---|
180 | async_sess_t *session_ns;
|
---|
181 |
|
---|
182 | /** Call data */
|
---|
183 | typedef struct {
|
---|
184 | link_t link;
|
---|
185 |
|
---|
186 | ipc_callid_t callid;
|
---|
187 | ipc_call_t call;
|
---|
188 | } msg_t;
|
---|
189 |
|
---|
190 | /** Message data */
|
---|
191 | typedef struct {
|
---|
192 | awaiter_t wdata;
|
---|
193 |
|
---|
194 | /** If reply was received. */
|
---|
195 | bool done;
|
---|
196 |
|
---|
197 | /** If the message / reply should be discarded on arrival. */
|
---|
198 | bool forget;
|
---|
199 |
|
---|
200 | /** If already destroyed. */
|
---|
201 | bool destroyed;
|
---|
202 |
|
---|
203 | /** Pointer to where the answer data is stored. */
|
---|
204 | ipc_call_t *dataptr;
|
---|
205 |
|
---|
206 | sysarg_t retval;
|
---|
207 | } amsg_t;
|
---|
208 |
|
---|
209 | /* Client connection data */
|
---|
210 | typedef struct {
|
---|
211 | ht_link_t link;
|
---|
212 |
|
---|
213 | task_id_t in_task_id;
|
---|
214 | atomic_t refcnt;
|
---|
215 | void *data;
|
---|
216 | } client_t;
|
---|
217 |
|
---|
218 | /* Server connection data */
|
---|
219 | typedef struct {
|
---|
220 | awaiter_t wdata;
|
---|
221 |
|
---|
222 | /** Hash table link. */
|
---|
223 | ht_link_t link;
|
---|
224 |
|
---|
225 | /** Incoming client task ID. */
|
---|
226 | task_id_t in_task_id;
|
---|
227 |
|
---|
228 | /** Incoming phone hash. */
|
---|
229 | sysarg_t in_phone_hash;
|
---|
230 |
|
---|
231 | /** Link to the client tracking structure. */
|
---|
232 | client_t *client;
|
---|
233 |
|
---|
234 | /** Messages that should be delivered to this fibril. */
|
---|
235 | list_t msg_queue;
|
---|
236 |
|
---|
237 | /** Identification of the opening call. */
|
---|
238 | ipc_callid_t callid;
|
---|
239 |
|
---|
240 | /** Call data of the opening call. */
|
---|
241 | ipc_call_t call;
|
---|
242 |
|
---|
243 | /** Identification of the closing call. */
|
---|
244 | ipc_callid_t close_callid;
|
---|
245 |
|
---|
246 | /** Fibril function that will be used to handle the connection. */
|
---|
247 | async_port_handler_t handler;
|
---|
248 |
|
---|
249 | /** Client data */
|
---|
250 | void *data;
|
---|
251 | } connection_t;
|
---|
252 |
|
---|
253 | /** Interface data */
|
---|
254 | typedef struct {
|
---|
255 | ht_link_t link;
|
---|
256 |
|
---|
257 | /** Interface ID */
|
---|
258 | iface_t iface;
|
---|
259 |
|
---|
260 | /** Futex protecting the hash table */
|
---|
261 | futex_t futex;
|
---|
262 |
|
---|
263 | /** Interface ports */
|
---|
264 | hash_table_t port_hash_table;
|
---|
265 |
|
---|
266 | /** Next available port ID */
|
---|
267 | port_id_t port_id_avail;
|
---|
268 | } interface_t;
|
---|
269 |
|
---|
270 | /* Port data */
|
---|
271 | typedef struct {
|
---|
272 | ht_link_t link;
|
---|
273 |
|
---|
274 | /** Port ID */
|
---|
275 | port_id_t id;
|
---|
276 |
|
---|
277 | /** Port connection handler */
|
---|
278 | async_port_handler_t handler;
|
---|
279 |
|
---|
280 | /** Client data */
|
---|
281 | void *data;
|
---|
282 | } port_t;
|
---|
283 |
|
---|
284 | /* Notification data */
|
---|
285 | typedef struct {
|
---|
286 | ht_link_t link;
|
---|
287 |
|
---|
288 | /** Notification method */
|
---|
289 | sysarg_t imethod;
|
---|
290 |
|
---|
291 | /** Notification handler */
|
---|
292 | async_notification_handler_t handler;
|
---|
293 |
|
---|
294 | /** Notification data */
|
---|
295 | void *data;
|
---|
296 | } notification_t;
|
---|
297 |
|
---|
298 | /** Identifier of the incoming connection handled by the current fibril. */
|
---|
299 | static fibril_local connection_t *fibril_connection;
|
---|
300 |
|
---|
301 | static void to_event_initialize(to_event_t *to)
|
---|
302 | {
|
---|
303 | struct timeval tv = { 0, 0 };
|
---|
304 |
|
---|
305 | to->inlist = false;
|
---|
306 | to->occurred = false;
|
---|
307 | link_initialize(&to->link);
|
---|
308 | to->expires = tv;
|
---|
309 | }
|
---|
310 |
|
---|
311 | static void wu_event_initialize(wu_event_t *wu)
|
---|
312 | {
|
---|
313 | wu->inlist = false;
|
---|
314 | link_initialize(&wu->link);
|
---|
315 | }
|
---|
316 |
|
---|
317 | void awaiter_initialize(awaiter_t *aw)
|
---|
318 | {
|
---|
319 | aw->fid = 0;
|
---|
320 | aw->active = false;
|
---|
321 | to_event_initialize(&aw->to_event);
|
---|
322 | wu_event_initialize(&aw->wu_event);
|
---|
323 | }
|
---|
324 |
|
---|
325 | static amsg_t *amsg_create(void)
|
---|
326 | {
|
---|
327 | amsg_t *msg = malloc(sizeof(amsg_t));
|
---|
328 | if (msg) {
|
---|
329 | msg->done = false;
|
---|
330 | msg->forget = false;
|
---|
331 | msg->destroyed = false;
|
---|
332 | msg->dataptr = NULL;
|
---|
333 | msg->retval = (sysarg_t) EINVAL;
|
---|
334 | awaiter_initialize(&msg->wdata);
|
---|
335 | }
|
---|
336 |
|
---|
337 | return msg;
|
---|
338 | }
|
---|
339 |
|
---|
340 | static void amsg_destroy(amsg_t *msg)
|
---|
341 | {
|
---|
342 | assert(!msg->destroyed);
|
---|
343 | msg->destroyed = true;
|
---|
344 | free(msg);
|
---|
345 | }
|
---|
346 |
|
---|
347 | static void *default_client_data_constructor(void)
|
---|
348 | {
|
---|
349 | return NULL;
|
---|
350 | }
|
---|
351 |
|
---|
352 | static void default_client_data_destructor(void *data)
|
---|
353 | {
|
---|
354 | }
|
---|
355 |
|
---|
356 | static async_client_data_ctor_t async_client_data_create =
|
---|
357 | default_client_data_constructor;
|
---|
358 | static async_client_data_dtor_t async_client_data_destroy =
|
---|
359 | default_client_data_destructor;
|
---|
360 |
|
---|
361 | void async_set_client_data_constructor(async_client_data_ctor_t ctor)
|
---|
362 | {
|
---|
363 | assert(async_client_data_create == default_client_data_constructor);
|
---|
364 | async_client_data_create = ctor;
|
---|
365 | }
|
---|
366 |
|
---|
367 | void async_set_client_data_destructor(async_client_data_dtor_t dtor)
|
---|
368 | {
|
---|
369 | assert(async_client_data_destroy == default_client_data_destructor);
|
---|
370 | async_client_data_destroy = dtor;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /** Default fallback fibril function.
|
---|
374 | *
|
---|
375 | * This fallback fibril function gets called on incomming
|
---|
376 | * connections that do not have a specific handler defined.
|
---|
377 | *
|
---|
378 | * @param callid Hash of the incoming call.
|
---|
379 | * @param call Data of the incoming call.
|
---|
380 | * @param arg Local argument
|
---|
381 | *
|
---|
382 | */
|
---|
383 | static void default_fallback_port_handler(ipc_callid_t callid, ipc_call_t *call,
|
---|
384 | void *arg)
|
---|
385 | {
|
---|
386 | ipc_answer_0(callid, ENOENT);
|
---|
387 | }
|
---|
388 |
|
---|
389 | static async_port_handler_t fallback_port_handler =
|
---|
390 | default_fallback_port_handler;
|
---|
391 | static void *fallback_port_data = NULL;
|
---|
392 |
|
---|
393 | static hash_table_t interface_hash_table;
|
---|
394 |
|
---|
395 | static size_t interface_key_hash(void *key)
|
---|
396 | {
|
---|
397 | iface_t iface = *(iface_t *) key;
|
---|
398 | return iface;
|
---|
399 | }
|
---|
400 |
|
---|
401 | static size_t interface_hash(const ht_link_t *item)
|
---|
402 | {
|
---|
403 | interface_t *interface = hash_table_get_inst(item, interface_t, link);
|
---|
404 | return interface_key_hash(&interface->iface);
|
---|
405 | }
|
---|
406 |
|
---|
407 | static bool interface_key_equal(void *key, const ht_link_t *item)
|
---|
408 | {
|
---|
409 | iface_t iface = *(iface_t *) key;
|
---|
410 | interface_t *interface = hash_table_get_inst(item, interface_t, link);
|
---|
411 | return iface == interface->iface;
|
---|
412 | }
|
---|
413 |
|
---|
414 | /** Operations for the port hash table. */
|
---|
415 | static hash_table_ops_t interface_hash_table_ops = {
|
---|
416 | .hash = interface_hash,
|
---|
417 | .key_hash = interface_key_hash,
|
---|
418 | .key_equal = interface_key_equal,
|
---|
419 | .equal = NULL,
|
---|
420 | .remove_callback = NULL
|
---|
421 | };
|
---|
422 |
|
---|
423 | static size_t port_key_hash(void *key)
|
---|
424 | {
|
---|
425 | port_id_t port_id = *(port_id_t *) key;
|
---|
426 | return port_id;
|
---|
427 | }
|
---|
428 |
|
---|
429 | static size_t port_hash(const ht_link_t *item)
|
---|
430 | {
|
---|
431 | port_t *port = hash_table_get_inst(item, port_t, link);
|
---|
432 | return port_key_hash(&port->id);
|
---|
433 | }
|
---|
434 |
|
---|
435 | static bool port_key_equal(void *key, const ht_link_t *item)
|
---|
436 | {
|
---|
437 | port_id_t port_id = *(port_id_t *) key;
|
---|
438 | port_t *port = hash_table_get_inst(item, port_t, link);
|
---|
439 | return port_id == port->id;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /** Operations for the port hash table. */
|
---|
443 | static hash_table_ops_t port_hash_table_ops = {
|
---|
444 | .hash = port_hash,
|
---|
445 | .key_hash = port_key_hash,
|
---|
446 | .key_equal = port_key_equal,
|
---|
447 | .equal = NULL,
|
---|
448 | .remove_callback = NULL
|
---|
449 | };
|
---|
450 |
|
---|
451 | static interface_t *async_new_interface(iface_t iface)
|
---|
452 | {
|
---|
453 | interface_t *interface =
|
---|
454 | (interface_t *) malloc(sizeof(interface_t));
|
---|
455 | if (!interface)
|
---|
456 | return NULL;
|
---|
457 |
|
---|
458 | bool ret = hash_table_create(&interface->port_hash_table, 0, 0,
|
---|
459 | &port_hash_table_ops);
|
---|
460 | if (!ret) {
|
---|
461 | free(interface);
|
---|
462 | return NULL;
|
---|
463 | }
|
---|
464 |
|
---|
465 | interface->iface = iface;
|
---|
466 | futex_initialize(&interface->futex, 1);
|
---|
467 | interface->port_id_avail = 0;
|
---|
468 |
|
---|
469 | hash_table_insert(&interface_hash_table, &interface->link);
|
---|
470 |
|
---|
471 | return interface;
|
---|
472 | }
|
---|
473 |
|
---|
474 | static port_t *async_new_port(interface_t *interface,
|
---|
475 | async_port_handler_t handler, void *data)
|
---|
476 | {
|
---|
477 | port_t *port = (port_t *) malloc(sizeof(port_t));
|
---|
478 | if (!port)
|
---|
479 | return NULL;
|
---|
480 |
|
---|
481 | futex_down(&interface->futex);
|
---|
482 |
|
---|
483 | port_id_t id = interface->port_id_avail;
|
---|
484 | interface->port_id_avail++;
|
---|
485 |
|
---|
486 | port->id = id;
|
---|
487 | port->handler = handler;
|
---|
488 | port->data = data;
|
---|
489 |
|
---|
490 | hash_table_insert(&interface->port_hash_table, &port->link);
|
---|
491 |
|
---|
492 | futex_up(&interface->futex);
|
---|
493 |
|
---|
494 | return port;
|
---|
495 | }
|
---|
496 |
|
---|
497 | /** Mutex protecting inactive_exch_list and avail_phone_cv.
|
---|
498 | *
|
---|
499 | */
|
---|
500 | static FIBRIL_MUTEX_INITIALIZE(async_sess_mutex);
|
---|
501 |
|
---|
502 | /** List of all currently inactive exchanges.
|
---|
503 | *
|
---|
504 | */
|
---|
505 | static LIST_INITIALIZE(inactive_exch_list);
|
---|
506 |
|
---|
507 | /** Condition variable to wait for a phone to become available.
|
---|
508 | *
|
---|
509 | */
|
---|
510 | static FIBRIL_CONDVAR_INITIALIZE(avail_phone_cv);
|
---|
511 |
|
---|
512 | int async_create_port(iface_t iface, async_port_handler_t handler,
|
---|
513 | void *data, port_id_t *port_id)
|
---|
514 | {
|
---|
515 | if ((iface & IFACE_MOD_MASK) == IFACE_MOD_CALLBACK)
|
---|
516 | return EINVAL;
|
---|
517 |
|
---|
518 | interface_t *interface;
|
---|
519 |
|
---|
520 | futex_down(&async_futex);
|
---|
521 |
|
---|
522 | ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
|
---|
523 | if (link)
|
---|
524 | interface = hash_table_get_inst(link, interface_t, link);
|
---|
525 | else
|
---|
526 | interface = async_new_interface(iface);
|
---|
527 |
|
---|
528 | if (!interface) {
|
---|
529 | futex_up(&async_futex);
|
---|
530 | return ENOMEM;
|
---|
531 | }
|
---|
532 |
|
---|
533 | port_t *port = async_new_port(interface, handler, data);
|
---|
534 | if (!port) {
|
---|
535 | futex_up(&async_futex);
|
---|
536 | return ENOMEM;
|
---|
537 | }
|
---|
538 |
|
---|
539 | *port_id = port->id;
|
---|
540 |
|
---|
541 | futex_up(&async_futex);
|
---|
542 |
|
---|
543 | return EOK;
|
---|
544 | }
|
---|
545 |
|
---|
546 | void async_set_fallback_port_handler(async_port_handler_t handler, void *data)
|
---|
547 | {
|
---|
548 | assert(handler != NULL);
|
---|
549 |
|
---|
550 | fallback_port_handler = handler;
|
---|
551 | fallback_port_data = data;
|
---|
552 | }
|
---|
553 |
|
---|
554 | static hash_table_t client_hash_table;
|
---|
555 | static hash_table_t conn_hash_table;
|
---|
556 | static hash_table_t notification_hash_table;
|
---|
557 | static LIST_INITIALIZE(timeout_list);
|
---|
558 |
|
---|
559 | static sysarg_t notification_avail = 0;
|
---|
560 |
|
---|
561 | static size_t client_key_hash(void *key)
|
---|
562 | {
|
---|
563 | task_id_t in_task_id = *(task_id_t *) key;
|
---|
564 | return in_task_id;
|
---|
565 | }
|
---|
566 |
|
---|
567 | static size_t client_hash(const ht_link_t *item)
|
---|
568 | {
|
---|
569 | client_t *client = hash_table_get_inst(item, client_t, link);
|
---|
570 | return client_key_hash(&client->in_task_id);
|
---|
571 | }
|
---|
572 |
|
---|
573 | static bool client_key_equal(void *key, const ht_link_t *item)
|
---|
574 | {
|
---|
575 | task_id_t in_task_id = *(task_id_t *) key;
|
---|
576 | client_t *client = hash_table_get_inst(item, client_t, link);
|
---|
577 | return in_task_id == client->in_task_id;
|
---|
578 | }
|
---|
579 |
|
---|
580 | /** Operations for the client hash table. */
|
---|
581 | static hash_table_ops_t client_hash_table_ops = {
|
---|
582 | .hash = client_hash,
|
---|
583 | .key_hash = client_key_hash,
|
---|
584 | .key_equal = client_key_equal,
|
---|
585 | .equal = NULL,
|
---|
586 | .remove_callback = NULL
|
---|
587 | };
|
---|
588 |
|
---|
589 | /** Compute hash into the connection hash table based on the source phone hash.
|
---|
590 | *
|
---|
591 | * @param key Pointer to source phone hash.
|
---|
592 | *
|
---|
593 | * @return Index into the connection hash table.
|
---|
594 | *
|
---|
595 | */
|
---|
596 | static size_t conn_key_hash(void *key)
|
---|
597 | {
|
---|
598 | sysarg_t in_phone_hash = *(sysarg_t *) key;
|
---|
599 | return in_phone_hash;
|
---|
600 | }
|
---|
601 |
|
---|
602 | static size_t conn_hash(const ht_link_t *item)
|
---|
603 | {
|
---|
604 | connection_t *conn = hash_table_get_inst(item, connection_t, link);
|
---|
605 | return conn_key_hash(&conn->in_phone_hash);
|
---|
606 | }
|
---|
607 |
|
---|
608 | static bool conn_key_equal(void *key, const ht_link_t *item)
|
---|
609 | {
|
---|
610 | sysarg_t in_phone_hash = *(sysarg_t *) key;
|
---|
611 | connection_t *conn = hash_table_get_inst(item, connection_t, link);
|
---|
612 | return (in_phone_hash == conn->in_phone_hash);
|
---|
613 | }
|
---|
614 |
|
---|
615 | /** Operations for the connection hash table. */
|
---|
616 | static hash_table_ops_t conn_hash_table_ops = {
|
---|
617 | .hash = conn_hash,
|
---|
618 | .key_hash = conn_key_hash,
|
---|
619 | .key_equal = conn_key_equal,
|
---|
620 | .equal = NULL,
|
---|
621 | .remove_callback = NULL
|
---|
622 | };
|
---|
623 |
|
---|
624 | static client_t *async_client_get(task_id_t client_id, bool create)
|
---|
625 | {
|
---|
626 | client_t *client = NULL;
|
---|
627 |
|
---|
628 | futex_down(&async_futex);
|
---|
629 | ht_link_t *link = hash_table_find(&client_hash_table, &client_id);
|
---|
630 | if (link) {
|
---|
631 | client = hash_table_get_inst(link, client_t, link);
|
---|
632 | atomic_inc(&client->refcnt);
|
---|
633 | } else if (create) {
|
---|
634 | client = malloc(sizeof(client_t));
|
---|
635 | if (client) {
|
---|
636 | client->in_task_id = client_id;
|
---|
637 | client->data = async_client_data_create();
|
---|
638 |
|
---|
639 | atomic_set(&client->refcnt, 1);
|
---|
640 | hash_table_insert(&client_hash_table, &client->link);
|
---|
641 | }
|
---|
642 | }
|
---|
643 |
|
---|
644 | futex_up(&async_futex);
|
---|
645 | return client;
|
---|
646 | }
|
---|
647 |
|
---|
648 | static void async_client_put(client_t *client)
|
---|
649 | {
|
---|
650 | bool destroy;
|
---|
651 |
|
---|
652 | futex_down(&async_futex);
|
---|
653 |
|
---|
654 | if (atomic_predec(&client->refcnt) == 0) {
|
---|
655 | hash_table_remove(&client_hash_table, &client->in_task_id);
|
---|
656 | destroy = true;
|
---|
657 | } else
|
---|
658 | destroy = false;
|
---|
659 |
|
---|
660 | futex_up(&async_futex);
|
---|
661 |
|
---|
662 | if (destroy) {
|
---|
663 | if (client->data)
|
---|
664 | async_client_data_destroy(client->data);
|
---|
665 |
|
---|
666 | free(client);
|
---|
667 | }
|
---|
668 | }
|
---|
669 |
|
---|
670 | /** Wrapper for client connection fibril.
|
---|
671 | *
|
---|
672 | * When a new connection arrives, a fibril with this implementing
|
---|
673 | * function is created.
|
---|
674 | *
|
---|
675 | * @param arg Connection structure pointer.
|
---|
676 | *
|
---|
677 | * @return Always zero.
|
---|
678 | *
|
---|
679 | */
|
---|
680 | static int connection_fibril(void *arg)
|
---|
681 | {
|
---|
682 | assert(arg);
|
---|
683 |
|
---|
684 | /*
|
---|
685 | * Setup fibril-local connection pointer.
|
---|
686 | */
|
---|
687 | fibril_connection = (connection_t *) arg;
|
---|
688 |
|
---|
689 | /*
|
---|
690 | * Add our reference for the current connection in the client task
|
---|
691 | * tracking structure. If this is the first reference, create and
|
---|
692 | * hash in a new tracking structure.
|
---|
693 | */
|
---|
694 |
|
---|
695 | client_t *client = async_client_get(fibril_connection->in_task_id, true);
|
---|
696 | if (!client) {
|
---|
697 | ipc_answer_0(fibril_connection->callid, ENOMEM);
|
---|
698 | return 0;
|
---|
699 | }
|
---|
700 |
|
---|
701 | fibril_connection->client = client;
|
---|
702 |
|
---|
703 | /*
|
---|
704 | * Call the connection handler function.
|
---|
705 | */
|
---|
706 | fibril_connection->handler(fibril_connection->callid,
|
---|
707 | &fibril_connection->call, fibril_connection->data);
|
---|
708 |
|
---|
709 | /*
|
---|
710 | * Remove the reference for this client task connection.
|
---|
711 | */
|
---|
712 | async_client_put(client);
|
---|
713 |
|
---|
714 | /*
|
---|
715 | * Remove myself from the connection hash table.
|
---|
716 | */
|
---|
717 | futex_down(&async_futex);
|
---|
718 | hash_table_remove(&conn_hash_table, &fibril_connection->in_phone_hash);
|
---|
719 | futex_up(&async_futex);
|
---|
720 |
|
---|
721 | /*
|
---|
722 | * Answer all remaining messages with EHANGUP.
|
---|
723 | */
|
---|
724 | while (!list_empty(&fibril_connection->msg_queue)) {
|
---|
725 | msg_t *msg =
|
---|
726 | list_get_instance(list_first(&fibril_connection->msg_queue),
|
---|
727 | msg_t, link);
|
---|
728 |
|
---|
729 | list_remove(&msg->link);
|
---|
730 | ipc_answer_0(msg->callid, EHANGUP);
|
---|
731 | free(msg);
|
---|
732 | }
|
---|
733 |
|
---|
734 | /*
|
---|
735 | * If the connection was hung-up, answer the last call,
|
---|
736 | * i.e. IPC_M_PHONE_HUNGUP.
|
---|
737 | */
|
---|
738 | if (fibril_connection->close_callid)
|
---|
739 | ipc_answer_0(fibril_connection->close_callid, EOK);
|
---|
740 |
|
---|
741 | free(fibril_connection);
|
---|
742 | return 0;
|
---|
743 | }
|
---|
744 |
|
---|
745 | /** Create a new fibril for a new connection.
|
---|
746 | *
|
---|
747 | * Create new fibril for connection, fill in connection structures
|
---|
748 | * and insert it into the hash table, so that later we can easily
|
---|
749 | * do routing of messages to particular fibrils.
|
---|
750 | *
|
---|
751 | * @param in_task_id Identification of the incoming connection.
|
---|
752 | * @param in_phone_hash Identification of the incoming connection.
|
---|
753 | * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
|
---|
754 | * If callid is zero, the connection was opened by
|
---|
755 | * accepting the IPC_M_CONNECT_TO_ME call and this
|
---|
756 | * function is called directly by the server.
|
---|
757 | * @param call Call data of the opening call.
|
---|
758 | * @param handler Connection handler.
|
---|
759 | * @param data Client argument to pass to the connection handler.
|
---|
760 | *
|
---|
761 | * @return New fibril id or NULL on failure.
|
---|
762 | *
|
---|
763 | */
|
---|
764 | static fid_t async_new_connection(task_id_t in_task_id, sysarg_t in_phone_hash,
|
---|
765 | ipc_callid_t callid, ipc_call_t *call, async_port_handler_t handler,
|
---|
766 | void *data)
|
---|
767 | {
|
---|
768 | connection_t *conn = malloc(sizeof(*conn));
|
---|
769 | if (!conn) {
|
---|
770 | if (callid)
|
---|
771 | ipc_answer_0(callid, ENOMEM);
|
---|
772 |
|
---|
773 | return (uintptr_t) NULL;
|
---|
774 | }
|
---|
775 |
|
---|
776 | conn->in_task_id = in_task_id;
|
---|
777 | conn->in_phone_hash = in_phone_hash;
|
---|
778 | list_initialize(&conn->msg_queue);
|
---|
779 | conn->callid = callid;
|
---|
780 | conn->close_callid = 0;
|
---|
781 | conn->handler = handler;
|
---|
782 | conn->data = data;
|
---|
783 |
|
---|
784 | if (call)
|
---|
785 | conn->call = *call;
|
---|
786 |
|
---|
787 | /* We will activate the fibril ASAP */
|
---|
788 | conn->wdata.active = true;
|
---|
789 | conn->wdata.fid = fibril_create(connection_fibril, conn);
|
---|
790 |
|
---|
791 | if (conn->wdata.fid == 0) {
|
---|
792 | free(conn);
|
---|
793 |
|
---|
794 | if (callid)
|
---|
795 | ipc_answer_0(callid, ENOMEM);
|
---|
796 |
|
---|
797 | return (uintptr_t) NULL;
|
---|
798 | }
|
---|
799 |
|
---|
800 | /* Add connection to the connection hash table */
|
---|
801 |
|
---|
802 | futex_down(&async_futex);
|
---|
803 | hash_table_insert(&conn_hash_table, &conn->link);
|
---|
804 | futex_up(&async_futex);
|
---|
805 |
|
---|
806 | fibril_add_ready(conn->wdata.fid);
|
---|
807 |
|
---|
808 | return conn->wdata.fid;
|
---|
809 | }
|
---|
810 |
|
---|
811 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
812 | *
|
---|
813 | * Ask through phone for a new connection to some service.
|
---|
814 | *
|
---|
815 | * @param exch Exchange for sending the message.
|
---|
816 | * @param iface Callback interface.
|
---|
817 | * @param arg1 User defined argument.
|
---|
818 | * @param arg2 User defined argument.
|
---|
819 | * @param handler Callback handler.
|
---|
820 | * @param data Handler data.
|
---|
821 | * @param port_id ID of the newly created port.
|
---|
822 | *
|
---|
823 | * @return Zero on success or a negative error code.
|
---|
824 | *
|
---|
825 | */
|
---|
826 | int async_create_callback_port(async_exch_t *exch, iface_t iface, sysarg_t arg1,
|
---|
827 | sysarg_t arg2, async_port_handler_t handler, void *data, port_id_t *port_id)
|
---|
828 | {
|
---|
829 | if ((iface & IFACE_MOD_CALLBACK) != IFACE_MOD_CALLBACK)
|
---|
830 | return EINVAL;
|
---|
831 |
|
---|
832 | if (exch == NULL)
|
---|
833 | return ENOENT;
|
---|
834 |
|
---|
835 | ipc_call_t answer;
|
---|
836 | aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, iface, arg1, arg2,
|
---|
837 | &answer);
|
---|
838 |
|
---|
839 | sysarg_t ret;
|
---|
840 | async_wait_for(req, &ret);
|
---|
841 | if (ret != EOK)
|
---|
842 | return (int) ret;
|
---|
843 |
|
---|
844 | sysarg_t phone_hash = IPC_GET_ARG5(answer);
|
---|
845 | interface_t *interface;
|
---|
846 |
|
---|
847 | futex_down(&async_futex);
|
---|
848 |
|
---|
849 | ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
|
---|
850 | if (link)
|
---|
851 | interface = hash_table_get_inst(link, interface_t, link);
|
---|
852 | else
|
---|
853 | interface = async_new_interface(iface);
|
---|
854 |
|
---|
855 | if (!interface) {
|
---|
856 | futex_up(&async_futex);
|
---|
857 | return ENOMEM;
|
---|
858 | }
|
---|
859 |
|
---|
860 | port_t *port = async_new_port(interface, handler, data);
|
---|
861 | if (!port) {
|
---|
862 | futex_up(&async_futex);
|
---|
863 | return ENOMEM;
|
---|
864 | }
|
---|
865 |
|
---|
866 | *port_id = port->id;
|
---|
867 |
|
---|
868 | futex_up(&async_futex);
|
---|
869 |
|
---|
870 | fid_t fid = async_new_connection(answer.in_task_id, phone_hash,
|
---|
871 | 0, NULL, handler, data);
|
---|
872 | if (fid == (uintptr_t) NULL)
|
---|
873 | return ENOMEM;
|
---|
874 |
|
---|
875 | return EOK;
|
---|
876 | }
|
---|
877 |
|
---|
878 | static size_t notification_key_hash(void *key)
|
---|
879 | {
|
---|
880 | sysarg_t id = *(sysarg_t *) key;
|
---|
881 | return id;
|
---|
882 | }
|
---|
883 |
|
---|
884 | static size_t notification_hash(const ht_link_t *item)
|
---|
885 | {
|
---|
886 | notification_t *notification =
|
---|
887 | hash_table_get_inst(item, notification_t, link);
|
---|
888 | return notification_key_hash(¬ification->imethod);
|
---|
889 | }
|
---|
890 |
|
---|
891 | static bool notification_key_equal(void *key, const ht_link_t *item)
|
---|
892 | {
|
---|
893 | sysarg_t id = *(sysarg_t *) key;
|
---|
894 | notification_t *notification =
|
---|
895 | hash_table_get_inst(item, notification_t, link);
|
---|
896 | return id == notification->imethod;
|
---|
897 | }
|
---|
898 |
|
---|
899 | /** Operations for the notification hash table. */
|
---|
900 | static hash_table_ops_t notification_hash_table_ops = {
|
---|
901 | .hash = notification_hash,
|
---|
902 | .key_hash = notification_key_hash,
|
---|
903 | .key_equal = notification_key_equal,
|
---|
904 | .equal = NULL,
|
---|
905 | .remove_callback = NULL
|
---|
906 | };
|
---|
907 |
|
---|
908 | /** Sort in current fibril's timeout request.
|
---|
909 | *
|
---|
910 | * @param wd Wait data of the current fibril.
|
---|
911 | *
|
---|
912 | */
|
---|
913 | void async_insert_timeout(awaiter_t *wd)
|
---|
914 | {
|
---|
915 | assert(wd);
|
---|
916 |
|
---|
917 | wd->to_event.occurred = false;
|
---|
918 | wd->to_event.inlist = true;
|
---|
919 |
|
---|
920 | link_t *tmp = timeout_list.head.next;
|
---|
921 | while (tmp != &timeout_list.head) {
|
---|
922 | awaiter_t *cur
|
---|
923 | = list_get_instance(tmp, awaiter_t, to_event.link);
|
---|
924 |
|
---|
925 | if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
|
---|
926 | break;
|
---|
927 |
|
---|
928 | tmp = tmp->next;
|
---|
929 | }
|
---|
930 |
|
---|
931 | list_insert_before(&wd->to_event.link, tmp);
|
---|
932 | }
|
---|
933 |
|
---|
934 | /** Try to route a call to an appropriate connection fibril.
|
---|
935 | *
|
---|
936 | * If the proper connection fibril is found, a message with the call is added to
|
---|
937 | * its message queue. If the fibril was not active, it is activated and all
|
---|
938 | * timeouts are unregistered.
|
---|
939 | *
|
---|
940 | * @param callid Hash of the incoming call.
|
---|
941 | * @param call Data of the incoming call.
|
---|
942 | *
|
---|
943 | * @return False if the call doesn't match any connection.
|
---|
944 | * @return True if the call was passed to the respective connection fibril.
|
---|
945 | *
|
---|
946 | */
|
---|
947 | static bool route_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
948 | {
|
---|
949 | assert(call);
|
---|
950 |
|
---|
951 | futex_down(&async_futex);
|
---|
952 |
|
---|
953 | ht_link_t *link = hash_table_find(&conn_hash_table, &call->in_phone_hash);
|
---|
954 | if (!link) {
|
---|
955 | futex_up(&async_futex);
|
---|
956 | return false;
|
---|
957 | }
|
---|
958 |
|
---|
959 | connection_t *conn = hash_table_get_inst(link, connection_t, link);
|
---|
960 |
|
---|
961 | msg_t *msg = malloc(sizeof(*msg));
|
---|
962 | if (!msg) {
|
---|
963 | futex_up(&async_futex);
|
---|
964 | return false;
|
---|
965 | }
|
---|
966 |
|
---|
967 | msg->callid = callid;
|
---|
968 | msg->call = *call;
|
---|
969 | list_append(&msg->link, &conn->msg_queue);
|
---|
970 |
|
---|
971 | if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
|
---|
972 | conn->close_callid = callid;
|
---|
973 |
|
---|
974 | /* If the connection fibril is waiting for an event, activate it */
|
---|
975 | if (!conn->wdata.active) {
|
---|
976 |
|
---|
977 | /* If in timeout list, remove it */
|
---|
978 | if (conn->wdata.to_event.inlist) {
|
---|
979 | conn->wdata.to_event.inlist = false;
|
---|
980 | list_remove(&conn->wdata.to_event.link);
|
---|
981 | }
|
---|
982 |
|
---|
983 | conn->wdata.active = true;
|
---|
984 | fibril_add_ready(conn->wdata.fid);
|
---|
985 | }
|
---|
986 |
|
---|
987 | futex_up(&async_futex);
|
---|
988 | return true;
|
---|
989 | }
|
---|
990 |
|
---|
991 | /** Process notification.
|
---|
992 | *
|
---|
993 | * @param callid Hash of the incoming call.
|
---|
994 | * @param call Data of the incoming call.
|
---|
995 | *
|
---|
996 | */
|
---|
997 | static void process_notification(ipc_callid_t callid, ipc_call_t *call)
|
---|
998 | {
|
---|
999 | async_notification_handler_t handler = NULL;
|
---|
1000 | void *data = NULL;
|
---|
1001 |
|
---|
1002 | assert(call);
|
---|
1003 |
|
---|
1004 | futex_down(&async_futex);
|
---|
1005 |
|
---|
1006 | ht_link_t *link = hash_table_find(¬ification_hash_table,
|
---|
1007 | &IPC_GET_IMETHOD(*call));
|
---|
1008 | if (link) {
|
---|
1009 | notification_t *notification =
|
---|
1010 | hash_table_get_inst(link, notification_t, link);
|
---|
1011 | handler = notification->handler;
|
---|
1012 | data = notification->data;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | futex_up(&async_futex);
|
---|
1016 |
|
---|
1017 | if (handler)
|
---|
1018 | handler(callid, call, data);
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | /** Subscribe to IRQ notification.
|
---|
1022 | *
|
---|
1023 | * @param inr IRQ number.
|
---|
1024 | * @param devno Device number of the device generating inr.
|
---|
1025 | * @param handler Notification handler.
|
---|
1026 | * @param data Notification handler client data.
|
---|
1027 | * @param ucode Top-half pseudocode handler.
|
---|
1028 | *
|
---|
1029 | * @return Zero on success or a negative error code.
|
---|
1030 | *
|
---|
1031 | */
|
---|
1032 | int async_irq_subscribe(int inr, int devno,
|
---|
1033 | async_notification_handler_t handler, void *data, const irq_code_t *ucode)
|
---|
1034 | {
|
---|
1035 | notification_t *notification =
|
---|
1036 | (notification_t *) malloc(sizeof(notification_t));
|
---|
1037 | if (!notification)
|
---|
1038 | return ENOMEM;
|
---|
1039 |
|
---|
1040 | futex_down(&async_futex);
|
---|
1041 |
|
---|
1042 | sysarg_t imethod = notification_avail;
|
---|
1043 | notification_avail++;
|
---|
1044 |
|
---|
1045 | notification->imethod = imethod;
|
---|
1046 | notification->handler = handler;
|
---|
1047 | notification->data = data;
|
---|
1048 |
|
---|
1049 | hash_table_insert(¬ification_hash_table, ¬ification->link);
|
---|
1050 |
|
---|
1051 | futex_up(&async_futex);
|
---|
1052 |
|
---|
1053 | return ipc_irq_subscribe(inr, devno, imethod, ucode);
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | /** Unsubscribe from IRQ notification.
|
---|
1057 | *
|
---|
1058 | * @param inr IRQ number.
|
---|
1059 | * @param devno Device number of the device generating inr.
|
---|
1060 | *
|
---|
1061 | * @return Zero on success or a negative error code.
|
---|
1062 | *
|
---|
1063 | */
|
---|
1064 | int async_irq_unsubscribe(int inr, int devno)
|
---|
1065 | {
|
---|
1066 | // TODO: Remove entry from hash table
|
---|
1067 | // to avoid memory leak
|
---|
1068 |
|
---|
1069 | return ipc_irq_unsubscribe(inr, devno);
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | /** Subscribe to event notifications.
|
---|
1073 | *
|
---|
1074 | * @param evno Event type to subscribe.
|
---|
1075 | * @param handler Notification handler.
|
---|
1076 | * @param data Notification handler client data.
|
---|
1077 | *
|
---|
1078 | * @return Zero on success or a negative error code.
|
---|
1079 | *
|
---|
1080 | */
|
---|
1081 | int async_event_subscribe(event_type_t evno,
|
---|
1082 | async_notification_handler_t handler, void *data)
|
---|
1083 | {
|
---|
1084 | notification_t *notification =
|
---|
1085 | (notification_t *) malloc(sizeof(notification_t));
|
---|
1086 | if (!notification)
|
---|
1087 | return ENOMEM;
|
---|
1088 |
|
---|
1089 | futex_down(&async_futex);
|
---|
1090 |
|
---|
1091 | sysarg_t imethod = notification_avail;
|
---|
1092 | notification_avail++;
|
---|
1093 |
|
---|
1094 | notification->imethod = imethod;
|
---|
1095 | notification->handler = handler;
|
---|
1096 | notification->data = data;
|
---|
1097 |
|
---|
1098 | hash_table_insert(¬ification_hash_table, ¬ification->link);
|
---|
1099 |
|
---|
1100 | futex_up(&async_futex);
|
---|
1101 |
|
---|
1102 | return ipc_event_subscribe(evno, imethod);
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | /** Subscribe to task event notifications.
|
---|
1106 | *
|
---|
1107 | * @param evno Event type to subscribe.
|
---|
1108 | * @param handler Notification handler.
|
---|
1109 | * @param data Notification handler client data.
|
---|
1110 | *
|
---|
1111 | * @return Zero on success or a negative error code.
|
---|
1112 | *
|
---|
1113 | */
|
---|
1114 | int async_event_task_subscribe(event_task_type_t evno,
|
---|
1115 | async_notification_handler_t handler, void *data)
|
---|
1116 | {
|
---|
1117 | notification_t *notification =
|
---|
1118 | (notification_t *) malloc(sizeof(notification_t));
|
---|
1119 | if (!notification)
|
---|
1120 | return ENOMEM;
|
---|
1121 |
|
---|
1122 | futex_down(&async_futex);
|
---|
1123 |
|
---|
1124 | sysarg_t imethod = notification_avail;
|
---|
1125 | notification_avail++;
|
---|
1126 |
|
---|
1127 | notification->imethod = imethod;
|
---|
1128 | notification->handler = handler;
|
---|
1129 | notification->data = data;
|
---|
1130 |
|
---|
1131 | hash_table_insert(¬ification_hash_table, ¬ification->link);
|
---|
1132 |
|
---|
1133 | futex_up(&async_futex);
|
---|
1134 |
|
---|
1135 | return ipc_event_task_subscribe(evno, imethod);
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | /** Unmask event notifications.
|
---|
1139 | *
|
---|
1140 | * @param evno Event type to unmask.
|
---|
1141 | *
|
---|
1142 | * @return Value returned by the kernel.
|
---|
1143 | *
|
---|
1144 | */
|
---|
1145 | int async_event_unmask(event_type_t evno)
|
---|
1146 | {
|
---|
1147 | return ipc_event_unmask(evno);
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | /** Unmask task event notifications.
|
---|
1151 | *
|
---|
1152 | * @param evno Event type to unmask.
|
---|
1153 | *
|
---|
1154 | * @return Value returned by the kernel.
|
---|
1155 | *
|
---|
1156 | */
|
---|
1157 | int async_event_task_unmask(event_task_type_t evno)
|
---|
1158 | {
|
---|
1159 | return ipc_event_task_unmask(evno);
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | /** Return new incoming message for the current (fibril-local) connection.
|
---|
1163 | *
|
---|
1164 | * @param call Storage where the incoming call data will be stored.
|
---|
1165 | * @param usecs Timeout in microseconds. Zero denotes no timeout.
|
---|
1166 | *
|
---|
1167 | * @return If no timeout was specified, then a hash of the
|
---|
1168 | * incoming call is returned. If a timeout is specified,
|
---|
1169 | * then a hash of the incoming call is returned unless
|
---|
1170 | * the timeout expires prior to receiving a message. In
|
---|
1171 | * that case zero is returned.
|
---|
1172 | *
|
---|
1173 | */
|
---|
1174 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
|
---|
1175 | {
|
---|
1176 | assert(call);
|
---|
1177 | assert(fibril_connection);
|
---|
1178 |
|
---|
1179 | /* Why doing this?
|
---|
1180 | * GCC 4.1.0 coughs on fibril_connection-> dereference.
|
---|
1181 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
|
---|
1182 | * I would never expect to find so many errors in
|
---|
1183 | * a compiler.
|
---|
1184 | */
|
---|
1185 | connection_t *conn = fibril_connection;
|
---|
1186 |
|
---|
1187 | futex_down(&async_futex);
|
---|
1188 |
|
---|
1189 | if (usecs) {
|
---|
1190 | getuptime(&conn->wdata.to_event.expires);
|
---|
1191 | tv_add_diff(&conn->wdata.to_event.expires, usecs);
|
---|
1192 | } else
|
---|
1193 | conn->wdata.to_event.inlist = false;
|
---|
1194 |
|
---|
1195 | /* If nothing in queue, wait until something arrives */
|
---|
1196 | while (list_empty(&conn->msg_queue)) {
|
---|
1197 | if (conn->close_callid) {
|
---|
1198 | /*
|
---|
1199 | * Handle the case when the connection was already
|
---|
1200 | * closed by the client but the server did not notice
|
---|
1201 | * the first IPC_M_PHONE_HUNGUP call and continues to
|
---|
1202 | * call async_get_call_timeout(). Repeat
|
---|
1203 | * IPC_M_PHONE_HUNGUP until the caller notices.
|
---|
1204 | */
|
---|
1205 | memset(call, 0, sizeof(ipc_call_t));
|
---|
1206 | IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
|
---|
1207 | futex_up(&async_futex);
|
---|
1208 | return conn->close_callid;
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | if (usecs)
|
---|
1212 | async_insert_timeout(&conn->wdata);
|
---|
1213 |
|
---|
1214 | conn->wdata.active = false;
|
---|
1215 |
|
---|
1216 | /*
|
---|
1217 | * Note: the current fibril will be rescheduled either due to a
|
---|
1218 | * timeout or due to an arriving message destined to it. In the
|
---|
1219 | * former case, handle_expired_timeouts() and, in the latter
|
---|
1220 | * case, route_call() will perform the wakeup.
|
---|
1221 | */
|
---|
1222 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
1223 |
|
---|
1224 | /*
|
---|
1225 | * Futex is up after getting back from async_manager.
|
---|
1226 | * Get it again.
|
---|
1227 | */
|
---|
1228 | futex_down(&async_futex);
|
---|
1229 | if ((usecs) && (conn->wdata.to_event.occurred)
|
---|
1230 | && (list_empty(&conn->msg_queue))) {
|
---|
1231 | /* If we timed out -> exit */
|
---|
1232 | futex_up(&async_futex);
|
---|
1233 | return 0;
|
---|
1234 | }
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | msg_t *msg = list_get_instance(list_first(&conn->msg_queue),
|
---|
1238 | msg_t, link);
|
---|
1239 | list_remove(&msg->link);
|
---|
1240 |
|
---|
1241 | ipc_callid_t callid = msg->callid;
|
---|
1242 | *call = msg->call;
|
---|
1243 | free(msg);
|
---|
1244 |
|
---|
1245 | futex_up(&async_futex);
|
---|
1246 | return callid;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | void *async_get_client_data(void)
|
---|
1250 | {
|
---|
1251 | assert(fibril_connection);
|
---|
1252 | return fibril_connection->client->data;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | void *async_get_client_data_by_id(task_id_t client_id)
|
---|
1256 | {
|
---|
1257 | client_t *client = async_client_get(client_id, false);
|
---|
1258 | if (!client)
|
---|
1259 | return NULL;
|
---|
1260 |
|
---|
1261 | if (!client->data) {
|
---|
1262 | async_client_put(client);
|
---|
1263 | return NULL;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 | return client->data;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | void async_put_client_data_by_id(task_id_t client_id)
|
---|
1270 | {
|
---|
1271 | client_t *client = async_client_get(client_id, false);
|
---|
1272 |
|
---|
1273 | assert(client);
|
---|
1274 | assert(client->data);
|
---|
1275 |
|
---|
1276 | /* Drop the reference we got in async_get_client_data_by_hash(). */
|
---|
1277 | async_client_put(client);
|
---|
1278 |
|
---|
1279 | /* Drop our own reference we got at the beginning of this function. */
|
---|
1280 | async_client_put(client);
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | static port_t *async_find_port(iface_t iface, port_id_t port_id)
|
---|
1284 | {
|
---|
1285 | port_t *port = NULL;
|
---|
1286 |
|
---|
1287 | futex_down(&async_futex);
|
---|
1288 |
|
---|
1289 | ht_link_t *link = hash_table_find(&interface_hash_table, &iface);
|
---|
1290 | if (link) {
|
---|
1291 | interface_t *interface =
|
---|
1292 | hash_table_get_inst(link, interface_t, link);
|
---|
1293 |
|
---|
1294 | link = hash_table_find(&interface->port_hash_table, &port_id);
|
---|
1295 | if (link)
|
---|
1296 | port = hash_table_get_inst(link, port_t, link);
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | futex_up(&async_futex);
|
---|
1300 |
|
---|
1301 | return port;
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | /** Handle a call that was received.
|
---|
1305 | *
|
---|
1306 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
---|
1307 | * Otherwise the call is routed to its connection fibril.
|
---|
1308 | *
|
---|
1309 | * @param callid Hash of the incoming call.
|
---|
1310 | * @param call Data of the incoming call.
|
---|
1311 | *
|
---|
1312 | */
|
---|
1313 | static void handle_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
1314 | {
|
---|
1315 | assert(call);
|
---|
1316 |
|
---|
1317 | /* Kernel notification */
|
---|
1318 | if ((callid & IPC_CALLID_NOTIFICATION)) {
|
---|
1319 | fibril_t *fibril = (fibril_t *) __tcb_get()->fibril_data;
|
---|
1320 | unsigned oldsw = fibril->switches;
|
---|
1321 |
|
---|
1322 | process_notification(callid, call);
|
---|
1323 |
|
---|
1324 | if (oldsw != fibril->switches) {
|
---|
1325 | /*
|
---|
1326 | * The notification handler did not execute atomically
|
---|
1327 | * and so the current manager fibril assumed the role of
|
---|
1328 | * a notification fibril. While waiting for its
|
---|
1329 | * resources, it switched to another manager fibril that
|
---|
1330 | * had already existed or it created a new one. We
|
---|
1331 | * therefore know there is at least yet another
|
---|
1332 | * manager fibril that can take over. We now kill the
|
---|
1333 | * current 'notification' fibril to prevent fibril
|
---|
1334 | * population explosion.
|
---|
1335 | */
|
---|
1336 | futex_down(&async_futex);
|
---|
1337 | fibril_switch(FIBRIL_FROM_DEAD);
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | return;
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | /* New connection */
|
---|
1344 | if (IPC_GET_IMETHOD(*call) == IPC_M_CONNECT_ME_TO) {
|
---|
1345 | iface_t iface = (iface_t) IPC_GET_ARG1(*call);
|
---|
1346 | sysarg_t in_phone_hash = IPC_GET_ARG5(*call);
|
---|
1347 |
|
---|
1348 | async_notification_handler_t handler = fallback_port_handler;
|
---|
1349 | void *data = fallback_port_data;
|
---|
1350 |
|
---|
1351 | // TODO: Currently ignores all ports but the first one
|
---|
1352 | port_t *port = async_find_port(iface, 0);
|
---|
1353 | if (port) {
|
---|
1354 | handler = port->handler;
|
---|
1355 | data = port->data;
|
---|
1356 | }
|
---|
1357 |
|
---|
1358 | async_new_connection(call->in_task_id, in_phone_hash, callid,
|
---|
1359 | call, handler, data);
|
---|
1360 | return;
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | /* Cloned connection */
|
---|
1364 | if (IPC_GET_IMETHOD(*call) == IPC_M_CLONE_ESTABLISH) {
|
---|
1365 | // TODO: Currently ignores ports altogether
|
---|
1366 |
|
---|
1367 | /* Open new connection with fibril, etc. */
|
---|
1368 | async_new_connection(call->in_task_id, IPC_GET_ARG5(*call),
|
---|
1369 | callid, call, fallback_port_handler, fallback_port_data);
|
---|
1370 | return;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | /* Try to route the call through the connection hash table */
|
---|
1374 | if (route_call(callid, call))
|
---|
1375 | return;
|
---|
1376 |
|
---|
1377 | /* Unknown call from unknown phone - hang it up */
|
---|
1378 | ipc_answer_0(callid, EHANGUP);
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | /** Fire all timeouts that expired. */
|
---|
1382 | static void handle_expired_timeouts(void)
|
---|
1383 | {
|
---|
1384 | struct timeval tv;
|
---|
1385 | getuptime(&tv);
|
---|
1386 |
|
---|
1387 | futex_down(&async_futex);
|
---|
1388 |
|
---|
1389 | link_t *cur = list_first(&timeout_list);
|
---|
1390 | while (cur != NULL) {
|
---|
1391 | awaiter_t *waiter =
|
---|
1392 | list_get_instance(cur, awaiter_t, to_event.link);
|
---|
1393 |
|
---|
1394 | if (tv_gt(&waiter->to_event.expires, &tv))
|
---|
1395 | break;
|
---|
1396 |
|
---|
1397 | list_remove(&waiter->to_event.link);
|
---|
1398 | waiter->to_event.inlist = false;
|
---|
1399 | waiter->to_event.occurred = true;
|
---|
1400 |
|
---|
1401 | /*
|
---|
1402 | * Redundant condition?
|
---|
1403 | * The fibril should not be active when it gets here.
|
---|
1404 | */
|
---|
1405 | if (!waiter->active) {
|
---|
1406 | waiter->active = true;
|
---|
1407 | fibril_add_ready(waiter->fid);
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | cur = list_first(&timeout_list);
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | futex_up(&async_futex);
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | /** Endless loop dispatching incoming calls and answers.
|
---|
1417 | *
|
---|
1418 | * @return Never returns.
|
---|
1419 | *
|
---|
1420 | */
|
---|
1421 | static int async_manager_worker(void)
|
---|
1422 | {
|
---|
1423 | while (true) {
|
---|
1424 | if (fibril_switch(FIBRIL_FROM_MANAGER)) {
|
---|
1425 | futex_up(&async_futex);
|
---|
1426 | /*
|
---|
1427 | * async_futex is always held when entering a manager
|
---|
1428 | * fibril.
|
---|
1429 | */
|
---|
1430 | continue;
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | futex_down(&async_futex);
|
---|
1434 |
|
---|
1435 | suseconds_t timeout;
|
---|
1436 | unsigned int flags = SYNCH_FLAGS_NONE;
|
---|
1437 | if (!list_empty(&timeout_list)) {
|
---|
1438 | awaiter_t *waiter = list_get_instance(
|
---|
1439 | list_first(&timeout_list), awaiter_t, to_event.link);
|
---|
1440 |
|
---|
1441 | struct timeval tv;
|
---|
1442 | getuptime(&tv);
|
---|
1443 |
|
---|
1444 | if (tv_gteq(&tv, &waiter->to_event.expires)) {
|
---|
1445 | futex_up(&async_futex);
|
---|
1446 | handle_expired_timeouts();
|
---|
1447 | /*
|
---|
1448 | * Notice that even if the event(s) already
|
---|
1449 | * expired (and thus the other fibril was
|
---|
1450 | * supposed to be running already),
|
---|
1451 | * we check for incoming IPC.
|
---|
1452 | *
|
---|
1453 | * Otherwise, a fibril that continuously
|
---|
1454 | * creates (almost) expired events could
|
---|
1455 | * prevent IPC retrieval from the kernel.
|
---|
1456 | */
|
---|
1457 | timeout = 0;
|
---|
1458 | flags = SYNCH_FLAGS_NON_BLOCKING;
|
---|
1459 |
|
---|
1460 | } else {
|
---|
1461 | timeout = tv_sub_diff(&waiter->to_event.expires,
|
---|
1462 | &tv);
|
---|
1463 | futex_up(&async_futex);
|
---|
1464 | }
|
---|
1465 | } else {
|
---|
1466 | futex_up(&async_futex);
|
---|
1467 | timeout = SYNCH_NO_TIMEOUT;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | atomic_inc(&threads_in_ipc_wait);
|
---|
1471 |
|
---|
1472 | ipc_call_t call;
|
---|
1473 | ipc_callid_t callid = ipc_wait_cycle(&call, timeout, flags);
|
---|
1474 |
|
---|
1475 | atomic_dec(&threads_in_ipc_wait);
|
---|
1476 |
|
---|
1477 | if (!callid) {
|
---|
1478 | handle_expired_timeouts();
|
---|
1479 | continue;
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 | if (callid & IPC_CALLID_ANSWERED)
|
---|
1483 | continue;
|
---|
1484 |
|
---|
1485 | handle_call(callid, &call);
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | return 0;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | /** Function to start async_manager as a standalone fibril.
|
---|
1492 | *
|
---|
1493 | * When more kernel threads are used, one async manager should exist per thread.
|
---|
1494 | *
|
---|
1495 | * @param arg Unused.
|
---|
1496 | * @return Never returns.
|
---|
1497 | *
|
---|
1498 | */
|
---|
1499 | static int async_manager_fibril(void *arg)
|
---|
1500 | {
|
---|
1501 | futex_up(&async_futex);
|
---|
1502 |
|
---|
1503 | /*
|
---|
1504 | * async_futex is always locked when entering manager
|
---|
1505 | */
|
---|
1506 | async_manager_worker();
|
---|
1507 |
|
---|
1508 | return 0;
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | /** Add one manager to manager list. */
|
---|
1512 | void async_create_manager(void)
|
---|
1513 | {
|
---|
1514 | fid_t fid = fibril_create_generic(async_manager_fibril, NULL, PAGE_SIZE);
|
---|
1515 | if (fid != 0)
|
---|
1516 | fibril_add_manager(fid);
|
---|
1517 | }
|
---|
1518 |
|
---|
1519 | /** Remove one manager from manager list */
|
---|
1520 | void async_destroy_manager(void)
|
---|
1521 | {
|
---|
1522 | fibril_remove_manager();
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | /** Initialize the async framework.
|
---|
1526 | *
|
---|
1527 | */
|
---|
1528 | void __async_init(void)
|
---|
1529 | {
|
---|
1530 | if (!hash_table_create(&interface_hash_table, 0, 0,
|
---|
1531 | &interface_hash_table_ops))
|
---|
1532 | abort();
|
---|
1533 |
|
---|
1534 | if (!hash_table_create(&client_hash_table, 0, 0, &client_hash_table_ops))
|
---|
1535 | abort();
|
---|
1536 |
|
---|
1537 | if (!hash_table_create(&conn_hash_table, 0, 0, &conn_hash_table_ops))
|
---|
1538 | abort();
|
---|
1539 |
|
---|
1540 | if (!hash_table_create(¬ification_hash_table, 0, 0,
|
---|
1541 | ¬ification_hash_table_ops))
|
---|
1542 | abort();
|
---|
1543 |
|
---|
1544 | session_ns = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
1545 | if (session_ns == NULL)
|
---|
1546 | abort();
|
---|
1547 |
|
---|
1548 | session_ns->iface = 0;
|
---|
1549 | session_ns->mgmt = EXCHANGE_ATOMIC;
|
---|
1550 | session_ns->phone = PHONE_NS;
|
---|
1551 | session_ns->arg1 = 0;
|
---|
1552 | session_ns->arg2 = 0;
|
---|
1553 | session_ns->arg3 = 0;
|
---|
1554 |
|
---|
1555 | fibril_mutex_initialize(&session_ns->remote_state_mtx);
|
---|
1556 | session_ns->remote_state_data = NULL;
|
---|
1557 |
|
---|
1558 | list_initialize(&session_ns->exch_list);
|
---|
1559 | fibril_mutex_initialize(&session_ns->mutex);
|
---|
1560 | atomic_set(&session_ns->refcnt, 0);
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | /** Reply received callback.
|
---|
1564 | *
|
---|
1565 | * This function is called whenever a reply for an asynchronous message sent out
|
---|
1566 | * by the asynchronous framework is received.
|
---|
1567 | *
|
---|
1568 | * Notify the fibril which is waiting for this message that it has arrived.
|
---|
1569 | *
|
---|
1570 | * @param arg Pointer to the asynchronous message record.
|
---|
1571 | * @param retval Value returned in the answer.
|
---|
1572 | * @param data Call data of the answer.
|
---|
1573 | *
|
---|
1574 | */
|
---|
1575 | void reply_received(void *arg, int retval, ipc_call_t *data)
|
---|
1576 | {
|
---|
1577 | assert(arg);
|
---|
1578 |
|
---|
1579 | futex_down(&async_futex);
|
---|
1580 |
|
---|
1581 | amsg_t *msg = (amsg_t *) arg;
|
---|
1582 | msg->retval = retval;
|
---|
1583 |
|
---|
1584 | /* Copy data after futex_down, just in case the call was detached */
|
---|
1585 | if ((msg->dataptr) && (data))
|
---|
1586 | *msg->dataptr = *data;
|
---|
1587 |
|
---|
1588 | write_barrier();
|
---|
1589 |
|
---|
1590 | /* Remove message from timeout list */
|
---|
1591 | if (msg->wdata.to_event.inlist)
|
---|
1592 | list_remove(&msg->wdata.to_event.link);
|
---|
1593 |
|
---|
1594 | msg->done = true;
|
---|
1595 |
|
---|
1596 | if (msg->forget) {
|
---|
1597 | assert(msg->wdata.active);
|
---|
1598 | amsg_destroy(msg);
|
---|
1599 | } else if (!msg->wdata.active) {
|
---|
1600 | msg->wdata.active = true;
|
---|
1601 | fibril_add_ready(msg->wdata.fid);
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | futex_up(&async_futex);
|
---|
1605 | }
|
---|
1606 |
|
---|
1607 | /** Send message and return id of the sent message.
|
---|
1608 | *
|
---|
1609 | * The return value can be used as input for async_wait() to wait for
|
---|
1610 | * completion.
|
---|
1611 | *
|
---|
1612 | * @param exch Exchange for sending the message.
|
---|
1613 | * @param imethod Service-defined interface and method.
|
---|
1614 | * @param arg1 Service-defined payload argument.
|
---|
1615 | * @param arg2 Service-defined payload argument.
|
---|
1616 | * @param arg3 Service-defined payload argument.
|
---|
1617 | * @param arg4 Service-defined payload argument.
|
---|
1618 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
1619 | * stored.
|
---|
1620 | *
|
---|
1621 | * @return Hash of the sent message or 0 on error.
|
---|
1622 | *
|
---|
1623 | */
|
---|
1624 | aid_t async_send_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
---|
1625 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
---|
1626 | {
|
---|
1627 | if (exch == NULL)
|
---|
1628 | return 0;
|
---|
1629 |
|
---|
1630 | amsg_t *msg = amsg_create();
|
---|
1631 | if (msg == NULL)
|
---|
1632 | return 0;
|
---|
1633 |
|
---|
1634 | msg->dataptr = dataptr;
|
---|
1635 | msg->wdata.active = true;
|
---|
1636 |
|
---|
1637 | ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4, msg,
|
---|
1638 | reply_received);
|
---|
1639 |
|
---|
1640 | return (aid_t) msg;
|
---|
1641 | }
|
---|
1642 |
|
---|
1643 | /** Send message and return id of the sent message
|
---|
1644 | *
|
---|
1645 | * The return value can be used as input for async_wait() to wait for
|
---|
1646 | * completion.
|
---|
1647 | *
|
---|
1648 | * @param exch Exchange for sending the message.
|
---|
1649 | * @param imethod Service-defined interface and method.
|
---|
1650 | * @param arg1 Service-defined payload argument.
|
---|
1651 | * @param arg2 Service-defined payload argument.
|
---|
1652 | * @param arg3 Service-defined payload argument.
|
---|
1653 | * @param arg4 Service-defined payload argument.
|
---|
1654 | * @param arg5 Service-defined payload argument.
|
---|
1655 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
1656 | * stored.
|
---|
1657 | *
|
---|
1658 | * @return Hash of the sent message or 0 on error.
|
---|
1659 | *
|
---|
1660 | */
|
---|
1661 | aid_t async_send_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
---|
1662 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
1663 | ipc_call_t *dataptr)
|
---|
1664 | {
|
---|
1665 | if (exch == NULL)
|
---|
1666 | return 0;
|
---|
1667 |
|
---|
1668 | amsg_t *msg = amsg_create();
|
---|
1669 | if (msg == NULL)
|
---|
1670 | return 0;
|
---|
1671 |
|
---|
1672 | msg->dataptr = dataptr;
|
---|
1673 | msg->wdata.active = true;
|
---|
1674 |
|
---|
1675 | ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4, arg5,
|
---|
1676 | msg, reply_received);
|
---|
1677 |
|
---|
1678 | return (aid_t) msg;
|
---|
1679 | }
|
---|
1680 |
|
---|
1681 | /** Wait for a message sent by the async framework.
|
---|
1682 | *
|
---|
1683 | * @param amsgid Hash of the message to wait for.
|
---|
1684 | * @param retval Pointer to storage where the retval of the answer will
|
---|
1685 | * be stored.
|
---|
1686 | *
|
---|
1687 | */
|
---|
1688 | void async_wait_for(aid_t amsgid, sysarg_t *retval)
|
---|
1689 | {
|
---|
1690 | assert(amsgid);
|
---|
1691 |
|
---|
1692 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
1693 |
|
---|
1694 | futex_down(&async_futex);
|
---|
1695 |
|
---|
1696 | assert(!msg->forget);
|
---|
1697 | assert(!msg->destroyed);
|
---|
1698 |
|
---|
1699 | if (msg->done) {
|
---|
1700 | futex_up(&async_futex);
|
---|
1701 | goto done;
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 | msg->wdata.fid = fibril_get_id();
|
---|
1705 | msg->wdata.active = false;
|
---|
1706 | msg->wdata.to_event.inlist = false;
|
---|
1707 |
|
---|
1708 | /* Leave the async_futex locked when entering this function */
|
---|
1709 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
1710 |
|
---|
1711 | /* Futex is up automatically after fibril_switch */
|
---|
1712 |
|
---|
1713 | done:
|
---|
1714 | if (retval)
|
---|
1715 | *retval = msg->retval;
|
---|
1716 |
|
---|
1717 | amsg_destroy(msg);
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | /** Wait for a message sent by the async framework, timeout variant.
|
---|
1721 | *
|
---|
1722 | * If the wait times out, the caller may choose to either wait again by calling
|
---|
1723 | * async_wait_for() or async_wait_timeout(), or forget the message via
|
---|
1724 | * async_forget().
|
---|
1725 | *
|
---|
1726 | * @param amsgid Hash of the message to wait for.
|
---|
1727 | * @param retval Pointer to storage where the retval of the answer will
|
---|
1728 | * be stored.
|
---|
1729 | * @param timeout Timeout in microseconds.
|
---|
1730 | *
|
---|
1731 | * @return Zero on success, ETIMEOUT if the timeout has expired.
|
---|
1732 | *
|
---|
1733 | */
|
---|
1734 | int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
|
---|
1735 | {
|
---|
1736 | assert(amsgid);
|
---|
1737 |
|
---|
1738 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
1739 |
|
---|
1740 | futex_down(&async_futex);
|
---|
1741 |
|
---|
1742 | assert(!msg->forget);
|
---|
1743 | assert(!msg->destroyed);
|
---|
1744 |
|
---|
1745 | if (msg->done) {
|
---|
1746 | futex_up(&async_futex);
|
---|
1747 | goto done;
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | /*
|
---|
1751 | * Negative timeout is converted to zero timeout to avoid
|
---|
1752 | * using tv_add with negative augmenter.
|
---|
1753 | */
|
---|
1754 | if (timeout < 0)
|
---|
1755 | timeout = 0;
|
---|
1756 |
|
---|
1757 | getuptime(&msg->wdata.to_event.expires);
|
---|
1758 | tv_add_diff(&msg->wdata.to_event.expires, timeout);
|
---|
1759 |
|
---|
1760 | /*
|
---|
1761 | * Current fibril is inserted as waiting regardless of the
|
---|
1762 | * "size" of the timeout.
|
---|
1763 | *
|
---|
1764 | * Checking for msg->done and immediately bailing out when
|
---|
1765 | * timeout == 0 would mean that the manager fibril would never
|
---|
1766 | * run (consider single threaded program).
|
---|
1767 | * Thus the IPC answer would be never retrieved from the kernel.
|
---|
1768 | *
|
---|
1769 | * Notice that the actual delay would be very small because we
|
---|
1770 | * - switch to manager fibril
|
---|
1771 | * - the manager sees expired timeout
|
---|
1772 | * - and thus adds us back to ready queue
|
---|
1773 | * - manager switches back to some ready fibril
|
---|
1774 | * (prior it, it checks for incoming IPC).
|
---|
1775 | *
|
---|
1776 | */
|
---|
1777 | msg->wdata.fid = fibril_get_id();
|
---|
1778 | msg->wdata.active = false;
|
---|
1779 | async_insert_timeout(&msg->wdata);
|
---|
1780 |
|
---|
1781 | /* Leave the async_futex locked when entering this function */
|
---|
1782 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
1783 |
|
---|
1784 | /* Futex is up automatically after fibril_switch */
|
---|
1785 |
|
---|
1786 | if (!msg->done)
|
---|
1787 | return ETIMEOUT;
|
---|
1788 |
|
---|
1789 | done:
|
---|
1790 | if (retval)
|
---|
1791 | *retval = msg->retval;
|
---|
1792 |
|
---|
1793 | amsg_destroy(msg);
|
---|
1794 |
|
---|
1795 | return 0;
|
---|
1796 | }
|
---|
1797 |
|
---|
1798 | /** Discard the message / reply on arrival.
|
---|
1799 | *
|
---|
1800 | * The message will be marked to be discarded once the reply arrives in
|
---|
1801 | * reply_received(). It is not allowed to call async_wait_for() or
|
---|
1802 | * async_wait_timeout() on this message after a call to this function.
|
---|
1803 | *
|
---|
1804 | * @param amsgid Hash of the message to forget.
|
---|
1805 | */
|
---|
1806 | void async_forget(aid_t amsgid)
|
---|
1807 | {
|
---|
1808 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
1809 |
|
---|
1810 | assert(msg);
|
---|
1811 | assert(!msg->forget);
|
---|
1812 | assert(!msg->destroyed);
|
---|
1813 |
|
---|
1814 | futex_down(&async_futex);
|
---|
1815 |
|
---|
1816 | if (msg->done) {
|
---|
1817 | amsg_destroy(msg);
|
---|
1818 | } else {
|
---|
1819 | msg->dataptr = NULL;
|
---|
1820 | msg->forget = true;
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 | futex_up(&async_futex);
|
---|
1824 | }
|
---|
1825 |
|
---|
1826 | /** Wait for specified time.
|
---|
1827 | *
|
---|
1828 | * The current fibril is suspended but the thread continues to execute.
|
---|
1829 | *
|
---|
1830 | * @param timeout Duration of the wait in microseconds.
|
---|
1831 | *
|
---|
1832 | */
|
---|
1833 | void async_usleep(suseconds_t timeout)
|
---|
1834 | {
|
---|
1835 | amsg_t *msg = amsg_create();
|
---|
1836 | if (!msg)
|
---|
1837 | return;
|
---|
1838 |
|
---|
1839 | msg->wdata.fid = fibril_get_id();
|
---|
1840 |
|
---|
1841 | getuptime(&msg->wdata.to_event.expires);
|
---|
1842 | tv_add_diff(&msg->wdata.to_event.expires, timeout);
|
---|
1843 |
|
---|
1844 | futex_down(&async_futex);
|
---|
1845 |
|
---|
1846 | async_insert_timeout(&msg->wdata);
|
---|
1847 |
|
---|
1848 | /* Leave the async_futex locked when entering this function */
|
---|
1849 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
1850 |
|
---|
1851 | /* Futex is up automatically after fibril_switch() */
|
---|
1852 |
|
---|
1853 | amsg_destroy(msg);
|
---|
1854 | }
|
---|
1855 |
|
---|
1856 | /** Pseudo-synchronous message sending - fast version.
|
---|
1857 | *
|
---|
1858 | * Send message asynchronously and return only after the reply arrives.
|
---|
1859 | *
|
---|
1860 | * This function can only transfer 4 register payload arguments. For
|
---|
1861 | * transferring more arguments, see the slower async_req_slow().
|
---|
1862 | *
|
---|
1863 | * @param exch Exchange for sending the message.
|
---|
1864 | * @param imethod Interface and method of the call.
|
---|
1865 | * @param arg1 Service-defined payload argument.
|
---|
1866 | * @param arg2 Service-defined payload argument.
|
---|
1867 | * @param arg3 Service-defined payload argument.
|
---|
1868 | * @param arg4 Service-defined payload argument.
|
---|
1869 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
1870 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
1871 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
1872 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
1873 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
1874 | *
|
---|
1875 | * @return Return code of the reply or a negative error code.
|
---|
1876 | *
|
---|
1877 | */
|
---|
1878 | sysarg_t async_req_fast(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
---|
1879 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
|
---|
1880 | sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
---|
1881 | {
|
---|
1882 | if (exch == NULL)
|
---|
1883 | return ENOENT;
|
---|
1884 |
|
---|
1885 | ipc_call_t result;
|
---|
1886 | aid_t aid = async_send_4(exch, imethod, arg1, arg2, arg3, arg4,
|
---|
1887 | &result);
|
---|
1888 |
|
---|
1889 | sysarg_t rc;
|
---|
1890 | async_wait_for(aid, &rc);
|
---|
1891 |
|
---|
1892 | if (r1)
|
---|
1893 | *r1 = IPC_GET_ARG1(result);
|
---|
1894 |
|
---|
1895 | if (r2)
|
---|
1896 | *r2 = IPC_GET_ARG2(result);
|
---|
1897 |
|
---|
1898 | if (r3)
|
---|
1899 | *r3 = IPC_GET_ARG3(result);
|
---|
1900 |
|
---|
1901 | if (r4)
|
---|
1902 | *r4 = IPC_GET_ARG4(result);
|
---|
1903 |
|
---|
1904 | if (r5)
|
---|
1905 | *r5 = IPC_GET_ARG5(result);
|
---|
1906 |
|
---|
1907 | return rc;
|
---|
1908 | }
|
---|
1909 |
|
---|
1910 | /** Pseudo-synchronous message sending - slow version.
|
---|
1911 | *
|
---|
1912 | * Send message asynchronously and return only after the reply arrives.
|
---|
1913 | *
|
---|
1914 | * @param exch Exchange for sending the message.
|
---|
1915 | * @param imethod Interface and method of the call.
|
---|
1916 | * @param arg1 Service-defined payload argument.
|
---|
1917 | * @param arg2 Service-defined payload argument.
|
---|
1918 | * @param arg3 Service-defined payload argument.
|
---|
1919 | * @param arg4 Service-defined payload argument.
|
---|
1920 | * @param arg5 Service-defined payload argument.
|
---|
1921 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
1922 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
1923 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
1924 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
1925 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
1926 | *
|
---|
1927 | * @return Return code of the reply or a negative error code.
|
---|
1928 | *
|
---|
1929 | */
|
---|
1930 | sysarg_t async_req_slow(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
---|
1931 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
|
---|
1932 | sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
---|
1933 | {
|
---|
1934 | if (exch == NULL)
|
---|
1935 | return ENOENT;
|
---|
1936 |
|
---|
1937 | ipc_call_t result;
|
---|
1938 | aid_t aid = async_send_5(exch, imethod, arg1, arg2, arg3, arg4, arg5,
|
---|
1939 | &result);
|
---|
1940 |
|
---|
1941 | sysarg_t rc;
|
---|
1942 | async_wait_for(aid, &rc);
|
---|
1943 |
|
---|
1944 | if (r1)
|
---|
1945 | *r1 = IPC_GET_ARG1(result);
|
---|
1946 |
|
---|
1947 | if (r2)
|
---|
1948 | *r2 = IPC_GET_ARG2(result);
|
---|
1949 |
|
---|
1950 | if (r3)
|
---|
1951 | *r3 = IPC_GET_ARG3(result);
|
---|
1952 |
|
---|
1953 | if (r4)
|
---|
1954 | *r4 = IPC_GET_ARG4(result);
|
---|
1955 |
|
---|
1956 | if (r5)
|
---|
1957 | *r5 = IPC_GET_ARG5(result);
|
---|
1958 |
|
---|
1959 | return rc;
|
---|
1960 | }
|
---|
1961 |
|
---|
1962 | void async_msg_0(async_exch_t *exch, sysarg_t imethod)
|
---|
1963 | {
|
---|
1964 | if (exch != NULL)
|
---|
1965 | ipc_call_async_0(exch->phone, imethod, NULL, NULL);
|
---|
1966 | }
|
---|
1967 |
|
---|
1968 | void async_msg_1(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1)
|
---|
1969 | {
|
---|
1970 | if (exch != NULL)
|
---|
1971 | ipc_call_async_1(exch->phone, imethod, arg1, NULL, NULL);
|
---|
1972 | }
|
---|
1973 |
|
---|
1974 | void async_msg_2(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
---|
1975 | sysarg_t arg2)
|
---|
1976 | {
|
---|
1977 | if (exch != NULL)
|
---|
1978 | ipc_call_async_2(exch->phone, imethod, arg1, arg2, NULL, NULL);
|
---|
1979 | }
|
---|
1980 |
|
---|
1981 | void async_msg_3(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
---|
1982 | sysarg_t arg2, sysarg_t arg3)
|
---|
1983 | {
|
---|
1984 | if (exch != NULL)
|
---|
1985 | ipc_call_async_3(exch->phone, imethod, arg1, arg2, arg3, NULL,
|
---|
1986 | NULL);
|
---|
1987 | }
|
---|
1988 |
|
---|
1989 | void async_msg_4(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
---|
1990 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
1991 | {
|
---|
1992 | if (exch != NULL)
|
---|
1993 | ipc_call_async_4(exch->phone, imethod, arg1, arg2, arg3, arg4,
|
---|
1994 | NULL, NULL);
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | void async_msg_5(async_exch_t *exch, sysarg_t imethod, sysarg_t arg1,
|
---|
1998 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
1999 | {
|
---|
2000 | if (exch != NULL)
|
---|
2001 | ipc_call_async_5(exch->phone, imethod, arg1, arg2, arg3, arg4,
|
---|
2002 | arg5, NULL, NULL);
|
---|
2003 | }
|
---|
2004 |
|
---|
2005 | sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
|
---|
2006 | {
|
---|
2007 | return ipc_answer_0(callid, retval);
|
---|
2008 | }
|
---|
2009 |
|
---|
2010 | sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
|
---|
2011 | {
|
---|
2012 | return ipc_answer_1(callid, retval, arg1);
|
---|
2013 | }
|
---|
2014 |
|
---|
2015 | sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
2016 | sysarg_t arg2)
|
---|
2017 | {
|
---|
2018 | return ipc_answer_2(callid, retval, arg1, arg2);
|
---|
2019 | }
|
---|
2020 |
|
---|
2021 | sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
2022 | sysarg_t arg2, sysarg_t arg3)
|
---|
2023 | {
|
---|
2024 | return ipc_answer_3(callid, retval, arg1, arg2, arg3);
|
---|
2025 | }
|
---|
2026 |
|
---|
2027 | sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
2028 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
2029 | {
|
---|
2030 | return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
|
---|
2031 | }
|
---|
2032 |
|
---|
2033 | sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
2034 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
2035 | {
|
---|
2036 | return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
|
---|
2037 | }
|
---|
2038 |
|
---|
2039 | int async_forward_fast(ipc_callid_t callid, async_exch_t *exch,
|
---|
2040 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
2041 | {
|
---|
2042 | if (exch == NULL)
|
---|
2043 | return ENOENT;
|
---|
2044 |
|
---|
2045 | return ipc_forward_fast(callid, exch->phone, imethod, arg1, arg2, mode);
|
---|
2046 | }
|
---|
2047 |
|
---|
2048 | int async_forward_slow(ipc_callid_t callid, async_exch_t *exch,
|
---|
2049 | sysarg_t imethod, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
2050 | sysarg_t arg4, sysarg_t arg5, unsigned int mode)
|
---|
2051 | {
|
---|
2052 | if (exch == NULL)
|
---|
2053 | return ENOENT;
|
---|
2054 |
|
---|
2055 | return ipc_forward_slow(callid, exch->phone, imethod, arg1, arg2, arg3,
|
---|
2056 | arg4, arg5, mode);
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
2060 | *
|
---|
2061 | * Ask through phone for a new connection to some service.
|
---|
2062 | *
|
---|
2063 | * @param exch Exchange for sending the message.
|
---|
2064 | * @param arg1 User defined argument.
|
---|
2065 | * @param arg2 User defined argument.
|
---|
2066 | * @param arg3 User defined argument.
|
---|
2067 | *
|
---|
2068 | * @return Zero on success or a negative error code.
|
---|
2069 | *
|
---|
2070 | */
|
---|
2071 | int async_connect_to_me(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
|
---|
2072 | sysarg_t arg3)
|
---|
2073 | {
|
---|
2074 | if (exch == NULL)
|
---|
2075 | return ENOENT;
|
---|
2076 |
|
---|
2077 | ipc_call_t answer;
|
---|
2078 | aid_t req = async_send_3(exch, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
|
---|
2079 | &answer);
|
---|
2080 |
|
---|
2081 | sysarg_t rc;
|
---|
2082 | async_wait_for(req, &rc);
|
---|
2083 | if (rc != EOK)
|
---|
2084 | return (int) rc;
|
---|
2085 |
|
---|
2086 | return EOK;
|
---|
2087 | }
|
---|
2088 |
|
---|
2089 | /** Wrapper for making IPC_M_CLONE_ESTABLISH calls using the async framework.
|
---|
2090 | *
|
---|
2091 | * Ask for a cloned connection to some service.
|
---|
2092 | *
|
---|
2093 | * @param mgmt Exchange management style.
|
---|
2094 | * @param exch Exchange for sending the message.
|
---|
2095 | *
|
---|
2096 | * @return New session on success or NULL on error.
|
---|
2097 | *
|
---|
2098 | */
|
---|
2099 | async_sess_t *async_clone_establish(exch_mgmt_t mgmt, async_exch_t *exch)
|
---|
2100 | {
|
---|
2101 | if (exch == NULL) {
|
---|
2102 | errno = ENOENT;
|
---|
2103 | return NULL;
|
---|
2104 | }
|
---|
2105 |
|
---|
2106 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
2107 | if (sess == NULL) {
|
---|
2108 | errno = ENOMEM;
|
---|
2109 | return NULL;
|
---|
2110 | }
|
---|
2111 |
|
---|
2112 | ipc_call_t result;
|
---|
2113 |
|
---|
2114 | amsg_t *msg = amsg_create();
|
---|
2115 | if (!msg) {
|
---|
2116 | free(sess);
|
---|
2117 | errno = ENOMEM;
|
---|
2118 | return NULL;
|
---|
2119 | }
|
---|
2120 |
|
---|
2121 | msg->dataptr = &result;
|
---|
2122 | msg->wdata.active = true;
|
---|
2123 |
|
---|
2124 | ipc_call_async_0(exch->phone, IPC_M_CLONE_ESTABLISH, msg,
|
---|
2125 | reply_received);
|
---|
2126 |
|
---|
2127 | sysarg_t rc;
|
---|
2128 | async_wait_for((aid_t) msg, &rc);
|
---|
2129 |
|
---|
2130 | if (rc != EOK) {
|
---|
2131 | errno = rc;
|
---|
2132 | free(sess);
|
---|
2133 | return NULL;
|
---|
2134 | }
|
---|
2135 |
|
---|
2136 | int phone = (int) IPC_GET_ARG5(result);
|
---|
2137 |
|
---|
2138 | if (phone < 0) {
|
---|
2139 | errno = phone;
|
---|
2140 | free(sess);
|
---|
2141 | return NULL;
|
---|
2142 | }
|
---|
2143 |
|
---|
2144 | sess->iface = 0;
|
---|
2145 | sess->mgmt = mgmt;
|
---|
2146 | sess->phone = phone;
|
---|
2147 | sess->arg1 = 0;
|
---|
2148 | sess->arg2 = 0;
|
---|
2149 | sess->arg3 = 0;
|
---|
2150 |
|
---|
2151 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
2152 | sess->remote_state_data = NULL;
|
---|
2153 |
|
---|
2154 | list_initialize(&sess->exch_list);
|
---|
2155 | fibril_mutex_initialize(&sess->mutex);
|
---|
2156 | atomic_set(&sess->refcnt, 0);
|
---|
2157 |
|
---|
2158 | return sess;
|
---|
2159 | }
|
---|
2160 |
|
---|
2161 | static int async_connect_me_to_internal(int phone, sysarg_t arg1, sysarg_t arg2,
|
---|
2162 | sysarg_t arg3, sysarg_t arg4)
|
---|
2163 | {
|
---|
2164 | ipc_call_t result;
|
---|
2165 |
|
---|
2166 | amsg_t *msg = amsg_create();
|
---|
2167 | if (!msg)
|
---|
2168 | return ENOENT;
|
---|
2169 |
|
---|
2170 | msg->dataptr = &result;
|
---|
2171 | msg->wdata.active = true;
|
---|
2172 |
|
---|
2173 | ipc_call_async_4(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3, arg4,
|
---|
2174 | msg, reply_received);
|
---|
2175 |
|
---|
2176 | sysarg_t rc;
|
---|
2177 | async_wait_for((aid_t) msg, &rc);
|
---|
2178 |
|
---|
2179 | if (rc != EOK)
|
---|
2180 | return rc;
|
---|
2181 |
|
---|
2182 | return (int) IPC_GET_ARG5(result);
|
---|
2183 | }
|
---|
2184 |
|
---|
2185 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
2186 | *
|
---|
2187 | * Ask through for a new connection to some service.
|
---|
2188 | *
|
---|
2189 | * @param mgmt Exchange management style.
|
---|
2190 | * @param exch Exchange for sending the message.
|
---|
2191 | * @param arg1 User defined argument.
|
---|
2192 | * @param arg2 User defined argument.
|
---|
2193 | * @param arg3 User defined argument.
|
---|
2194 | *
|
---|
2195 | * @return New session on success or NULL on error.
|
---|
2196 | *
|
---|
2197 | */
|
---|
2198 | async_sess_t *async_connect_me_to(exch_mgmt_t mgmt, async_exch_t *exch,
|
---|
2199 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
---|
2200 | {
|
---|
2201 | if (exch == NULL) {
|
---|
2202 | errno = ENOENT;
|
---|
2203 | return NULL;
|
---|
2204 | }
|
---|
2205 |
|
---|
2206 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
2207 | if (sess == NULL) {
|
---|
2208 | errno = ENOMEM;
|
---|
2209 | return NULL;
|
---|
2210 | }
|
---|
2211 |
|
---|
2212 | int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
|
---|
2213 | 0);
|
---|
2214 | if (phone < 0) {
|
---|
2215 | errno = phone;
|
---|
2216 | free(sess);
|
---|
2217 | return NULL;
|
---|
2218 | }
|
---|
2219 |
|
---|
2220 | sess->iface = 0;
|
---|
2221 | sess->mgmt = mgmt;
|
---|
2222 | sess->phone = phone;
|
---|
2223 | sess->arg1 = arg1;
|
---|
2224 | sess->arg2 = arg2;
|
---|
2225 | sess->arg3 = arg3;
|
---|
2226 |
|
---|
2227 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
2228 | sess->remote_state_data = NULL;
|
---|
2229 |
|
---|
2230 | list_initialize(&sess->exch_list);
|
---|
2231 | fibril_mutex_initialize(&sess->mutex);
|
---|
2232 | atomic_set(&sess->refcnt, 0);
|
---|
2233 |
|
---|
2234 | return sess;
|
---|
2235 | }
|
---|
2236 |
|
---|
2237 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
2238 | *
|
---|
2239 | * Ask through phone for a new connection to some service and block until
|
---|
2240 | * success.
|
---|
2241 | *
|
---|
2242 | * @param exch Exchange for sending the message.
|
---|
2243 | * @param iface Connection interface.
|
---|
2244 | * @param arg2 User defined argument.
|
---|
2245 | * @param arg3 User defined argument.
|
---|
2246 | *
|
---|
2247 | * @return New session on success or NULL on error.
|
---|
2248 | *
|
---|
2249 | */
|
---|
2250 | async_sess_t *async_connect_me_to_iface(async_exch_t *exch, iface_t iface,
|
---|
2251 | sysarg_t arg2, sysarg_t arg3)
|
---|
2252 | {
|
---|
2253 | if (exch == NULL) {
|
---|
2254 | errno = ENOENT;
|
---|
2255 | return NULL;
|
---|
2256 | }
|
---|
2257 |
|
---|
2258 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
2259 | if (sess == NULL) {
|
---|
2260 | errno = ENOMEM;
|
---|
2261 | return NULL;
|
---|
2262 | }
|
---|
2263 |
|
---|
2264 | int phone = async_connect_me_to_internal(exch->phone, iface, arg2,
|
---|
2265 | arg3, 0);
|
---|
2266 | if (phone < 0) {
|
---|
2267 | errno = phone;
|
---|
2268 | free(sess);
|
---|
2269 | return NULL;
|
---|
2270 | }
|
---|
2271 |
|
---|
2272 | sess->iface = iface;
|
---|
2273 | sess->phone = phone;
|
---|
2274 | sess->arg1 = iface;
|
---|
2275 | sess->arg2 = arg2;
|
---|
2276 | sess->arg3 = arg3;
|
---|
2277 |
|
---|
2278 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
2279 | sess->remote_state_data = NULL;
|
---|
2280 |
|
---|
2281 | list_initialize(&sess->exch_list);
|
---|
2282 | fibril_mutex_initialize(&sess->mutex);
|
---|
2283 | atomic_set(&sess->refcnt, 0);
|
---|
2284 |
|
---|
2285 | return sess;
|
---|
2286 | }
|
---|
2287 |
|
---|
2288 | /** Set arguments for new connections.
|
---|
2289 | *
|
---|
2290 | * FIXME This is an ugly hack to work around the problem that parallel
|
---|
2291 | * exchanges are implemented using parallel connections. When we create
|
---|
2292 | * a callback session, the framework does not know arguments for the new
|
---|
2293 | * connections.
|
---|
2294 | *
|
---|
2295 | * The proper solution seems to be to implement parallel exchanges using
|
---|
2296 | * tagging.
|
---|
2297 | */
|
---|
2298 | void async_sess_args_set(async_sess_t *sess, sysarg_t arg1, sysarg_t arg2,
|
---|
2299 | sysarg_t arg3)
|
---|
2300 | {
|
---|
2301 | sess->arg1 = arg1;
|
---|
2302 | sess->arg2 = arg2;
|
---|
2303 | sess->arg3 = arg3;
|
---|
2304 | }
|
---|
2305 |
|
---|
2306 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
2307 | *
|
---|
2308 | * Ask through phone for a new connection to some service and block until
|
---|
2309 | * success.
|
---|
2310 | *
|
---|
2311 | * @param mgmt Exchange management style.
|
---|
2312 | * @param exch Exchange for sending the message.
|
---|
2313 | * @param arg1 User defined argument.
|
---|
2314 | * @param arg2 User defined argument.
|
---|
2315 | * @param arg3 User defined argument.
|
---|
2316 | *
|
---|
2317 | * @return New session on success or NULL on error.
|
---|
2318 | *
|
---|
2319 | */
|
---|
2320 | async_sess_t *async_connect_me_to_blocking(exch_mgmt_t mgmt, async_exch_t *exch,
|
---|
2321 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3)
|
---|
2322 | {
|
---|
2323 | if (exch == NULL) {
|
---|
2324 | errno = ENOENT;
|
---|
2325 | return NULL;
|
---|
2326 | }
|
---|
2327 |
|
---|
2328 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
2329 | if (sess == NULL) {
|
---|
2330 | errno = ENOMEM;
|
---|
2331 | return NULL;
|
---|
2332 | }
|
---|
2333 |
|
---|
2334 | int phone = async_connect_me_to_internal(exch->phone, arg1, arg2, arg3,
|
---|
2335 | IPC_FLAG_BLOCKING);
|
---|
2336 |
|
---|
2337 | if (phone < 0) {
|
---|
2338 | errno = phone;
|
---|
2339 | free(sess);
|
---|
2340 | return NULL;
|
---|
2341 | }
|
---|
2342 |
|
---|
2343 | sess->iface = 0;
|
---|
2344 | sess->mgmt = mgmt;
|
---|
2345 | sess->phone = phone;
|
---|
2346 | sess->arg1 = arg1;
|
---|
2347 | sess->arg2 = arg2;
|
---|
2348 | sess->arg3 = arg3;
|
---|
2349 |
|
---|
2350 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
2351 | sess->remote_state_data = NULL;
|
---|
2352 |
|
---|
2353 | list_initialize(&sess->exch_list);
|
---|
2354 | fibril_mutex_initialize(&sess->mutex);
|
---|
2355 | atomic_set(&sess->refcnt, 0);
|
---|
2356 |
|
---|
2357 | return sess;
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
2361 | *
|
---|
2362 | * Ask through phone for a new connection to some service and block until
|
---|
2363 | * success.
|
---|
2364 | *
|
---|
2365 | * @param exch Exchange for sending the message.
|
---|
2366 | * @param iface Connection interface.
|
---|
2367 | * @param arg2 User defined argument.
|
---|
2368 | * @param arg3 User defined argument.
|
---|
2369 | *
|
---|
2370 | * @return New session on success or NULL on error.
|
---|
2371 | *
|
---|
2372 | */
|
---|
2373 | async_sess_t *async_connect_me_to_blocking_iface(async_exch_t *exch, iface_t iface,
|
---|
2374 | sysarg_t arg2, sysarg_t arg3)
|
---|
2375 | {
|
---|
2376 | if (exch == NULL) {
|
---|
2377 | errno = ENOENT;
|
---|
2378 | return NULL;
|
---|
2379 | }
|
---|
2380 |
|
---|
2381 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
2382 | if (sess == NULL) {
|
---|
2383 | errno = ENOMEM;
|
---|
2384 | return NULL;
|
---|
2385 | }
|
---|
2386 |
|
---|
2387 | int phone = async_connect_me_to_internal(exch->phone, iface, arg2,
|
---|
2388 | arg3, IPC_FLAG_BLOCKING);
|
---|
2389 | if (phone < 0) {
|
---|
2390 | errno = phone;
|
---|
2391 | free(sess);
|
---|
2392 | return NULL;
|
---|
2393 | }
|
---|
2394 |
|
---|
2395 | sess->iface = iface;
|
---|
2396 | sess->phone = phone;
|
---|
2397 | sess->arg1 = iface;
|
---|
2398 | sess->arg2 = arg2;
|
---|
2399 | sess->arg3 = arg3;
|
---|
2400 |
|
---|
2401 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
2402 | sess->remote_state_data = NULL;
|
---|
2403 |
|
---|
2404 | list_initialize(&sess->exch_list);
|
---|
2405 | fibril_mutex_initialize(&sess->mutex);
|
---|
2406 | atomic_set(&sess->refcnt, 0);
|
---|
2407 |
|
---|
2408 | return sess;
|
---|
2409 | }
|
---|
2410 |
|
---|
2411 | /** Connect to a task specified by id.
|
---|
2412 | *
|
---|
2413 | */
|
---|
2414 | async_sess_t *async_connect_kbox(task_id_t id)
|
---|
2415 | {
|
---|
2416 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
2417 | if (sess == NULL) {
|
---|
2418 | errno = ENOMEM;
|
---|
2419 | return NULL;
|
---|
2420 | }
|
---|
2421 |
|
---|
2422 | int phone = ipc_connect_kbox(id);
|
---|
2423 | if (phone < 0) {
|
---|
2424 | errno = phone;
|
---|
2425 | free(sess);
|
---|
2426 | return NULL;
|
---|
2427 | }
|
---|
2428 |
|
---|
2429 | sess->iface = 0;
|
---|
2430 | sess->mgmt = EXCHANGE_ATOMIC;
|
---|
2431 | sess->phone = phone;
|
---|
2432 | sess->arg1 = 0;
|
---|
2433 | sess->arg2 = 0;
|
---|
2434 | sess->arg3 = 0;
|
---|
2435 |
|
---|
2436 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
2437 | sess->remote_state_data = NULL;
|
---|
2438 |
|
---|
2439 | list_initialize(&sess->exch_list);
|
---|
2440 | fibril_mutex_initialize(&sess->mutex);
|
---|
2441 | atomic_set(&sess->refcnt, 0);
|
---|
2442 |
|
---|
2443 | return sess;
|
---|
2444 | }
|
---|
2445 |
|
---|
2446 | static int async_hangup_internal(int phone)
|
---|
2447 | {
|
---|
2448 | return ipc_hangup(phone);
|
---|
2449 | }
|
---|
2450 |
|
---|
2451 | /** Wrapper for ipc_hangup.
|
---|
2452 | *
|
---|
2453 | * @param sess Session to hung up.
|
---|
2454 | *
|
---|
2455 | * @return Zero on success or a negative error code.
|
---|
2456 | *
|
---|
2457 | */
|
---|
2458 | int async_hangup(async_sess_t *sess)
|
---|
2459 | {
|
---|
2460 | async_exch_t *exch;
|
---|
2461 |
|
---|
2462 | assert(sess);
|
---|
2463 |
|
---|
2464 | if (atomic_get(&sess->refcnt) > 0)
|
---|
2465 | return EBUSY;
|
---|
2466 |
|
---|
2467 | fibril_mutex_lock(&async_sess_mutex);
|
---|
2468 |
|
---|
2469 | int rc = async_hangup_internal(sess->phone);
|
---|
2470 |
|
---|
2471 | while (!list_empty(&sess->exch_list)) {
|
---|
2472 | exch = (async_exch_t *)
|
---|
2473 | list_get_instance(list_first(&sess->exch_list),
|
---|
2474 | async_exch_t, sess_link);
|
---|
2475 |
|
---|
2476 | list_remove(&exch->sess_link);
|
---|
2477 | list_remove(&exch->global_link);
|
---|
2478 | async_hangup_internal(exch->phone);
|
---|
2479 | free(exch);
|
---|
2480 | }
|
---|
2481 |
|
---|
2482 | free(sess);
|
---|
2483 |
|
---|
2484 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
2485 |
|
---|
2486 | return rc;
|
---|
2487 | }
|
---|
2488 |
|
---|
2489 | /** Interrupt one thread of this task from waiting for IPC. */
|
---|
2490 | void async_poke(void)
|
---|
2491 | {
|
---|
2492 | ipc_poke();
|
---|
2493 | }
|
---|
2494 |
|
---|
2495 | /** Start new exchange in a session.
|
---|
2496 | *
|
---|
2497 | * @param session Session.
|
---|
2498 | *
|
---|
2499 | * @return New exchange or NULL on error.
|
---|
2500 | *
|
---|
2501 | */
|
---|
2502 | async_exch_t *async_exchange_begin(async_sess_t *sess)
|
---|
2503 | {
|
---|
2504 | if (sess == NULL)
|
---|
2505 | return NULL;
|
---|
2506 |
|
---|
2507 | exch_mgmt_t mgmt = sess->mgmt;
|
---|
2508 | if (sess->iface != 0)
|
---|
2509 | mgmt = sess->iface & IFACE_EXCHANGE_MASK;
|
---|
2510 |
|
---|
2511 | async_exch_t *exch = NULL;
|
---|
2512 |
|
---|
2513 | fibril_mutex_lock(&async_sess_mutex);
|
---|
2514 |
|
---|
2515 | if (!list_empty(&sess->exch_list)) {
|
---|
2516 | /*
|
---|
2517 | * There are inactive exchanges in the session.
|
---|
2518 | */
|
---|
2519 | exch = (async_exch_t *)
|
---|
2520 | list_get_instance(list_first(&sess->exch_list),
|
---|
2521 | async_exch_t, sess_link);
|
---|
2522 |
|
---|
2523 | list_remove(&exch->sess_link);
|
---|
2524 | list_remove(&exch->global_link);
|
---|
2525 | } else {
|
---|
2526 | /*
|
---|
2527 | * There are no available exchanges in the session.
|
---|
2528 | */
|
---|
2529 |
|
---|
2530 | if ((mgmt == EXCHANGE_ATOMIC) ||
|
---|
2531 | (mgmt == EXCHANGE_SERIALIZE)) {
|
---|
2532 | exch = (async_exch_t *) malloc(sizeof(async_exch_t));
|
---|
2533 | if (exch != NULL) {
|
---|
2534 | link_initialize(&exch->sess_link);
|
---|
2535 | link_initialize(&exch->global_link);
|
---|
2536 | exch->sess = sess;
|
---|
2537 | exch->phone = sess->phone;
|
---|
2538 | }
|
---|
2539 | } else if (mgmt == EXCHANGE_PARALLEL) {
|
---|
2540 | int phone;
|
---|
2541 |
|
---|
2542 | retry:
|
---|
2543 | /*
|
---|
2544 | * Make a one-time attempt to connect a new data phone.
|
---|
2545 | */
|
---|
2546 | phone = async_connect_me_to_internal(sess->phone, sess->arg1,
|
---|
2547 | sess->arg2, sess->arg3, 0);
|
---|
2548 | if (phone >= 0) {
|
---|
2549 | exch = (async_exch_t *) malloc(sizeof(async_exch_t));
|
---|
2550 | if (exch != NULL) {
|
---|
2551 | link_initialize(&exch->sess_link);
|
---|
2552 | link_initialize(&exch->global_link);
|
---|
2553 | exch->sess = sess;
|
---|
2554 | exch->phone = phone;
|
---|
2555 | } else
|
---|
2556 | async_hangup_internal(phone);
|
---|
2557 | } else if (!list_empty(&inactive_exch_list)) {
|
---|
2558 | /*
|
---|
2559 | * We did not manage to connect a new phone. But we
|
---|
2560 | * can try to close some of the currently inactive
|
---|
2561 | * connections in other sessions and try again.
|
---|
2562 | */
|
---|
2563 | exch = (async_exch_t *)
|
---|
2564 | list_get_instance(list_first(&inactive_exch_list),
|
---|
2565 | async_exch_t, global_link);
|
---|
2566 |
|
---|
2567 | list_remove(&exch->sess_link);
|
---|
2568 | list_remove(&exch->global_link);
|
---|
2569 | async_hangup_internal(exch->phone);
|
---|
2570 | free(exch);
|
---|
2571 | goto retry;
|
---|
2572 | } else {
|
---|
2573 | /*
|
---|
2574 | * Wait for a phone to become available.
|
---|
2575 | */
|
---|
2576 | fibril_condvar_wait(&avail_phone_cv, &async_sess_mutex);
|
---|
2577 | goto retry;
|
---|
2578 | }
|
---|
2579 | }
|
---|
2580 | }
|
---|
2581 |
|
---|
2582 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
2583 |
|
---|
2584 | if (exch != NULL) {
|
---|
2585 | atomic_inc(&sess->refcnt);
|
---|
2586 |
|
---|
2587 | if (mgmt == EXCHANGE_SERIALIZE)
|
---|
2588 | fibril_mutex_lock(&sess->mutex);
|
---|
2589 | }
|
---|
2590 |
|
---|
2591 | return exch;
|
---|
2592 | }
|
---|
2593 |
|
---|
2594 | /** Finish an exchange.
|
---|
2595 | *
|
---|
2596 | * @param exch Exchange to finish.
|
---|
2597 | *
|
---|
2598 | */
|
---|
2599 | void async_exchange_end(async_exch_t *exch)
|
---|
2600 | {
|
---|
2601 | if (exch == NULL)
|
---|
2602 | return;
|
---|
2603 |
|
---|
2604 | async_sess_t *sess = exch->sess;
|
---|
2605 | assert(sess != NULL);
|
---|
2606 |
|
---|
2607 | exch_mgmt_t mgmt = sess->mgmt;
|
---|
2608 | if (sess->iface != 0)
|
---|
2609 | mgmt = sess->iface & IFACE_EXCHANGE_MASK;
|
---|
2610 |
|
---|
2611 | atomic_dec(&sess->refcnt);
|
---|
2612 |
|
---|
2613 | if (mgmt == EXCHANGE_SERIALIZE)
|
---|
2614 | fibril_mutex_unlock(&sess->mutex);
|
---|
2615 |
|
---|
2616 | fibril_mutex_lock(&async_sess_mutex);
|
---|
2617 |
|
---|
2618 | list_append(&exch->sess_link, &sess->exch_list);
|
---|
2619 | list_append(&exch->global_link, &inactive_exch_list);
|
---|
2620 | fibril_condvar_signal(&avail_phone_cv);
|
---|
2621 |
|
---|
2622 | fibril_mutex_unlock(&async_sess_mutex);
|
---|
2623 | }
|
---|
2624 |
|
---|
2625 | /** Wrapper for IPC_M_SHARE_IN calls using the async framework.
|
---|
2626 | *
|
---|
2627 | * @param exch Exchange for sending the message.
|
---|
2628 | * @param size Size of the destination address space area.
|
---|
2629 | * @param arg User defined argument.
|
---|
2630 | * @param flags Storage for the received flags. Can be NULL.
|
---|
2631 | * @param dst Address of the storage for the destination address space area
|
---|
2632 | * base address. Cannot be NULL.
|
---|
2633 | *
|
---|
2634 | * @return Zero on success or a negative error code from errno.h.
|
---|
2635 | *
|
---|
2636 | */
|
---|
2637 | int async_share_in_start(async_exch_t *exch, size_t size, sysarg_t arg,
|
---|
2638 | unsigned int *flags, void **dst)
|
---|
2639 | {
|
---|
2640 | if (exch == NULL)
|
---|
2641 | return ENOENT;
|
---|
2642 |
|
---|
2643 | sysarg_t _flags = 0;
|
---|
2644 | sysarg_t _dst = (sysarg_t) -1;
|
---|
2645 | int res = async_req_2_4(exch, IPC_M_SHARE_IN, (sysarg_t) size,
|
---|
2646 | arg, NULL, &_flags, NULL, &_dst);
|
---|
2647 |
|
---|
2648 | if (flags)
|
---|
2649 | *flags = (unsigned int) _flags;
|
---|
2650 |
|
---|
2651 | *dst = (void *) _dst;
|
---|
2652 | return res;
|
---|
2653 | }
|
---|
2654 |
|
---|
2655 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
---|
2656 | *
|
---|
2657 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
|
---|
2658 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2659 | * argument.
|
---|
2660 | *
|
---|
2661 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
2662 | *
|
---|
2663 | * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
|
---|
2664 | * @param size Destination address space area size.
|
---|
2665 | *
|
---|
2666 | * @return True on success, false on failure.
|
---|
2667 | *
|
---|
2668 | */
|
---|
2669 | bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
|
---|
2670 | {
|
---|
2671 | assert(callid);
|
---|
2672 | assert(size);
|
---|
2673 |
|
---|
2674 | ipc_call_t data;
|
---|
2675 | *callid = async_get_call(&data);
|
---|
2676 |
|
---|
2677 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
|
---|
2678 | return false;
|
---|
2679 |
|
---|
2680 | *size = (size_t) IPC_GET_ARG1(data);
|
---|
2681 | return true;
|
---|
2682 | }
|
---|
2683 |
|
---|
2684 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
---|
2685 | *
|
---|
2686 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_IN
|
---|
2687 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2688 | * argument.
|
---|
2689 | *
|
---|
2690 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
2691 | * @param src Source address space base.
|
---|
2692 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
2693 | *
|
---|
2694 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
2695 | *
|
---|
2696 | */
|
---|
2697 | int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
|
---|
2698 | {
|
---|
2699 | return ipc_answer_3(callid, EOK, (sysarg_t) src, (sysarg_t) flags,
|
---|
2700 | (sysarg_t) __entry);
|
---|
2701 | }
|
---|
2702 |
|
---|
2703 | /** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
|
---|
2704 | *
|
---|
2705 | * @param exch Exchange for sending the message.
|
---|
2706 | * @param src Source address space area base address.
|
---|
2707 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
2708 | *
|
---|
2709 | * @return Zero on success or a negative error code from errno.h.
|
---|
2710 | *
|
---|
2711 | */
|
---|
2712 | int async_share_out_start(async_exch_t *exch, void *src, unsigned int flags)
|
---|
2713 | {
|
---|
2714 | if (exch == NULL)
|
---|
2715 | return ENOENT;
|
---|
2716 |
|
---|
2717 | return async_req_3_0(exch, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
|
---|
2718 | (sysarg_t) flags);
|
---|
2719 | }
|
---|
2720 |
|
---|
2721 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
---|
2722 | *
|
---|
2723 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
|
---|
2724 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2725 | * argument.
|
---|
2726 | *
|
---|
2727 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
2728 | *
|
---|
2729 | * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
|
---|
2730 | * @param size Storage for the source address space area size.
|
---|
2731 | * @param flags Storage for the sharing flags.
|
---|
2732 | *
|
---|
2733 | * @return True on success, false on failure.
|
---|
2734 | *
|
---|
2735 | */
|
---|
2736 | bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
|
---|
2737 | {
|
---|
2738 | assert(callid);
|
---|
2739 | assert(size);
|
---|
2740 | assert(flags);
|
---|
2741 |
|
---|
2742 | ipc_call_t data;
|
---|
2743 | *callid = async_get_call(&data);
|
---|
2744 |
|
---|
2745 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
|
---|
2746 | return false;
|
---|
2747 |
|
---|
2748 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
2749 | *flags = (unsigned int) IPC_GET_ARG3(data);
|
---|
2750 | return true;
|
---|
2751 | }
|
---|
2752 |
|
---|
2753 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
---|
2754 | *
|
---|
2755 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
|
---|
2756 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2757 | * argument.
|
---|
2758 | *
|
---|
2759 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
2760 | * @param dst Address of the storage for the destination address space area
|
---|
2761 | * base address.
|
---|
2762 | *
|
---|
2763 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
2764 | *
|
---|
2765 | */
|
---|
2766 | int async_share_out_finalize(ipc_callid_t callid, void **dst)
|
---|
2767 | {
|
---|
2768 | return ipc_answer_2(callid, EOK, (sysarg_t) __entry, (sysarg_t) dst);
|
---|
2769 | }
|
---|
2770 |
|
---|
2771 | /** Start IPC_M_DATA_READ using the async framework.
|
---|
2772 | *
|
---|
2773 | * @param exch Exchange for sending the message.
|
---|
2774 | * @param dst Address of the beginning of the destination buffer.
|
---|
2775 | * @param size Size of the destination buffer (in bytes).
|
---|
2776 | * @param dataptr Storage of call data (arg 2 holds actual data size).
|
---|
2777 | *
|
---|
2778 | * @return Hash of the sent message or 0 on error.
|
---|
2779 | *
|
---|
2780 | */
|
---|
2781 | aid_t async_data_read(async_exch_t *exch, void *dst, size_t size,
|
---|
2782 | ipc_call_t *dataptr)
|
---|
2783 | {
|
---|
2784 | return async_send_2(exch, IPC_M_DATA_READ, (sysarg_t) dst,
|
---|
2785 | (sysarg_t) size, dataptr);
|
---|
2786 | }
|
---|
2787 |
|
---|
2788 | /** Wrapper for IPC_M_DATA_READ calls using the async framework.
|
---|
2789 | *
|
---|
2790 | * @param exch Exchange for sending the message.
|
---|
2791 | * @param dst Address of the beginning of the destination buffer.
|
---|
2792 | * @param size Size of the destination buffer.
|
---|
2793 | *
|
---|
2794 | * @return Zero on success or a negative error code from errno.h.
|
---|
2795 | *
|
---|
2796 | */
|
---|
2797 | int async_data_read_start(async_exch_t *exch, void *dst, size_t size)
|
---|
2798 | {
|
---|
2799 | if (exch == NULL)
|
---|
2800 | return ENOENT;
|
---|
2801 |
|
---|
2802 | return async_req_2_0(exch, IPC_M_DATA_READ, (sysarg_t) dst,
|
---|
2803 | (sysarg_t) size);
|
---|
2804 | }
|
---|
2805 |
|
---|
2806 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
---|
2807 | *
|
---|
2808 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
|
---|
2809 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2810 | * argument.
|
---|
2811 | *
|
---|
2812 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
2813 | *
|
---|
2814 | * @param callid Storage for the hash of the IPC_M_DATA_READ.
|
---|
2815 | * @param size Storage for the maximum size. Can be NULL.
|
---|
2816 | *
|
---|
2817 | * @return True on success, false on failure.
|
---|
2818 | *
|
---|
2819 | */
|
---|
2820 | bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
|
---|
2821 | {
|
---|
2822 | ipc_call_t data;
|
---|
2823 | return async_data_read_receive_call(callid, &data, size);
|
---|
2824 | }
|
---|
2825 |
|
---|
2826 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
---|
2827 | *
|
---|
2828 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
|
---|
2829 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2830 | * argument.
|
---|
2831 | *
|
---|
2832 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
2833 | *
|
---|
2834 | * @param callid Storage for the hash of the IPC_M_DATA_READ.
|
---|
2835 | * @param size Storage for the maximum size. Can be NULL.
|
---|
2836 | *
|
---|
2837 | * @return True on success, false on failure.
|
---|
2838 | *
|
---|
2839 | */
|
---|
2840 | bool async_data_read_receive_call(ipc_callid_t *callid, ipc_call_t *data,
|
---|
2841 | size_t *size)
|
---|
2842 | {
|
---|
2843 | assert(callid);
|
---|
2844 | assert(data);
|
---|
2845 |
|
---|
2846 | *callid = async_get_call(data);
|
---|
2847 |
|
---|
2848 | if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_READ)
|
---|
2849 | return false;
|
---|
2850 |
|
---|
2851 | if (size)
|
---|
2852 | *size = (size_t) IPC_GET_ARG2(*data);
|
---|
2853 |
|
---|
2854 | return true;
|
---|
2855 | }
|
---|
2856 |
|
---|
2857 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
---|
2858 | *
|
---|
2859 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
---|
2860 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2861 | * argument.
|
---|
2862 | *
|
---|
2863 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
2864 | * @param src Source address for the IPC_M_DATA_READ call.
|
---|
2865 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
---|
2866 | * the maximum size announced by the sender.
|
---|
2867 | *
|
---|
2868 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
2869 | *
|
---|
2870 | */
|
---|
2871 | int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
|
---|
2872 | {
|
---|
2873 | return ipc_answer_2(callid, EOK, (sysarg_t) src, (sysarg_t) size);
|
---|
2874 | }
|
---|
2875 |
|
---|
2876 | /** Wrapper for forwarding any read request
|
---|
2877 | *
|
---|
2878 | */
|
---|
2879 | int async_data_read_forward_fast(async_exch_t *exch, sysarg_t imethod,
|
---|
2880 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
2881 | ipc_call_t *dataptr)
|
---|
2882 | {
|
---|
2883 | if (exch == NULL)
|
---|
2884 | return ENOENT;
|
---|
2885 |
|
---|
2886 | ipc_callid_t callid;
|
---|
2887 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
2888 | ipc_answer_0(callid, EINVAL);
|
---|
2889 | return EINVAL;
|
---|
2890 | }
|
---|
2891 |
|
---|
2892 | aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
|
---|
2893 | dataptr);
|
---|
2894 | if (msg == 0) {
|
---|
2895 | ipc_answer_0(callid, EINVAL);
|
---|
2896 | return EINVAL;
|
---|
2897 | }
|
---|
2898 |
|
---|
2899 | int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
|
---|
2900 | IPC_FF_ROUTE_FROM_ME);
|
---|
2901 | if (retval != EOK) {
|
---|
2902 | async_forget(msg);
|
---|
2903 | ipc_answer_0(callid, retval);
|
---|
2904 | return retval;
|
---|
2905 | }
|
---|
2906 |
|
---|
2907 | sysarg_t rc;
|
---|
2908 | async_wait_for(msg, &rc);
|
---|
2909 |
|
---|
2910 | return (int) rc;
|
---|
2911 | }
|
---|
2912 |
|
---|
2913 | /** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
|
---|
2914 | *
|
---|
2915 | * @param exch Exchange for sending the message.
|
---|
2916 | * @param src Address of the beginning of the source buffer.
|
---|
2917 | * @param size Size of the source buffer.
|
---|
2918 | *
|
---|
2919 | * @return Zero on success or a negative error code from errno.h.
|
---|
2920 | *
|
---|
2921 | */
|
---|
2922 | int async_data_write_start(async_exch_t *exch, const void *src, size_t size)
|
---|
2923 | {
|
---|
2924 | if (exch == NULL)
|
---|
2925 | return ENOENT;
|
---|
2926 |
|
---|
2927 | return async_req_2_0(exch, IPC_M_DATA_WRITE, (sysarg_t) src,
|
---|
2928 | (sysarg_t) size);
|
---|
2929 | }
|
---|
2930 |
|
---|
2931 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
---|
2932 | *
|
---|
2933 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
|
---|
2934 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2935 | * argument.
|
---|
2936 | *
|
---|
2937 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
2938 | *
|
---|
2939 | * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
|
---|
2940 | * @param size Storage for the suggested size. May be NULL.
|
---|
2941 | *
|
---|
2942 | * @return True on success, false on failure.
|
---|
2943 | *
|
---|
2944 | */
|
---|
2945 | bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
|
---|
2946 | {
|
---|
2947 | ipc_call_t data;
|
---|
2948 | return async_data_write_receive_call(callid, &data, size);
|
---|
2949 | }
|
---|
2950 |
|
---|
2951 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
---|
2952 | *
|
---|
2953 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
|
---|
2954 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2955 | * argument.
|
---|
2956 | *
|
---|
2957 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
2958 | *
|
---|
2959 | * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
|
---|
2960 | * @param data Storage for the ipc call data.
|
---|
2961 | * @param size Storage for the suggested size. May be NULL.
|
---|
2962 | *
|
---|
2963 | * @return True on success, false on failure.
|
---|
2964 | *
|
---|
2965 | */
|
---|
2966 | bool async_data_write_receive_call(ipc_callid_t *callid, ipc_call_t *data,
|
---|
2967 | size_t *size)
|
---|
2968 | {
|
---|
2969 | assert(callid);
|
---|
2970 | assert(data);
|
---|
2971 |
|
---|
2972 | *callid = async_get_call(data);
|
---|
2973 |
|
---|
2974 | if (IPC_GET_IMETHOD(*data) != IPC_M_DATA_WRITE)
|
---|
2975 | return false;
|
---|
2976 |
|
---|
2977 | if (size)
|
---|
2978 | *size = (size_t) IPC_GET_ARG2(*data);
|
---|
2979 |
|
---|
2980 | return true;
|
---|
2981 | }
|
---|
2982 |
|
---|
2983 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
---|
2984 | *
|
---|
2985 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
|
---|
2986 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
2987 | * argument.
|
---|
2988 | *
|
---|
2989 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
2990 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
---|
2991 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
---|
2992 | *
|
---|
2993 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
2994 | *
|
---|
2995 | */
|
---|
2996 | int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
|
---|
2997 | {
|
---|
2998 | return ipc_answer_2(callid, EOK, (sysarg_t) dst, (sysarg_t) size);
|
---|
2999 | }
|
---|
3000 |
|
---|
3001 | /** Wrapper for receiving binary data or strings
|
---|
3002 | *
|
---|
3003 | * This wrapper only makes it more comfortable to use async_data_write_*
|
---|
3004 | * functions to receive binary data or strings.
|
---|
3005 | *
|
---|
3006 | * @param data Pointer to data pointer (which should be later disposed
|
---|
3007 | * by free()). If the operation fails, the pointer is not
|
---|
3008 | * touched.
|
---|
3009 | * @param nullterm If true then the received data is always zero terminated.
|
---|
3010 | * This also causes to allocate one extra byte beyond the
|
---|
3011 | * raw transmitted data.
|
---|
3012 | * @param min_size Minimum size (in bytes) of the data to receive.
|
---|
3013 | * @param max_size Maximum size (in bytes) of the data to receive. 0 means
|
---|
3014 | * no limit.
|
---|
3015 | * @param granulariy If non-zero then the size of the received data has to
|
---|
3016 | * be divisible by this value.
|
---|
3017 | * @param received If not NULL, the size of the received data is stored here.
|
---|
3018 | *
|
---|
3019 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
3020 | *
|
---|
3021 | */
|
---|
3022 | int async_data_write_accept(void **data, const bool nullterm,
|
---|
3023 | const size_t min_size, const size_t max_size, const size_t granularity,
|
---|
3024 | size_t *received)
|
---|
3025 | {
|
---|
3026 | assert(data);
|
---|
3027 |
|
---|
3028 | ipc_callid_t callid;
|
---|
3029 | size_t size;
|
---|
3030 | if (!async_data_write_receive(&callid, &size)) {
|
---|
3031 | ipc_answer_0(callid, EINVAL);
|
---|
3032 | return EINVAL;
|
---|
3033 | }
|
---|
3034 |
|
---|
3035 | if (size < min_size) {
|
---|
3036 | ipc_answer_0(callid, EINVAL);
|
---|
3037 | return EINVAL;
|
---|
3038 | }
|
---|
3039 |
|
---|
3040 | if ((max_size > 0) && (size > max_size)) {
|
---|
3041 | ipc_answer_0(callid, EINVAL);
|
---|
3042 | return EINVAL;
|
---|
3043 | }
|
---|
3044 |
|
---|
3045 | if ((granularity > 0) && ((size % granularity) != 0)) {
|
---|
3046 | ipc_answer_0(callid, EINVAL);
|
---|
3047 | return EINVAL;
|
---|
3048 | }
|
---|
3049 |
|
---|
3050 | void *arg_data;
|
---|
3051 |
|
---|
3052 | if (nullterm)
|
---|
3053 | arg_data = malloc(size + 1);
|
---|
3054 | else
|
---|
3055 | arg_data = malloc(size);
|
---|
3056 |
|
---|
3057 | if (arg_data == NULL) {
|
---|
3058 | ipc_answer_0(callid, ENOMEM);
|
---|
3059 | return ENOMEM;
|
---|
3060 | }
|
---|
3061 |
|
---|
3062 | int rc = async_data_write_finalize(callid, arg_data, size);
|
---|
3063 | if (rc != EOK) {
|
---|
3064 | free(arg_data);
|
---|
3065 | return rc;
|
---|
3066 | }
|
---|
3067 |
|
---|
3068 | if (nullterm)
|
---|
3069 | ((char *) arg_data)[size] = 0;
|
---|
3070 |
|
---|
3071 | *data = arg_data;
|
---|
3072 | if (received != NULL)
|
---|
3073 | *received = size;
|
---|
3074 |
|
---|
3075 | return EOK;
|
---|
3076 | }
|
---|
3077 |
|
---|
3078 | /** Wrapper for voiding any data that is about to be received
|
---|
3079 | *
|
---|
3080 | * This wrapper can be used to void any pending data
|
---|
3081 | *
|
---|
3082 | * @param retval Error value from @ref errno.h to be returned to the caller.
|
---|
3083 | *
|
---|
3084 | */
|
---|
3085 | void async_data_write_void(sysarg_t retval)
|
---|
3086 | {
|
---|
3087 | ipc_callid_t callid;
|
---|
3088 | async_data_write_receive(&callid, NULL);
|
---|
3089 | ipc_answer_0(callid, retval);
|
---|
3090 | }
|
---|
3091 |
|
---|
3092 | /** Wrapper for forwarding any data that is about to be received
|
---|
3093 | *
|
---|
3094 | */
|
---|
3095 | int async_data_write_forward_fast(async_exch_t *exch, sysarg_t imethod,
|
---|
3096 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4,
|
---|
3097 | ipc_call_t *dataptr)
|
---|
3098 | {
|
---|
3099 | if (exch == NULL)
|
---|
3100 | return ENOENT;
|
---|
3101 |
|
---|
3102 | ipc_callid_t callid;
|
---|
3103 | if (!async_data_write_receive(&callid, NULL)) {
|
---|
3104 | ipc_answer_0(callid, EINVAL);
|
---|
3105 | return EINVAL;
|
---|
3106 | }
|
---|
3107 |
|
---|
3108 | aid_t msg = async_send_fast(exch, imethod, arg1, arg2, arg3, arg4,
|
---|
3109 | dataptr);
|
---|
3110 | if (msg == 0) {
|
---|
3111 | ipc_answer_0(callid, EINVAL);
|
---|
3112 | return EINVAL;
|
---|
3113 | }
|
---|
3114 |
|
---|
3115 | int retval = ipc_forward_fast(callid, exch->phone, 0, 0, 0,
|
---|
3116 | IPC_FF_ROUTE_FROM_ME);
|
---|
3117 | if (retval != EOK) {
|
---|
3118 | async_forget(msg);
|
---|
3119 | ipc_answer_0(callid, retval);
|
---|
3120 | return retval;
|
---|
3121 | }
|
---|
3122 |
|
---|
3123 | sysarg_t rc;
|
---|
3124 | async_wait_for(msg, &rc);
|
---|
3125 |
|
---|
3126 | return (int) rc;
|
---|
3127 | }
|
---|
3128 |
|
---|
3129 | /** Wrapper for sending an exchange over different exchange for cloning
|
---|
3130 | *
|
---|
3131 | * @param exch Exchange to be used for sending.
|
---|
3132 | * @param clone_exch Exchange to be cloned.
|
---|
3133 | *
|
---|
3134 | */
|
---|
3135 | int async_exchange_clone(async_exch_t *exch, async_exch_t *clone_exch)
|
---|
3136 | {
|
---|
3137 | return async_req_1_0(exch, IPC_M_CONNECTION_CLONE, clone_exch->phone);
|
---|
3138 | }
|
---|
3139 |
|
---|
3140 | /** Wrapper for receiving the IPC_M_CONNECTION_CLONE calls.
|
---|
3141 | *
|
---|
3142 | * If the current call is IPC_M_CONNECTION_CLONE then a new
|
---|
3143 | * async session is created for the accepted phone.
|
---|
3144 | *
|
---|
3145 | * @param mgmt Exchange management style.
|
---|
3146 | *
|
---|
3147 | * @return New async session or NULL on failure.
|
---|
3148 | *
|
---|
3149 | */
|
---|
3150 | async_sess_t *async_clone_receive(exch_mgmt_t mgmt)
|
---|
3151 | {
|
---|
3152 | /* Accept the phone */
|
---|
3153 | ipc_call_t call;
|
---|
3154 | ipc_callid_t callid = async_get_call(&call);
|
---|
3155 | int phone = (int) IPC_GET_ARG1(call);
|
---|
3156 |
|
---|
3157 | if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECTION_CLONE) ||
|
---|
3158 | (phone < 0)) {
|
---|
3159 | async_answer_0(callid, EINVAL);
|
---|
3160 | return NULL;
|
---|
3161 | }
|
---|
3162 |
|
---|
3163 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
3164 | if (sess == NULL) {
|
---|
3165 | async_answer_0(callid, ENOMEM);
|
---|
3166 | return NULL;
|
---|
3167 | }
|
---|
3168 |
|
---|
3169 | sess->iface = 0;
|
---|
3170 | sess->mgmt = mgmt;
|
---|
3171 | sess->phone = phone;
|
---|
3172 | sess->arg1 = 0;
|
---|
3173 | sess->arg2 = 0;
|
---|
3174 | sess->arg3 = 0;
|
---|
3175 |
|
---|
3176 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
3177 | sess->remote_state_data = NULL;
|
---|
3178 |
|
---|
3179 | list_initialize(&sess->exch_list);
|
---|
3180 | fibril_mutex_initialize(&sess->mutex);
|
---|
3181 | atomic_set(&sess->refcnt, 0);
|
---|
3182 |
|
---|
3183 | /* Acknowledge the cloned phone */
|
---|
3184 | async_answer_0(callid, EOK);
|
---|
3185 |
|
---|
3186 | return sess;
|
---|
3187 | }
|
---|
3188 |
|
---|
3189 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
---|
3190 | *
|
---|
3191 | * If the current call is IPC_M_CONNECT_TO_ME then a new
|
---|
3192 | * async session is created for the accepted phone.
|
---|
3193 | *
|
---|
3194 | * @param mgmt Exchange management style.
|
---|
3195 | *
|
---|
3196 | * @return New async session.
|
---|
3197 | * @return NULL on failure.
|
---|
3198 | *
|
---|
3199 | */
|
---|
3200 | async_sess_t *async_callback_receive(exch_mgmt_t mgmt)
|
---|
3201 | {
|
---|
3202 | /* Accept the phone */
|
---|
3203 | ipc_call_t call;
|
---|
3204 | ipc_callid_t callid = async_get_call(&call);
|
---|
3205 | int phone = (int) IPC_GET_ARG5(call);
|
---|
3206 |
|
---|
3207 | if ((IPC_GET_IMETHOD(call) != IPC_M_CONNECT_TO_ME) ||
|
---|
3208 | (phone < 0)) {
|
---|
3209 | async_answer_0(callid, EINVAL);
|
---|
3210 | return NULL;
|
---|
3211 | }
|
---|
3212 |
|
---|
3213 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
3214 | if (sess == NULL) {
|
---|
3215 | async_answer_0(callid, ENOMEM);
|
---|
3216 | return NULL;
|
---|
3217 | }
|
---|
3218 |
|
---|
3219 | sess->iface = 0;
|
---|
3220 | sess->mgmt = mgmt;
|
---|
3221 | sess->phone = phone;
|
---|
3222 | sess->arg1 = 0;
|
---|
3223 | sess->arg2 = 0;
|
---|
3224 | sess->arg3 = 0;
|
---|
3225 |
|
---|
3226 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
3227 | sess->remote_state_data = NULL;
|
---|
3228 |
|
---|
3229 | list_initialize(&sess->exch_list);
|
---|
3230 | fibril_mutex_initialize(&sess->mutex);
|
---|
3231 | atomic_set(&sess->refcnt, 0);
|
---|
3232 |
|
---|
3233 | /* Acknowledge the connected phone */
|
---|
3234 | async_answer_0(callid, EOK);
|
---|
3235 |
|
---|
3236 | return sess;
|
---|
3237 | }
|
---|
3238 |
|
---|
3239 | /** Wrapper for receiving the IPC_M_CONNECT_TO_ME calls.
|
---|
3240 | *
|
---|
3241 | * If the call is IPC_M_CONNECT_TO_ME then a new
|
---|
3242 | * async session is created. However, the phone is
|
---|
3243 | * not accepted automatically.
|
---|
3244 | *
|
---|
3245 | * @param mgmt Exchange management style.
|
---|
3246 | * @param call Call data.
|
---|
3247 | *
|
---|
3248 | * @return New async session.
|
---|
3249 | * @return NULL on failure.
|
---|
3250 | * @return NULL if the call is not IPC_M_CONNECT_TO_ME.
|
---|
3251 | *
|
---|
3252 | */
|
---|
3253 | async_sess_t *async_callback_receive_start(exch_mgmt_t mgmt, ipc_call_t *call)
|
---|
3254 | {
|
---|
3255 | int phone = (int) IPC_GET_ARG5(*call);
|
---|
3256 |
|
---|
3257 | if ((IPC_GET_IMETHOD(*call) != IPC_M_CONNECT_TO_ME) ||
|
---|
3258 | (phone < 0))
|
---|
3259 | return NULL;
|
---|
3260 |
|
---|
3261 | async_sess_t *sess = (async_sess_t *) malloc(sizeof(async_sess_t));
|
---|
3262 | if (sess == NULL)
|
---|
3263 | return NULL;
|
---|
3264 |
|
---|
3265 | sess->iface = 0;
|
---|
3266 | sess->mgmt = mgmt;
|
---|
3267 | sess->phone = phone;
|
---|
3268 | sess->arg1 = 0;
|
---|
3269 | sess->arg2 = 0;
|
---|
3270 | sess->arg3 = 0;
|
---|
3271 |
|
---|
3272 | fibril_mutex_initialize(&sess->remote_state_mtx);
|
---|
3273 | sess->remote_state_data = NULL;
|
---|
3274 |
|
---|
3275 | list_initialize(&sess->exch_list);
|
---|
3276 | fibril_mutex_initialize(&sess->mutex);
|
---|
3277 | atomic_set(&sess->refcnt, 0);
|
---|
3278 |
|
---|
3279 | return sess;
|
---|
3280 | }
|
---|
3281 |
|
---|
3282 | int async_state_change_start(async_exch_t *exch, sysarg_t arg1, sysarg_t arg2,
|
---|
3283 | sysarg_t arg3, async_exch_t *other_exch)
|
---|
3284 | {
|
---|
3285 | return async_req_5_0(exch, IPC_M_STATE_CHANGE_AUTHORIZE,
|
---|
3286 | arg1, arg2, arg3, 0, other_exch->phone);
|
---|
3287 | }
|
---|
3288 |
|
---|
3289 | bool async_state_change_receive(ipc_callid_t *callid, sysarg_t *arg1,
|
---|
3290 | sysarg_t *arg2, sysarg_t *arg3)
|
---|
3291 | {
|
---|
3292 | assert(callid);
|
---|
3293 |
|
---|
3294 | ipc_call_t call;
|
---|
3295 | *callid = async_get_call(&call);
|
---|
3296 |
|
---|
3297 | if (IPC_GET_IMETHOD(call) != IPC_M_STATE_CHANGE_AUTHORIZE)
|
---|
3298 | return false;
|
---|
3299 |
|
---|
3300 | if (arg1)
|
---|
3301 | *arg1 = IPC_GET_ARG1(call);
|
---|
3302 | if (arg2)
|
---|
3303 | *arg2 = IPC_GET_ARG2(call);
|
---|
3304 | if (arg3)
|
---|
3305 | *arg3 = IPC_GET_ARG3(call);
|
---|
3306 |
|
---|
3307 | return true;
|
---|
3308 | }
|
---|
3309 |
|
---|
3310 | int async_state_change_finalize(ipc_callid_t callid, async_exch_t *other_exch)
|
---|
3311 | {
|
---|
3312 | return ipc_answer_1(callid, EOK, other_exch->phone);
|
---|
3313 | }
|
---|
3314 |
|
---|
3315 | /** Lock and get session remote state
|
---|
3316 | *
|
---|
3317 | * Lock and get the local replica of the remote state
|
---|
3318 | * in stateful sessions. The call should be paired
|
---|
3319 | * with async_remote_state_release*().
|
---|
3320 | *
|
---|
3321 | * @param[in] sess Stateful session.
|
---|
3322 | *
|
---|
3323 | * @return Local replica of the remote state.
|
---|
3324 | *
|
---|
3325 | */
|
---|
3326 | void *async_remote_state_acquire(async_sess_t *sess)
|
---|
3327 | {
|
---|
3328 | fibril_mutex_lock(&sess->remote_state_mtx);
|
---|
3329 | return sess->remote_state_data;
|
---|
3330 | }
|
---|
3331 |
|
---|
3332 | /** Update the session remote state
|
---|
3333 | *
|
---|
3334 | * Update the local replica of the remote state
|
---|
3335 | * in stateful sessions. The remote state must
|
---|
3336 | * be already locked.
|
---|
3337 | *
|
---|
3338 | * @param[in] sess Stateful session.
|
---|
3339 | * @param[in] state New local replica of the remote state.
|
---|
3340 | *
|
---|
3341 | */
|
---|
3342 | void async_remote_state_update(async_sess_t *sess, void *state)
|
---|
3343 | {
|
---|
3344 | assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
|
---|
3345 | sess->remote_state_data = state;
|
---|
3346 | }
|
---|
3347 |
|
---|
3348 | /** Release the session remote state
|
---|
3349 | *
|
---|
3350 | * Unlock the local replica of the remote state
|
---|
3351 | * in stateful sessions.
|
---|
3352 | *
|
---|
3353 | * @param[in] sess Stateful session.
|
---|
3354 | *
|
---|
3355 | */
|
---|
3356 | void async_remote_state_release(async_sess_t *sess)
|
---|
3357 | {
|
---|
3358 | assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
|
---|
3359 |
|
---|
3360 | fibril_mutex_unlock(&sess->remote_state_mtx);
|
---|
3361 | }
|
---|
3362 |
|
---|
3363 | /** Release the session remote state and end an exchange
|
---|
3364 | *
|
---|
3365 | * Unlock the local replica of the remote state
|
---|
3366 | * in stateful sessions. This is convenience function
|
---|
3367 | * which gets the session pointer from the exchange
|
---|
3368 | * and also ends the exchange.
|
---|
3369 | *
|
---|
3370 | * @param[in] exch Stateful session's exchange.
|
---|
3371 | *
|
---|
3372 | */
|
---|
3373 | void async_remote_state_release_exchange(async_exch_t *exch)
|
---|
3374 | {
|
---|
3375 | if (exch == NULL)
|
---|
3376 | return;
|
---|
3377 |
|
---|
3378 | async_sess_t *sess = exch->sess;
|
---|
3379 | assert(fibril_mutex_is_locked(&sess->remote_state_mtx));
|
---|
3380 |
|
---|
3381 | async_exchange_end(exch);
|
---|
3382 | fibril_mutex_unlock(&sess->remote_state_mtx);
|
---|
3383 | }
|
---|
3384 |
|
---|
3385 | void *async_as_area_create(void *base, size_t size, unsigned int flags,
|
---|
3386 | async_sess_t *pager, sysarg_t id1, sysarg_t id2, sysarg_t id3)
|
---|
3387 | {
|
---|
3388 | as_area_pager_info_t pager_info = {
|
---|
3389 | .pager = pager->phone,
|
---|
3390 | .id1 = id1,
|
---|
3391 | .id2 = id2,
|
---|
3392 | .id3 = id3
|
---|
3393 | };
|
---|
3394 | return as_area_create(base, size, flags, &pager_info);
|
---|
3395 | }
|
---|
3396 |
|
---|
3397 | /** @}
|
---|
3398 | */
|
---|