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