Rev 1532 | Rev 1547 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1532 | Rev 1536 | ||
---|---|---|---|
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 |
- | |
220 | */ |
219 | */ |
221 | static void insert_timeout(awaiter_t *wd) |
220 | static void insert_timeout(awaiter_t *wd) |
222 | { |
221 | { |
223 | link_t *tmp; |
222 | link_t *tmp; |
224 | awaiter_t *cur; |
223 | awaiter_t *cur; |
225 | 224 | ||
226 | wd->timedout = 0; |
225 | wd->timedout = 0; |
227 | 226 | ||
228 | tmp = timeout_list.next; |
227 | tmp = timeout_list.next; |
229 | while (tmp != &timeout_list) { |
228 | while (tmp != &timeout_list) { |
230 | cur = list_get_instance(tmp, awaiter_t, link); |
229 | cur = list_get_instance(tmp, awaiter_t, link); |
231 | if (tv_gteq(&cur->expires, &wd->expires)) |
230 | if (tv_gteq(&cur->expires, &wd->expires)) |
232 | break; |
231 | break; |
233 | tmp = tmp->next; |
232 | tmp = tmp->next; |
234 | } |
233 | } |
235 | list_append(&wd->link, tmp); |
234 | list_append(&wd->link, tmp); |
236 | } |
235 | } |
237 | 236 | ||
238 | /*************************************************/ |
237 | /*************************************************/ |
239 | 238 | ||
240 | /** Try to route a call to an appropriate connection thread |
239 | /** Try to route a call to an appropriate connection thread |
241 | * |
240 | * |
242 | */ |
241 | */ |
243 | static int route_call(ipc_callid_t callid, ipc_call_t *call) |
242 | static int route_call(ipc_callid_t callid, ipc_call_t *call) |
244 | { |
243 | { |
245 | connection_t *conn; |
244 | connection_t *conn; |
246 | msg_t *msg; |
245 | msg_t *msg; |
247 | link_t *hlp; |
246 | link_t *hlp; |
248 | unsigned long key; |
247 | unsigned long key; |
249 | 248 | ||
250 | futex_down(&async_futex); |
249 | futex_down(&async_futex); |
251 | 250 | ||
252 | key = call->in_phone_hash; |
251 | key = call->in_phone_hash; |
253 | hlp = hash_table_find(&conn_hash_table, &key); |
252 | hlp = hash_table_find(&conn_hash_table, &key); |
254 | if (!hlp) { |
253 | if (!hlp) { |
255 | futex_up(&async_futex); |
254 | futex_up(&async_futex); |
256 | return 0; |
255 | return 0; |
257 | } |
256 | } |
258 | conn = hash_table_get_instance(hlp, connection_t, link); |
257 | conn = hash_table_get_instance(hlp, connection_t, link); |
259 | 258 | ||
260 | msg = malloc(sizeof(*msg)); |
259 | msg = malloc(sizeof(*msg)); |
261 | msg->callid = callid; |
260 | msg->callid = callid; |
262 | msg->call = *call; |
261 | msg->call = *call; |
263 | list_append(&msg->link, &conn->msg_queue); |
262 | list_append(&msg->link, &conn->msg_queue); |
264 | 263 | ||
265 | /* If the call is waiting for event, run it */ |
264 | /* If the call is waiting for event, run it */ |
266 | if (!conn->wdata.active) { |
265 | if (!conn->wdata.active) { |
267 | /* If in timeout list, remove it */ |
266 | /* If in timeout list, remove it */ |
268 | if (conn->wdata.inlist) { |
267 | if (conn->wdata.inlist) { |
269 | conn->wdata.inlist = 0; |
268 | conn->wdata.inlist = 0; |
270 | list_remove(&conn->wdata.link); |
269 | list_remove(&conn->wdata.link); |
271 | } |
270 | } |
272 | conn->wdata.active = 1; |
271 | conn->wdata.active = 1; |
273 | psthread_add_ready(conn->wdata.ptid); |
272 | psthread_add_ready(conn->wdata.ptid); |
274 | } |
273 | } |
275 | 274 | ||
276 | futex_up(&async_futex); |
275 | futex_up(&async_futex); |
277 | 276 | ||
278 | return 1; |
277 | return 1; |
279 | } |
278 | } |
280 | 279 | ||
281 | /** Return new incoming message for current(thread-local) connection */ |
280 | /** Return new incoming message for current(thread-local) connection */ |
282 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs) |
281 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs) |
283 | { |
282 | { |
284 | msg_t *msg; |
283 | msg_t *msg; |
285 | ipc_callid_t callid; |
284 | ipc_callid_t callid; |
- | 285 | connection_t *conn; |
|
286 | 286 | ||
287 | assert(PS_connection); |
287 | assert(PS_connection); |
- | 288 | /* GCC 4.1.0 coughs on PS_connection-> dereference, |
|
- | 289 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot. |
|
- | 290 | * I would never expect to find so many errors in |
|
- | 291 | * compiler *($&$(*&$ |
|
- | 292 | */ |
|
- | 293 | conn = PS_connection; |
|
288 | 294 | ||
289 | if (usecs < 0) /* TODO: let it get through the ipc_call once */ |
295 | if (usecs < 0) /* TODO: let it get through the ipc_call once */ |
290 | return 0; |
296 | return 0; |
291 | 297 | ||
292 | futex_down(&async_futex); |
298 | futex_down(&async_futex); |
293 | 299 | ||
294 | if (usecs) { |
300 | if (usecs) { |
295 | gettimeofday(&PS_connection->wdata.expires, NULL); |
301 | gettimeofday(&conn->wdata.expires, NULL); |
296 | tv_add(&PS_connection->wdata.expires, usecs); |
302 | tv_add(&conn->wdata.expires, usecs); |
297 | } else { |
303 | } else { |
298 | PS_connection->wdata.inlist = 0; |
304 | conn->wdata.inlist = 0; |
299 | } |
305 | } |
300 | /* If nothing in queue, wait until something appears */ |
306 | /* If nothing in queue, wait until something appears */ |
301 | while (list_empty(&PS_connection->msg_queue)) { |
307 | while (list_empty(&conn->msg_queue)) { |
302 | if (usecs) { |
308 | if (usecs) { |
303 | PS_connection->wdata.inlist = 1; |
309 | conn->wdata.inlist = 1; |
304 | insert_timeout(&PS_connection->wdata); |
310 | insert_timeout(&conn->wdata); |
305 | } |
311 | } |
306 | PS_connection->wdata.active = 0; |
312 | conn->wdata.active = 0; |
307 | psthread_schedule_next_adv(PS_TO_MANAGER); |
313 | psthread_schedule_next_adv(PS_TO_MANAGER); |
308 | /* Futex is up after getting back from async_manager |
314 | /* Futex is up after getting back from async_manager |
309 | * get it again */ |
315 | * get it again */ |
310 | futex_down(&async_futex); |
316 | futex_down(&async_futex); |
311 | if (usecs && PS_connection->wdata.timedout && \ |
317 | if (usecs && conn->wdata.timedout && \ |
312 | list_empty(&PS_connection->msg_queue)) { |
318 | list_empty(&conn->msg_queue)) { |
313 | /* If we timed out-> exit */ |
319 | /* If we timed out-> exit */ |
314 | futex_up(&async_futex); |
320 | futex_up(&async_futex); |
315 | return 0; |
321 | return 0; |
316 | } |
322 | } |
317 | } |
323 | } |
318 | 324 | ||
319 | msg = list_get_instance(PS_connection->msg_queue.next, msg_t, link); |
325 | msg = list_get_instance(conn->msg_queue.next, msg_t, link); |
320 | list_remove(&msg->link); |
326 | list_remove(&msg->link); |
321 | callid = msg->callid; |
327 | callid = msg->callid; |
322 | *call = msg->call; |
328 | *call = msg->call; |
323 | free(msg); |
329 | free(msg); |
324 | 330 | ||
325 | futex_up(&async_futex); |
331 | futex_up(&async_futex); |
326 | return callid; |
332 | return callid; |
327 | } |
333 | } |
328 | 334 | ||
329 | /** Thread function that gets created on new connection |
335 | /** Thread function that gets created on new connection |
330 | * |
336 | * |
331 | * This function is defined as a weak symbol - to be redefined in |
337 | * This function is defined as a weak symbol - to be redefined in |
332 | * user code. |
338 | * user code. |
333 | */ |
339 | */ |
334 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call) |
340 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call) |
335 | { |
341 | { |
336 | ipc_answer_fast(callid, ENOENT, 0, 0); |
342 | ipc_answer_fast(callid, ENOENT, 0, 0); |
337 | } |
343 | } |
338 | 344 | ||
339 | /** Function that gets called on interrupt receival |
345 | /** Function that gets called on interrupt receival |
340 | * |
346 | * |
341 | * This function is defined as a weak symbol - to be redefined in |
347 | * This function is defined as a weak symbol - to be redefined in |
342 | * user code. |
348 | * user code. |
343 | */ |
349 | */ |
344 | void interrupt_received(ipc_call_t *call) |
350 | void interrupt_received(ipc_call_t *call) |
345 | { |
351 | { |
346 | } |
352 | } |
347 | 353 | ||
348 | 354 | ||
349 | /** Wrapper for client connection thread |
355 | /** Wrapper for client connection thread |
350 | * |
356 | * |
351 | * When new connection arrives, thread with this function is created. |
357 | * When new connection arrives, thread with this function is created. |
352 | * It calls client_connection and does final cleanup. |
358 | * It calls client_connection and does final cleanup. |
353 | * |
359 | * |
354 | * @parameter arg Connection structure pointer |
360 | * @parameter arg Connection structure pointer |
355 | */ |
361 | */ |
356 | static int connection_thread(void *arg) |
362 | static int connection_thread(void *arg) |
357 | { |
363 | { |
358 | unsigned long key; |
364 | unsigned long key; |
359 | msg_t *msg; |
365 | msg_t *msg; |
360 | 366 | ||
361 | /* Setup thread local connection pointer */ |
367 | /* Setup thread local connection pointer */ |
362 | PS_connection = (connection_t *)arg; |
368 | PS_connection = (connection_t *)arg; |
363 | PS_connection->cthread(PS_connection->callid, &PS_connection->call); |
369 | PS_connection->cthread(PS_connection->callid, &PS_connection->call); |
364 | 370 | ||
365 | /* Remove myself from connection hash table */ |
371 | /* Remove myself from connection hash table */ |
366 | futex_down(&async_futex); |
372 | futex_down(&async_futex); |
367 | key = PS_connection->in_phone_hash; |
373 | key = PS_connection->in_phone_hash; |
368 | hash_table_remove(&conn_hash_table, &key, 1); |
374 | hash_table_remove(&conn_hash_table, &key, 1); |
369 | futex_up(&async_futex); |
375 | futex_up(&async_futex); |
370 | /* Answer all remaining messages with ehangup */ |
376 | /* Answer all remaining messages with ehangup */ |
371 | while (!list_empty(&PS_connection->msg_queue)) { |
377 | while (!list_empty(&PS_connection->msg_queue)) { |
372 | msg = list_get_instance(PS_connection->msg_queue.next, msg_t, link); |
378 | msg = list_get_instance(PS_connection->msg_queue.next, msg_t, link); |
373 | list_remove(&msg->link); |
379 | list_remove(&msg->link); |
374 | ipc_answer_fast(msg->callid, EHANGUP, 0, 0); |
380 | ipc_answer_fast(msg->callid, EHANGUP, 0, 0); |
375 | free(msg); |
381 | free(msg); |
376 | } |
382 | } |
377 | } |
383 | } |
378 | 384 | ||
379 | /** Create new thread for a new connection |
385 | /** Create new thread for a new connection |
380 | * |
386 | * |
381 | * Creates new thread for connection, fills in connection |
387 | * Creates new thread for connection, fills in connection |
382 | * structures and inserts it into the hash table, so that |
388 | * structures and inserts it into the hash table, so that |
383 | * later we can easily do routing of messages to particular |
389 | * later we can easily do routing of messages to particular |
384 | * threads. |
390 | * threads. |
385 | * |
391 | * |
386 | * @param in_phone_hash Identification of the incoming connection |
392 | * @param in_phone_hash Identification of the incoming connection |
387 | * @param callid Callid of the IPC_M_CONNECT_ME_TO packet |
393 | * @param callid Callid of the IPC_M_CONNECT_ME_TO packet |
388 | * @param call Call data of the opening packet |
394 | * @param call Call data of the opening packet |
389 | * @param cthread Thread function that should be called upon |
395 | * @param cthread Thread function that should be called upon |
390 | * opening the connection |
396 | * opening the connection |
391 | * @return New thread id |
397 | * @return New thread id |
392 | */ |
398 | */ |
393 | pstid_t async_new_connection(ipcarg_t in_phone_hash,ipc_callid_t callid, |
399 | pstid_t async_new_connection(ipcarg_t in_phone_hash,ipc_callid_t callid, |
394 | ipc_call_t *call, |
400 | ipc_call_t *call, |
395 | void (*cthread)(ipc_callid_t,ipc_call_t *)) |
401 | void (*cthread)(ipc_callid_t,ipc_call_t *)) |
396 | { |
402 | { |
397 | pstid_t ptid; |
403 | pstid_t ptid; |
398 | connection_t *conn; |
404 | connection_t *conn; |
399 | unsigned long key; |
405 | unsigned long key; |
400 | 406 | ||
401 | conn = malloc(sizeof(*conn)); |
407 | conn = malloc(sizeof(*conn)); |
402 | if (!conn) { |
408 | if (!conn) { |
403 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
409 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
404 | return NULL; |
410 | return NULL; |
405 | } |
411 | } |
406 | conn->in_phone_hash = in_phone_hash; |
412 | conn->in_phone_hash = in_phone_hash; |
407 | list_initialize(&conn->msg_queue); |
413 | list_initialize(&conn->msg_queue); |
408 | conn->callid = callid; |
414 | conn->callid = callid; |
409 | if (call) |
415 | if (call) |
410 | conn->call = *call; |
416 | conn->call = *call; |
411 | conn->wdata.active = 1; /* We will activate it asap */ |
417 | conn->wdata.active = 1; /* We will activate it asap */ |
412 | conn->cthread = cthread; |
418 | conn->cthread = cthread; |
413 | 419 | ||
414 | conn->wdata.ptid = psthread_create(connection_thread, conn); |
420 | conn->wdata.ptid = psthread_create(connection_thread, conn); |
415 | if (!conn->wdata.ptid) { |
421 | if (!conn->wdata.ptid) { |
416 | free(conn); |
422 | free(conn); |
417 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
423 | ipc_answer_fast(callid, ENOMEM, 0, 0); |
418 | return NULL; |
424 | return NULL; |
419 | } |
425 | } |
420 | /* Add connection to hash table */ |
426 | /* Add connection to hash table */ |
421 | key = conn->in_phone_hash; |
427 | key = conn->in_phone_hash; |
422 | futex_down(&async_futex); |
428 | futex_down(&async_futex); |
423 | hash_table_insert(&conn_hash_table, &key, &conn->link); |
429 | hash_table_insert(&conn_hash_table, &key, &conn->link); |
424 | futex_up(&async_futex); |
430 | futex_up(&async_futex); |
425 | 431 | ||
426 | psthread_add_ready(conn->wdata.ptid); |
432 | psthread_add_ready(conn->wdata.ptid); |
427 | 433 | ||
428 | return conn->wdata.ptid; |
434 | return conn->wdata.ptid; |
429 | } |
435 | } |
430 | 436 | ||
431 | /** Handle call that was received */ |
437 | /** Handle call that was received */ |
432 | static void handle_call(ipc_callid_t callid, ipc_call_t *call) |
438 | static void handle_call(ipc_callid_t callid, ipc_call_t *call) |
433 | { |
439 | { |
434 | /* Unrouted call - do some default behaviour */ |
440 | /* Unrouted call - do some default behaviour */ |
435 | switch (IPC_GET_METHOD(*call)) { |
441 | switch (IPC_GET_METHOD(*call)) { |
436 | case IPC_M_INTERRUPT: |
442 | case IPC_M_INTERRUPT: |
437 | interrupt_received(call); |
443 | interrupt_received(call); |
438 | return; |
444 | return; |
439 | case IPC_M_CONNECT_ME_TO: |
445 | case IPC_M_CONNECT_ME_TO: |
440 | /* Open new connection with thread etc. */ |
446 | /* Open new connection with thread etc. */ |
441 | async_new_connection(IPC_GET_ARG3(*call), callid, call, client_connection); |
447 | async_new_connection(IPC_GET_ARG3(*call), callid, call, client_connection); |
442 | return; |
448 | return; |
443 | } |
449 | } |
444 | 450 | ||
445 | /* Try to route call through connection tables */ |
451 | /* Try to route call through connection tables */ |
446 | if (route_call(callid, call)) |
452 | if (route_call(callid, call)) |
447 | return; |
453 | return; |
448 | 454 | ||
449 | /* Unknown call from unknown phone - hang it up */ |
455 | /* Unknown call from unknown phone - hang it up */ |
450 | ipc_answer_fast(callid, EHANGUP, 0, 0); |
456 | ipc_answer_fast(callid, EHANGUP, 0, 0); |
451 | } |
457 | } |
452 | 458 | ||
453 | /** Fire all timeouts that expired */ |
459 | /** Fire all timeouts that expired |
- | 460 | * |
|
- | 461 | */ |
|
454 | static void handle_expired_timeouts(void) |
462 | static void handle_expired_timeouts(void) |
455 | { |
463 | { |
456 | struct timeval tv; |
464 | struct timeval tv; |
457 | awaiter_t *waiter; |
465 | awaiter_t *waiter; |
458 | link_t *cur; |
466 | link_t *cur; |
459 | 467 | ||
460 | gettimeofday(&tv,NULL); |
468 | gettimeofday(&tv,NULL); |
461 | futex_down(&async_futex); |
469 | futex_down(&async_futex); |
462 | 470 | ||
463 | cur = timeout_list.next; |
471 | cur = timeout_list.next; |
464 | while (cur != &timeout_list) { |
472 | while (cur != &timeout_list) { |
465 | waiter = list_get_instance(cur,awaiter_t,link); |
473 | waiter = list_get_instance(cur,awaiter_t,link); |
466 | if (tv_gt(&waiter->expires, &tv)) |
474 | if (tv_gt(&waiter->expires, &tv)) |
467 | break; |
475 | break; |
468 | cur = cur->next; |
476 | cur = cur->next; |
469 | list_remove(&waiter->link); |
477 | list_remove(&waiter->link); |
470 | waiter->inlist = 0; |
478 | waiter->inlist = 0; |
471 | waiter->timedout = 1; |
479 | waiter->timedout = 1; |
472 | /* Redundant condition? The thread should not |
480 | /* Redundant condition? The thread should not |
473 | * be active when it gets here. |
481 | * be active when it gets here. |
474 | */ |
482 | */ |
475 | if (!waiter->active) { |
483 | if (!waiter->active) { |
476 | waiter->active = 1; |
484 | waiter->active = 1; |
477 | psthread_add_ready(waiter->ptid); |
485 | psthread_add_ready(waiter->ptid); |
478 | } |
486 | } |
479 | } |
487 | } |
480 | 488 | ||
481 | futex_up(&async_futex); |
489 | futex_up(&async_futex); |
482 | } |
490 | } |
483 | 491 | ||
484 | /** Endless loop dispatching incoming calls and answers */ |
492 | /** Endless loop dispatching incoming calls and answers */ |
485 | int async_manager(void) |
493 | int async_manager(void) |
486 | { |
494 | { |
487 | ipc_call_t call; |
495 | ipc_call_t call; |
488 | ipc_callid_t callid; |
496 | ipc_callid_t callid; |
489 | int timeout; |
497 | int timeout; |
490 | awaiter_t *waiter; |
498 | awaiter_t *waiter; |
491 | struct timeval tv; |
499 | struct timeval tv; |
492 | 500 | ||
493 | while (1) { |
501 | while (1) { |
494 | if (psthread_schedule_next_adv(PS_FROM_MANAGER)) { |
502 | if (psthread_schedule_next_adv(PS_FROM_MANAGER)) { |
495 | futex_up(&async_futex); /* async_futex is always held |
503 | futex_up(&async_futex); /* async_futex is always held |
496 | * when entering manager thread |
504 | * when entering manager thread |
497 | */ |
505 | */ |
498 | continue; |
506 | continue; |
499 | } |
507 | } |
500 | futex_down(&async_futex); |
508 | futex_down(&async_futex); |
501 | if (!list_empty(&timeout_list)) { |
509 | if (!list_empty(&timeout_list)) { |
502 | waiter = list_get_instance(timeout_list.next,awaiter_t,link); |
510 | waiter = list_get_instance(timeout_list.next,awaiter_t,link); |
503 | gettimeofday(&tv,NULL); |
511 | gettimeofday(&tv,NULL); |
504 | if (tv_gteq(&tv, &waiter->expires)) { |
512 | if (tv_gteq(&tv, &waiter->expires)) { |
- | 513 | futex_up(&async_futex); |
|
505 | handle_expired_timeouts(); |
514 | handle_expired_timeouts(); |
506 | continue; |
515 | continue; |
507 | } else |
516 | } else |
508 | timeout = tv_sub(&waiter->expires, &tv); |
517 | timeout = tv_sub(&waiter->expires, &tv); |
509 | } else |
518 | } else |
510 | timeout = SYNCH_NO_TIMEOUT; |
519 | timeout = SYNCH_NO_TIMEOUT; |
511 | futex_up(&async_futex); |
520 | futex_up(&async_futex); |
512 | 521 | ||
513 | callid = ipc_wait_cycle(&call, timeout, SYNCH_FLAGS_NONE); |
522 | callid = ipc_wait_cycle(&call, timeout, SYNCH_FLAGS_NONE); |
514 | 523 | ||
515 | if (!callid) { |
524 | if (!callid) { |
516 | handle_expired_timeouts(); |
525 | handle_expired_timeouts(); |
517 | continue; |
526 | continue; |
518 | } |
527 | } |
519 | 528 | ||
520 | if (callid & IPC_CALLID_ANSWERED) |
529 | if (callid & IPC_CALLID_ANSWERED) |
521 | continue; |
530 | continue; |
522 | 531 | ||
523 | handle_call(callid, &call); |
532 | handle_call(callid, &call); |
524 | } |
533 | } |
525 | } |
534 | } |
526 | 535 | ||
527 | /** Function to start async_manager as a standalone thread |
536 | /** Function to start async_manager as a standalone thread |
528 | * |
537 | * |
529 | * When more kernel threads are used, one async manager should |
538 | * When more kernel threads are used, one async manager should |
530 | * exist per thread. The particular implementation may change, |
539 | * exist per thread. The particular implementation may change, |
531 | * currently one async_manager is started automatically per kernel |
540 | * currently one async_manager is started automatically per kernel |
532 | * thread except main thread. |
541 | * thread except main thread. |
533 | */ |
542 | */ |
534 | static int async_manager_thread(void *arg) |
543 | static int async_manager_thread(void *arg) |
535 | { |
544 | { |
536 | futex_up(&async_futex); /* async_futex is always locked when entering |
545 | futex_up(&async_futex); /* async_futex is always locked when entering |
537 | * manager */ |
546 | * manager */ |
538 | async_manager(); |
547 | async_manager(); |
539 | } |
548 | } |
540 | 549 | ||
541 | /** Add one manager to manager list */ |
550 | /** Add one manager to manager list */ |
542 | void async_create_manager(void) |
551 | void async_create_manager(void) |
543 | { |
552 | { |
544 | pstid_t ptid; |
553 | pstid_t ptid; |
545 | 554 | ||
546 | ptid = psthread_create(async_manager_thread, NULL); |
555 | ptid = psthread_create(async_manager_thread, NULL); |
547 | psthread_add_manager(ptid); |
556 | psthread_add_manager(ptid); |
548 | } |
557 | } |
549 | 558 | ||
550 | /** Remove one manager from manager list */ |
559 | /** Remove one manager from manager list */ |
551 | void async_destroy_manager(void) |
560 | void async_destroy_manager(void) |
552 | { |
561 | { |
553 | psthread_remove_manager(); |
562 | psthread_remove_manager(); |
554 | } |
563 | } |
555 | 564 | ||
556 | /** Initialize internal structures needed for async manager */ |
565 | /** Initialize internal structures needed for async manager */ |
557 | int _async_init(void) |
566 | int _async_init(void) |
558 | { |
567 | { |
559 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1, &conn_hash_table_ops)) { |
568 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1, &conn_hash_table_ops)) { |
560 | printf("%s: cannot create hash table\n", "async"); |
569 | printf("%s: cannot create hash table\n", "async"); |
561 | return ENOMEM; |
570 | return ENOMEM; |
562 | } |
571 | } |
563 | 572 | ||
564 | } |
573 | } |
565 | 574 | ||
566 | /** IPC handler for messages in async framework |
575 | /** IPC handler for messages in async framework |
567 | * |
576 | * |
568 | * Notify thread that is waiting for this message, that it arrived |
577 | * Notify thread that is waiting for this message, that it arrived |
569 | */ |
578 | */ |
570 | static void reply_received(void *private, int retval, |
579 | static void reply_received(void *private, int retval, |
571 | ipc_call_t *data) |
580 | ipc_call_t *data) |
572 | { |
581 | { |
573 | amsg_t *msg = (amsg_t *) private; |
582 | amsg_t *msg = (amsg_t *) private; |
574 | 583 | ||
575 | msg->retval = retval; |
584 | msg->retval = retval; |
576 | 585 | ||
577 | futex_down(&async_futex); |
586 | futex_down(&async_futex); |
578 | /* Copy data after futex_down, just in case the |
587 | /* Copy data after futex_down, just in case the |
579 | * call was detached |
588 | * call was detached |
580 | */ |
589 | */ |
581 | if (msg->dataptr) |
590 | if (msg->dataptr) |
582 | *msg->dataptr = *data; |
591 | *msg->dataptr = *data; |
583 | 592 | ||
584 | write_barrier(); |
593 | write_barrier(); |
585 | /* Remove message from timeout list */ |
594 | /* Remove message from timeout list */ |
586 | if (msg->wdata.inlist) |
595 | if (msg->wdata.inlist) |
587 | list_remove(&msg->wdata.link); |
596 | list_remove(&msg->wdata.link); |
588 | msg->done = 1; |
597 | msg->done = 1; |
589 | if (! msg->wdata.active) { |
598 | if (! msg->wdata.active) { |
590 | msg->wdata.active = 1; |
599 | msg->wdata.active = 1; |
591 | psthread_add_ready(msg->wdata.ptid); |
600 | psthread_add_ready(msg->wdata.ptid); |
592 | } |
601 | } |
593 | futex_up(&async_futex); |
602 | futex_up(&async_futex); |
594 | } |
603 | } |
595 | 604 | ||
596 | /** Send message and return id of the sent message |
605 | /** Send message and return id of the sent message |
597 | * |
606 | * |
598 | * The return value can be used as input for async_wait() to wait |
607 | * The return value can be used as input for async_wait() to wait |
599 | * for completion. |
608 | * for completion. |
600 | */ |
609 | */ |
601 | aid_t async_send_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, |
610 | aid_t async_send_2(int phoneid, ipcarg_t method, ipcarg_t arg1, ipcarg_t arg2, |
602 | ipc_call_t *dataptr) |
611 | ipc_call_t *dataptr) |
603 | { |
612 | { |
604 | amsg_t *msg; |
613 | amsg_t *msg; |
605 | 614 | ||
606 | msg = malloc(sizeof(*msg)); |
615 | msg = malloc(sizeof(*msg)); |
607 | msg->done = 0; |
616 | msg->done = 0; |
608 | msg->dataptr = dataptr; |
617 | msg->dataptr = dataptr; |
609 | 618 | ||
610 | msg->wdata.active = 1; /* We may sleep in next method, but it |
619 | msg->wdata.active = 1; /* We may sleep in next method, but it |
611 | * will use it's own mechanism */ |
620 | * will use it's own mechanism */ |
612 | ipc_call_async_2(phoneid,method,arg1,arg2,msg,reply_received,1); |
621 | ipc_call_async_2(phoneid,method,arg1,arg2,msg,reply_received,1); |
613 | 622 | ||
614 | return (aid_t) msg; |
623 | return (aid_t) msg; |
615 | } |
624 | } |
616 | 625 | ||
617 | /** Wait for a message sent by async framework |
626 | /** Wait for a message sent by async framework |
618 | * |
627 | * |
619 | * @param amsgid Message ID to wait for |
628 | * @param amsgid Message ID to wait for |
620 | * @param retval Pointer to variable where will be stored retval |
629 | * @param retval Pointer to variable where will be stored retval |
621 | * of the answered message. If NULL, it is ignored. |
630 | * of the answered message. If NULL, it is ignored. |
622 | * |
631 | * |
623 | */ |
632 | */ |
624 | void async_wait_for(aid_t amsgid, ipcarg_t *retval) |
633 | void async_wait_for(aid_t amsgid, ipcarg_t *retval) |
625 | { |
634 | { |
626 | amsg_t *msg = (amsg_t *) amsgid; |
635 | amsg_t *msg = (amsg_t *) amsgid; |
627 | connection_t *conn; |
636 | connection_t *conn; |
628 | 637 | ||
629 | futex_down(&async_futex); |
638 | futex_down(&async_futex); |
630 | if (msg->done) { |
639 | if (msg->done) { |
631 | futex_up(&async_futex); |
640 | futex_up(&async_futex); |
632 | goto done; |
641 | goto done; |
633 | } |
642 | } |
634 | 643 | ||
635 | msg->wdata.ptid = psthread_get_id(); |
644 | msg->wdata.ptid = psthread_get_id(); |
636 | msg->wdata.active = 0; |
645 | msg->wdata.active = 0; |
637 | msg->wdata.inlist = 0; |
646 | msg->wdata.inlist = 0; |
638 | /* Leave locked async_futex when entering this function */ |
647 | /* Leave locked async_futex when entering this function */ |
639 | psthread_schedule_next_adv(PS_TO_MANAGER); |
648 | psthread_schedule_next_adv(PS_TO_MANAGER); |
640 | /* futex is up automatically after psthread_schedule_next...*/ |
649 | /* futex is up automatically after psthread_schedule_next...*/ |
641 | done: |
650 | done: |
642 | if (retval) |
651 | if (retval) |
643 | *retval = msg->retval; |
652 | *retval = msg->retval; |
644 | free(msg); |
653 | free(msg); |
645 | } |
654 | } |
646 | 655 | ||
647 | /** Wait for a message sent by async framework with timeout |
656 | /** Wait for a message sent by async framework with timeout |
648 | * |
657 | * |
649 | * @param amsgid Message ID to wait for |
658 | * @param amsgid Message ID to wait for |
650 | * @param retval Pointer to variable where will be stored retval |
659 | * @param retval Pointer to variable where will be stored retval |
651 | * of the answered message. If NULL, it is ignored. |
660 | * of the answered message. If NULL, it is ignored. |
652 | * @param timeout Timeout in usecs |
661 | * @param timeout Timeout in usecs |
653 | * @return 0 on success, ETIMEOUT if timeout expired |
662 | * @return 0 on success, ETIMEOUT if timeout expired |
654 | * |
663 | * |
655 | */ |
664 | */ |
656 | int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout) |
665 | int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout) |
657 | { |
666 | { |
658 | amsg_t *msg = (amsg_t *) amsgid; |
667 | amsg_t *msg = (amsg_t *) amsgid; |
659 | connection_t *conn; |
668 | connection_t *conn; |
660 | 669 | ||
661 | /* TODO: Let it go through the event read at least once */ |
670 | /* TODO: Let it go through the event read at least once */ |
662 | if (timeout < 0) |
671 | if (timeout < 0) |
663 | return ETIMEOUT; |
672 | return ETIMEOUT; |
664 | 673 | ||
665 | futex_down(&async_futex); |
674 | futex_down(&async_futex); |
666 | if (msg->done) { |
675 | if (msg->done) { |
667 | futex_up(&async_futex); |
676 | futex_up(&async_futex); |
668 | goto done; |
677 | goto done; |
669 | } |
678 | } |
670 | 679 | ||
671 | gettimeofday(&msg->wdata.expires, NULL); |
680 | gettimeofday(&msg->wdata.expires, NULL); |
672 | tv_add(&msg->wdata.expires, timeout); |
681 | tv_add(&msg->wdata.expires, timeout); |
673 | 682 | ||
674 | msg->wdata.ptid = psthread_get_id(); |
683 | msg->wdata.ptid = psthread_get_id(); |
675 | msg->wdata.active = 0; |
684 | msg->wdata.active = 0; |
676 | msg->wdata.inlist = 1; |
685 | msg->wdata.inlist = 1; |
677 | 686 | ||
678 | insert_timeout(&msg->wdata); |
687 | insert_timeout(&msg->wdata); |
679 | 688 | ||
680 | /* Leave locked async_futex when entering this function */ |
689 | /* Leave locked async_futex when entering this function */ |
681 | psthread_schedule_next_adv(PS_TO_MANAGER); |
690 | psthread_schedule_next_adv(PS_TO_MANAGER); |
682 | /* futex is up automatically after psthread_schedule_next...*/ |
691 | /* futex is up automatically after psthread_schedule_next...*/ |
683 | 692 | ||
684 | if (!msg->done) |
693 | if (!msg->done) |
685 | return ETIMEOUT; |
694 | return ETIMEOUT; |
686 | 695 | ||
687 | done: |
696 | done: |
688 | if (retval) |
697 | if (retval) |
689 | *retval = msg->retval; |
698 | *retval = msg->retval; |
690 | free(msg); |
699 | free(msg); |
691 | 700 | ||
692 | return 0; |
701 | return 0; |
693 | } |
702 | } |
694 | 703 | ||
695 | /** Wait specified time, but in the meantime handle incoming events |
704 | /** Wait specified time, but in the meantime handle incoming events |
696 | * |
705 | * |
697 | * @param timeout Time in microseconds to wait |
706 | * @param timeout Time in microseconds to wait |
698 | */ |
707 | */ |
699 | void async_usleep(suseconds_t timeout) |
708 | void async_usleep(suseconds_t timeout) |
700 | { |
709 | { |
701 | amsg_t *msg; |
710 | amsg_t *msg; |
702 | 711 | ||
703 | msg = malloc(sizeof(*msg)); |
712 | msg = malloc(sizeof(*msg)); |
704 | if (!msg) |
713 | if (!msg) |
705 | return; |
714 | return; |
706 | 715 | ||
707 | msg->wdata.ptid = psthread_get_id(); |
716 | msg->wdata.ptid = psthread_get_id(); |
708 | msg->wdata.inlist = 1; |
717 | msg->wdata.inlist = 1; |
709 | msg->wdata.active = 0; |
718 | msg->wdata.active = 0; |
710 | 719 | ||
711 | gettimeofday(&msg->wdata.expires, NULL); |
720 | gettimeofday(&msg->wdata.expires, NULL); |
712 | tv_add(&msg->wdata.expires, timeout); |
721 | tv_add(&msg->wdata.expires, timeout); |
713 | 722 | ||
714 | futex_down(&async_futex); |
723 | futex_down(&async_futex); |
715 | insert_timeout(&msg->wdata); |
724 | insert_timeout(&msg->wdata); |
716 | /* Leave locked async_futex when entering this function */ |
725 | /* Leave locked async_futex when entering this function */ |
717 | psthread_schedule_next_adv(PS_TO_MANAGER); |
726 | psthread_schedule_next_adv(PS_TO_MANAGER); |
718 | /* futex is up automatically after psthread_schedule_next...*/ |
727 | /* futex is up automatically after psthread_schedule_next...*/ |
719 | free(msg); |
728 | free(msg); |
720 | } |
729 | } |
721 | 730 | ||
722 | /** Set function that is called, IPC_M_CONNECT_ME_TO is received |
731 | /** Set function that is called, IPC_M_CONNECT_ME_TO is received |
723 | * |
732 | * |
724 | * @param conn Function that will form new psthread. |
733 | * @param conn Function that will form new psthread. |
725 | */ |
734 | */ |
726 | void async_set_client_connection(async_client_conn_t conn) |
735 | void async_set_client_connection(async_client_conn_t conn) |
727 | { |
736 | { |
728 | client_connection = conn; |
737 | client_connection = conn; |
729 | } |
738 | } |
730 | 739 |