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