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