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