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