Rev 1441 | Rev 1453 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1351 | palkovsky | 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 | |||
1392 | palkovsky | 29 | /** |
30 | * Asynchronous library |
||
31 | * |
||
32 | * The aim of this library is facilitating writing programs utilizing |
||
33 | * the asynchronous nature of Helenos IPC, yet using a normal way |
||
34 | * of programming. |
||
35 | * |
||
36 | * You should be able to write very simple multithreaded programs, |
||
37 | * the async framework will automatically take care of most synchronization |
||
38 | * problems. |
||
39 | * |
||
40 | * Default semantics: |
||
41 | * - send() - send asynchronously. If the kernel refuses to send more |
||
42 | * messages, [ try to get responses from kernel, if nothing |
||
43 | * found, might try synchronous ] |
||
44 | * |
||
45 | * Example of use: |
||
46 | * |
||
47 | * 1) Multithreaded client application |
||
48 | * create_thread(thread1); |
||
49 | * create_thread(thread2); |
||
50 | * ... |
||
51 | * |
||
52 | * thread1() { |
||
53 | * conn = ipc_connect_me_to(); |
||
54 | * c1 = send(conn); |
||
55 | * c2 = send(conn); |
||
56 | * wait_for(c1); |
||
57 | * wait_for(c2); |
||
58 | * } |
||
59 | * |
||
60 | * |
||
61 | * 2) Multithreaded server application |
||
62 | * main() { |
||
1407 | palkovsky | 63 | * async_manager(); |
1392 | palkovsky | 64 | * } |
65 | * |
||
66 | * |
||
1407 | palkovsky | 67 | * client_connection(icallid, *icall) { |
68 | * if (want_refuse) { |
||
69 | * ipc_answer_fast(icallid, ELIMIT, 0, 0); |
||
70 | * return; |
||
71 | * } |
||
72 | * ipc_answer_fast(icallid, 0, 0, 0); |
||
1392 | palkovsky | 73 | * |
1407 | palkovsky | 74 | * callid = async_get_call(&call); |
75 | * handle(callid, call); |
||
76 | * ipc_answer_fast(callid, 1,2,3); |
||
77 | * |
||
78 | * callid = async_get_call(&call); |
||
1392 | palkovsky | 79 | * .... |
80 | * } |
||
1404 | palkovsky | 81 | * |
1405 | decky | 82 | * TODO: Detaching/joining dead psthreads? |
1392 | palkovsky | 83 | */ |
84 | #include <futex.h> |
||
85 | #include <async.h> |
||
86 | #include <psthread.h> |
||
87 | #include <stdio.h> |
||
88 | #include <libadt/hash_table.h> |
||
89 | #include <libadt/list.h> |
||
90 | #include <ipc/ipc.h> |
||
91 | #include <assert.h> |
||
92 | #include <errno.h> |
||
1441 | palkovsky | 93 | #include <time.h> |
94 | #include <arch/barrier.h> |
||
1392 | palkovsky | 95 | |
1427 | palkovsky | 96 | static atomic_t async_futex = FUTEX_INITIALIZER; |
1392 | palkovsky | 97 | static hash_table_t conn_hash_table; |
1441 | palkovsky | 98 | static LIST_INITIALIZE(timeout_list); |
1392 | palkovsky | 99 | |
100 | typedef struct { |
||
1427 | palkovsky | 101 | pstid_t ptid; /**< Thread waiting for this message */ |
102 | int active; /**< If this thread is currently active */ |
||
103 | int done; /**< If reply was received */ |
||
104 | ipc_call_t *dataptr; /**< Pointer where the answer data |
||
105 | * should be stored */ |
||
1441 | palkovsky | 106 | struct timeval expires; /**< Expiration time for waiting thread */ |
107 | int has_timeout; /**< If true, this struct is in timeout list */ |
||
108 | link_t link; |
||
109 | |||
1427 | palkovsky | 110 | ipcarg_t retval; |
111 | } amsg_t; |
||
112 | |||
113 | typedef struct { |
||
1392 | palkovsky | 114 | link_t link; |
115 | ipc_callid_t callid; |
||
116 | ipc_call_t call; |
||
117 | } msg_t; |
||
118 | |||
119 | typedef struct { |
||
120 | link_t link; |
||
121 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
||
122 | link_t msg_queue; /**< Messages that should be delivered to this thread */ |
||
123 | pstid_t ptid; /**< Thread associated with this connection */ |
||
124 | int active; /**< If this thread is currently active */ |
||
125 | /* Structures for connection opening packet */ |
||
126 | ipc_callid_t callid; |
||
127 | ipc_call_t call; |
||
1407 | palkovsky | 128 | void (*cthread)(ipc_callid_t,ipc_call_t *); |
1392 | palkovsky | 129 | } connection_t; |
130 | |||
131 | __thread connection_t *PS_connection; |
||
132 | |||
1441 | palkovsky | 133 | /** Add microseconds to give timeval */ |
134 | static void tv_add(struct timeval *tv, suseconds_t usecs) |
||
135 | { |
||
136 | tv->tv_sec += usecs / 1000000; |
||
137 | tv->tv_usec += usecs % 1000000; |
||
138 | if (tv->tv_usec > 1000000) { |
||
139 | tv->tv_sec++; |
||
140 | tv->tv_usec -= 1000000; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** Subtract 2 timevals, return microseconds difference */ |
||
145 | static suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2) |
||
146 | { |
||
147 | suseconds_t result; |
||
148 | |||
149 | result = tv1->tv_usec - tv2->tv_usec; |
||
150 | result += (tv1->tv_sec - tv2->tv_sec) * 1000000; |
||
151 | |||
152 | return result; |
||
153 | } |
||
154 | |||
155 | /** Compare timeval |
||
156 | * |
||
157 | * @return 1 if tv1 > tv2, otherwise 0 |
||
158 | */ |
||
159 | static int tv_gt(struct timeval *tv1, struct timeval *tv2) |
||
160 | { |
||
161 | if (tv1->tv_sec > tv2->tv_sec) |
||
162 | return 1; |
||
163 | if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec) |
||
164 | return 1; |
||
165 | return 0; |
||
166 | } |
||
167 | |||
1392 | palkovsky | 168 | /* Hash table functions */ |
1404 | palkovsky | 169 | #define CONN_HASH_TABLE_CHAINS 32 |
1392 | palkovsky | 170 | |
171 | static hash_index_t conn_hash(unsigned long *key) |
||
1351 | palkovsky | 172 | { |
1392 | palkovsky | 173 | assert(key); |
1404 | palkovsky | 174 | return ((*key) >> 4) % CONN_HASH_TABLE_CHAINS; |
1351 | palkovsky | 175 | } |
176 | |||
1392 | palkovsky | 177 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item) |
1351 | palkovsky | 178 | { |
1392 | palkovsky | 179 | connection_t *hs; |
180 | |||
181 | hs = hash_table_get_instance(item, connection_t, link); |
||
182 | |||
183 | return key[0] == hs->in_phone_hash; |
||
1351 | palkovsky | 184 | } |
185 | |||
1392 | palkovsky | 186 | static void conn_remove(link_t *item) |
1351 | palkovsky | 187 | { |
1392 | palkovsky | 188 | free(hash_table_get_instance(item, connection_t, link)); |
1351 | palkovsky | 189 | } |
190 | |||
1392 | palkovsky | 191 | |
192 | /** Operations for NS hash table. */ |
||
193 | static hash_table_operations_t conn_hash_table_ops = { |
||
194 | .hash = conn_hash, |
||
195 | .compare = conn_compare, |
||
196 | .remove_callback = conn_remove |
||
197 | }; |
||
198 | |||
1427 | palkovsky | 199 | /*************************************************/ |
200 | |||
1392 | palkovsky | 201 | /** Try to route a call to an appropriate connection thread |
202 | * |
||
203 | */ |
||
204 | static int route_call(ipc_callid_t callid, ipc_call_t *call) |
||
1351 | palkovsky | 205 | { |
1392 | palkovsky | 206 | connection_t *conn; |
207 | msg_t *msg; |
||
208 | link_t *hlp; |
||
209 | unsigned long key; |
||
210 | |||
1427 | palkovsky | 211 | futex_down(&async_futex); |
1392 | palkovsky | 212 | |
213 | key = call->in_phone_hash; |
||
214 | hlp = hash_table_find(&conn_hash_table, &key); |
||
215 | if (!hlp) { |
||
1427 | palkovsky | 216 | futex_up(&async_futex); |
1392 | palkovsky | 217 | return 0; |
1351 | palkovsky | 218 | } |
1392 | palkovsky | 219 | conn = hash_table_get_instance(hlp, connection_t, link); |
1351 | palkovsky | 220 | |
1392 | palkovsky | 221 | msg = malloc(sizeof(*msg)); |
222 | msg->callid = callid; |
||
223 | msg->call = *call; |
||
224 | list_append(&msg->link, &conn->msg_queue); |
||
225 | |||
226 | if (!conn->active) { |
||
227 | conn->active = 1; |
||
228 | psthread_add_ready(conn->ptid); |
||
229 | } |
||
1351 | palkovsky | 230 | |
1427 | palkovsky | 231 | futex_up(&async_futex); |
1392 | palkovsky | 232 | |
233 | return 1; |
||
234 | } |
||
235 | |||
1404 | palkovsky | 236 | /** Return new incoming message for current(thread-local) connection */ |
1392 | palkovsky | 237 | ipc_callid_t async_get_call(ipc_call_t *call) |
238 | { |
||
239 | msg_t *msg; |
||
240 | ipc_callid_t callid; |
||
241 | connection_t *conn; |
||
242 | |||
1427 | palkovsky | 243 | futex_down(&async_futex); |
1392 | palkovsky | 244 | |
245 | conn = PS_connection; |
||
246 | /* If nothing in queue, wait until something appears */ |
||
247 | if (list_empty(&conn->msg_queue)) { |
||
248 | conn->active = 0; |
||
249 | psthread_schedule_next_adv(PS_TO_MANAGER); |
||
1351 | palkovsky | 250 | } |
251 | |||
1392 | palkovsky | 252 | msg = list_get_instance(conn->msg_queue.next, msg_t, link); |
253 | list_remove(&msg->link); |
||
254 | callid = msg->callid; |
||
255 | *call = msg->call; |
||
256 | free(msg); |
||
257 | |||
1427 | palkovsky | 258 | futex_up(&async_futex); |
1392 | palkovsky | 259 | return callid; |
1351 | palkovsky | 260 | } |
261 | |||
1404 | palkovsky | 262 | /** Thread function that gets created on new connection |
263 | * |
||
264 | * This function is defined as a weak symbol - to be redefined in |
||
265 | * user code. |
||
266 | */ |
||
1392 | palkovsky | 267 | void client_connection(ipc_callid_t callid, ipc_call_t *call) |
268 | { |
||
1404 | palkovsky | 269 | ipc_answer_fast(callid, ENOENT, 0, 0); |
1392 | palkovsky | 270 | } |
1351 | palkovsky | 271 | |
1452 | palkovsky | 272 | /** Function that gets created on interrupt receival |
273 | * |
||
274 | * This function is defined as a weak symbol - to be redefined in |
||
275 | * user code. |
||
276 | */ |
||
277 | void interrupt_received(ipc_call_t *call) |
||
278 | { |
||
279 | } |
||
280 | |||
281 | |||
1404 | palkovsky | 282 | /** Wrapper for client connection thread |
283 | * |
||
284 | * When new connection arrives, thread with this function is created. |
||
285 | * It calls client_connection and does final cleanup. |
||
286 | * |
||
287 | * @parameter arg Connection structure pointer |
||
288 | */ |
||
1392 | palkovsky | 289 | static int connection_thread(void *arg) |
1351 | palkovsky | 290 | { |
1404 | palkovsky | 291 | unsigned long key; |
292 | msg_t *msg; |
||
1408 | palkovsky | 293 | connection_t *conn; |
1404 | palkovsky | 294 | |
1392 | palkovsky | 295 | /* Setup thread local connection pointer */ |
296 | PS_connection = (connection_t *)arg; |
||
1408 | palkovsky | 297 | conn = PS_connection; |
298 | conn->cthread(conn->callid, &conn->call); |
||
1392 | palkovsky | 299 | |
1404 | palkovsky | 300 | /* Remove myself from connection hash table */ |
1427 | palkovsky | 301 | futex_down(&async_futex); |
1408 | palkovsky | 302 | key = conn->in_phone_hash; |
1404 | palkovsky | 303 | hash_table_remove(&conn_hash_table, &key, 1); |
1427 | palkovsky | 304 | futex_up(&async_futex); |
1404 | palkovsky | 305 | /* Answer all remaining messages with ehangup */ |
1408 | palkovsky | 306 | while (!list_empty(&conn->msg_queue)) { |
307 | msg = list_get_instance(conn->msg_queue.next, msg_t, link); |
||
1404 | palkovsky | 308 | list_remove(&msg->link); |
309 | ipc_answer_fast(msg->callid, EHANGUP, 0, 0); |
||
310 | free(msg); |
||
311 | } |
||
1351 | palkovsky | 312 | } |
1392 | palkovsky | 313 | |
314 | /** Create new thread for a new connection |
||
315 | * |
||
316 | * Creates new thread for connection, fills in connection |
||
317 | * structures and inserts it into the hash table, so that |
||
318 | * later we can easily do routing of messages to particular |
||
319 | * threads. |
||
1407 | palkovsky | 320 | * |
1452 | palkovsky | 321 | * @param in_phone_hash Identification of the incoming connection |
1407 | palkovsky | 322 | * @param callid Callid of the IPC_M_CONNECT_ME_TO packet |
323 | * @param call Call data of the opening packet |
||
324 | * @param cthread Thread function that should be called upon |
||
325 | * opening the connection |
||
326 | * @return New thread id |
||
1392 | palkovsky | 327 | */ |
1452 | palkovsky | 328 | pstid_t async_new_connection(ipcarg_t in_phone_hash,ipc_callid_t callid, |
329 | ipc_call_t *call, |
||
1407 | palkovsky | 330 | void (*cthread)(ipc_callid_t,ipc_call_t *)) |
1392 | palkovsky | 331 | { |
332 | pstid_t ptid; |
||
333 | connection_t *conn; |
||
334 | unsigned long key; |
||
335 | |||
336 | conn = malloc(sizeof(*conn)); |
||
337 | if (!conn) { |
||
338 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
||
1407 | palkovsky | 339 | return NULL; |
1392 | palkovsky | 340 | } |
1452 | palkovsky | 341 | conn->in_phone_hash = in_phone_hash; |
1392 | palkovsky | 342 | list_initialize(&conn->msg_queue); |
343 | conn->ptid = psthread_create(connection_thread, conn); |
||
344 | conn->callid = callid; |
||
345 | conn->call = *call; |
||
346 | conn->active = 1; /* We will activate it asap */ |
||
1407 | palkovsky | 347 | conn->cthread = cthread; |
1392 | palkovsky | 348 | list_initialize(&conn->link); |
349 | if (!conn->ptid) { |
||
350 | free(conn); |
||
351 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
||
1407 | palkovsky | 352 | return NULL; |
1392 | palkovsky | 353 | } |
354 | key = conn->in_phone_hash; |
||
1427 | palkovsky | 355 | futex_down(&async_futex); |
1392 | palkovsky | 356 | /* Add connection to hash table */ |
357 | hash_table_insert(&conn_hash_table, &key, &conn->link); |
||
1427 | palkovsky | 358 | futex_up(&async_futex); |
1392 | palkovsky | 359 | |
360 | psthread_add_ready(conn->ptid); |
||
1407 | palkovsky | 361 | |
362 | return conn->ptid; |
||
1392 | palkovsky | 363 | } |
364 | |||
1427 | palkovsky | 365 | /** Handle call that was received */ |
1392 | palkovsky | 366 | static void handle_call(ipc_callid_t callid, ipc_call_t *call) |
367 | { |
||
1452 | palkovsky | 368 | /* Unrouted call - do some default behaviour */ |
1392 | palkovsky | 369 | switch (IPC_GET_METHOD(*call)) { |
370 | case IPC_M_INTERRUPT: |
||
1452 | palkovsky | 371 | interrupt_received(call); |
372 | return; |
||
1392 | palkovsky | 373 | case IPC_M_CONNECT_ME_TO: |
374 | /* Open new connection with thread etc. */ |
||
1452 | palkovsky | 375 | async_new_connection(IPC_GET_ARG3(*call), callid, call, client_connection); |
376 | return; |
||
1392 | palkovsky | 377 | } |
1452 | palkovsky | 378 | |
379 | /* Try to route call through connection tables */ |
||
380 | if (route_call(callid, call)) |
||
381 | return; |
||
382 | |||
383 | /* Unknown call from unknown phone - hang it up */ |
||
384 | ipc_answer_fast(callid, EHANGUP, 0, 0); |
||
1392 | palkovsky | 385 | } |
386 | |||
1441 | palkovsky | 387 | /** Fire all timeouts that expired */ |
388 | static void handle_expired_timeouts(void) |
||
389 | { |
||
390 | struct timeval tv; |
||
391 | amsg_t *amsg; |
||
392 | link_t *cur; |
||
393 | |||
394 | gettimeofday(&tv,NULL); |
||
395 | futex_down(&async_futex); |
||
396 | |||
397 | cur = timeout_list.next; |
||
398 | while (cur != &timeout_list) { |
||
399 | amsg = list_get_instance(cur,amsg_t,link); |
||
400 | if (tv_gt(&amsg->expires, &tv)) |
||
401 | break; |
||
402 | cur = cur->next; |
||
403 | list_remove(&amsg->link); |
||
404 | amsg->has_timeout = 0; |
||
405 | /* Redundant condition? The thread should not |
||
406 | * be active when it gets here. |
||
407 | */ |
||
408 | if (!amsg->active) { |
||
409 | amsg->active = 1; |
||
410 | psthread_add_ready(amsg->ptid); |
||
411 | } |
||
412 | } |
||
413 | |||
414 | futex_up(&async_futex); |
||
415 | } |
||
416 | |||
1392 | palkovsky | 417 | /** Endless loop dispatching incoming calls and answers */ |
1441 | palkovsky | 418 | int async_manager(void) |
1392 | palkovsky | 419 | { |
420 | ipc_call_t call; |
||
421 | ipc_callid_t callid; |
||
1435 | palkovsky | 422 | int timeout; |
1441 | palkovsky | 423 | amsg_t *amsg; |
424 | struct timeval tv; |
||
1392 | palkovsky | 425 | |
426 | while (1) { |
||
427 | if (psthread_schedule_next_adv(PS_FROM_MANAGER)) { |
||
1427 | palkovsky | 428 | futex_up(&async_futex); /* async_futex is always held |
1392 | palkovsky | 429 | * when entering manager thread |
430 | */ |
||
431 | continue; |
||
432 | } |
||
1441 | palkovsky | 433 | futex_down(&async_futex); |
434 | if (!list_empty(&timeout_list)) { |
||
435 | amsg = list_get_instance(timeout_list.next,amsg_t,link); |
||
436 | gettimeofday(&tv,NULL); |
||
437 | if (tv_gt(&tv, &amsg->expires)) { |
||
438 | handle_expired_timeouts(); |
||
439 | continue; |
||
440 | } else |
||
441 | timeout = tv_sub(&amsg->expires, &tv); |
||
442 | } else |
||
1435 | palkovsky | 443 | timeout = SYNCH_NO_TIMEOUT; |
1441 | palkovsky | 444 | futex_up(&async_futex); |
445 | |||
1435 | palkovsky | 446 | callid = ipc_wait_cycle(&call, timeout, SYNCH_BLOCKING); |
1392 | palkovsky | 447 | |
1435 | palkovsky | 448 | if (!callid) { |
1441 | palkovsky | 449 | handle_expired_timeouts(); |
1435 | palkovsky | 450 | continue; |
451 | } |
||
452 | |||
1392 | palkovsky | 453 | if (callid & IPC_CALLID_ANSWERED) |
454 | continue; |
||
1427 | palkovsky | 455 | |
1392 | palkovsky | 456 | handle_call(callid, &call); |
457 | } |
||
458 | } |
||
459 | |||
1404 | palkovsky | 460 | /** Function to start async_manager as a standalone thread |
461 | * |
||
462 | * When more kernel threads are used, one async manager should |
||
463 | * exist per thread. The particular implementation may change, |
||
464 | * currently one async_manager is started automatically per kernel |
||
465 | * thread except main thread. |
||
466 | */ |
||
1392 | palkovsky | 467 | static int async_manager_thread(void *arg) |
468 | { |
||
1427 | palkovsky | 469 | futex_up(&async_futex); /* async_futex is always locked when entering |
1392 | palkovsky | 470 | * manager */ |
471 | async_manager(); |
||
472 | } |
||
473 | |||
474 | /** Add one manager to manager list */ |
||
475 | void async_create_manager(void) |
||
476 | { |
||
477 | pstid_t ptid; |
||
478 | |||
479 | ptid = psthread_create(async_manager_thread, NULL); |
||
480 | psthread_add_manager(ptid); |
||
481 | } |
||
482 | |||
483 | /** Remove one manager from manager list */ |
||
484 | void async_destroy_manager(void) |
||
485 | { |
||
486 | psthread_remove_manager(); |
||
487 | } |
||
488 | |||
489 | /** Initialize internal structures needed for async manager */ |
||
490 | int _async_init(void) |
||
491 | { |
||
1404 | palkovsky | 492 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1, &conn_hash_table_ops)) { |
1392 | palkovsky | 493 | printf("%s: cannot create hash table\n", "async"); |
494 | return ENOMEM; |
||
495 | } |
||
496 | |||
497 | } |
||
1427 | palkovsky | 498 | |
499 | /** IPC handler for messages in async framework |
||
500 | * |
||
501 | * Notify thread that is waiting for this message, that it arrived |
||
502 | */ |
||
503 | static void reply_received(void *private, int retval, |
||
504 | ipc_call_t *data) |
||
505 | { |
||
506 | amsg_t *msg = (amsg_t *) private; |
||
507 | |||
508 | msg->retval = retval; |
||
509 | |||
510 | futex_down(&async_futex); |
||
511 | /* Copy data after futex_down, just in case the |
||
512 | * call was detached |
||
513 | */ |
||
514 | if (msg->dataptr) |
||
515 | *msg->dataptr = *data; |
||
1435 | palkovsky | 516 | |
1441 | palkovsky | 517 | write_barrier(); |
518 | /* Remove message from timeout list */ |
||
519 | if (msg->has_timeout) |
||
520 | list_remove(&msg->link); |
||
1427 | palkovsky | 521 | msg->done = 1; |
522 | if (! msg->active) { |
||
523 | msg->active = 1; |
||
524 | psthread_add_ready(msg->ptid); |
||
525 | } |
||
526 | futex_up(&async_futex); |
||
527 | } |
||
528 | |||
529 | /** Send message and return id of the sent message |
||
530 | * |
||
531 | * The return value can be used as input for async_wait() to wait |
||
532 | * for completion. |
||
533 | */ |
||
534 | aid_t async_send_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, |
||
535 | ipc_call_t *dataptr) |
||
536 | { |
||
537 | amsg_t *msg; |
||
538 | |||
539 | msg = malloc(sizeof(*msg)); |
||
540 | msg->active = 1; |
||
541 | msg->done = 0; |
||
542 | msg->dataptr = dataptr; |
||
543 | ipc_call_async_2(phoneid,method,arg1,arg2,msg,reply_received); |
||
544 | |||
545 | return (aid_t) msg; |
||
546 | } |
||
547 | |||
548 | /** Wait for a message sent by async framework |
||
549 | * |
||
550 | * @param amsgid Message ID to wait for |
||
551 | * @param retval Pointer to variable where will be stored retval |
||
552 | * of the answered message. If NULL, it is ignored. |
||
553 | * |
||
554 | */ |
||
555 | void async_wait_for(aid_t amsgid, ipcarg_t *retval) |
||
556 | { |
||
557 | amsg_t *msg = (amsg_t *) amsgid; |
||
558 | connection_t *conn; |
||
559 | |||
560 | futex_down(&async_futex); |
||
561 | if (msg->done) { |
||
562 | futex_up(&async_futex); |
||
563 | goto done; |
||
564 | } |
||
565 | |||
566 | msg->ptid = psthread_get_id(); |
||
567 | msg->active = 0; |
||
1441 | palkovsky | 568 | msg->has_timeout = 0; |
1427 | palkovsky | 569 | /* Leave locked async_futex when entering this function */ |
570 | psthread_schedule_next_adv(PS_TO_MANAGER); |
||
571 | /* futex is up automatically after psthread_schedule_next...*/ |
||
572 | done: |
||
573 | if (retval) |
||
574 | *retval = msg->retval; |
||
575 | free(msg); |
||
576 | } |
||
1435 | palkovsky | 577 | |
1441 | palkovsky | 578 | /** Insert sort timeout msg into timeouts list |
579 | * |
||
580 | * Assume async_futex is held |
||
581 | */ |
||
582 | static void insert_timeout(amsg_t *msg) |
||
583 | { |
||
584 | link_t *tmp; |
||
585 | amsg_t *cur; |
||
1435 | palkovsky | 586 | |
1441 | palkovsky | 587 | tmp = timeout_list.next; |
588 | while (tmp != &timeout_list) { |
||
589 | cur = list_get_instance(tmp, amsg_t, link); |
||
590 | if (tv_gt(&cur->expires, &msg->expires)) |
||
591 | break; |
||
592 | tmp = tmp->next; |
||
593 | } |
||
594 | list_append(&msg->link, tmp); |
||
595 | } |
||
1435 | palkovsky | 596 | |
1441 | palkovsky | 597 | /** Wait for a message sent by async framework with timeout |
598 | * |
||
599 | * @param amsgid Message ID to wait for |
||
600 | * @param retval Pointer to variable where will be stored retval |
||
601 | * of the answered message. If NULL, it is ignored. |
||
602 | * @param timeout Timeout in usecs |
||
603 | * @return 0 on success, ETIMEOUT if timeout expired |
||
604 | * |
||
605 | */ |
||
606 | int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout) |
||
607 | { |
||
608 | amsg_t *msg = (amsg_t *) amsgid; |
||
609 | connection_t *conn; |
||
1435 | palkovsky | 610 | |
1441 | palkovsky | 611 | futex_down(&async_futex); |
612 | if (msg->done) { |
||
613 | futex_up(&async_futex); |
||
614 | goto done; |
||
615 | } |
||
1435 | palkovsky | 616 | |
1441 | palkovsky | 617 | msg->ptid = psthread_get_id(); |
618 | msg->active = 0; |
||
619 | msg->has_timeout = 1; |
||
1435 | palkovsky | 620 | |
1441 | palkovsky | 621 | gettimeofday(&msg->expires, NULL); |
622 | tv_add(&msg->expires, timeout); |
||
623 | insert_timeout(msg); |
||
1435 | palkovsky | 624 | |
1441 | palkovsky | 625 | /* Leave locked async_futex when entering this function */ |
626 | psthread_schedule_next_adv(PS_TO_MANAGER); |
||
627 | /* futex is up automatically after psthread_schedule_next...*/ |
||
1435 | palkovsky | 628 | |
1441 | palkovsky | 629 | if (!msg->done) |
630 | return ETIMEOUT; |
||
631 | |||
632 | done: |
||
633 | if (retval) |
||
634 | *retval = msg->retval; |
||
635 | free(msg); |
||
636 | |||
637 | return 0; |
||
638 | } |
||
639 | |||
1452 | palkovsky | 640 | /** Wait specified time, but in the meantime handle incoming events |
641 | * |
||
642 | * @param timeout Time in microseconds to wait |
||
643 | */ |
||
644 | void async_usleep(suseconds_t timeout) |
||
645 | { |
||
646 | amsg_t *msg; |
||
647 | |||
648 | msg = malloc(sizeof(*msg)); |
||
649 | if (!msg) |
||
650 | return; |
||
651 | |||
652 | msg->ptid = psthread_get_id(); |
||
653 | msg->active = 0; |
||
654 | msg->has_timeout = 1; |
||
655 | |||
656 | gettimeofday(&msg->expires, NULL); |
||
657 | tv_add(&msg->expires, timeout); |
||
658 | |||
659 | futex_down(&async_futex); |
||
660 | insert_timeout(msg); |
||
661 | /* Leave locked async_futex when entering this function */ |
||
662 | psthread_schedule_next_adv(PS_TO_MANAGER); |
||
663 | /* futex is up automatically after psthread_schedule_next...*/ |
||
664 | free(msg); |
||
665 | } |