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