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