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