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