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