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