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