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 | * Default semantics:
|
---|
46 | * - async_send_*(): Send asynchronously. If the kernel refuses to send
|
---|
47 | * more messages, [ try to get responses from kernel, if
|
---|
48 | * nothing found, might try synchronous ]
|
---|
49 | *
|
---|
50 | * Example of use (pseudo C):
|
---|
51 | *
|
---|
52 | * 1) Multithreaded client application
|
---|
53 | *
|
---|
54 | * fibril_create(fibril1, ...);
|
---|
55 | * fibril_create(fibril2, ...);
|
---|
56 | * ...
|
---|
57 | *
|
---|
58 | * int fibril1(void *arg)
|
---|
59 | * {
|
---|
60 | * conn = ipc_connect_me_to();
|
---|
61 | * c1 = async_send(conn);
|
---|
62 | * c2 = async_send(conn);
|
---|
63 | * async_wait_for(c1);
|
---|
64 | * async_wait_for(c2);
|
---|
65 | * ...
|
---|
66 | * }
|
---|
67 | *
|
---|
68 | *
|
---|
69 | * 2) Multithreaded server application
|
---|
70 | *
|
---|
71 | * main()
|
---|
72 | * {
|
---|
73 | * async_manager();
|
---|
74 | * }
|
---|
75 | *
|
---|
76 | * my_client_connection(icallid, *icall)
|
---|
77 | * {
|
---|
78 | * if (want_refuse) {
|
---|
79 | * ipc_answer_0(icallid, ELIMIT);
|
---|
80 | * return;
|
---|
81 | * }
|
---|
82 | * ipc_answer_0(icallid, EOK);
|
---|
83 | *
|
---|
84 | * callid = async_get_call(&call);
|
---|
85 | * somehow_handle_the_call(callid, call);
|
---|
86 | * ipc_answer_2(callid, 1, 2, 3);
|
---|
87 | *
|
---|
88 | * callid = async_get_call(&call);
|
---|
89 | * ...
|
---|
90 | * }
|
---|
91 | *
|
---|
92 | */
|
---|
93 |
|
---|
94 | #include <futex.h>
|
---|
95 | #include <async.h>
|
---|
96 | #include <async_priv.h>
|
---|
97 | #include <fibril.h>
|
---|
98 | #include <stdio.h>
|
---|
99 | #include <adt/hash_table.h>
|
---|
100 | #include <adt/list.h>
|
---|
101 | #include <ipc/ipc.h>
|
---|
102 | #include <assert.h>
|
---|
103 | #include <errno.h>
|
---|
104 | #include <sys/time.h>
|
---|
105 | #include <arch/barrier.h>
|
---|
106 | #include <bool.h>
|
---|
107 |
|
---|
108 | atomic_t async_futex = FUTEX_INITIALIZER;
|
---|
109 |
|
---|
110 | /** Number of threads waiting for IPC in the kernel. */
|
---|
111 | atomic_t threads_in_ipc_wait = { 0 };
|
---|
112 |
|
---|
113 | typedef struct {
|
---|
114 | awaiter_t wdata;
|
---|
115 |
|
---|
116 | /** If reply was received. */
|
---|
117 | bool done;
|
---|
118 |
|
---|
119 | /** Pointer to where the answer data is stored. */
|
---|
120 | ipc_call_t *dataptr;
|
---|
121 |
|
---|
122 | ipcarg_t retval;
|
---|
123 | } amsg_t;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Structures of this type are used to group information about a call and a
|
---|
127 | * message queue link.
|
---|
128 | */
|
---|
129 | typedef struct {
|
---|
130 | link_t link;
|
---|
131 | ipc_callid_t callid;
|
---|
132 | ipc_call_t call;
|
---|
133 | } msg_t;
|
---|
134 |
|
---|
135 | typedef struct {
|
---|
136 | awaiter_t wdata;
|
---|
137 |
|
---|
138 | /** Hash table link. */
|
---|
139 | link_t link;
|
---|
140 |
|
---|
141 | /** Incoming phone hash. */
|
---|
142 | ipcarg_t in_phone_hash;
|
---|
143 |
|
---|
144 | /** Messages that should be delivered to this fibril. */
|
---|
145 | link_t msg_queue;
|
---|
146 |
|
---|
147 | /** Identification of the opening call. */
|
---|
148 | ipc_callid_t callid;
|
---|
149 | /** Call data of the opening call. */
|
---|
150 | ipc_call_t call;
|
---|
151 |
|
---|
152 | /** Identification of the closing call. */
|
---|
153 | ipc_callid_t close_callid;
|
---|
154 |
|
---|
155 | /** Fibril function that will be used to handle the connection. */
|
---|
156 | void (*cfibril)(ipc_callid_t, ipc_call_t *);
|
---|
157 | } connection_t;
|
---|
158 |
|
---|
159 | /** Identifier of the incoming connection handled by the current fibril. */
|
---|
160 | fibril_local connection_t *FIBRIL_connection;
|
---|
161 |
|
---|
162 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call);
|
---|
163 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call);
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Pointer to a fibril function that will be used to handle connections.
|
---|
167 | */
|
---|
168 | static async_client_conn_t client_connection = default_client_connection;
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Pointer to a fibril function that will be used to handle interrupt
|
---|
172 | * notifications.
|
---|
173 | */
|
---|
174 | static async_client_conn_t interrupt_received = default_interrupt_received;
|
---|
175 |
|
---|
176 | static hash_table_t conn_hash_table;
|
---|
177 | static LIST_INITIALIZE(timeout_list);
|
---|
178 |
|
---|
179 | #define CONN_HASH_TABLE_CHAINS 32
|
---|
180 |
|
---|
181 | /** Compute hash into the connection hash table based on the source phone hash.
|
---|
182 | *
|
---|
183 | * @param key Pointer to source phone hash.
|
---|
184 | *
|
---|
185 | * @return Index into the connection hash table.
|
---|
186 | *
|
---|
187 | */
|
---|
188 | static hash_index_t conn_hash(unsigned long *key)
|
---|
189 | {
|
---|
190 | assert(key);
|
---|
191 | return (((*key) >> 4) % CONN_HASH_TABLE_CHAINS);
|
---|
192 | }
|
---|
193 |
|
---|
194 | /** Compare hash table item with a key.
|
---|
195 | *
|
---|
196 | * @param key Array containing the source phone hash as the only item.
|
---|
197 | * @param keys Expected 1 but ignored.
|
---|
198 | * @param item Connection hash table item.
|
---|
199 | *
|
---|
200 | * @return True on match, false otherwise.
|
---|
201 | *
|
---|
202 | */
|
---|
203 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
204 | {
|
---|
205 | connection_t *hs = hash_table_get_instance(item, connection_t, link);
|
---|
206 | return (key[0] == hs->in_phone_hash);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /** Connection hash table removal callback function.
|
---|
210 | *
|
---|
211 | * This function is called whenever a connection is removed from the connection
|
---|
212 | * hash table.
|
---|
213 | *
|
---|
214 | * @param item Connection hash table item being removed.
|
---|
215 | *
|
---|
216 | */
|
---|
217 | static void conn_remove(link_t *item)
|
---|
218 | {
|
---|
219 | free(hash_table_get_instance(item, connection_t, link));
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 | /** Operations for the connection hash table. */
|
---|
224 | static hash_table_operations_t conn_hash_table_ops = {
|
---|
225 | .hash = conn_hash,
|
---|
226 | .compare = conn_compare,
|
---|
227 | .remove_callback = conn_remove
|
---|
228 | };
|
---|
229 |
|
---|
230 | /** Sort in current fibril's timeout request.
|
---|
231 | *
|
---|
232 | * @param wd Wait data of the current fibril.
|
---|
233 | *
|
---|
234 | */
|
---|
235 | void async_insert_timeout(awaiter_t *wd)
|
---|
236 | {
|
---|
237 | wd->to_event.occurred = false;
|
---|
238 | wd->to_event.inlist = true;
|
---|
239 |
|
---|
240 | link_t *tmp = timeout_list.next;
|
---|
241 | while (tmp != &timeout_list) {
|
---|
242 | awaiter_t *cur;
|
---|
243 |
|
---|
244 | cur = list_get_instance(tmp, awaiter_t, to_event.link);
|
---|
245 | if (tv_gteq(&cur->to_event.expires, &wd->to_event.expires))
|
---|
246 | break;
|
---|
247 | tmp = tmp->next;
|
---|
248 | }
|
---|
249 |
|
---|
250 | list_append(&wd->to_event.link, tmp);
|
---|
251 | }
|
---|
252 |
|
---|
253 | /** Try to route a call to an appropriate connection fibril.
|
---|
254 | *
|
---|
255 | * If the proper connection fibril is found, a message with the call is added to
|
---|
256 | * its message queue. If the fibril was not active, it is activated and all
|
---|
257 | * timeouts are unregistered.
|
---|
258 | *
|
---|
259 | * @param callid Hash of the incoming call.
|
---|
260 | * @param call Data of the incoming call.
|
---|
261 | *
|
---|
262 | * @return False if the call doesn't match any connection.
|
---|
263 | * True if the call was passed to the respective connection fibril.
|
---|
264 | *
|
---|
265 | */
|
---|
266 | static bool route_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
267 | {
|
---|
268 | futex_down(&async_futex);
|
---|
269 |
|
---|
270 | unsigned long key = call->in_phone_hash;
|
---|
271 | link_t *hlp = hash_table_find(&conn_hash_table, &key);
|
---|
272 |
|
---|
273 | if (!hlp) {
|
---|
274 | futex_up(&async_futex);
|
---|
275 | return false;
|
---|
276 | }
|
---|
277 |
|
---|
278 | connection_t *conn = hash_table_get_instance(hlp, connection_t, link);
|
---|
279 |
|
---|
280 | msg_t *msg = malloc(sizeof(*msg));
|
---|
281 | if (!msg) {
|
---|
282 | futex_up(&async_futex);
|
---|
283 | return false;
|
---|
284 | }
|
---|
285 |
|
---|
286 | msg->callid = callid;
|
---|
287 | msg->call = *call;
|
---|
288 | list_append(&msg->link, &conn->msg_queue);
|
---|
289 |
|
---|
290 | if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP)
|
---|
291 | conn->close_callid = callid;
|
---|
292 |
|
---|
293 | /* If the connection fibril is waiting for an event, activate it */
|
---|
294 | if (!conn->wdata.active) {
|
---|
295 |
|
---|
296 | /* If in timeout list, remove it */
|
---|
297 | if (conn->wdata.to_event.inlist) {
|
---|
298 | conn->wdata.to_event.inlist = false;
|
---|
299 | list_remove(&conn->wdata.to_event.link);
|
---|
300 | }
|
---|
301 |
|
---|
302 | conn->wdata.active = true;
|
---|
303 | fibril_add_ready(conn->wdata.fid);
|
---|
304 | }
|
---|
305 |
|
---|
306 | futex_up(&async_futex);
|
---|
307 | return true;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /** Notification fibril.
|
---|
311 | *
|
---|
312 | * When a notification arrives, a fibril with this implementing function is
|
---|
313 | * created. It calls interrupt_received() and does the final cleanup.
|
---|
314 | *
|
---|
315 | * @param arg Message structure pointer.
|
---|
316 | *
|
---|
317 | * @return Always zero.
|
---|
318 | *
|
---|
319 | */
|
---|
320 | static int notification_fibril(void *arg)
|
---|
321 | {
|
---|
322 | msg_t *msg = (msg_t *) arg;
|
---|
323 | interrupt_received(msg->callid, &msg->call);
|
---|
324 |
|
---|
325 | free(msg);
|
---|
326 | return 0;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /** Process interrupt notification.
|
---|
330 | *
|
---|
331 | * A new fibril is created which would process the notification.
|
---|
332 | *
|
---|
333 | * @param callid Hash of the incoming call.
|
---|
334 | * @param call Data of the incoming call.
|
---|
335 | *
|
---|
336 | * @return False if an error occured.
|
---|
337 | * True if the call was passed to the notification fibril.
|
---|
338 | *
|
---|
339 | */
|
---|
340 | static bool process_notification(ipc_callid_t callid, ipc_call_t *call)
|
---|
341 | {
|
---|
342 | futex_down(&async_futex);
|
---|
343 |
|
---|
344 | msg_t *msg = malloc(sizeof(*msg));
|
---|
345 | if (!msg) {
|
---|
346 | futex_up(&async_futex);
|
---|
347 | return false;
|
---|
348 | }
|
---|
349 |
|
---|
350 | msg->callid = callid;
|
---|
351 | msg->call = *call;
|
---|
352 |
|
---|
353 | fid_t fid = fibril_create(notification_fibril, msg);
|
---|
354 | fibril_add_ready(fid);
|
---|
355 |
|
---|
356 | futex_up(&async_futex);
|
---|
357 | return true;
|
---|
358 | }
|
---|
359 |
|
---|
360 | /** Return new incoming message for the current (fibril-local) connection.
|
---|
361 | *
|
---|
362 | * @param call Storage where the incoming call data will be stored.
|
---|
363 | * @param usecs Timeout in microseconds. Zero denotes no timeout.
|
---|
364 | *
|
---|
365 | * @return If no timeout was specified, then a hash of the
|
---|
366 | * incoming call is returned. If a timeout is specified,
|
---|
367 | * then a hash of the incoming call is returned unless
|
---|
368 | * the timeout expires prior to receiving a message. In
|
---|
369 | * that case zero is returned.
|
---|
370 | *
|
---|
371 | */
|
---|
372 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs)
|
---|
373 | {
|
---|
374 | assert(FIBRIL_connection);
|
---|
375 |
|
---|
376 | /* Why doing this?
|
---|
377 | * GCC 4.1.0 coughs on FIBRIL_connection-> dereference.
|
---|
378 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot.
|
---|
379 | * I would never expect to find so many errors in
|
---|
380 | * a compiler.
|
---|
381 | */
|
---|
382 | connection_t *conn = FIBRIL_connection;
|
---|
383 |
|
---|
384 | futex_down(&async_futex);
|
---|
385 |
|
---|
386 | if (usecs) {
|
---|
387 | gettimeofday(&conn->wdata.to_event.expires, NULL);
|
---|
388 | tv_add(&conn->wdata.to_event.expires, usecs);
|
---|
389 | } else
|
---|
390 | conn->wdata.to_event.inlist = false;
|
---|
391 |
|
---|
392 | /* If nothing in queue, wait until something arrives */
|
---|
393 | while (list_empty(&conn->msg_queue)) {
|
---|
394 | if (conn->close_callid) {
|
---|
395 | /*
|
---|
396 | * Handle the case when the connection was already
|
---|
397 | * closed by the client but the server did not notice
|
---|
398 | * the first IPC_M_PHONE_HUNGUP call and continues to
|
---|
399 | * call async_get_call_timeout(). Repeat
|
---|
400 | * IPC_M_PHONE_HUNGUP until the caller notices.
|
---|
401 | */
|
---|
402 | memset(call, 0, sizeof(ipc_call_t));
|
---|
403 | IPC_SET_METHOD(*call, IPC_M_PHONE_HUNGUP);
|
---|
404 | futex_up(&async_futex);
|
---|
405 | return conn->close_callid;
|
---|
406 | }
|
---|
407 |
|
---|
408 | if (usecs)
|
---|
409 | async_insert_timeout(&conn->wdata);
|
---|
410 |
|
---|
411 | conn->wdata.active = false;
|
---|
412 |
|
---|
413 | /*
|
---|
414 | * Note: the current fibril will be rescheduled either due to a
|
---|
415 | * timeout or due to an arriving message destined to it. In the
|
---|
416 | * former case, handle_expired_timeouts() and, in the latter
|
---|
417 | * case, route_call() will perform the wakeup.
|
---|
418 | */
|
---|
419 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * Futex is up after getting back from async_manager.
|
---|
423 | * Get it again.
|
---|
424 | */
|
---|
425 | futex_down(&async_futex);
|
---|
426 | if ((usecs) && (conn->wdata.to_event.occurred)
|
---|
427 | && (list_empty(&conn->msg_queue))) {
|
---|
428 | /* If we timed out -> exit */
|
---|
429 | futex_up(&async_futex);
|
---|
430 | return 0;
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link);
|
---|
435 | list_remove(&msg->link);
|
---|
436 |
|
---|
437 | ipc_callid_t callid = msg->callid;
|
---|
438 | *call = msg->call;
|
---|
439 | free(msg);
|
---|
440 |
|
---|
441 | futex_up(&async_futex);
|
---|
442 | return callid;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /** Default fibril function that gets called to handle new connection.
|
---|
446 | *
|
---|
447 | * This function is defined as a weak symbol - to be redefined in user code.
|
---|
448 | *
|
---|
449 | * @param callid Hash of the incoming call.
|
---|
450 | * @param call Data of the incoming call.
|
---|
451 | *
|
---|
452 | */
|
---|
453 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call)
|
---|
454 | {
|
---|
455 | ipc_answer_0(callid, ENOENT);
|
---|
456 | }
|
---|
457 |
|
---|
458 | /** Default fibril function that gets called to handle interrupt notifications.
|
---|
459 | *
|
---|
460 | * This function is defined as a weak symbol - to be redefined in user code.
|
---|
461 | *
|
---|
462 | * @param callid Hash of the incoming call.
|
---|
463 | * @param call Data of the incoming call.
|
---|
464 | *
|
---|
465 | */
|
---|
466 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call)
|
---|
467 | {
|
---|
468 | }
|
---|
469 |
|
---|
470 | /** Wrapper for client connection fibril.
|
---|
471 | *
|
---|
472 | * When a new connection arrives, a fibril with this implementing function is
|
---|
473 | * created. It calls client_connection() and does the final cleanup.
|
---|
474 | *
|
---|
475 | * @param arg Connection structure pointer.
|
---|
476 | *
|
---|
477 | * @return Always zero.
|
---|
478 | *
|
---|
479 | */
|
---|
480 | static int connection_fibril(void *arg)
|
---|
481 | {
|
---|
482 | /*
|
---|
483 | * Setup fibril-local connection pointer and call client_connection().
|
---|
484 | *
|
---|
485 | */
|
---|
486 | FIBRIL_connection = (connection_t *) arg;
|
---|
487 | FIBRIL_connection->cfibril(FIBRIL_connection->callid,
|
---|
488 | &FIBRIL_connection->call);
|
---|
489 |
|
---|
490 | /* Remove myself from the connection hash table */
|
---|
491 | futex_down(&async_futex);
|
---|
492 | unsigned long key = FIBRIL_connection->in_phone_hash;
|
---|
493 | hash_table_remove(&conn_hash_table, &key, 1);
|
---|
494 | futex_up(&async_futex);
|
---|
495 |
|
---|
496 | /* Answer all remaining messages with EHANGUP */
|
---|
497 | while (!list_empty(&FIBRIL_connection->msg_queue)) {
|
---|
498 | msg_t *msg;
|
---|
499 |
|
---|
500 | msg = list_get_instance(FIBRIL_connection->msg_queue.next,
|
---|
501 | msg_t, link);
|
---|
502 | list_remove(&msg->link);
|
---|
503 | ipc_answer_0(msg->callid, EHANGUP);
|
---|
504 | free(msg);
|
---|
505 | }
|
---|
506 |
|
---|
507 | if (FIBRIL_connection->close_callid)
|
---|
508 | ipc_answer_0(FIBRIL_connection->close_callid, EOK);
|
---|
509 |
|
---|
510 | return 0;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /** Create a new fibril for a new connection.
|
---|
514 | *
|
---|
515 | * Create new fibril for connection, fill in connection structures and inserts
|
---|
516 | * it into the hash table, so that later we can easily do routing of messages to
|
---|
517 | * particular fibrils.
|
---|
518 | *
|
---|
519 | * @param in_phone_hash Identification of the incoming connection.
|
---|
520 | * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call.
|
---|
521 | * If callid is zero, the connection was opened by
|
---|
522 | * accepting the IPC_M_CONNECT_TO_ME call and this function
|
---|
523 | * is called directly by the server.
|
---|
524 | * @param call Call data of the opening call.
|
---|
525 | * @param cfibril Fibril function that should be called upon opening the
|
---|
526 | * connection.
|
---|
527 | *
|
---|
528 | * @return New fibril id or NULL on failure.
|
---|
529 | *
|
---|
530 | */
|
---|
531 | fid_t async_new_connection(ipcarg_t in_phone_hash, ipc_callid_t callid,
|
---|
532 | ipc_call_t *call, void (*cfibril)(ipc_callid_t, ipc_call_t *))
|
---|
533 | {
|
---|
534 | connection_t *conn = malloc(sizeof(*conn));
|
---|
535 | if (!conn) {
|
---|
536 | if (callid)
|
---|
537 | ipc_answer_0(callid, ENOMEM);
|
---|
538 | return NULL;
|
---|
539 | }
|
---|
540 |
|
---|
541 | conn->in_phone_hash = in_phone_hash;
|
---|
542 | list_initialize(&conn->msg_queue);
|
---|
543 | conn->callid = callid;
|
---|
544 | conn->close_callid = 0;
|
---|
545 |
|
---|
546 | if (call)
|
---|
547 | conn->call = *call;
|
---|
548 |
|
---|
549 | /* We will activate the fibril ASAP */
|
---|
550 | conn->wdata.active = true;
|
---|
551 | conn->cfibril = cfibril;
|
---|
552 | conn->wdata.fid = fibril_create(connection_fibril, conn);
|
---|
553 |
|
---|
554 | if (!conn->wdata.fid) {
|
---|
555 | free(conn);
|
---|
556 | if (callid)
|
---|
557 | ipc_answer_0(callid, ENOMEM);
|
---|
558 | return NULL;
|
---|
559 | }
|
---|
560 |
|
---|
561 | /* Add connection to the connection hash table */
|
---|
562 | unsigned long key = conn->in_phone_hash;
|
---|
563 |
|
---|
564 | futex_down(&async_futex);
|
---|
565 | hash_table_insert(&conn_hash_table, &key, &conn->link);
|
---|
566 | futex_up(&async_futex);
|
---|
567 |
|
---|
568 | fibril_add_ready(conn->wdata.fid);
|
---|
569 |
|
---|
570 | return conn->wdata.fid;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /** Handle a call that was received.
|
---|
574 | *
|
---|
575 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created.
|
---|
576 | * Otherwise the call is routed to its connection fibril.
|
---|
577 | *
|
---|
578 | * @param callid Hash of the incoming call.
|
---|
579 | * @param call Data of the incoming call.
|
---|
580 | *
|
---|
581 | */
|
---|
582 | static void handle_call(ipc_callid_t callid, ipc_call_t *call)
|
---|
583 | {
|
---|
584 | /* Unrouted call - do some default behaviour */
|
---|
585 | if ((callid & IPC_CALLID_NOTIFICATION)) {
|
---|
586 | process_notification(callid, call);
|
---|
587 | goto out;
|
---|
588 | }
|
---|
589 |
|
---|
590 | switch (IPC_GET_METHOD(*call)) {
|
---|
591 | case IPC_M_CONNECT_ME:
|
---|
592 | case IPC_M_CONNECT_ME_TO:
|
---|
593 | /* Open new connection with fibril etc. */
|
---|
594 | async_new_connection(IPC_GET_ARG5(*call), callid, call,
|
---|
595 | client_connection);
|
---|
596 | goto out;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /* Try to route the call through the connection hash table */
|
---|
600 | if (route_call(callid, call))
|
---|
601 | goto out;
|
---|
602 |
|
---|
603 | /* Unknown call from unknown phone - hang it up */
|
---|
604 | ipc_answer_0(callid, EHANGUP);
|
---|
605 | return;
|
---|
606 |
|
---|
607 | out:
|
---|
608 | ;
|
---|
609 | }
|
---|
610 |
|
---|
611 | /** Fire all timeouts that expired. */
|
---|
612 | static void handle_expired_timeouts(void)
|
---|
613 | {
|
---|
614 | struct timeval tv;
|
---|
615 | gettimeofday(&tv, NULL);
|
---|
616 |
|
---|
617 | futex_down(&async_futex);
|
---|
618 |
|
---|
619 | link_t *cur = timeout_list.next;
|
---|
620 | while (cur != &timeout_list) {
|
---|
621 | awaiter_t *waiter;
|
---|
622 |
|
---|
623 | waiter = list_get_instance(cur, awaiter_t, to_event.link);
|
---|
624 | if (tv_gt(&waiter->to_event.expires, &tv))
|
---|
625 | break;
|
---|
626 |
|
---|
627 | cur = cur->next;
|
---|
628 |
|
---|
629 | list_remove(&waiter->to_event.link);
|
---|
630 | waiter->to_event.inlist = false;
|
---|
631 | waiter->to_event.occurred = true;
|
---|
632 |
|
---|
633 | /*
|
---|
634 | * Redundant condition?
|
---|
635 | * The fibril should not be active when it gets here.
|
---|
636 | */
|
---|
637 | if (!waiter->active) {
|
---|
638 | waiter->active = true;
|
---|
639 | fibril_add_ready(waiter->fid);
|
---|
640 | }
|
---|
641 | }
|
---|
642 |
|
---|
643 | futex_up(&async_futex);
|
---|
644 | }
|
---|
645 |
|
---|
646 | /** Endless loop dispatching incoming calls and answers.
|
---|
647 | *
|
---|
648 | * @return Never returns.
|
---|
649 | *
|
---|
650 | */
|
---|
651 | static int async_manager_worker(void)
|
---|
652 | {
|
---|
653 | while (true) {
|
---|
654 | if (fibril_switch(FIBRIL_FROM_MANAGER)) {
|
---|
655 | futex_up(&async_futex);
|
---|
656 | /*
|
---|
657 | * async_futex is always held when entering a manager
|
---|
658 | * fibril.
|
---|
659 | */
|
---|
660 | continue;
|
---|
661 | }
|
---|
662 |
|
---|
663 | futex_down(&async_futex);
|
---|
664 |
|
---|
665 | suseconds_t timeout;
|
---|
666 | if (!list_empty(&timeout_list)) {
|
---|
667 | awaiter_t *waiter = list_get_instance(timeout_list.next,
|
---|
668 | awaiter_t, to_event.link);
|
---|
669 |
|
---|
670 | struct timeval tv;
|
---|
671 | gettimeofday(&tv, NULL);
|
---|
672 |
|
---|
673 | if (tv_gteq(&tv, &waiter->to_event.expires)) {
|
---|
674 | futex_up(&async_futex);
|
---|
675 | handle_expired_timeouts();
|
---|
676 | continue;
|
---|
677 | } else
|
---|
678 | timeout = tv_sub(&waiter->to_event.expires,
|
---|
679 | &tv);
|
---|
680 | } else
|
---|
681 | timeout = SYNCH_NO_TIMEOUT;
|
---|
682 |
|
---|
683 | futex_up(&async_futex);
|
---|
684 |
|
---|
685 | atomic_inc(&threads_in_ipc_wait);
|
---|
686 |
|
---|
687 | ipc_call_t call;
|
---|
688 | ipc_callid_t callid = ipc_wait_cycle(&call, timeout,
|
---|
689 | SYNCH_FLAGS_NONE);
|
---|
690 |
|
---|
691 | atomic_dec(&threads_in_ipc_wait);
|
---|
692 |
|
---|
693 | if (!callid) {
|
---|
694 | handle_expired_timeouts();
|
---|
695 | continue;
|
---|
696 | }
|
---|
697 |
|
---|
698 | if (callid & IPC_CALLID_ANSWERED)
|
---|
699 | continue;
|
---|
700 |
|
---|
701 | handle_call(callid, &call);
|
---|
702 | }
|
---|
703 |
|
---|
704 | return 0;
|
---|
705 | }
|
---|
706 |
|
---|
707 | /** Function to start async_manager as a standalone fibril.
|
---|
708 | *
|
---|
709 | * When more kernel threads are used, one async manager should exist per thread.
|
---|
710 | *
|
---|
711 | * @param arg Unused.
|
---|
712 | * @return Never returns.
|
---|
713 | *
|
---|
714 | */
|
---|
715 | static int async_manager_fibril(void *arg)
|
---|
716 | {
|
---|
717 | futex_up(&async_futex);
|
---|
718 |
|
---|
719 | /*
|
---|
720 | * async_futex is always locked when entering manager
|
---|
721 | */
|
---|
722 | async_manager_worker();
|
---|
723 |
|
---|
724 | return 0;
|
---|
725 | }
|
---|
726 |
|
---|
727 | /** Add one manager to manager list. */
|
---|
728 | void async_create_manager(void)
|
---|
729 | {
|
---|
730 | fid_t fid = fibril_create(async_manager_fibril, NULL);
|
---|
731 | fibril_add_manager(fid);
|
---|
732 | }
|
---|
733 |
|
---|
734 | /** Remove one manager from manager list */
|
---|
735 | void async_destroy_manager(void)
|
---|
736 | {
|
---|
737 | fibril_remove_manager();
|
---|
738 | }
|
---|
739 |
|
---|
740 | /** Initialize the async framework.
|
---|
741 | *
|
---|
742 | * @return Zero on success or an error code.
|
---|
743 | */
|
---|
744 | int __async_init(void)
|
---|
745 | {
|
---|
746 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1,
|
---|
747 | &conn_hash_table_ops)) {
|
---|
748 | printf("%s: cannot create hash table\n", "async");
|
---|
749 | return ENOMEM;
|
---|
750 | }
|
---|
751 |
|
---|
752 | return 0;
|
---|
753 | }
|
---|
754 |
|
---|
755 | /** Reply received callback.
|
---|
756 | *
|
---|
757 | * This function is called whenever a reply for an asynchronous message sent out
|
---|
758 | * by the asynchronous framework is received.
|
---|
759 | *
|
---|
760 | * Notify the fibril which is waiting for this message that it has arrived.
|
---|
761 | *
|
---|
762 | * @param arg Pointer to the asynchronous message record.
|
---|
763 | * @param retval Value returned in the answer.
|
---|
764 | * @param data Call data of the answer.
|
---|
765 | */
|
---|
766 | static void reply_received(void *arg, int retval, ipc_call_t *data)
|
---|
767 | {
|
---|
768 | futex_down(&async_futex);
|
---|
769 |
|
---|
770 | amsg_t *msg = (amsg_t *) arg;
|
---|
771 | msg->retval = retval;
|
---|
772 |
|
---|
773 | /* Copy data after futex_down, just in case the call was detached */
|
---|
774 | if ((msg->dataptr) && (data))
|
---|
775 | *msg->dataptr = *data;
|
---|
776 |
|
---|
777 | write_barrier();
|
---|
778 |
|
---|
779 | /* Remove message from timeout list */
|
---|
780 | if (msg->wdata.to_event.inlist)
|
---|
781 | list_remove(&msg->wdata.to_event.link);
|
---|
782 |
|
---|
783 | msg->done = true;
|
---|
784 | if (!msg->wdata.active) {
|
---|
785 | msg->wdata.active = true;
|
---|
786 | fibril_add_ready(msg->wdata.fid);
|
---|
787 | }
|
---|
788 |
|
---|
789 | futex_up(&async_futex);
|
---|
790 | }
|
---|
791 |
|
---|
792 | /** Send message and return id of the sent message.
|
---|
793 | *
|
---|
794 | * The return value can be used as input for async_wait() to wait for
|
---|
795 | * completion.
|
---|
796 | *
|
---|
797 | * @param phoneid Handle of the phone that will be used for the send.
|
---|
798 | * @param method Service-defined method.
|
---|
799 | * @param arg1 Service-defined payload argument.
|
---|
800 | * @param arg2 Service-defined payload argument.
|
---|
801 | * @param arg3 Service-defined payload argument.
|
---|
802 | * @param arg4 Service-defined payload argument.
|
---|
803 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
804 | * stored.
|
---|
805 | *
|
---|
806 | * @return Hash of the sent message or 0 on error.
|
---|
807 | *
|
---|
808 | */
|
---|
809 | aid_t async_send_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
810 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr)
|
---|
811 | {
|
---|
812 | amsg_t *msg = malloc(sizeof(*msg));
|
---|
813 |
|
---|
814 | if (!msg)
|
---|
815 | return 0;
|
---|
816 |
|
---|
817 | msg->done = false;
|
---|
818 | msg->dataptr = dataptr;
|
---|
819 |
|
---|
820 | msg->wdata.to_event.inlist = false;
|
---|
821 | /* We may sleep in the next method, but it will use its own mechanism */
|
---|
822 | msg->wdata.active = true;
|
---|
823 |
|
---|
824 | ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg,
|
---|
825 | reply_received, true);
|
---|
826 |
|
---|
827 | return (aid_t) msg;
|
---|
828 | }
|
---|
829 |
|
---|
830 | /** Send message and return id of the sent message
|
---|
831 | *
|
---|
832 | * The return value can be used as input for async_wait() to wait for
|
---|
833 | * completion.
|
---|
834 | *
|
---|
835 | * @param phoneid Handle of the phone that will be used for the send.
|
---|
836 | * @param method Service-defined method.
|
---|
837 | * @param arg1 Service-defined payload argument.
|
---|
838 | * @param arg2 Service-defined payload argument.
|
---|
839 | * @param arg3 Service-defined payload argument.
|
---|
840 | * @param arg4 Service-defined payload argument.
|
---|
841 | * @param arg5 Service-defined payload argument.
|
---|
842 | * @param dataptr If non-NULL, storage where the reply data will be
|
---|
843 | * stored.
|
---|
844 | *
|
---|
845 | * @return Hash of the sent message or 0 on error.
|
---|
846 | *
|
---|
847 | */
|
---|
848 | aid_t async_send_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
849 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5,
|
---|
850 | ipc_call_t *dataptr)
|
---|
851 | {
|
---|
852 | amsg_t *msg = malloc(sizeof(*msg));
|
---|
853 |
|
---|
854 | if (!msg)
|
---|
855 | return 0;
|
---|
856 |
|
---|
857 | msg->done = false;
|
---|
858 | msg->dataptr = dataptr;
|
---|
859 |
|
---|
860 | msg->wdata.to_event.inlist = false;
|
---|
861 | /* We may sleep in next method, but it will use its own mechanism */
|
---|
862 | msg->wdata.active = true;
|
---|
863 |
|
---|
864 | ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg,
|
---|
865 | reply_received, true);
|
---|
866 |
|
---|
867 | return (aid_t) msg;
|
---|
868 | }
|
---|
869 |
|
---|
870 | /** Wait for a message sent by the async framework.
|
---|
871 | *
|
---|
872 | * @param amsgid Hash of the message to wait for.
|
---|
873 | * @param retval Pointer to storage where the retval of the answer will
|
---|
874 | * be stored.
|
---|
875 | *
|
---|
876 | */
|
---|
877 | void async_wait_for(aid_t amsgid, ipcarg_t *retval)
|
---|
878 | {
|
---|
879 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
880 |
|
---|
881 | futex_down(&async_futex);
|
---|
882 | if (msg->done) {
|
---|
883 | futex_up(&async_futex);
|
---|
884 | goto done;
|
---|
885 | }
|
---|
886 |
|
---|
887 | msg->wdata.fid = fibril_get_id();
|
---|
888 | msg->wdata.active = false;
|
---|
889 | msg->wdata.to_event.inlist = false;
|
---|
890 |
|
---|
891 | /* Leave the async_futex locked when entering this function */
|
---|
892 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
893 |
|
---|
894 | /* Futex is up automatically after fibril_switch */
|
---|
895 |
|
---|
896 | done:
|
---|
897 | if (retval)
|
---|
898 | *retval = msg->retval;
|
---|
899 |
|
---|
900 | free(msg);
|
---|
901 | }
|
---|
902 |
|
---|
903 | /** Wait for a message sent by the async framework, timeout variant.
|
---|
904 | *
|
---|
905 | * @param amsgid Hash of the message to wait for.
|
---|
906 | * @param retval Pointer to storage where the retval of the answer will
|
---|
907 | * be stored.
|
---|
908 | * @param timeout Timeout in microseconds.
|
---|
909 | *
|
---|
910 | * @return Zero on success, ETIMEOUT if the timeout has expired.
|
---|
911 | *
|
---|
912 | */
|
---|
913 | int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout)
|
---|
914 | {
|
---|
915 | amsg_t *msg = (amsg_t *) amsgid;
|
---|
916 |
|
---|
917 | /* TODO: Let it go through the event read at least once */
|
---|
918 | if (timeout < 0)
|
---|
919 | return ETIMEOUT;
|
---|
920 |
|
---|
921 | futex_down(&async_futex);
|
---|
922 | if (msg->done) {
|
---|
923 | futex_up(&async_futex);
|
---|
924 | goto done;
|
---|
925 | }
|
---|
926 |
|
---|
927 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
---|
928 | tv_add(&msg->wdata.to_event.expires, timeout);
|
---|
929 |
|
---|
930 | msg->wdata.fid = fibril_get_id();
|
---|
931 | msg->wdata.active = false;
|
---|
932 | async_insert_timeout(&msg->wdata);
|
---|
933 |
|
---|
934 | /* Leave the async_futex locked when entering this function */
|
---|
935 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
936 |
|
---|
937 | /* Futex is up automatically after fibril_switch */
|
---|
938 |
|
---|
939 | if (!msg->done)
|
---|
940 | return ETIMEOUT;
|
---|
941 |
|
---|
942 | done:
|
---|
943 | if (retval)
|
---|
944 | *retval = msg->retval;
|
---|
945 |
|
---|
946 | free(msg);
|
---|
947 |
|
---|
948 | return 0;
|
---|
949 | }
|
---|
950 |
|
---|
951 | /** Wait for specified time.
|
---|
952 | *
|
---|
953 | * The current fibril is suspended but the thread continues to execute.
|
---|
954 | *
|
---|
955 | * @param timeout Duration of the wait in microseconds.
|
---|
956 | *
|
---|
957 | */
|
---|
958 | void async_usleep(suseconds_t timeout)
|
---|
959 | {
|
---|
960 | amsg_t *msg = malloc(sizeof(*msg));
|
---|
961 |
|
---|
962 | if (!msg)
|
---|
963 | return;
|
---|
964 |
|
---|
965 | msg->wdata.fid = fibril_get_id();
|
---|
966 | msg->wdata.active = false;
|
---|
967 |
|
---|
968 | gettimeofday(&msg->wdata.to_event.expires, NULL);
|
---|
969 | tv_add(&msg->wdata.to_event.expires, timeout);
|
---|
970 |
|
---|
971 | futex_down(&async_futex);
|
---|
972 |
|
---|
973 | async_insert_timeout(&msg->wdata);
|
---|
974 |
|
---|
975 | /* Leave the async_futex locked when entering this function */
|
---|
976 | fibril_switch(FIBRIL_TO_MANAGER);
|
---|
977 |
|
---|
978 | /* Futex is up automatically after fibril_switch() */
|
---|
979 |
|
---|
980 | free(msg);
|
---|
981 | }
|
---|
982 |
|
---|
983 | /** Setter for client_connection function pointer.
|
---|
984 | *
|
---|
985 | * @param conn Function that will implement a new connection fibril.
|
---|
986 | *
|
---|
987 | */
|
---|
988 | void async_set_client_connection(async_client_conn_t conn)
|
---|
989 | {
|
---|
990 | client_connection = conn;
|
---|
991 | }
|
---|
992 |
|
---|
993 | /** Setter for interrupt_received function pointer.
|
---|
994 | *
|
---|
995 | * @param intr Function that will implement a new interrupt
|
---|
996 | * notification fibril.
|
---|
997 | */
|
---|
998 | void async_set_interrupt_received(async_client_conn_t intr)
|
---|
999 | {
|
---|
1000 | interrupt_received = intr;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | /** Pseudo-synchronous message sending - fast version.
|
---|
1004 | *
|
---|
1005 | * Send message asynchronously and return only after the reply arrives.
|
---|
1006 | *
|
---|
1007 | * This function can only transfer 4 register payload arguments. For
|
---|
1008 | * transferring more arguments, see the slower async_req_slow().
|
---|
1009 | *
|
---|
1010 | * @param phoneid Hash of the phone through which to make the call.
|
---|
1011 | * @param method Method of the call.
|
---|
1012 | * @param arg1 Service-defined payload argument.
|
---|
1013 | * @param arg2 Service-defined payload argument.
|
---|
1014 | * @param arg3 Service-defined payload argument.
|
---|
1015 | * @param arg4 Service-defined payload argument.
|
---|
1016 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
1017 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
1018 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
1019 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
1020 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
1021 | *
|
---|
1022 | * @return Return code of the reply or a negative error code.
|
---|
1023 | *
|
---|
1024 | */
|
---|
1025 | ipcarg_t async_req_fast(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
1026 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t *r1, ipcarg_t *r2,
|
---|
1027 | ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
|
---|
1028 | {
|
---|
1029 | ipc_call_t result;
|
---|
1030 | aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4,
|
---|
1031 | &result);
|
---|
1032 |
|
---|
1033 | ipcarg_t rc;
|
---|
1034 | async_wait_for(eid, &rc);
|
---|
1035 |
|
---|
1036 | if (r1)
|
---|
1037 | *r1 = IPC_GET_ARG1(result);
|
---|
1038 |
|
---|
1039 | if (r2)
|
---|
1040 | *r2 = IPC_GET_ARG2(result);
|
---|
1041 |
|
---|
1042 | if (r3)
|
---|
1043 | *r3 = IPC_GET_ARG3(result);
|
---|
1044 |
|
---|
1045 | if (r4)
|
---|
1046 | *r4 = IPC_GET_ARG4(result);
|
---|
1047 |
|
---|
1048 | if (r5)
|
---|
1049 | *r5 = IPC_GET_ARG5(result);
|
---|
1050 |
|
---|
1051 | return rc;
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | /** Pseudo-synchronous message sending - slow version.
|
---|
1055 | *
|
---|
1056 | * Send message asynchronously and return only after the reply arrives.
|
---|
1057 | *
|
---|
1058 | * @param phoneid Hash of the phone through which to make the call.
|
---|
1059 | * @param method Method of the call.
|
---|
1060 | * @param arg1 Service-defined payload argument.
|
---|
1061 | * @param arg2 Service-defined payload argument.
|
---|
1062 | * @param arg3 Service-defined payload argument.
|
---|
1063 | * @param arg4 Service-defined payload argument.
|
---|
1064 | * @param arg5 Service-defined payload argument.
|
---|
1065 | * @param r1 If non-NULL, storage for the 1st reply argument.
|
---|
1066 | * @param r2 If non-NULL, storage for the 2nd reply argument.
|
---|
1067 | * @param r3 If non-NULL, storage for the 3rd reply argument.
|
---|
1068 | * @param r4 If non-NULL, storage for the 4th reply argument.
|
---|
1069 | * @param r5 If non-NULL, storage for the 5th reply argument.
|
---|
1070 | *
|
---|
1071 | * @return Return code of the reply or a negative error code.
|
---|
1072 | *
|
---|
1073 | */
|
---|
1074 | ipcarg_t async_req_slow(int phoneid, ipcarg_t method, ipcarg_t arg1,
|
---|
1075 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *r1,
|
---|
1076 | ipcarg_t *r2, ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5)
|
---|
1077 | {
|
---|
1078 | ipc_call_t result;
|
---|
1079 | aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5,
|
---|
1080 | &result);
|
---|
1081 |
|
---|
1082 | ipcarg_t rc;
|
---|
1083 | async_wait_for(eid, &rc);
|
---|
1084 |
|
---|
1085 | if (r1)
|
---|
1086 | *r1 = IPC_GET_ARG1(result);
|
---|
1087 |
|
---|
1088 | if (r2)
|
---|
1089 | *r2 = IPC_GET_ARG2(result);
|
---|
1090 |
|
---|
1091 | if (r3)
|
---|
1092 | *r3 = IPC_GET_ARG3(result);
|
---|
1093 |
|
---|
1094 | if (r4)
|
---|
1095 | *r4 = IPC_GET_ARG4(result);
|
---|
1096 |
|
---|
1097 | if (r5)
|
---|
1098 | *r5 = IPC_GET_ARG5(result);
|
---|
1099 |
|
---|
1100 | return rc;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | /** Wrapper for making IPC_M_SHARE_IN calls using the async framework.
|
---|
1104 | *
|
---|
1105 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
1106 | * @param dst Destination address space area base.
|
---|
1107 | * @param size Size of the destination address space area.
|
---|
1108 | * @param arg User defined argument.
|
---|
1109 | * @param flags Storage where the received flags will be stored. Can be
|
---|
1110 | * NULL.
|
---|
1111 | *
|
---|
1112 | * @return Zero on success or a negative error code from errno.h.
|
---|
1113 | */
|
---|
1114 | int async_share_in_start(int phoneid, void *dst, size_t size, ipcarg_t arg,
|
---|
1115 | int *flags)
|
---|
1116 | {
|
---|
1117 | int res;
|
---|
1118 | sysarg_t tmp_flags;
|
---|
1119 | res = async_req_3_2(phoneid, IPC_M_SHARE_IN, (ipcarg_t) dst,
|
---|
1120 | (ipcarg_t) size, arg, NULL, &tmp_flags);
|
---|
1121 | if (flags)
|
---|
1122 | *flags = tmp_flags;
|
---|
1123 | return res;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | /** Wrapper for receiving the IPC_M_SHARE_IN calls using the async framework.
|
---|
1127 | *
|
---|
1128 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_IN calls
|
---|
1129 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
1130 | *
|
---|
1131 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1132 | *
|
---|
1133 | * @param callid Storage where the hash of the IPC_M_SHARE_IN call will
|
---|
1134 | * be stored.
|
---|
1135 | * @param size Destination address space area size.
|
---|
1136 | *
|
---|
1137 | * @return Non-zero on success, zero on failure.
|
---|
1138 | */
|
---|
1139 | int async_share_in_receive(ipc_callid_t *callid, size_t *size)
|
---|
1140 | {
|
---|
1141 | ipc_call_t data;
|
---|
1142 |
|
---|
1143 | assert(callid);
|
---|
1144 | assert(size);
|
---|
1145 |
|
---|
1146 | *callid = async_get_call(&data);
|
---|
1147 | if (IPC_GET_METHOD(data) != IPC_M_SHARE_IN)
|
---|
1148 | return 0;
|
---|
1149 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
1150 | return 1;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | /** Wrapper for answering the IPC_M_SHARE_IN calls using the async framework.
|
---|
1154 | *
|
---|
1155 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
|
---|
1156 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
1157 | *
|
---|
1158 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
1159 | * @param src Source address space base.
|
---|
1160 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
1161 | *
|
---|
1162 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1163 | */
|
---|
1164 | int async_share_in_finalize(ipc_callid_t callid, void *src, int flags)
|
---|
1165 | {
|
---|
1166 | return ipc_share_in_finalize(callid, src, flags);
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | /** Wrapper for making IPC_M_SHARE_OUT calls using the async framework.
|
---|
1170 | *
|
---|
1171 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
1172 | * @param src Source address space area base address.
|
---|
1173 | * @param flags Flags to be used for sharing. Bits can be only cleared.
|
---|
1174 | *
|
---|
1175 | * @return Zero on success or a negative error code from errno.h.
|
---|
1176 | */
|
---|
1177 | int async_share_out_start(int phoneid, void *src, int flags)
|
---|
1178 | {
|
---|
1179 | return async_req_3_0(phoneid, IPC_M_SHARE_OUT, (ipcarg_t) src, 0,
|
---|
1180 | (ipcarg_t) flags);
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | /** Wrapper for receiving the IPC_M_SHARE_OUT calls using the async framework.
|
---|
1184 | *
|
---|
1185 | * This wrapper only makes it more comfortable to receive IPC_M_SHARE_OUT calls
|
---|
1186 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
1187 | *
|
---|
1188 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1189 | *
|
---|
1190 | * @param callid Storage where the hash of the IPC_M_SHARE_OUT call will
|
---|
1191 | * be stored.
|
---|
1192 | * @param size Storage where the source address space area size will be
|
---|
1193 | * stored.
|
---|
1194 | * @param flags Storage where the sharing flags will be stored.
|
---|
1195 | *
|
---|
1196 | * @return Non-zero on success, zero on failure.
|
---|
1197 | */
|
---|
1198 | int async_share_out_receive(ipc_callid_t *callid, size_t *size, int *flags)
|
---|
1199 | {
|
---|
1200 | ipc_call_t data;
|
---|
1201 |
|
---|
1202 | assert(callid);
|
---|
1203 | assert(size);
|
---|
1204 | assert(flags);
|
---|
1205 |
|
---|
1206 | *callid = async_get_call(&data);
|
---|
1207 | if (IPC_GET_METHOD(data) != IPC_M_SHARE_OUT)
|
---|
1208 | return 0;
|
---|
1209 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
1210 | *flags = (int) IPC_GET_ARG3(data);
|
---|
1211 | return 1;
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | /** Wrapper for answering the IPC_M_SHARE_OUT calls using the async framework.
|
---|
1215 | *
|
---|
1216 | * This wrapper only makes it more comfortable to answer IPC_M_SHARE_OUT calls
|
---|
1217 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
1218 | *
|
---|
1219 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
1220 | * @param dst Destination address space area base address.
|
---|
1221 | *
|
---|
1222 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1223 | */
|
---|
1224 | int async_share_out_finalize(ipc_callid_t callid, void *dst)
|
---|
1225 | {
|
---|
1226 | return ipc_share_out_finalize(callid, dst);
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 |
|
---|
1230 | /** Wrapper for making IPC_M_DATA_READ calls using the async framework.
|
---|
1231 | *
|
---|
1232 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
1233 | * @param dst Address of the beginning of the destination buffer.
|
---|
1234 | * @param size Size of the destination buffer.
|
---|
1235 | *
|
---|
1236 | * @return Zero on success or a negative error code from errno.h.
|
---|
1237 | */
|
---|
1238 | int async_data_read_start(int phoneid, void *dst, size_t size)
|
---|
1239 | {
|
---|
1240 | return async_req_2_0(phoneid, IPC_M_DATA_READ, (ipcarg_t) dst,
|
---|
1241 | (ipcarg_t) size);
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 | /** Wrapper for receiving the IPC_M_DATA_READ calls using the async framework.
|
---|
1245 | *
|
---|
1246 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_READ calls
|
---|
1247 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
1248 | *
|
---|
1249 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1250 | *
|
---|
1251 | * @param callid Storage where the hash of the IPC_M_DATA_READ call will
|
---|
1252 | * be stored.
|
---|
1253 | * @param size Storage where the maximum size will be stored. Can be
|
---|
1254 | * NULL.
|
---|
1255 | *
|
---|
1256 | * @return Non-zero on success, zero on failure.
|
---|
1257 | */
|
---|
1258 | int async_data_read_receive(ipc_callid_t *callid, size_t *size)
|
---|
1259 | {
|
---|
1260 | ipc_call_t data;
|
---|
1261 |
|
---|
1262 | assert(callid);
|
---|
1263 |
|
---|
1264 | *callid = async_get_call(&data);
|
---|
1265 | if (IPC_GET_METHOD(data) != IPC_M_DATA_READ)
|
---|
1266 | return 0;
|
---|
1267 | if (size)
|
---|
1268 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
1269 | return 1;
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | /** Wrapper for answering the IPC_M_DATA_READ calls using the async framework.
|
---|
1273 | *
|
---|
1274 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_READ calls
|
---|
1275 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
1276 | *
|
---|
1277 | * @param callid Hash of the IPC_M_DATA_READ call to answer.
|
---|
1278 | * @param src Source address for the IPC_M_DATA_READ call.
|
---|
1279 | * @param size Size for the IPC_M_DATA_READ call. Can be smaller than
|
---|
1280 | * the maximum size announced by the sender.
|
---|
1281 | *
|
---|
1282 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1283 | */
|
---|
1284 | int async_data_read_finalize(ipc_callid_t callid, const void *src, size_t size)
|
---|
1285 | {
|
---|
1286 | return ipc_data_read_finalize(callid, src, size);
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | /** Wrapper for making IPC_M_DATA_WRITE calls using the async framework.
|
---|
1290 | *
|
---|
1291 | * @param phoneid Phone that will be used to contact the receiving side.
|
---|
1292 | * @param src Address of the beginning of the source buffer.
|
---|
1293 | * @param size Size of the source buffer.
|
---|
1294 | *
|
---|
1295 | * @return Zero on success or a negative error code from errno.h.
|
---|
1296 | */
|
---|
1297 | int async_data_write_start(int phoneid, const void *src, size_t size)
|
---|
1298 | {
|
---|
1299 | return async_req_2_0(phoneid, IPC_M_DATA_WRITE, (ipcarg_t) src,
|
---|
1300 | (ipcarg_t) size);
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | /** Wrapper for receiving the IPC_M_DATA_WRITE calls using the async framework.
|
---|
1304 | *
|
---|
1305 | * This wrapper only makes it more comfortable to receive IPC_M_DATA_WRITE calls
|
---|
1306 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
1307 | *
|
---|
1308 | * So far, this wrapper is to be used from within a connection fibril.
|
---|
1309 | *
|
---|
1310 | * @param callid Storage where the hash of the IPC_M_DATA_WRITE call will
|
---|
1311 | * be stored.
|
---|
1312 | * @param size Storage where the suggested size will be stored. May be
|
---|
1313 | * NULL
|
---|
1314 | *
|
---|
1315 | * @return Non-zero on success, zero on failure.
|
---|
1316 | */
|
---|
1317 | int async_data_write_receive(ipc_callid_t *callid, size_t *size)
|
---|
1318 | {
|
---|
1319 | ipc_call_t data;
|
---|
1320 |
|
---|
1321 | assert(callid);
|
---|
1322 |
|
---|
1323 | *callid = async_get_call(&data);
|
---|
1324 | if (IPC_GET_METHOD(data) != IPC_M_DATA_WRITE)
|
---|
1325 | return 0;
|
---|
1326 | if (size)
|
---|
1327 | *size = (size_t) IPC_GET_ARG2(data);
|
---|
1328 | return 1;
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 | /** Wrapper for answering the IPC_M_DATA_WRITE calls using the async framework.
|
---|
1332 | *
|
---|
1333 | * This wrapper only makes it more comfortable to answer IPC_M_DATA_WRITE calls
|
---|
1334 | * so that the user doesn't have to remember the meaning of each IPC argument.
|
---|
1335 | *
|
---|
1336 | * @param callid Hash of the IPC_M_DATA_WRITE call to answer.
|
---|
1337 | * @param dst Final destination address for the IPC_M_DATA_WRITE call.
|
---|
1338 | * @param size Final size for the IPC_M_DATA_WRITE call.
|
---|
1339 | *
|
---|
1340 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1341 | */
|
---|
1342 | int async_data_write_finalize(ipc_callid_t callid, void *dst, size_t size)
|
---|
1343 | {
|
---|
1344 | return ipc_data_write_finalize(callid, dst, size);
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | /** Wrapper for receiving blobs via the async_data_write_*
|
---|
1348 | *
|
---|
1349 | * This wrapper only makes it more comfortable to use async_data_write_*
|
---|
1350 | * functions to receive blobs.
|
---|
1351 | *
|
---|
1352 | * @param blob Pointer to data pointer (which should be later disposed
|
---|
1353 | * by free()). If the operation fails, the pointer is not
|
---|
1354 | * touched.
|
---|
1355 | * @param max_size Maximum size (in bytes) of the blob to receive. 0 means
|
---|
1356 | * no limit.
|
---|
1357 | * @param received If not NULL, the size of the received data is stored here.
|
---|
1358 | *
|
---|
1359 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1360 | *
|
---|
1361 | */
|
---|
1362 | int async_data_blob_receive(char **blob, const size_t max_size, size_t *received)
|
---|
1363 | {
|
---|
1364 | ipc_callid_t callid;
|
---|
1365 | size_t size;
|
---|
1366 | if (!async_data_write_receive(&callid, &size)) {
|
---|
1367 | ipc_answer_0(callid, EINVAL);
|
---|
1368 | return EINVAL;
|
---|
1369 | }
|
---|
1370 |
|
---|
1371 | if ((max_size > 0) && (size > max_size)) {
|
---|
1372 | ipc_answer_0(callid, EINVAL);
|
---|
1373 | return EINVAL;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | char *data = (char *) malloc(size);
|
---|
1377 | if (data == NULL) {
|
---|
1378 | ipc_answer_0(callid, ENOMEM);
|
---|
1379 | return ENOMEM;
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | int rc = async_data_write_finalize(callid, data, size);
|
---|
1383 | if (rc != EOK) {
|
---|
1384 | free(data);
|
---|
1385 | return rc;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | *blob = data;
|
---|
1389 | if (received != NULL)
|
---|
1390 | *received = size;
|
---|
1391 |
|
---|
1392 | return EOK;
|
---|
1393 | }
|
---|
1394 |
|
---|
1395 | /** Wrapper for receiving strings via the async_data_write_*
|
---|
1396 | *
|
---|
1397 | * This wrapper only makes it more comfortable to use async_data_write_*
|
---|
1398 | * functions to receive strings.
|
---|
1399 | *
|
---|
1400 | * @param str Pointer to string pointer (which should be later disposed
|
---|
1401 | * by free()). If the operation fails, the pointer is not
|
---|
1402 | * touched.
|
---|
1403 | * @param max_size Maximum size (in bytes) of the string to receive. 0 means
|
---|
1404 | * no limit.
|
---|
1405 | *
|
---|
1406 | * @return Zero on success or a value from @ref errno.h on failure.
|
---|
1407 | *
|
---|
1408 | */
|
---|
1409 | int async_data_string_receive(char **str, const size_t max_size)
|
---|
1410 | {
|
---|
1411 | ipc_callid_t callid;
|
---|
1412 | size_t size;
|
---|
1413 | if (!async_data_write_receive(&callid, &size)) {
|
---|
1414 | ipc_answer_0(callid, EINVAL);
|
---|
1415 | return EINVAL;
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | if ((max_size > 0) && (size > max_size)) {
|
---|
1419 | ipc_answer_0(callid, EINVAL);
|
---|
1420 | return EINVAL;
|
---|
1421 | }
|
---|
1422 |
|
---|
1423 | char *data = (char *) malloc(size + 1);
|
---|
1424 | if (data == NULL) {
|
---|
1425 | ipc_answer_0(callid, ENOMEM);
|
---|
1426 | return ENOMEM;
|
---|
1427 | }
|
---|
1428 |
|
---|
1429 | int rc = async_data_write_finalize(callid, data, size);
|
---|
1430 | if (rc != EOK) {
|
---|
1431 | free(data);
|
---|
1432 | return rc;
|
---|
1433 | }
|
---|
1434 |
|
---|
1435 | data[size] = 0;
|
---|
1436 | *str = data;
|
---|
1437 | return EOK;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | /** @}
|
---|
1441 | */
|
---|