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 synchronization problems.
|
---|
44 | *
|
---|
45 | * Example of use (pseudo C):
|
---|
46 | *
|
---|
47 | * 1) Multithreaded client application
|
---|
48 | *
|
---|
49 | * fibril_create(fibril1, ...);
|
---|
50 | * fibril_create(fibril2, ...);
|
---|
51 | * ...
|
---|
52 | *
|
---|
53 | * int fibril1(void *arg)
|
---|
54 | * {
|
---|
55 | * conn = async_connect_me_to();
|
---|
56 | * c1 = async_send(conn);
|
---|
57 | * c2 = async_send(conn);
|
---|
58 | * async_wait_for(c1);
|
---|
59 | * async_wait_for(c2);
|
---|
60 | * ...
|
---|
61 | * }
|
---|
62 | *
|
---|
63 | *
|
---|
64 | * 2) Multithreaded server application
|
---|
65 | *
|
---|
66 | * main()
|
---|
67 | * {
|
---|
68 | * async_manager();
|
---|
69 | * }
|
---|
70 | *
|
---|
71 | * my_client_connection(icallid, *icall)
|
---|
72 | * {
|
---|
73 | * if (want_refuse) {
|
---|
74 | * async_answer_0(icallid, ELIMIT);
|
---|
75 | * return;
|
---|
76 | * }
|
---|
77 | * async_answer_0(icallid, EOK);
|
---|
78 | *
|
---|
79 | * callid = async_get_call(&call);
|
---|
80 | * somehow_handle_the_call(callid, call);
|
---|
81 | * async_answer_2(callid, 1, 2, 3);
|
---|
82 | *
|
---|
83 | * callid = async_get_call(&call);
|
---|
84 | * ...
|
---|
85 | * }
|
---|
86 | *
|
---|
87 | */
|
---|
88 |
|
---|
89 | #define LIBC_ASYNC_C_
|
---|
90 | #include <ipc/ipc.h>
|
---|
91 | #include <async.h>
|
---|
92 | #undef LIBC_ASYNC_C_
|
---|
93 |
|
---|
94 | #include <futex.h>
|
---|
95 | #include <fibril.h>
|
---|
96 | #include <stdio.h>
|
---|
97 | #include <adt/hash_table.h>
|
---|
98 | #include <adt/list.h>
|
---|
99 | #include <assert.h>
|
---|
100 | #include <errno.h>
|
---|
101 | #include <sys/time.h>
|
---|
102 | #include <arch/barrier.h>
|
---|
103 | #include <bool.h>
|
---|
104 | #include "private/async.h"
|
---|
105 |
|
---|
106 | atomic_t async_futex = FUTEX_INITIALIZER;
|
---|
107 |
|
---|
108 | /** Number of threads waiting for IPC in the kernel. */
|
---|
109 | atomic_t threads_in_ipc_wait = { 0 };
|
---|
110 |
|
---|
111 | typedef struct {
|
---|
112 | awaiter_t wdata;
|
---|
113 |
|
---|
114 | /** If reply was received. */
|
---|
115 | bool done;
|
---|
116 |
|
---|
117 | /** Pointer to where the answer data is stored. */
|
---|
118 | ipc_call_t *dataptr;
|
---|
119 |
|
---|
120 | sysarg_t retval;
|
---|
121 | } amsg_t;
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Structures of this type are used to group information about
|
---|
125 | * a call and about a message queue link.
|
---|
126 | */
|
---|
127 | typedef struct {
|
---|
128 | link_t link;
|
---|
129 | ipc_callid_t callid;
|
---|
130 | ipc_call_t call;
|
---|
131 | } msg_t;
|
---|
132 |
|
---|
133 | typedef struct {
|
---|
134 | sysarg_t in_task_hash;
|
---|
135 | link_t link;
|
---|
136 | int refcnt;
|
---|
137 | void *data;
|
---|
138 | } client_t;
|
---|
139 |
|
---|
140 | typedef struct {
|
---|
141 | awaiter_t wdata;
|
---|
142 |
|
---|
143 | /** Hash table link. */
|
---|
144 | link_t link;
|
---|
145 |
|
---|
146 | /** Incoming client task hash. */
|
---|
147 | sysarg_t in_task_hash;
|
---|
148 | /** Incoming phone hash. */
|
---|
149 | sysarg_t in_phone_hash;
|
---|
150 |
|
---|
151 | /** Link to the client tracking structure. */
|
---|
152 | client_t *client;
|
---|
153 |
|
---|
154 | /** Messages that should be delivered to this fibril. */
|
---|
155 | link_t msg_queue;
|
---|
156 |
|
---|
157 | /** Identification of the opening call. */
|
---|
158 | ipc_callid_t callid;
|
---|
159 | /** Call data of the opening call. */
|
---|
160 | ipc_call_t call;
|
---|
161 |
|
---|
162 | /** Identification of the closing call. */
|
---|
163 | ipc_callid_t close_callid;
|
---|
164 |
|
---|
165 | /** Fibril function that will be used to handle the connection. */
|
---|
166 | void (*cfibril)(ipc_callid_t, ipc_call_t *);
|
---|
167 | } connection_t;
|
---|
168 |
|
---|
169 | /** Identifier of the incoming connection handled by the current fibril. */
|
---|
170 | static fibril_local connection_t *FIBRIL_connection;
|
---|
171 |
|
---|
172 | static void *default_client_data_constructor(void)
|
---|
173 | {
|
---|
174 | return NULL;
|
---|
175 | }
|
---|
176 |
|
---|
177 | static void default_client_data_destructor(void *data)
|
---|
178 | {
|
---|
179 | }
|
---|
180 |
|
---|
181 | static async_client_data_ctor_t async_client_data_create =
|
---|
182 | default_client_data_constructor;
|
---|
183 | static async_client_data_dtor_t async_client_data_destroy =
|
---|
184 | default_client_data_destructor;
|
---|
185 |
|
---|
186 | void async_set_client_data_constructor(async_client_data_ctor_t ctor)
|
---|
187 | {
|
---|
188 | async_client_data_create = ctor;
|
---|
189 | }
|
---|
190 |
|
---|
191 | void async_set_client_data_destructor(async_client_data_dtor_t dtor)
|
---|
192 | {
|
---|
193 | async_client_data_destroy = dtor;
|
---|
194 | }
|
---|
195 |
|
---|
196 | void *async_client_data_get(void)
|
---|
197 | {
|
---|
198 | assert(FIBRIL_connection);
|
---|
199 | return FIBRIL_connection->client->data;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /** Default fibril function that gets called to handle new connection.
|
---|
203 | *
|
---|
204 | * This function is defined as a weak symbol - to be redefined in user code.
|
---|
205 | *
|
---|
206 | * @param callid Hash of the incoming call.
|
---|
207 | * @param call Data of the incoming call.
|
---|
208 | *
|
---|
209 | */
|
---|
210 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
|
---|
211 | {
|
---|
212 | ipc_answer_0(callid, ENOENT);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Pointer to a fibril function that will be used to handle connections.
|
---|
217 | */
|
---|
218 | static async_client_conn_t client_connection = default_client_connection;
|
---|
219 |
|
---|
220 | /** Default fibril function that gets called to handle interrupt notifications.
|
---|
221 | *
|
---|
222 | * This function is defined as a weak symbol - to be redefined in user code.
|
---|
223 | *
|
---|
224 | * @param callid Hash of the incoming call.
|
---|
225 | * @param call Data of the incoming call.
|
---|
226 | *
|
---|
227 | */
|
---|
228 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
|
---|
229 | {
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Pointer to a fibril function that will be used to handle interrupt
|
---|
234 | * notifications.
|
---|
235 | */
|
---|
236 | static async_client_conn_t interrupt_received = default_interrupt_received;
|
---|
237 |
|
---|
238 | static hash_table_t client_hash_table;
|
---|
239 | static hash_table_t conn_hash_table;
|
---|
240 | static LIST_INITIALIZE(timeout_list);
|
---|
241 |
|
---|
242 | #define CLIENT_HASH_TABLE_BUCKETS 32
|
---|
243 | #define CONN_HASH_TABLE_BUCKETS 32
|
---|
244 |
|
---|
245 | static hash_index_t client_hash(unsigned long key[])
|
---|
246 | {
|
---|
247 | assert(key);
|
---|
248 | return (((key[0]) >> 4) % CLIENT_HASH_TABLE_BUCKETS);
|
---|
249 | }
|
---|
250 |
|
---|
251 | static int client_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
252 | {
|
---|
253 | client_t *client = hash_table_get_instance(item, client_t, link);
|
---|
254 | return (key[0] == client->in_task_hash);
|
---|
255 | }
|
---|
256 |
|
---|
257 | static void client_remove(link_t *item)
|
---|
258 | {
|
---|
259 | }
|
---|
260 |
|
---|
261 | /** Operations for the client hash table. */
|
---|
262 | static hash_table_operations_t client_hash_table_ops = {
|
---|
263 | .hash = client_hash,
|
---|
264 | .compare = client_compare,
|
---|
265 | .remove_callback = client_remove
|
---|
266 | };
|
---|
267 |
|
---|
268 | /** Compute hash into the connection hash table based on the source phone hash.
|
---|
269 | *
|
---|
270 | * @param key Pointer to source phone hash.
|
---|
271 | *
|
---|
272 | * @return Index into the connection hash table.
|
---|
273 | *
|
---|
274 | */
|
---|
275 | static hash_index_t conn_hash(unsigned long key[])
|
---|
276 | {
|
---|
277 | assert(key);
|
---|
278 | return (((key[0]) >> 4) % CONN_HASH_TABLE_BUCKETS);
|
---|
279 | }
|
---|
280 |
|
---|
281 | /** Compare hash table item with a key.
|
---|
282 | *
|
---|
283 | * @param key Array containing the source phone hash as the only item.
|
---|
284 | * @param keys Expected 1 but ignored.
|
---|
285 | * @param item Connection hash table item.
|
---|
286 | *
|
---|
287 | * @return True on match, false otherwise.
|
---|
288 | *
|
---|
289 | */
|
---|
290 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
291 | {
|
---|
292 | connection_t *conn = hash_table_get_instance(item, connection_t, link);
|
---|
293 | return (key[0] == conn->in_phone_hash);
|
---|
294 | }
|
---|
295 |
|
---|
296 | static void conn_remove(link_t *item)
|
---|
297 | {
|
---|
298 | }
|
---|
299 |
|
---|
300 | /** Operations for the connection hash table. */
|
---|
301 | static hash_table_operations_t conn_hash_table_ops = {
|
---|
302 | .hash = conn_hash,
|
---|
303 | .compare = conn_compare,
|
---|
304 | .remove_callback = conn_remove
|
---|
305 | };
|
---|
306 |
|
---|
307 | /** Sort in current fibril's timeout request.
|
---|
308 | *
|
---|
309 | * @param wd Wait data of the current fibril.
|
---|
310 | *
|
---|
311 | */
|
---|
312 | void async_insert_timeout(awaiter_t *wd)
|
---|
313 | {
|
---|
314 | wd->to_event.occurred = false;
|
---|
315 | wd->to_event.inlist = true;
|
---|
316 |
|
---|
317 | link_t *tmp = timeout_list.next;
|
---|
318 | while (tmp != &timeout_list) {
|
---|
319 | awaiter_t *cur
|
---|
320 | = list_get_instance(tmp, awaiter_t, to_event.link);
|
---|
321 |
|
---|
322 | if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
|
---|
323 | break;
|
---|
324 |
|
---|
325 | tmp = tmp->next;
|
---|
326 | }
|
---|
327 |
|
---|
328 | list_append(&wd->to_event.link, tmp);
|
---|
329 | }
|
---|
330 |
|
---|
331 | /** Try to route a call to an appropriate connection fibril.
|
---|
332 | *
|
---|
333 | * If the proper connection fibril is found, a message with the call is added to
|
---|
334 | * its message queue. If the fibril was not active, it is activated and all
|
---|
335 | * timeouts are unregistered.
|
---|
336 | *
|
---|
337 | * @param callid Hash of the incoming call.
|
---|
338 | * @param call Data of the incoming call.
|
---|
339 | *
|
---|
340 | * @return False if the call doesn't match any connection.
|
---|
341 | * @return True if the call was passed to the respective connection fibril.
|
---|
342 | *
|
---|
343 | */
|
---|
344 | static bool route_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
345 | {
|
---|
346 | futex_down(&async_futex);
|
---|
347 |
|
---|
348 | unsigned long key = call->in_phone_hash;
|
---|
349 | link_t *hlp = hash_table_find(&conn_hash_table, &key);
|
---|
350 |
|
---|
351 | if (!hlp) {
|
---|
352 | futex_up(&async_futex);
|
---|
353 | return false;
|
---|
354 | }
|
---|
355 |
|
---|
356 | connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
|
---|
357 |
|
---|
358 | msg_t *msg = malloc(sizeof(*msg));
|
---|
359 | if (!msg) {
|
---|
360 | futex_up(&async_futex);
|
---|
361 | return false;
|
---|
362 | }
|
---|
363 |
|
---|
364 | msg->callid = callid;
|
---|
365 | msg->call = *call;
|
---|
366 | list_append(&msg->link, &conn->msg_queue);
|
---|
367 |
|
---|
368 | if (IPC_GET_IMETHOD(*call) == IPC_M_PHONE_HUNGUP)
|
---|
369 | conn->close_callid = callid;
|
---|
370 |
|
---|
371 | /* If the connection fibril is waiting for an event, activate it */
|
---|
372 | if (!conn->wdata.active) {
|
---|
373 |
|
---|
374 | /* If in timeout list, remove it */
|
---|
375 | if (conn->wdata.to_event.inlist) {
|
---|
376 | conn->wdata.to_event.inlist = false;
|
---|
377 | list_remove(&conn->wdata.to_event.link);
|
---|
378 | }
|
---|
379 |
|
---|
380 | conn->wdata.active = true;
|
---|
381 | fibril_add_ready(conn->wdata.fid);
|
---|
382 | }
|
---|
383 |
|
---|
384 | futex_up(&async_futex);
|
---|
385 | return true;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /** Notification fibril.
|
---|
389 | *
|
---|
390 | * When a notification arrives, a fibril with this implementing function is
|
---|
391 | * created. It calls interrupt_received() and does the final cleanup.
|
---|
392 | *
|
---|
393 | * @param arg Message structure pointer.
|
---|
394 | *
|
---|
395 | * @return Always zero.
|
---|
396 | *
|
---|
397 | */
|
---|
398 | static int notification_fibril(void *arg)
|
---|
399 | {
|
---|
400 | msg_t *msg = (msg_t *) arg;
|
---|
401 | interrupt_received(msg->callid, &msg->call);
|
---|
402 |
|
---|
403 | free(msg);
|
---|
404 | return 0;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /** Process interrupt notification.
|
---|
408 | *
|
---|
409 | * A new fibril is created which would process the notification.
|
---|
410 | *
|
---|
411 | * @param callid Hash of the incoming call.
|
---|
412 | * @param call Data of the incoming call.
|
---|
413 | *
|
---|
414 | * @return False if an error occured.
|
---|
415 | * True if the call was passed to the notification fibril.
|
---|
416 | *
|
---|
417 | */
|
---|
418 | static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
|
---|
419 | {
|
---|
420 | futex_down(&async_futex);
|
---|
421 |
|
---|
422 | msg_t *msg = malloc(sizeof(*msg));
|
---|
423 | if (!msg) {
|
---|
424 | futex_up(&async_futex);
|
---|
425 | return false;
|
---|
426 | }
|
---|
427 |
|
---|
428 | msg->callid = callid;
|
---|
429 | msg->call = *call;
|
---|
430 |
|
---|
431 | fid_t fid = fibril_create(notification_fibril, msg);
|
---|
432 | if (fid == 0) {
|
---|
433 | free(msg);
|
---|
434 | futex_up(&async_futex);
|
---|
435 | return false;
|
---|
436 | }
|
---|
437 |
|
---|
438 | fibril_add_ready(fid);
|
---|
439 |
|
---|
440 | futex_up(&async_futex);
|
---|
441 | return true;
|
---|
442 | }
|
---|
443 |
|
---|
444 | /** Return new incoming message for the current (fibril-local) connection.
|
---|
445 | *
|
---|
446 | * @param call Storage where the incoming call data will be stored.
|
---|
447 | * @param usecs Timeout in microseconds. Zero denotes no timeout.
|
---|
448 | *
|
---|
449 | * @return If no timeout was specified, then a hash of the
|
---|
450 | * incoming call is returned. If a timeout is specified,
|
---|
451 | * then a hash of the incoming call is returned unless
|
---|
452 | * the timeout expires prior to receiving a message. In
|
---|
453 | * that case zero is returned.
|
---|
454 | *
|
---|
455 | */
|
---|
456 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
|
---|
457 | {
|
---|
458 | assert(FIBRIL_connection);
|
---|
459 |
|
---|
460 | /* Why doing this?
|
---|
461 | * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
|
---|
462 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
|
---|
463 | * I would never expect to find so many errors in
|
---|
464 | * a compiler.
|
---|
465 | */
|
---|
466 | connection_t *conn = FIBRIL_connection;
|
---|
467 |
|
---|
468 | futex_down(&async_futex);
|
---|
469 |
|
---|
470 | if (usecs) {
|
---|
471 | gettimeofday(&conn->wdata.to_event.expires, NULL);
|
---|
472 | tv_add(&conn->wdata.to_event.expires, usecs);
|
---|
473 | } else
|
---|
474 | conn->wdata.to_event.inlist = false;
|
---|
475 |
|
---|
476 | /* If nothing in queue, wait until something arrives */
|
---|
477 | while (list_empty(&conn->msg_queue)) {
|
---|
478 | if (conn->close_callid) {
|
---|
479 | /*
|
---|
480 | * Handle the case when the connection was already
|
---|
481 | * closed by the client but the server did not notice
|
---|
482 | * the first IPC_M_PHONE_HUNGUP call and continues to
|
---|
483 | * call async_get_call_timeout(). Repeat
|
---|
484 | * IPC_M_PHONE_HUNGUP until the caller notices.
|
---|
485 | */
|
---|
486 | memset(call, 0, sizeof(ipc_call_t));
|
---|
487 | IPC_SET_IMETHOD(*call, IPC_M_PHONE_HUNGUP);
|
---|
488 | futex_up(&async_futex);
|
---|
489 | return conn->close_callid;
|
---|
490 | }
|
---|
491 |
|
---|
492 | if (usecs)
|
---|
493 | async_insert_timeout(&conn->wdata);
|
---|
494 |
|
---|
495 | conn->wdata.active = false;
|
---|
496 |
|
---|
497 | /*
|
---|
498 | * Note: the current fibril will be rescheduled either due to a
|
---|
499 | * timeout or due to an arriving message destined to it. In the
|
---|
500 | * former case, handle_expired_timeouts() and, in the latter
|
---|
501 | * case, route_call() will perform the wakeup.
|
---|
502 | */
|
---|
503 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
504 |
|
---|
505 | /*
|
---|
506 | * Futex is up after getting back from async_manager.
|
---|
507 | * Get it again.
|
---|
508 | */
|
---|
509 | futex_down(&async_futex);
|
---|
510 | if ((usecs) && (conn->wdata.to_event.occurred)
|
---|
511 | && (list_empty(&conn->msg_queue))) {
|
---|
512 | /* If we timed out -> exit */
|
---|
513 | futex_up(&async_futex);
|
---|
514 | return 0;
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
|
---|
519 | list_remove(&msg->link);
|
---|
520 |
|
---|
521 | ipc_callid_t callid = msg->callid;
|
---|
522 | *call = msg->call;
|
---|
523 | free(msg);
|
---|
524 |
|
---|
525 | futex_up(&async_futex);
|
---|
526 | return callid;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** Wrapper for client connection fibril.
|
---|
530 | *
|
---|
531 | * When a new connection arrives, a fibril with this implementing function is
|
---|
532 | * created. It calls client_connection() and does the final cleanup.
|
---|
533 | *
|
---|
534 | * @param arg Connection structure pointer.
|
---|
535 | *
|
---|
536 | * @return Always zero.
|
---|
537 | *
|
---|
538 | */
|
---|
539 | static int connection_fibril(void *arg)
|
---|
540 | {
|
---|
541 | /*
|
---|
542 | * Setup fibril-local connection pointer.
|
---|
543 | */
|
---|
544 | FIBRIL_connection = (connection_t *) arg;
|
---|
545 |
|
---|
546 | futex_down(&async_futex);
|
---|
547 |
|
---|
548 | /*
|
---|
549 | * Add our reference for the current connection in the client task
|
---|
550 | * tracking structure. If this is the first reference, create and
|
---|
551 | * hash in a new tracking structure.
|
---|
552 | */
|
---|
553 |
|
---|
554 | unsigned long key = FIBRIL_connection->in_task_hash;
|
---|
555 | link_t *lnk = hash_table_find(&client_hash_table, &key);
|
---|
556 |
|
---|
557 | client_t *client;
|
---|
558 |
|
---|
559 | if (lnk) {
|
---|
560 | client = hash_table_get_instance(lnk, client_t, link);
|
---|
561 | client->refcnt++;
|
---|
562 | } else {
|
---|
563 | client = malloc(sizeof(client_t));
|
---|
564 | if (!client) {
|
---|
565 | ipc_answer_0(FIBRIL_connection->callid, ENOMEM);
|
---|
566 | futex_up(&async_futex);
|
---|
567 | return 0;
|
---|
568 | }
|
---|
569 |
|
---|
570 | client->in_task_hash = FIBRIL_connection->in_task_hash;
|
---|
571 |
|
---|
572 | async_serialize_start();
|
---|
573 | client->data = async_client_data_create();
|
---|
574 | async_serialize_end();
|
---|
575 |
|
---|
576 | client->refcnt = 1;
|
---|
577 | hash_table_insert(&client_hash_table, &key, &client->link);
|
---|
578 | }
|
---|
579 |
|
---|
580 | futex_up(&async_futex);
|
---|
581 |
|
---|
582 | FIBRIL_connection->client = client;
|
---|
583 |
|
---|
584 | /*
|
---|
585 | * Call the connection handler function.
|
---|
586 | */
|
---|
587 | FIBRIL_connection->cfibril(FIBRIL_connection->callid,
|
---|
588 | &FIBRIL_connection->call);
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * Remove the reference for this client task connection.
|
---|
592 | */
|
---|
593 | bool destroy;
|
---|
594 |
|
---|
595 | futex_down(&async_futex);
|
---|
596 |
|
---|
597 | if (--client->refcnt == 0) {
|
---|
598 | hash_table_remove(&client_hash_table, &key, 1);
|
---|
599 | destroy = true;
|
---|
600 | } else
|
---|
601 | destroy = false;
|
---|
602 |
|
---|
603 | futex_up(&async_futex);
|
---|
604 |
|
---|
605 | if (destroy) {
|
---|
606 | if (client->data)
|
---|
607 | async_client_data_destroy(client->data);
|
---|
608 |
|
---|
609 | free(client);
|
---|
610 | }
|
---|
611 |
|
---|
612 | /*
|
---|
613 | * Remove myself from the connection hash table.
|
---|
614 | */
|
---|
615 | futex_down(&async_futex);
|
---|
616 | key = FIBRIL_connection->in_phone_hash;
|
---|
617 | hash_table_remove(&conn_hash_table, &key, 1);
|
---|
618 | futex_up(&async_futex);
|
---|
619 |
|
---|
620 | /*
|
---|
621 | * Answer all remaining messages with EHANGUP.
|
---|
622 | */
|
---|
623 | while (!list_empty(&FIBRIL_connection->msg_queue)) {
|
---|
624 | msg_t *msg =
|
---|
625 | list_get_instance(FIBRIL_connection->msg_queue.next, msg_t,
|
---|
626 | link);
|
---|
627 |
|
---|
628 | list_remove(&msg->link);
|
---|
629 | ipc_answer_0(msg->callid, EHANGUP);
|
---|
630 | free(msg);
|
---|
631 | }
|
---|
632 |
|
---|
633 | /*
|
---|
634 | * If the connection was hung-up, answer the last call,
|
---|
635 | * i.e. IPC_M_PHONE_HUNGUP.
|
---|
636 | */
|
---|
637 | if (FIBRIL_connection->close_callid)
|
---|
638 | ipc_answer_0(FIBRIL_connection->close_callid, EOK);
|
---|
639 |
|
---|
640 | free(FIBRIL_connection);
|
---|
641 | return 0;
|
---|
642 | }
|
---|
643 |
|
---|
644 | /** Create a new fibril for a new connection.
|
---|
645 | *
|
---|
646 | * Create new fibril for connection, fill in connection structures and inserts
|
---|
647 | * it into the hash table, so that later we can easily do routing of messages to
|
---|
648 | * particular fibrils.
|
---|
649 | *
|
---|
650 | * @param in_task_hash Identification of the incoming connection.
|
---|
651 | * @param in_phone_hash Identification of the incoming connection.
|
---|
652 | * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
|
---|
653 | * If callid is zero, the connection was opened by
|
---|
654 | * accepting the IPC_M_CONNECT_TO_ME call and this function
|
---|
655 | * is called directly by the server.
|
---|
656 | * @param call Call data of the opening call.
|
---|
657 | * @param cfibril Fibril function that should be called upon opening the
|
---|
658 | * connection.
|
---|
659 | *
|
---|
660 | * @return New fibril id or NULL on failure.
|
---|
661 | *
|
---|
662 | */
|
---|
663 | fid_t async_new_connection(sysarg_t in_task_hash, sysarg_t in_phone_hash,
|
---|
664 | ipc_callid_t callid, ipc_call_t *call,
|
---|
665 | void (*cfibril)(ipc_callid_t, ipc_call_t *))
|
---|
666 | {
|
---|
667 | connection_t *conn = malloc(sizeof(*conn));
|
---|
668 | if (!conn) {
|
---|
669 | if (callid)
|
---|
670 | ipc_answer_0(callid, ENOMEM);
|
---|
671 |
|
---|
672 | return (uintptr_t) NULL;
|
---|
673 | }
|
---|
674 |
|
---|
675 | conn->in_task_hash = in_task_hash;
|
---|
676 | conn->in_phone_hash = in_phone_hash;
|
---|
677 | list_initialize(&conn->msg_queue);
|
---|
678 | conn->callid = callid;
|
---|
679 | conn->close_callid = 0;
|
---|
680 |
|
---|
681 | if (call)
|
---|
682 | conn->call = *call;
|
---|
683 |
|
---|
684 | /* We will activate the fibril ASAP */
|
---|
685 | conn->wdata.active = true;
|
---|
686 | conn->cfibril = cfibril;
|
---|
687 | conn->wdata.fid = fibril_create(connection_fibril, conn);
|
---|
688 |
|
---|
689 | if (conn->wdata.fid == 0) {
|
---|
690 | free(conn);
|
---|
691 |
|
---|
692 | if (callid)
|
---|
693 | ipc_answer_0(callid, ENOMEM);
|
---|
694 |
|
---|
695 | return (uintptr_t) NULL;
|
---|
696 | }
|
---|
697 |
|
---|
698 | /* Add connection to the connection hash table */
|
---|
699 | unsigned long key = conn->in_phone_hash;
|
---|
700 |
|
---|
701 | futex_down(&async_futex);
|
---|
702 | hash_table_insert(&conn_hash_table, &key, &conn->link);
|
---|
703 | futex_up(&async_futex);
|
---|
704 |
|
---|
705 | fibril_add_ready(conn->wdata.fid);
|
---|
706 |
|
---|
707 | return conn->wdata.fid;
|
---|
708 | }
|
---|
709 |
|
---|
710 | /** Handle a call that was received.
|
---|
711 | *
|
---|
712 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
---|
713 | * Otherwise the call is routed to its connection fibril.
|
---|
714 | *
|
---|
715 | * @param callid Hash of the incoming call.
|
---|
716 | * @param call Data of the incoming call.
|
---|
717 | *
|
---|
718 | */
|
---|
719 | static void handle_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
720 | {
|
---|
721 | /* Unrouted call - take some default action */
|
---|
722 | if ((callid & IPC_CALLID_NOTIFICATION)) {
|
---|
723 | process_notification(callid, call);
|
---|
724 | return;
|
---|
725 | }
|
---|
726 |
|
---|
727 | switch (IPC_GET_IMETHOD(*call)) {
|
---|
728 | case IPC_M_CONNECT_ME:
|
---|
729 | case IPC_M_CONNECT_ME_TO:
|
---|
730 | /* Open new connection with fibril, etc. */
|
---|
731 | async_new_connection(call->in_task_hash, IPC_GET_ARG5(*call),
|
---|
732 | callid, call, client_connection);
|
---|
733 | return;
|
---|
734 | }
|
---|
735 |
|
---|
736 | /* Try to route the call through the connection hash table */
|
---|
737 | if (route_call(callid, call))
|
---|
738 | return;
|
---|
739 |
|
---|
740 | /* Unknown call from unknown phone - hang it up */
|
---|
741 | ipc_answer_0(callid, EHANGUP);
|
---|
742 | }
|
---|
743 |
|
---|
744 | /** Fire all timeouts that expired. */
|
---|
745 | static void handle_expired_timeouts(void)
|
---|
746 | {
|
---|
747 | struct timeval tv;
|
---|
748 | gettimeofday(&tv, NULL);
|
---|
749 |
|
---|
750 | futex_down(&async_futex);
|
---|
751 |
|
---|
752 | link_t *cur = timeout_list.next;
|
---|
753 | while (cur != &timeout_list) {
|
---|
754 | awaiter_t *waiter =
|
---|
755 | list_get_instance(cur, awaiter_t, to_event.link);
|
---|
756 |
|
---|
757 | if (tv_gt(&waiter->to_event.expires, &tv))
|
---|
758 | break;
|
---|
759 |
|
---|
760 | cur = cur->next;
|
---|
761 |
|
---|
762 | list_remove(&waiter->to_event.link);
|
---|
763 | waiter->to_event.inlist = false;
|
---|
764 | waiter->to_event.occurred = true;
|
---|
765 |
|
---|
766 | /*
|
---|
767 | * Redundant condition?
|
---|
768 | * The fibril should not be active when it gets here.
|
---|
769 | */
|
---|
770 | if (!waiter->active) {
|
---|
771 | waiter->active = true;
|
---|
772 | fibril_add_ready(waiter->fid);
|
---|
773 | }
|
---|
774 | }
|
---|
775 |
|
---|
776 | futex_up(&async_futex);
|
---|
777 | }
|
---|
778 |
|
---|
779 | /** Endless loop dispatching incoming calls and answers.
|
---|
780 | *
|
---|
781 | * @return Never returns.
|
---|
782 | *
|
---|
783 | */
|
---|
784 | static int async_manager_worker(void)
|
---|
785 | {
|
---|
786 | while (true) {
|
---|
787 | if (fibril_switch(FIBRIL_FROM_MANAGER)) {
|
---|
788 | futex_up(&async_futex);
|
---|
789 | /*
|
---|
790 | * async_futex is always held when entering a manager
|
---|
791 | * fibril.
|
---|
792 | */
|
---|
793 | continue;
|
---|
794 | }
|
---|
795 |
|
---|
796 | futex_down(&async_futex);
|
---|
797 |
|
---|
798 | suseconds_t timeout;
|
---|
799 | if (!list_empty(&timeout_list)) {
|
---|
800 | awaiter_t *waiter = list_get_instance(timeout_list.next,
|
---|
801 | awaiter_t, to_event.link);
|
---|
802 |
|
---|
803 | struct timeval tv;
|
---|
804 | gettimeofday(&tv, NULL);
|
---|
805 |
|
---|
806 | if (tv_gteq(&tv, &waiter->to_event.expires)) {
|
---|
807 | futex_up(&async_futex);
|
---|
808 | handle_expired_timeouts();
|
---|
809 | continue;
|
---|
810 | } else
|
---|
811 | timeout = tv_sub(&waiter->to_event.expires, &tv);
|
---|
812 | } else
|
---|
813 | timeout = SYNCH_NO_TIMEOUT;
|
---|
814 |
|
---|
815 | futex_up(&async_futex);
|
---|
816 |
|
---|
817 | atomic_inc(&threads_in_ipc_wait);
|
---|
818 |
|
---|
819 | ipc_call_t call;
|
---|
820 | ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
|
---|
821 | SYNCH_FLAGS_NONE);
|
---|
822 |
|
---|
823 | atomic_dec(&threads_in_ipc_wait);
|
---|
824 |
|
---|
825 | if (!callid) {
|
---|
826 | handle_expired_timeouts();
|
---|
827 | continue;
|
---|
828 | }
|
---|
829 |
|
---|
830 | if (callid & IPC_CALLID_ANSWERED)
|
---|
831 | continue;
|
---|
832 |
|
---|
833 | handle_call(callid, &call);
|
---|
834 | }
|
---|
835 |
|
---|
836 | return 0;
|
---|
837 | }
|
---|
838 |
|
---|
839 | /** Function to start async_manager as a standalone fibril.
|
---|
840 | *
|
---|
841 | * When more kernel threads are used, one async manager should exist per thread.
|
---|
842 | *
|
---|
843 | * @param arg Unused.
|
---|
844 | * @return Never returns.
|
---|
845 | *
|
---|
846 | */
|
---|
847 | static int async_manager_fibril(void *arg)
|
---|
848 | {
|
---|
849 | futex_up(&async_futex);
|
---|
850 |
|
---|
851 | /*
|
---|
852 | * async_futex is always locked when entering manager
|
---|
853 | */
|
---|
854 | async_manager_worker();
|
---|
855 |
|
---|
856 | return 0;
|
---|
857 | }
|
---|
858 |
|
---|
859 | /** Add one manager to manager list. */
|
---|
860 | void async_create_manager(void)
|
---|
861 | {
|
---|
862 | fid_t fid = fibril_create(async_manager_fibril, NULL);
|
---|
863 | if (fid != 0)
|
---|
864 | fibril_add_manager(fid);
|
---|
865 | }
|
---|
866 |
|
---|
867 | /** Remove one manager from manager list */
|
---|
868 | void async_destroy_manager(void)
|
---|
869 | {
|
---|
870 | fibril_remove_manager();
|
---|
871 | }
|
---|
872 |
|
---|
873 | /** Initialize the async framework.
|
---|
874 | *
|
---|
875 | */
|
---|
876 | void __async_init(void)
|
---|
877 | {
|
---|
878 | if (!hash_table_create(&client_hash_table, CLIENT_HASH_TABLE_BUCKETS, 1,
|
---|
879 | &client_hash_table_ops))
|
---|
880 | abort();
|
---|
881 |
|
---|
882 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_BUCKETS, 1,
|
---|
883 | &conn_hash_table_ops))
|
---|
884 | abort();
|
---|
885 | }
|
---|
886 |
|
---|
887 | /** Reply received callback.
|
---|
888 | *
|
---|
889 | * This function is called whenever a reply for an asynchronous message sent out
|
---|
890 | * by the asynchronous framework is received.
|
---|
891 | *
|
---|
892 | * Notify the fibril which is waiting for this message that it has arrived.
|
---|
893 | *
|
---|
894 | * @param arg Pointer to the asynchronous message record.
|
---|
895 | * @param retval Value returned in the answer.
|
---|
896 | * @param data Call data of the answer.
|
---|
897 | *
|
---|
898 | */
|
---|
899 | static void reply_received(void *arg, int retval, ipc_call_t *data)
|
---|
900 | {
|
---|
901 | futex_down(&async_futex);
|
---|
902 |
|
---|
903 | amsg_t *msg = (amsg_t *) arg;
|
---|
904 | msg->retval = retval;
|
---|
905 |
|
---|
906 | /* Copy data after futex_down, just in case the call was detached */
|
---|
907 | if ((msg->dataptr) && (data))
|
---|
908 | *msg->dataptr = *data;
|
---|
909 |
|
---|
910 | write_barrier();
|
---|
911 |
|
---|
912 | /* Remove message from timeout list */
|
---|
913 | if (msg->wdata.to_event.inlist)
|
---|
914 | list_remove(&msg->wdata.to_event.link);
|
---|
915 |
|
---|
916 | msg->done = true;
|
---|
917 | if (!msg->wdata.active) {
|
---|
918 | msg->wdata.active = true;
|
---|
919 | fibril_add_ready(msg->wdata.fid);
|
---|
920 | }
|
---|
921 |
|
---|
922 | futex_up(&async_futex);
|
---|
923 | }
|
---|
924 |
|
---|
925 | /** Send message and return id of the sent message.
|
---|
926 | *
|
---|
927 | * The return value can be used as input for async_wait() to wait for
|
---|
928 | * completion.
|
---|
929 | *
|
---|
930 | * @param phoneid Handle of the phone that will be used for the send.
|
---|
931 | * @param method Service-defined method.
|
---|
932 | * @param arg1 Service-defined payload argument.
|
---|
933 | * @param arg2 Service-defined payload argument.
|
---|
934 | * @param arg3 Service-defined payload argument.
|
---|
935 | * @param arg4 Service-defined payload argument.
|
---|
936 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
937 | * stored.
|
---|
938 | *
|
---|
939 | * @return Hash of the sent message or 0 on error.
|
---|
940 | *
|
---|
941 | */
|
---|
942 | aid_t async_send_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
943 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
---|
944 | {
|
---|
945 | amsg_t *msg = malloc(sizeof(amsg_t));
|
---|
946 |
|
---|
947 | if (!msg)
|
---|
948 | return 0;
|
---|
949 |
|
---|
950 | msg->done = false;
|
---|
951 | msg->dataptr = dataptr;
|
---|
952 |
|
---|
953 | msg->wdata.to_event.inlist = false;
|
---|
954 |
|
---|
955 | /*
|
---|
956 | * We may sleep in the next method,
|
---|
957 | * but it will use its own means
|
---|
958 | */
|
---|
959 | msg->wdata.active = true;
|
---|
960 |
|
---|
961 | ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
|
---|
962 | reply_received, true);
|
---|
963 |
|
---|
964 | return (aid_t) msg;
|
---|
965 | }
|
---|
966 |
|
---|
967 | /** Send message and return id of the sent message
|
---|
968 | *
|
---|
969 | * The return value can be used as input for async_wait() to wait for
|
---|
970 | * completion.
|
---|
971 | *
|
---|
972 | * @param phoneid Handle of the phone that will be used for the send.
|
---|
973 | * @param method Service-defined method.
|
---|
974 | * @param arg1 Service-defined payload argument.
|
---|
975 | * @param arg2 Service-defined payload argument.
|
---|
976 | * @param arg3 Service-defined payload argument.
|
---|
977 | * @param arg4 Service-defined payload argument.
|
---|
978 | * @param arg5 Service-defined payload argument.
|
---|
979 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
980 | * stored.
|
---|
981 | *
|
---|
982 | * @return Hash of the sent message or 0 on error.
|
---|
983 | *
|
---|
984 | */
|
---|
985 | aid_t async_send_slow(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
986 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
987 | ipc_call_t *dataptr)
|
---|
988 | {
|
---|
989 | amsg_t *msg = malloc(sizeof(amsg_t));
|
---|
990 |
|
---|
991 | if (!msg)
|
---|
992 | return 0;
|
---|
993 |
|
---|
994 | msg->done = false;
|
---|
995 | msg->dataptr = dataptr;
|
---|
996 |
|
---|
997 | msg->wdata.to_event.inlist = false;
|
---|
998 |
|
---|
999 | /*
|
---|
1000 | * We may sleep in the next method,
|
---|
1001 | * but it will use its own means
|
---|
1002 | */
|
---|
1003 | msg->wdata.active = true;
|
---|
1004 |
|
---|
1005 | ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
|
---|
1006 | reply_received, true);
|
---|
1007 |
|
---|
1008 | return (aid_t) msg;
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | /** Wait for a message sent by the async framework.
|
---|
1012 | *
|
---|
1013 | * @param amsgid Hash of the message to wait for.
|
---|
1014 | * @param retval Pointer to storage where the retval of the answer will
|
---|
1015 | * be stored.
|
---|
1016 | *
|
---|
1017 | */
|
---|
1018 | void async_wait_for(aid_t amsgid, sysarg_t *retval)
|
---|
1019 | {
|
---|
1020 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
1021 |
|
---|
1022 | futex_down(&async_futex);
|
---|
1023 | if (msg->done) {
|
---|
1024 | futex_up(&async_futex);
|
---|
1025 | goto done;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | msg->wdata.fid = fibril_get_id();
|
---|
1029 | msg->wdata.active = false;
|
---|
1030 | msg->wdata.to_event.inlist = false;
|
---|
1031 |
|
---|
1032 | /* Leave the async_futex locked when entering this function */
|
---|
1033 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
1034 |
|
---|
1035 | /* Futex is up automatically after fibril_switch */
|
---|
1036 |
|
---|
1037 | done:
|
---|
1038 | if (retval)
|
---|
1039 | *retval = msg->retval;
|
---|
1040 |
|
---|
1041 | free(msg);
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | /** Wait for a message sent by the async framework, timeout variant.
|
---|
1045 | *
|
---|
1046 | * @param amsgid Hash of the message to wait for.
|
---|
1047 | * @param retval Pointer to storage where the retval of the answer will
|
---|
1048 | * be stored.
|
---|
1049 | * @param timeout Timeout in microseconds.
|
---|
1050 | *
|
---|
1051 | * @return Zero on success, ETIMEOUT if the timeout has expired.
|
---|
1052 | *
|
---|
1053 | */
|
---|
1054 | int async_wait_timeout(aid_t amsgid, sysarg_t *retval, suseconds_t timeout)
|
---|
1055 | {
|
---|
1056 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
1057 |
|
---|
1058 | /* TODO: Let it go through the event read at least once */
|
---|
1059 | if (timeout < 0)
|
---|
1060 | return ETIMEOUT;
|
---|
1061 |
|
---|
1062 | futex_down(&async_futex);
|
---|
1063 | if (msg->done) {
|
---|
1064 | futex_up(&async_futex);
|
---|
1065 | goto done;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
---|
1069 | tv_add(&msg->wdata.to_event.expires, timeout);
|
---|
1070 |
|
---|
1071 | msg->wdata.fid = fibril_get_id();
|
---|
1072 | msg->wdata.active = false;
|
---|
1073 | async_insert_timeout(&msg->wdata);
|
---|
1074 |
|
---|
1075 | /* Leave the async_futex locked when entering this function */
|
---|
1076 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
1077 |
|
---|
1078 | /* Futex is up automatically after fibril_switch */
|
---|
1079 |
|
---|
1080 | if (!msg->done)
|
---|
1081 | return ETIMEOUT;
|
---|
1082 |
|
---|
1083 | done:
|
---|
1084 | if (retval)
|
---|
1085 | *retval = msg->retval;
|
---|
1086 |
|
---|
1087 | free(msg);
|
---|
1088 |
|
---|
1089 | return 0;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 | /** Wait for specified time.
|
---|
1093 | *
|
---|
1094 | * The current fibril is suspended but the thread continues to execute.
|
---|
1095 | *
|
---|
1096 | * @param timeout Duration of the wait in microseconds.
|
---|
1097 | *
|
---|
1098 | */
|
---|
1099 | void async_usleep(suseconds_t timeout)
|
---|
1100 | {
|
---|
1101 | amsg_t *msg = malloc(sizeof(amsg_t));
|
---|
1102 |
|
---|
1103 | if (!msg)
|
---|
1104 | return;
|
---|
1105 |
|
---|
1106 | msg->wdata.fid = fibril_get_id();
|
---|
1107 | msg->wdata.active = false;
|
---|
1108 |
|
---|
1109 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
---|
1110 | tv_add(&msg->wdata.to_event.expires, timeout);
|
---|
1111 |
|
---|
1112 | futex_down(&async_futex);
|
---|
1113 |
|
---|
1114 | async_insert_timeout(&msg->wdata);
|
---|
1115 |
|
---|
1116 | /* Leave the async_futex locked when entering this function */
|
---|
1117 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
1118 |
|
---|
1119 | /* Futex is up automatically after fibril_switch() */
|
---|
1120 |
|
---|
1121 | free(msg);
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | /** Setter for client_connection function pointer.
|
---|
1125 | *
|
---|
1126 | * @param conn Function that will implement a new connection fibril.
|
---|
1127 | *
|
---|
1128 | */
|
---|
1129 | void async_set_client_connection(async_client_conn_t conn)
|
---|
1130 | {
|
---|
1131 | client_connection = conn;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | /** Setter for interrupt_received function pointer.
|
---|
1135 | *
|
---|
1136 | * @param intr Function that will implement a new interrupt
|
---|
1137 | * notification fibril.
|
---|
1138 | */
|
---|
1139 | void async_set_interrupt_received(async_client_conn_t intr)
|
---|
1140 | {
|
---|
1141 | interrupt_received = intr;
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | /** Pseudo-synchronous message sending - fast version.
|
---|
1145 | *
|
---|
1146 | * Send message asynchronously and return only after the reply arrives.
|
---|
1147 | *
|
---|
1148 | * This function can only transfer 4 register payload arguments. For
|
---|
1149 | * transferring more arguments, see the slower async_req_slow().
|
---|
1150 | *
|
---|
1151 | * @param phoneid Hash of the phone through which to make the call.
|
---|
1152 | * @param method Method of the call.
|
---|
1153 | * @param arg1 Service-defined payload argument.
|
---|
1154 | * @param arg2 Service-defined payload argument.
|
---|
1155 | * @param arg3 Service-defined payload argument.
|
---|
1156 | * @param arg4 Service-defined payload argument.
|
---|
1157 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
1158 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
1159 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
1160 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
1161 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
1162 | *
|
---|
1163 | * @return Return code of the reply or a negative error code.
|
---|
1164 | *
|
---|
1165 | */
|
---|
1166 | sysarg_t async_req_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
1167 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t *r1, sysarg_t *r2,
|
---|
1168 | sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
---|
1169 | {
|
---|
1170 | ipc_call_t result;
|
---|
1171 | aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
1172 | &result);
|
---|
1173 |
|
---|
1174 | sysarg_t rc;
|
---|
1175 | async_wait_for(eid, &rc);
|
---|
1176 |
|
---|
1177 | if (r1)
|
---|
1178 | *r1 = IPC_GET_ARG1(result);
|
---|
1179 |
|
---|
1180 | if (r2)
|
---|
1181 | *r2 = IPC_GET_ARG2(result);
|
---|
1182 |
|
---|
1183 | if (r3)
|
---|
1184 | *r3 = IPC_GET_ARG3(result);
|
---|
1185 |
|
---|
1186 | if (r4)
|
---|
1187 | *r4 = IPC_GET_ARG4(result);
|
---|
1188 |
|
---|
1189 | if (r5)
|
---|
1190 | *r5 = IPC_GET_ARG5(result);
|
---|
1191 |
|
---|
1192 | return rc;
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | /** Pseudo-synchronous message sending - slow version.
|
---|
1196 | *
|
---|
1197 | * Send message asynchronously and return only after the reply arrives.
|
---|
1198 | *
|
---|
1199 | * @param phoneid Hash of the phone through which to make the call.
|
---|
1200 | * @param method Method of the call.
|
---|
1201 | * @param arg1 Service-defined payload argument.
|
---|
1202 | * @param arg2 Service-defined payload argument.
|
---|
1203 | * @param arg3 Service-defined payload argument.
|
---|
1204 | * @param arg4 Service-defined payload argument.
|
---|
1205 | * @param arg5 Service-defined payload argument.
|
---|
1206 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
1207 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
1208 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
1209 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
1210 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
1211 | *
|
---|
1212 | * @return Return code of the reply or a negative error code.
|
---|
1213 | *
|
---|
1214 | */
|
---|
1215 | sysarg_t async_req_slow(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
1216 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5, sysarg_t *r1,
|
---|
1217 | sysarg_t *r2, sysarg_t *r3, sysarg_t *r4, sysarg_t *r5)
|
---|
1218 | {
|
---|
1219 | ipc_call_t result;
|
---|
1220 | aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
|
---|
1221 | &result);
|
---|
1222 |
|
---|
1223 | sysarg_t rc;
|
---|
1224 | async_wait_for(eid, &rc);
|
---|
1225 |
|
---|
1226 | if (r1)
|
---|
1227 | *r1 = IPC_GET_ARG1(result);
|
---|
1228 |
|
---|
1229 | if (r2)
|
---|
1230 | *r2 = IPC_GET_ARG2(result);
|
---|
1231 |
|
---|
1232 | if (r3)
|
---|
1233 | *r3 = IPC_GET_ARG3(result);
|
---|
1234 |
|
---|
1235 | if (r4)
|
---|
1236 | *r4 = IPC_GET_ARG4(result);
|
---|
1237 |
|
---|
1238 | if (r5)
|
---|
1239 | *r5 = IPC_GET_ARG5(result);
|
---|
1240 |
|
---|
1241 | return rc;
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | void async_msg_0(int phone, sysarg_t imethod)
|
---|
1245 | {
|
---|
1246 | ipc_call_async_0(phone, imethod, NULL, NULL, true);
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | void async_msg_1(int phone, sysarg_t imethod, sysarg_t arg1)
|
---|
1250 | {
|
---|
1251 | ipc_call_async_1(phone, imethod, arg1, NULL, NULL, true);
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | void async_msg_2(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2)
|
---|
1255 | {
|
---|
1256 | ipc_call_async_2(phone, imethod, arg1, arg2, NULL, NULL, true);
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | void async_msg_3(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
|
---|
1260 | sysarg_t arg3)
|
---|
1261 | {
|
---|
1262 | ipc_call_async_3(phone, imethod, arg1, arg2, arg3, NULL, NULL, true);
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | void async_msg_4(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
|
---|
1266 | sysarg_t arg3, sysarg_t arg4)
|
---|
1267 | {
|
---|
1268 | ipc_call_async_4(phone, imethod, arg1, arg2, arg3, arg4, NULL, NULL,
|
---|
1269 | true);
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | void async_msg_5(int phone, sysarg_t imethod, sysarg_t arg1, sysarg_t arg2,
|
---|
1273 | sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
1274 | {
|
---|
1275 | ipc_call_async_5(phone, imethod, arg1, arg2, arg3, arg4, arg5, NULL,
|
---|
1276 | NULL, true);
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | sysarg_t async_answer_0(ipc_callid_t callid, sysarg_t retval)
|
---|
1280 | {
|
---|
1281 | return ipc_answer_0(callid, retval);
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | sysarg_t async_answer_1(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1)
|
---|
1285 | {
|
---|
1286 | return ipc_answer_1(callid, retval, arg1);
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | sysarg_t async_answer_2(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
1290 | sysarg_t arg2)
|
---|
1291 | {
|
---|
1292 | return ipc_answer_2(callid, retval, arg1, arg2);
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | sysarg_t async_answer_3(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
1296 | sysarg_t arg2, sysarg_t arg3)
|
---|
1297 | {
|
---|
1298 | return ipc_answer_3(callid, retval, arg1, arg2, arg3);
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | sysarg_t async_answer_4(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
1302 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4)
|
---|
1303 | {
|
---|
1304 | return ipc_answer_4(callid, retval, arg1, arg2, arg3, arg4);
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | sysarg_t async_answer_5(ipc_callid_t callid, sysarg_t retval, sysarg_t arg1,
|
---|
1308 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5)
|
---|
1309 | {
|
---|
1310 | return ipc_answer_5(callid, retval, arg1, arg2, arg3, arg4, arg5);
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | int async_forward_fast(ipc_callid_t callid, int phoneid, sysarg_t imethod,
|
---|
1314 | sysarg_t arg1, sysarg_t arg2, unsigned int mode)
|
---|
1315 | {
|
---|
1316 | return ipc_forward_fast(callid, phoneid, imethod, arg1, arg2, mode);
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | int async_forward_slow(ipc_callid_t callid, int phoneid, sysarg_t imethod,
|
---|
1320 | sysarg_t arg1, sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, sysarg_t arg5,
|
---|
1321 | unsigned int mode)
|
---|
1322 | {
|
---|
1323 | return ipc_forward_slow(callid, phoneid, imethod, arg1, arg2, arg3, arg4,
|
---|
1324 | arg5, mode);
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | /** Wrapper for making IPC_M_CONNECT_TO_ME calls using the async framework.
|
---|
1328 | *
|
---|
1329 | * Ask through phone for a new connection to some service.
|
---|
1330 | *
|
---|
1331 | * @param phone Phone handle used for contacting the other side.
|
---|
1332 | * @param arg1 User defined argument.
|
---|
1333 | * @param arg2 User defined argument.
|
---|
1334 | * @param arg3 User defined argument.
|
---|
1335 | * @param client_receiver Connection handing routine.
|
---|
1336 | *
|
---|
1337 | * @return New phone handle on success or a negative error code.
|
---|
1338 | *
|
---|
1339 | */
|
---|
1340 | int async_connect_to_me(int phone, sysarg_t arg1, sysarg_t arg2,
|
---|
1341 | sysarg_t arg3, async_client_conn_t client_receiver)
|
---|
1342 | {
|
---|
1343 | sysarg_t task_hash;
|
---|
1344 | sysarg_t phone_hash;
|
---|
1345 | int rc = async_req_3_5(phone, IPC_M_CONNECT_TO_ME, arg1, arg2, arg3,
|
---|
1346 | NULL, NULL, NULL, &task_hash, &phone_hash);
|
---|
1347 | if (rc != EOK)
|
---|
1348 | return rc;
|
---|
1349 |
|
---|
1350 | if (client_receiver != NULL)
|
---|
1351 | async_new_connection(task_hash, phone_hash, 0, NULL,
|
---|
1352 | client_receiver);
|
---|
1353 |
|
---|
1354 | return EOK;
|
---|
1355 | }
|
---|
1356 |
|
---|
1357 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
1358 | *
|
---|
1359 | * Ask through phone for a new connection to some service.
|
---|
1360 | *
|
---|
1361 | * @param phone Phone handle used for contacting the other side.
|
---|
1362 | * @param arg1 User defined argument.
|
---|
1363 | * @param arg2 User defined argument.
|
---|
1364 | * @param arg3 User defined argument.
|
---|
1365 | *
|
---|
1366 | * @return New phone handle on success or a negative error code.
|
---|
1367 | *
|
---|
1368 | */
|
---|
1369 | int async_connect_me_to(int phone, sysarg_t arg1, sysarg_t arg2,
|
---|
1370 | sysarg_t arg3)
|
---|
1371 | {
|
---|
1372 | sysarg_t newphid;
|
---|
1373 | int rc = async_req_3_5(phone, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
|
---|
1374 | NULL, NULL, NULL, NULL, &newphid);
|
---|
1375 |
|
---|
1376 | if (rc != EOK)
|
---|
1377 | return rc;
|
---|
1378 |
|
---|
1379 | return newphid;
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | /** Wrapper for making IPC_M_CONNECT_ME_TO calls using the async framework.
|
---|
1383 | *
|
---|
1384 | * Ask through phone for a new connection to some service and block until
|
---|
1385 | * success.
|
---|
1386 | *
|
---|
1387 | * @param phoneid Phone handle used for contacting the other side.
|
---|
1388 | * @param arg1 User defined argument.
|
---|
1389 | * @param arg2 User defined argument.
|
---|
1390 | * @param arg3 User defined argument.
|
---|
1391 | *
|
---|
1392 | * @return New phone handle on success or a negative error code.
|
---|
1393 | *
|
---|
1394 | */
|
---|
1395 | int async_connect_me_to_blocking(int phoneid, sysarg_t arg1, sysarg_t arg2,
|
---|
1396 | sysarg_t arg3)
|
---|
1397 | {
|
---|
1398 | sysarg_t newphid;
|
---|
1399 | int rc = async_req_4_5(phoneid, IPC_M_CONNECT_ME_TO, arg1, arg2, arg3,
|
---|
1400 | IPC_FLAG_BLOCKING, NULL, NULL, NULL, NULL, &newphid);
|
---|
1401 |
|
---|
1402 | if (rc != EOK)
|
---|
1403 | return rc;
|
---|
1404 |
|
---|
1405 | return newphid;
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | /** Connect to a task specified by id.
|
---|
1409 | *
|
---|
1410 | */
|
---|
1411 | int async_connect_kbox(task_id_t id)
|
---|
1412 | {
|
---|
1413 | return ipc_connect_kbox(id);
|
---|
1414 | }
|
---|
1415 |
|
---|
1416 | /** Wrapper for ipc_hangup.
|
---|
1417 | *
|
---|
1418 | * @param phone Phone handle to hung up.
|
---|
1419 | *
|
---|
1420 | * @return Zero on success or a negative error code.
|
---|
1421 | *
|
---|
1422 | */
|
---|
1423 | int async_hangup(int phone)
|
---|
1424 | {
|
---|
1425 | return ipc_hangup(phone);
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | /** Interrupt one thread of this task from waiting for IPC. */
|
---|
1429 | void async_poke(void)
|
---|
1430 | {
|
---|
1431 | ipc_poke();
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 | /** Wrapper for IPC_M_SHARE_IN calls using the async framework.
|
---|
1435 | *
|
---|
1436 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
1437 | * @param dst Destination address space area base.
|
---|
1438 | * @param size Size of the destination address space area.
|
---|
1439 | * @param arg User defined argument.
|
---|
1440 | * @param flags Storage for the received flags. Can be NULL.
|
---|
1441 | *
|
---|
1442 | * @return Zero on success or a negative error code from errno.h.
|
---|
1443 | *
|
---|
1444 | */
|
---|
1445 | int async_share_in_start(int phoneid, void *dst, size_t size, sysarg_t arg,
|
---|
1446 | unsigned int *flags)
|
---|
1447 | {
|
---|
1448 | sysarg_t tmp_flags;
|
---|
1449 | int res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (sysarg_t) dst,
|
---|
1450 | (sysarg_t) size, arg, NULL, &tmp_flags);
|
---|
1451 |
|
---|
1452 | if (flags)
|
---|
1453 | *flags = (unsigned int) tmp_flags;
|
---|
1454 |
|
---|
1455 | return res;
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
---|
1459 | *
|
---|
1460 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN
|
---|
1461 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1462 | * argument.
|
---|
1463 | *
|
---|
1464 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1465 | *
|
---|
1466 | * @param callid Storage for the hash of the IPC_M_SHARE_IN call.
|
---|
1467 | * @param size Destination address space area size.
|
---|
1468 | *
|
---|
1469 | * @return True on success, false on failure.
|
---|
1470 | *
|
---|
1471 | */
|
---|
1472 | bool async_share_in_receive(ipc_callid_t *callid, size_t *size)
|
---|
1473 | {
|
---|
1474 | assert(callid);
|
---|
1475 | assert(size);
|
---|
1476 |
|
---|
1477 | ipc_call_t data;
|
---|
1478 | *callid = async_get_call(&data);
|
---|
1479 |
|
---|
1480 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_IN)
|
---|
1481 | return false;
|
---|
1482 |
|
---|
1483 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
1484 | return true;
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
---|
1488 | *
|
---|
1489 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
---|
1490 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1491 | * argument.
|
---|
1492 | *
|
---|
1493 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
1494 | * @param src Source address space base.
|
---|
1495 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
1496 | *
|
---|
1497 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1498 | *
|
---|
1499 | */
|
---|
1500 | int async_share_in_finalize(ipc_callid_t callid, void *src, unsigned int flags)
|
---|
1501 | {
|
---|
1502 | return ipc_share_in_finalize(callid, src, flags);
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | /** Wrapper for IPC_M_SHARE_OUT calls using the async framework.
|
---|
1506 | *
|
---|
1507 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
1508 | * @param src Source address space area base address.
|
---|
1509 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
1510 | *
|
---|
1511 | * @return Zero on success or a negative error code from errno.h.
|
---|
1512 | *
|
---|
1513 | */
|
---|
1514 | int async_share_out_start(int phoneid, void *src, unsigned int flags)
|
---|
1515 | {
|
---|
1516 | return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (sysarg_t) src, 0,
|
---|
1517 | (sysarg_t) flags);
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
---|
1521 | *
|
---|
1522 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT
|
---|
1523 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1524 | * argument.
|
---|
1525 | *
|
---|
1526 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1527 | *
|
---|
1528 | * @param callid Storage for the hash of the IPC_M_SHARE_OUT call.
|
---|
1529 | * @param size Storage for the source address space area size.
|
---|
1530 | * @param flags Storage for the sharing flags.
|
---|
1531 | *
|
---|
1532 | * @return True on success, false on failure.
|
---|
1533 | *
|
---|
1534 | */
|
---|
1535 | bool async_share_out_receive(ipc_callid_t *callid, size_t *size, unsigned int *flags)
|
---|
1536 | {
|
---|
1537 | assert(callid);
|
---|
1538 | assert(size);
|
---|
1539 | assert(flags);
|
---|
1540 |
|
---|
1541 | ipc_call_t data;
|
---|
1542 | *callid = async_get_call(&data);
|
---|
1543 |
|
---|
1544 | if (IPC_GET_IMETHOD(data) != IPC_M_SHARE_OUT)
|
---|
1545 | return false;
|
---|
1546 |
|
---|
1547 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
1548 | *flags = (unsigned int) IPC_GET_ARG3(data);
|
---|
1549 | return true;
|
---|
1550 | }
|
---|
1551 |
|
---|
1552 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
---|
1553 | *
|
---|
1554 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT
|
---|
1555 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1556 | * argument.
|
---|
1557 | *
|
---|
1558 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
1559 | * @param dst Destination address space area base address.
|
---|
1560 | *
|
---|
1561 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1562 | *
|
---|
1563 | */
|
---|
1564 | int async_share_out_finalize(ipc_callid_t callid, void *dst)
|
---|
1565 | {
|
---|
1566 | return ipc_share_out_finalize(callid, dst);
|
---|
1567 | }
|
---|
1568 |
|
---|
1569 | /** Wrapper for IPC_M_DATA_READ calls using the async framework.
|
---|
1570 | *
|
---|
1571 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
1572 | * @param dst Address of the beginning of the destination buffer.
|
---|
1573 | * @param size Size of the destination buffer.
|
---|
1574 | * @param flags Flags to control the data transfer.
|
---|
1575 | *
|
---|
1576 | * @return Zero on success or a negative error code from errno.h.
|
---|
1577 | *
|
---|
1578 | */
|
---|
1579 | int
|
---|
1580 | async_data_read_start_flexible(int phoneid, void *dst, size_t size, int flags)
|
---|
1581 | {
|
---|
1582 | return async_req_3_0(phoneid, IPC_M_DATA_READ, (sysarg_t) dst,
|
---|
1583 | (sysarg_t) size, (sysarg_t) flags);
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
---|
1587 | *
|
---|
1588 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ
|
---|
1589 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1590 | * argument.
|
---|
1591 | *
|
---|
1592 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1593 | *
|
---|
1594 | * @param callid Storage for the hash of the IPC_M_DATA_READ.
|
---|
1595 | * @param size Storage for the maximum size. Can be NULL.
|
---|
1596 | *
|
---|
1597 | * @return True on success, false on failure.
|
---|
1598 | *
|
---|
1599 | */
|
---|
1600 | bool async_data_read_receive(ipc_callid_t *callid, size_t *size)
|
---|
1601 | {
|
---|
1602 | assert(callid);
|
---|
1603 |
|
---|
1604 | ipc_call_t data;
|
---|
1605 | *callid = async_get_call(&data);
|
---|
1606 |
|
---|
1607 | if (IPC_GET_IMETHOD(data) != IPC_M_DATA_READ)
|
---|
1608 | return false;
|
---|
1609 |
|
---|
1610 | if (size)
|
---|
1611 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
1612 |
|
---|
1613 | return true;
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
---|
1617 | *
|
---|
1618 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ
|
---|
1619 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1620 | * argument.
|
---|
1621 | *
|
---|
1622 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
1623 | * @param src Source address for the IPC_M_DATA_READ call.
|
---|
1624 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
---|
1625 | * the maximum size announced by the sender.
|
---|
1626 | *
|
---|
1627 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1628 | *
|
---|
1629 | */
|
---|
1630 | int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
|
---|
1631 | {
|
---|
1632 | return ipc_data_read_finalize(callid, src, size);
|
---|
1633 | }
|
---|
1634 |
|
---|
1635 | /** Wrapper for forwarding any read request
|
---|
1636 | *
|
---|
1637 | */
|
---|
1638 | int async_data_read_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
1639 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
---|
1640 | {
|
---|
1641 | ipc_callid_t callid;
|
---|
1642 | if (!async_data_read_receive(&callid, NULL)) {
|
---|
1643 | ipc_answer_0(callid, EINVAL);
|
---|
1644 | return EINVAL;
|
---|
1645 | }
|
---|
1646 |
|
---|
1647 | aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
1648 | dataptr);
|
---|
1649 | if (msg == 0) {
|
---|
1650 | ipc_answer_0(callid, EINVAL);
|
---|
1651 | return EINVAL;
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
|
---|
1655 | IPC_FF_ROUTE_FROM_ME);
|
---|
1656 | if (retval != EOK) {
|
---|
1657 | async_wait_for(msg, NULL);
|
---|
1658 | ipc_answer_0(callid, retval);
|
---|
1659 | return retval;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | sysarg_t rc;
|
---|
1663 | async_wait_for(msg, &rc);
|
---|
1664 |
|
---|
1665 | return (int) rc;
|
---|
1666 | }
|
---|
1667 |
|
---|
1668 | /** Wrapper for IPC_M_DATA_WRITE calls using the async framework.
|
---|
1669 | *
|
---|
1670 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
1671 | * @param src Address of the beginning of the source buffer.
|
---|
1672 | * @param size Size of the source buffer.
|
---|
1673 | * @param flags Flags to control the data transfer.
|
---|
1674 | *
|
---|
1675 | * @return Zero on success or a negative error code from errno.h.
|
---|
1676 | *
|
---|
1677 | */
|
---|
1678 | int
|
---|
1679 | async_data_write_start_flexible(int phoneid, const void *src, size_t size,
|
---|
1680 | int flags)
|
---|
1681 | {
|
---|
1682 | return async_req_3_0(phoneid, IPC_M_DATA_WRITE, (sysarg_t) src,
|
---|
1683 | (sysarg_t) size, (sysarg_t) flags);
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
---|
1687 | *
|
---|
1688 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE
|
---|
1689 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1690 | * argument.
|
---|
1691 | *
|
---|
1692 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1693 | *
|
---|
1694 | * @param callid Storage for the hash of the IPC_M_DATA_WRITE.
|
---|
1695 | * @param size Storage for the suggested size. May be NULL.
|
---|
1696 | *
|
---|
1697 | * @return True on success, false on failure.
|
---|
1698 | *
|
---|
1699 | */
|
---|
1700 | bool async_data_write_receive(ipc_callid_t *callid, size_t *size)
|
---|
1701 | {
|
---|
1702 | assert(callid);
|
---|
1703 |
|
---|
1704 | ipc_call_t data;
|
---|
1705 | *callid = async_get_call(&data);
|
---|
1706 |
|
---|
1707 | if (IPC_GET_IMETHOD(data) != IPC_M_DATA_WRITE)
|
---|
1708 | return false;
|
---|
1709 |
|
---|
1710 | if (size)
|
---|
1711 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
1712 |
|
---|
1713 | return true;
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
---|
1717 | *
|
---|
1718 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE
|
---|
1719 | * calls so that the user doesn't have to remember the meaning of each IPC
|
---|
1720 | * argument.
|
---|
1721 | *
|
---|
1722 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
1723 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
---|
1724 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
---|
1725 | *
|
---|
1726 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1727 | *
|
---|
1728 | */
|
---|
1729 | int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
|
---|
1730 | {
|
---|
1731 | return ipc_data_write_finalize(callid, dst, size);
|
---|
1732 | }
|
---|
1733 |
|
---|
1734 | /** Wrapper for receiving binary data or strings
|
---|
1735 | *
|
---|
1736 | * This wrapper only makes it more comfortable to use async_data_write_*
|
---|
1737 | * functions to receive binary data or strings.
|
---|
1738 | *
|
---|
1739 | * @param data Pointer to data pointer (which should be later disposed
|
---|
1740 | * by free()). If the operation fails, the pointer is not
|
---|
1741 | * touched.
|
---|
1742 | * @param nullterm If true then the received data is always zero terminated.
|
---|
1743 | * This also causes to allocate one extra byte beyond the
|
---|
1744 | * raw transmitted data.
|
---|
1745 | * @param min_size Minimum size (in bytes) of the data to receive.
|
---|
1746 | * @param max_size Maximum size (in bytes) of the data to receive. 0 means
|
---|
1747 | * no limit.
|
---|
1748 | * @param granulariy If non-zero then the size of the received data has to
|
---|
1749 | * be divisible by this value.
|
---|
1750 | * @param received If not NULL, the size of the received data is stored here.
|
---|
1751 | *
|
---|
1752 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1753 | *
|
---|
1754 | */
|
---|
1755 | int async_data_write_accept(void **data, const bool nullterm,
|
---|
1756 | const size_t min_size, const size_t max_size, const size_t granularity,
|
---|
1757 | size_t *received)
|
---|
1758 | {
|
---|
1759 | ipc_callid_t callid;
|
---|
1760 | size_t size;
|
---|
1761 | if (!async_data_write_receive(&callid, &size)) {
|
---|
1762 | ipc_answer_0(callid, EINVAL);
|
---|
1763 | return EINVAL;
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | if (size < min_size) {
|
---|
1767 | ipc_answer_0(callid, EINVAL);
|
---|
1768 | return EINVAL;
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | if ((max_size > 0) && (size > max_size)) {
|
---|
1772 | ipc_answer_0(callid, EINVAL);
|
---|
1773 | return EINVAL;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | if ((granularity > 0) && ((size % granularity) != 0)) {
|
---|
1777 | ipc_answer_0(callid, EINVAL);
|
---|
1778 | return EINVAL;
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 | void *_data;
|
---|
1782 |
|
---|
1783 | if (nullterm)
|
---|
1784 | _data = malloc(size + 1);
|
---|
1785 | else
|
---|
1786 | _data = malloc(size);
|
---|
1787 |
|
---|
1788 | if (_data == NULL) {
|
---|
1789 | ipc_answer_0(callid, ENOMEM);
|
---|
1790 | return ENOMEM;
|
---|
1791 | }
|
---|
1792 |
|
---|
1793 | int rc = async_data_write_finalize(callid, _data, size);
|
---|
1794 | if (rc != EOK) {
|
---|
1795 | free(_data);
|
---|
1796 | return rc;
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | if (nullterm)
|
---|
1800 | ((char *) _data)[size] = 0;
|
---|
1801 |
|
---|
1802 | *data = _data;
|
---|
1803 | if (received != NULL)
|
---|
1804 | *received = size;
|
---|
1805 |
|
---|
1806 | return EOK;
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | /** Wrapper for voiding any data that is about to be received
|
---|
1810 | *
|
---|
1811 | * This wrapper can be used to void any pending data
|
---|
1812 | *
|
---|
1813 | * @param retval Error value from @ref errno.h to be returned to the caller.
|
---|
1814 | *
|
---|
1815 | */
|
---|
1816 | void async_data_write_void(sysarg_t retval)
|
---|
1817 | {
|
---|
1818 | ipc_callid_t callid;
|
---|
1819 | async_data_write_receive(&callid, NULL);
|
---|
1820 | ipc_answer_0(callid, retval);
|
---|
1821 | }
|
---|
1822 |
|
---|
1823 | /** Wrapper for forwarding any data that is about to be received
|
---|
1824 | *
|
---|
1825 | */
|
---|
1826 | int async_data_write_forward_fast(int phoneid, sysarg_t method, sysarg_t arg1,
|
---|
1827 | sysarg_t arg2, sysarg_t arg3, sysarg_t arg4, ipc_call_t *dataptr)
|
---|
1828 | {
|
---|
1829 | ipc_callid_t callid;
|
---|
1830 | if (!async_data_write_receive(&callid, NULL)) {
|
---|
1831 | ipc_answer_0(callid, EINVAL);
|
---|
1832 | return EINVAL;
|
---|
1833 | }
|
---|
1834 |
|
---|
1835 | aid_t msg = async_send_fast(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
1836 | dataptr);
|
---|
1837 | if (msg == 0) {
|
---|
1838 | ipc_answer_0(callid, EINVAL);
|
---|
1839 | return EINVAL;
|
---|
1840 | }
|
---|
1841 |
|
---|
1842 | int retval = ipc_forward_fast(callid, phoneid, 0, 0, 0,
|
---|
1843 | IPC_FF_ROUTE_FROM_ME);
|
---|
1844 | if (retval != EOK) {
|
---|
1845 | async_wait_for(msg, NULL);
|
---|
1846 | ipc_answer_0(callid, retval);
|
---|
1847 | return retval;
|
---|
1848 | }
|
---|
1849 |
|
---|
1850 | sysarg_t rc;
|
---|
1851 | async_wait_for(msg, &rc);
|
---|
1852 |
|
---|
1853 | return (int) rc;
|
---|
1854 | }
|
---|
1855 |
|
---|
1856 | /** @}
|
---|
1857 | */
|
---|