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