Rev 1405 | Rev 1408 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1405 | Rev 1407 | ||
---|---|---|---|
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 | * wait_for_connection(new_connection); |
63 | * async_manager(); |
64 | * } |
64 | * } |
65 | * |
65 | * |
66 | * |
66 | * |
67 | * new_connection(int connection) { |
67 | * client_connection(icallid, *icall) { |
68 | * accept(connection); |
68 | * if (want_refuse) { |
- | 69 | * ipc_answer_fast(icallid, ELIMIT, 0, 0); |
|
69 | * msg = get_msg(); |
70 | * return; |
- | 71 | * } |
|
- | 72 | * ipc_answer_fast(icallid, 0, 0, 0); |
|
- | 73 | * |
|
- | 74 | * callid = async_get_call(&call); |
|
70 | * handle(msg); |
75 | * handle(callid, call); |
71 | * answer(msg); |
76 | * ipc_answer_fast(callid, 1,2,3); |
72 | * |
77 | * |
73 | * msg = get_msg(); |
78 | * callid = async_get_call(&call); |
74 | * .... |
79 | * .... |
75 | * } |
80 | * } |
76 | * |
81 | * |
77 | * TODO: Detaching/joining dead psthreads? |
82 | * TODO: Detaching/joining dead psthreads? |
78 | */ |
83 | */ |
79 | #include <futex.h> |
84 | #include <futex.h> |
80 | #include <async.h> |
85 | #include <async.h> |
81 | #include <psthread.h> |
86 | #include <psthread.h> |
82 | #include <stdio.h> |
87 | #include <stdio.h> |
83 | #include <libadt/hash_table.h> |
88 | #include <libadt/hash_table.h> |
84 | #include <libadt/list.h> |
89 | #include <libadt/list.h> |
85 | #include <ipc/ipc.h> |
90 | #include <ipc/ipc.h> |
86 | #include <assert.h> |
91 | #include <assert.h> |
87 | #include <errno.h> |
92 | #include <errno.h> |
88 | 93 | ||
89 | static atomic_t conn_futex = FUTEX_INITIALIZER; |
94 | static atomic_t conn_futex = FUTEX_INITIALIZER; |
90 | static hash_table_t conn_hash_table; |
95 | static hash_table_t conn_hash_table; |
91 | 96 | ||
92 | typedef struct { |
97 | typedef struct { |
93 | link_t link; |
98 | link_t link; |
94 | ipc_callid_t callid; |
99 | ipc_callid_t callid; |
95 | ipc_call_t call; |
100 | ipc_call_t call; |
96 | } msg_t; |
101 | } msg_t; |
97 | 102 | ||
98 | typedef struct { |
103 | typedef struct { |
99 | link_t link; |
104 | link_t link; |
100 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
105 | ipcarg_t in_phone_hash; /**< Incoming phone hash. */ |
101 | link_t msg_queue; /**< Messages that should be delivered to this thread */ |
106 | link_t msg_queue; /**< Messages that should be delivered to this thread */ |
102 | pstid_t ptid; /**< Thread associated with this connection */ |
107 | pstid_t ptid; /**< Thread associated with this connection */ |
103 | int active; /**< If this thread is currently active */ |
108 | int active; /**< If this thread is currently active */ |
104 | /* Structures for connection opening packet */ |
109 | /* Structures for connection opening packet */ |
105 | ipc_callid_t callid; |
110 | ipc_callid_t callid; |
106 | ipc_call_t call; |
111 | ipc_call_t call; |
- | 112 | void (*cthread)(ipc_callid_t,ipc_call_t *); |
|
107 | } connection_t; |
113 | } connection_t; |
108 | 114 | ||
109 | __thread connection_t *PS_connection; |
115 | __thread connection_t *PS_connection; |
110 | 116 | ||
111 | /* Hash table functions */ |
117 | /* Hash table functions */ |
112 | 118 | ||
113 | #define CONN_HASH_TABLE_CHAINS 32 |
119 | #define CONN_HASH_TABLE_CHAINS 32 |
114 | 120 | ||
115 | static hash_index_t conn_hash(unsigned long *key) |
121 | static hash_index_t conn_hash(unsigned long *key) |
116 | { |
122 | { |
117 | assert(key); |
123 | assert(key); |
118 | return ((*key) >> 4) % CONN_HASH_TABLE_CHAINS; |
124 | return ((*key) >> 4) % CONN_HASH_TABLE_CHAINS; |
119 | } |
125 | } |
120 | 126 | ||
121 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item) |
127 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item) |
122 | { |
128 | { |
123 | connection_t *hs; |
129 | connection_t *hs; |
124 | 130 | ||
125 | hs = hash_table_get_instance(item, connection_t, link); |
131 | hs = hash_table_get_instance(item, connection_t, link); |
126 | 132 | ||
127 | return key[0] == hs->in_phone_hash; |
133 | return key[0] == hs->in_phone_hash; |
128 | } |
134 | } |
129 | 135 | ||
130 | static void conn_remove(link_t *item) |
136 | static void conn_remove(link_t *item) |
131 | { |
137 | { |
132 | free(hash_table_get_instance(item, connection_t, link)); |
138 | free(hash_table_get_instance(item, connection_t, link)); |
133 | } |
139 | } |
134 | 140 | ||
135 | 141 | ||
136 | /** Operations for NS hash table. */ |
142 | /** Operations for NS hash table. */ |
137 | static hash_table_operations_t conn_hash_table_ops = { |
143 | static hash_table_operations_t conn_hash_table_ops = { |
138 | .hash = conn_hash, |
144 | .hash = conn_hash, |
139 | .compare = conn_compare, |
145 | .compare = conn_compare, |
140 | .remove_callback = conn_remove |
146 | .remove_callback = conn_remove |
141 | }; |
147 | }; |
142 | 148 | ||
143 | /** Try to route a call to an appropriate connection thread |
149 | /** Try to route a call to an appropriate connection thread |
144 | * |
150 | * |
145 | */ |
151 | */ |
146 | static int route_call(ipc_callid_t callid, ipc_call_t *call) |
152 | static int route_call(ipc_callid_t callid, ipc_call_t *call) |
147 | { |
153 | { |
148 | connection_t *conn; |
154 | connection_t *conn; |
149 | msg_t *msg; |
155 | msg_t *msg; |
150 | link_t *hlp; |
156 | link_t *hlp; |
151 | unsigned long key; |
157 | unsigned long key; |
152 | 158 | ||
153 | futex_down(&conn_futex); |
159 | futex_down(&conn_futex); |
154 | 160 | ||
155 | key = call->in_phone_hash; |
161 | key = call->in_phone_hash; |
156 | hlp = hash_table_find(&conn_hash_table, &key); |
162 | hlp = hash_table_find(&conn_hash_table, &key); |
157 | if (!hlp) { |
163 | if (!hlp) { |
158 | futex_up(&conn_futex); |
164 | futex_up(&conn_futex); |
159 | return 0; |
165 | return 0; |
160 | } |
166 | } |
161 | conn = hash_table_get_instance(hlp, connection_t, link); |
167 | conn = hash_table_get_instance(hlp, connection_t, link); |
162 | 168 | ||
163 | msg = malloc(sizeof(*msg)); |
169 | msg = malloc(sizeof(*msg)); |
164 | msg->callid = callid; |
170 | msg->callid = callid; |
165 | msg->call = *call; |
171 | msg->call = *call; |
166 | list_append(&msg->link, &conn->msg_queue); |
172 | list_append(&msg->link, &conn->msg_queue); |
167 | 173 | ||
168 | if (!conn->active) { |
174 | if (!conn->active) { |
169 | conn->active = 1; |
175 | conn->active = 1; |
170 | psthread_add_ready(conn->ptid); |
176 | psthread_add_ready(conn->ptid); |
171 | } |
177 | } |
172 | 178 | ||
173 | futex_up(&conn_futex); |
179 | futex_up(&conn_futex); |
174 | 180 | ||
175 | return 1; |
181 | return 1; |
176 | } |
182 | } |
177 | 183 | ||
178 | /** Return new incoming message for current(thread-local) connection */ |
184 | /** Return new incoming message for current(thread-local) connection */ |
179 | ipc_callid_t async_get_call(ipc_call_t *call) |
185 | ipc_callid_t async_get_call(ipc_call_t *call) |
180 | { |
186 | { |
181 | msg_t *msg; |
187 | msg_t *msg; |
182 | ipc_callid_t callid; |
188 | ipc_callid_t callid; |
183 | connection_t *conn; |
189 | connection_t *conn; |
184 | 190 | ||
185 | futex_down(&conn_futex); |
191 | futex_down(&conn_futex); |
186 | 192 | ||
187 | conn = PS_connection; |
193 | conn = PS_connection; |
188 | /* If nothing in queue, wait until something appears */ |
194 | /* If nothing in queue, wait until something appears */ |
189 | if (list_empty(&conn->msg_queue)) { |
195 | if (list_empty(&conn->msg_queue)) { |
190 | conn->active = 0; |
196 | conn->active = 0; |
191 | psthread_schedule_next_adv(PS_TO_MANAGER); |
197 | psthread_schedule_next_adv(PS_TO_MANAGER); |
192 | } |
198 | } |
193 | 199 | ||
194 | msg = list_get_instance(conn->msg_queue.next, msg_t, link); |
200 | msg = list_get_instance(conn->msg_queue.next, msg_t, link); |
195 | list_remove(&msg->link); |
201 | list_remove(&msg->link); |
196 | callid = msg->callid; |
202 | callid = msg->callid; |
197 | *call = msg->call; |
203 | *call = msg->call; |
198 | free(msg); |
204 | free(msg); |
199 | 205 | ||
200 | futex_up(&conn_futex); |
206 | futex_up(&conn_futex); |
201 | return callid; |
207 | return callid; |
202 | } |
208 | } |
203 | 209 | ||
204 | /** Thread function that gets created on new connection |
210 | /** Thread function that gets created on new connection |
205 | * |
211 | * |
206 | * This function is defined as a weak symbol - to be redefined in |
212 | * This function is defined as a weak symbol - to be redefined in |
207 | * user code. |
213 | * user code. |
208 | */ |
214 | */ |
209 | void client_connection(ipc_callid_t callid, ipc_call_t *call) |
215 | void client_connection(ipc_callid_t callid, ipc_call_t *call) |
210 | { |
216 | { |
211 | ipc_answer_fast(callid, ENOENT, 0, 0); |
217 | ipc_answer_fast(callid, ENOENT, 0, 0); |
212 | } |
218 | } |
213 | 219 | ||
214 | /** Wrapper for client connection thread |
220 | /** Wrapper for client connection thread |
215 | * |
221 | * |
216 | * When new connection arrives, thread with this function is created. |
222 | * When new connection arrives, thread with this function is created. |
217 | * It calls client_connection and does final cleanup. |
223 | * It calls client_connection and does final cleanup. |
218 | * |
224 | * |
219 | * @parameter arg Connection structure pointer |
225 | * @parameter arg Connection structure pointer |
220 | */ |
226 | */ |
221 | static int connection_thread(void *arg) |
227 | static int connection_thread(void *arg) |
222 | { |
228 | { |
223 | unsigned long key; |
229 | unsigned long key; |
224 | msg_t *msg; |
230 | msg_t *msg; |
225 | 231 | ||
226 | /* Setup thread local connection pointer */ |
232 | /* Setup thread local connection pointer */ |
227 | PS_connection = (connection_t *)arg; |
233 | PS_connection = (connection_t *)arg; |
228 | client_connection(PS_connection->callid, &PS_connection->call); |
234 | PS_connection->cthread(PS_connection->callid, &PS_connection->call); |
229 | 235 | ||
230 | /* Remove myself from connection hash table */ |
236 | /* Remove myself from connection hash table */ |
231 | futex_down(&conn_futex); |
237 | futex_down(&conn_futex); |
232 | key = PS_connection->in_phone_hash; |
238 | key = PS_connection->in_phone_hash; |
233 | hash_table_remove(&conn_hash_table, &key, 1); |
239 | hash_table_remove(&conn_hash_table, &key, 1); |
234 | futex_up(&conn_futex); |
240 | futex_up(&conn_futex); |
235 | /* Answer all remaining messages with ehangup */ |
241 | /* Answer all remaining messages with ehangup */ |
236 | while (!list_empty(&PS_connection->msg_queue)) { |
242 | while (!list_empty(&PS_connection->msg_queue)) { |
237 | msg = list_get_instance(PS_connection->msg_queue.next, msg_t, link); |
243 | msg = list_get_instance(PS_connection->msg_queue.next, msg_t, link); |
238 | list_remove(&msg->link); |
244 | list_remove(&msg->link); |
239 | ipc_answer_fast(msg->callid, EHANGUP, 0, 0); |
245 | ipc_answer_fast(msg->callid, EHANGUP, 0, 0); |
240 | free(msg); |
246 | free(msg); |
241 | } |
247 | } |
242 | } |
248 | } |
243 | 249 | ||
244 | /** Create new thread for a new connection |
250 | /** Create new thread for a new connection |
245 | * |
251 | * |
246 | * Creates new thread for connection, fills in connection |
252 | * Creates new thread for connection, fills in connection |
247 | * structures and inserts it into the hash table, so that |
253 | * structures and inserts it into the hash table, so that |
248 | * later we can easily do routing of messages to particular |
254 | * later we can easily do routing of messages to particular |
249 | * threads. |
255 | * threads. |
- | 256 | * |
|
- | 257 | * @param callid Callid of the IPC_M_CONNECT_ME_TO packet |
|
- | 258 | * @param call Call data of the opening packet |
|
- | 259 | * @param cthread Thread function that should be called upon |
|
- | 260 | * opening the connection |
|
- | 261 | * @return New thread id |
|
250 | */ |
262 | */ |
251 | static void new_connection(ipc_callid_t callid, ipc_call_t *call) |
263 | pstid_t async_new_connection(ipc_callid_t callid, ipc_call_t *call, |
- | 264 | void (*cthread)(ipc_callid_t,ipc_call_t *)) |
|
252 | { |
265 | { |
253 | pstid_t ptid; |
266 | pstid_t ptid; |
254 | connection_t *conn; |
267 | connection_t *conn; |
255 | unsigned long key; |
268 | unsigned long key; |
256 | 269 | ||
257 | conn = malloc(sizeof(*conn)); |
270 | conn = malloc(sizeof(*conn)); |
258 | if (!conn) { |
271 | if (!conn) { |
259 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
272 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
260 | return; |
273 | return NULL; |
261 | } |
274 | } |
262 | conn->in_phone_hash = IPC_GET_ARG3(*call); |
275 | conn->in_phone_hash = IPC_GET_ARG3(*call); |
263 | list_initialize(&conn->msg_queue); |
276 | list_initialize(&conn->msg_queue); |
264 | conn->ptid = psthread_create(connection_thread, conn); |
277 | conn->ptid = psthread_create(connection_thread, conn); |
265 | conn->callid = callid; |
278 | conn->callid = callid; |
266 | conn->call = *call; |
279 | conn->call = *call; |
267 | conn->active = 1; /* We will activate it asap */ |
280 | conn->active = 1; /* We will activate it asap */ |
- | 281 | conn->cthread = cthread; |
|
268 | list_initialize(&conn->link); |
282 | list_initialize(&conn->link); |
269 | if (!conn->ptid) { |
283 | if (!conn->ptid) { |
270 | free(conn); |
284 | free(conn); |
271 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
285 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
272 | return; |
286 | return NULL; |
273 | } |
287 | } |
274 | key = conn->in_phone_hash; |
288 | key = conn->in_phone_hash; |
275 | futex_down(&conn_futex); |
289 | futex_down(&conn_futex); |
276 | /* Add connection to hash table */ |
290 | /* Add connection to hash table */ |
277 | hash_table_insert(&conn_hash_table, &key, &conn->link); |
291 | hash_table_insert(&conn_hash_table, &key, &conn->link); |
278 | futex_up(&conn_futex); |
292 | futex_up(&conn_futex); |
279 | 293 | ||
280 | psthread_add_ready(conn->ptid); |
294 | psthread_add_ready(conn->ptid); |
- | 295 | ||
- | 296 | return conn->ptid; |
|
281 | } |
297 | } |
282 | 298 | ||
283 | /** Handle call to a task */ |
299 | /** Handle call to a task */ |
284 | static void handle_call(ipc_callid_t callid, ipc_call_t *call) |
300 | static void handle_call(ipc_callid_t callid, ipc_call_t *call) |
285 | { |
301 | { |
286 | if (route_call(callid, call)) |
302 | if (route_call(callid, call)) |
287 | return; |
303 | return; |
288 | 304 | ||
289 | switch (IPC_GET_METHOD(*call)) { |
305 | switch (IPC_GET_METHOD(*call)) { |
290 | case IPC_M_INTERRUPT: |
306 | case IPC_M_INTERRUPT: |
291 | break; |
307 | break; |
292 | case IPC_M_CONNECT_ME_TO: |
308 | case IPC_M_CONNECT_ME_TO: |
293 | /* Open new connection with thread etc. */ |
309 | /* Open new connection with thread etc. */ |
294 | new_connection(callid, call); |
310 | async_new_connection(callid, call, client_connection); |
295 | break; |
311 | break; |
296 | default: |
312 | default: |
297 | ipc_answer_fast(callid, EHANGUP, 0, 0); |
313 | ipc_answer_fast(callid, EHANGUP, 0, 0); |
298 | } |
314 | } |
299 | } |
315 | } |
300 | 316 | ||
301 | /** Endless loop dispatching incoming calls and answers */ |
317 | /** Endless loop dispatching incoming calls and answers */ |
302 | int async_manager() |
318 | int async_manager() |
303 | { |
319 | { |
304 | ipc_call_t call; |
320 | ipc_call_t call; |
305 | ipc_callid_t callid; |
321 | ipc_callid_t callid; |
306 | 322 | ||
307 | while (1) { |
323 | while (1) { |
308 | if (psthread_schedule_next_adv(PS_FROM_MANAGER)) { |
324 | if (psthread_schedule_next_adv(PS_FROM_MANAGER)) { |
309 | futex_up(&conn_futex); /* conn_futex is always held |
325 | futex_up(&conn_futex); /* conn_futex is always held |
310 | * when entering manager thread |
326 | * when entering manager thread |
311 | */ |
327 | */ |
312 | continue; |
328 | continue; |
313 | } |
329 | } |
314 | callid = ipc_wait_cycle(&call,SYNCH_NO_TIMEOUT,SYNCH_BLOCKING); |
330 | callid = ipc_wait_cycle(&call,SYNCH_NO_TIMEOUT,SYNCH_BLOCKING); |
315 | 331 | ||
316 | if (callid & IPC_CALLID_ANSWERED) |
332 | if (callid & IPC_CALLID_ANSWERED) |
317 | continue; |
333 | continue; |
318 | handle_call(callid, &call); |
334 | handle_call(callid, &call); |
319 | } |
335 | } |
320 | } |
336 | } |
321 | 337 | ||
322 | /** Function to start async_manager as a standalone thread |
338 | /** Function to start async_manager as a standalone thread |
323 | * |
339 | * |
324 | * When more kernel threads are used, one async manager should |
340 | * When more kernel threads are used, one async manager should |
325 | * exist per thread. The particular implementation may change, |
341 | * exist per thread. The particular implementation may change, |
326 | * currently one async_manager is started automatically per kernel |
342 | * currently one async_manager is started automatically per kernel |
327 | * thread except main thread. |
343 | * thread except main thread. |
328 | */ |
344 | */ |
329 | static int async_manager_thread(void *arg) |
345 | static int async_manager_thread(void *arg) |
330 | { |
346 | { |
331 | futex_up(&conn_futex); /* conn_futex is always locked when entering |
347 | futex_up(&conn_futex); /* conn_futex is always locked when entering |
332 | * manager */ |
348 | * manager */ |
333 | async_manager(); |
349 | async_manager(); |
334 | } |
350 | } |
335 | 351 | ||
336 | /** Add one manager to manager list */ |
352 | /** Add one manager to manager list */ |
337 | void async_create_manager(void) |
353 | void async_create_manager(void) |
338 | { |
354 | { |
339 | pstid_t ptid; |
355 | pstid_t ptid; |
340 | 356 | ||
341 | ptid = psthread_create(async_manager_thread, NULL); |
357 | ptid = psthread_create(async_manager_thread, NULL); |
342 | psthread_add_manager(ptid); |
358 | psthread_add_manager(ptid); |
343 | } |
359 | } |
344 | 360 | ||
345 | /** Remove one manager from manager list */ |
361 | /** Remove one manager from manager list */ |
346 | void async_destroy_manager(void) |
362 | void async_destroy_manager(void) |
347 | { |
363 | { |
348 | psthread_remove_manager(); |
364 | psthread_remove_manager(); |
349 | } |
365 | } |
350 | 366 | ||
351 | /** Initialize internal structures needed for async manager */ |
367 | /** Initialize internal structures needed for async manager */ |
352 | int _async_init(void) |
368 | int _async_init(void) |
353 | { |
369 | { |
354 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1, &conn_hash_table_ops)) { |
370 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1, &conn_hash_table_ops)) { |
355 | printf("%s: cannot create hash table\n", "async"); |
371 | printf("%s: cannot create hash table\n", "async"); |
356 | return ENOMEM; |
372 | return ENOMEM; |
357 | } |
373 | } |
358 | 374 | ||
359 | } |
375 | } |
360 | 376 |