Rev 4420 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4420 | Rev 4537 | ||
---|---|---|---|
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 to provide a facility for writing programs which |
38 | * The aim of this library is to provide a facility for writing programs which |
39 | * utilize the asynchronous nature of HelenOS IPC, yet using a normal way of |
39 | * utilize the asynchronous nature of HelenOS IPC, yet using a normal way of |
40 | * programming. |
40 | * programming. |
41 | * |
41 | * |
42 | * You should be able to write very simple multithreaded programs, the async |
42 | * You should be able to write very simple multithreaded programs, the async |
43 | * framework will automatically take care of most synchronization problems. |
43 | * framework will automatically take care of most synchronization problems. |
44 | * |
44 | * |
45 | * Default semantics: |
45 | * Default semantics: |
46 | * - async_send_*(): Send asynchronously. If the kernel refuses to send |
46 | * - async_send_*(): Send asynchronously. If the kernel refuses to send |
47 | * more messages, [ try to get responses from kernel, if |
47 | * more messages, [ try to get responses from kernel, if |
48 | * nothing found, might try synchronous ] |
48 | * nothing found, might try synchronous ] |
49 | * |
49 | * |
50 | * Example of use (pseudo C): |
50 | * Example of use (pseudo C): |
51 | * |
51 | * |
52 | * 1) Multithreaded client application |
52 | * 1) Multithreaded client application |
53 | * |
53 | * |
54 | * fibril_create(fibril1, ...); |
54 | * fibril_create(fibril1, ...); |
55 | * fibril_create(fibril2, ...); |
55 | * fibril_create(fibril2, ...); |
56 | * ... |
56 | * ... |
57 | * |
57 | * |
58 | * int fibril1(void *arg) |
58 | * int fibril1(void *arg) |
59 | * { |
59 | * { |
60 | * conn = ipc_connect_me_to(); |
60 | * conn = ipc_connect_me_to(); |
61 | * c1 = async_send(conn); |
61 | * c1 = async_send(conn); |
62 | * c2 = async_send(conn); |
62 | * c2 = async_send(conn); |
63 | * async_wait_for(c1); |
63 | * async_wait_for(c1); |
64 | * async_wait_for(c2); |
64 | * async_wait_for(c2); |
65 | * ... |
65 | * ... |
66 | * } |
66 | * } |
67 | * |
67 | * |
68 | * |
68 | * |
69 | * 2) Multithreaded server application |
69 | * 2) Multithreaded server application |
70 | * |
70 | * |
71 | * main() |
71 | * main() |
72 | * { |
72 | * { |
73 | * async_manager(); |
73 | * async_manager(); |
74 | * } |
74 | * } |
75 | * |
75 | * |
76 | * my_client_connection(icallid, *icall) |
76 | * my_client_connection(icallid, *icall) |
77 | * { |
77 | * { |
78 | * if (want_refuse) { |
78 | * if (want_refuse) { |
79 | * ipc_answer_0(icallid, ELIMIT); |
79 | * ipc_answer_0(icallid, ELIMIT); |
80 | * return; |
80 | * return; |
81 | * } |
81 | * } |
82 | * ipc_answer_0(icallid, EOK); |
82 | * ipc_answer_0(icallid, EOK); |
83 | * |
83 | * |
84 | * callid = async_get_call(&call); |
84 | * callid = async_get_call(&call); |
85 | * handle_call(callid, call); |
85 | * handle_call(callid, call); |
86 | * ipc_answer_2(callid, 1, 2, 3); |
86 | * ipc_answer_2(callid, 1, 2, 3); |
87 | * |
87 | * |
88 | * callid = async_get_call(&call); |
88 | * callid = async_get_call(&call); |
89 | * ... |
89 | * ... |
90 | * } |
90 | * } |
91 | * |
91 | * |
92 | */ |
92 | */ |
93 | 93 | ||
94 | #include <futex.h> |
94 | #include <futex.h> |
95 | #include <async.h> |
95 | #include <async.h> |
96 | #include <fibril.h> |
96 | #include <fibril.h> |
97 | #include <stdio.h> |
97 | #include <stdio.h> |
98 | #include <libadt/hash_table.h> |
98 | #include <adt/hash_table.h> |
99 | #include <libadt/list.h> |
99 | #include <adt/list.h> |
100 | #include <ipc/ipc.h> |
100 | #include <ipc/ipc.h> |
101 | #include <assert.h> |
101 | #include <assert.h> |
102 | #include <errno.h> |
102 | #include <errno.h> |
103 | #include <sys/time.h> |
103 | #include <sys/time.h> |
104 | #include <arch/barrier.h> |
104 | #include <arch/barrier.h> |
105 | #include <bool.h> |
105 | #include <bool.h> |
106 | 106 | ||
107 | atomic_t async_futex = FUTEX_INITIALIZER; |
107 | atomic_t async_futex = FUTEX_INITIALIZER; |
108 | 108 | ||
109 | /** Structures of this type represent a waiting fibril. */ |
109 | /** Structures of this type represent a waiting fibril. */ |
110 | typedef struct { |
110 | typedef struct { |
111 | /** Expiration time. */ |
111 | /** Expiration time. */ |
112 | struct timeval expires; |
112 | struct timeval expires; |
113 | 113 | ||
114 | /** If true, this struct is in the timeout list. */ |
114 | /** If true, this struct is in the timeout list. */ |
115 | bool inlist; |
115 | bool inlist; |
116 | 116 | ||
117 | /** Timeout list link. */ |
117 | /** Timeout list link. */ |
118 | link_t link; |
118 | link_t link; |
119 | 119 | ||
120 | /** Identification of and link to the waiting fibril. */ |
120 | /** Identification of and link to the waiting fibril. */ |
121 | fid_t fid; |
121 | fid_t fid; |
122 | 122 | ||
123 | /** If true, this fibril is currently active. */ |
123 | /** If true, this fibril is currently active. */ |
124 | bool active; |
124 | bool active; |
125 | 125 | ||
126 | /** If true, we have timed out. */ |
126 | /** If true, we have timed out. */ |
127 | bool timedout; |
127 | bool timedout; |
128 | } awaiter_t; |
128 | } awaiter_t; |
129 | 129 | ||
130 | typedef struct { |
130 | typedef struct { |
131 | awaiter_t wdata; |
131 | awaiter_t wdata; |
132 | 132 | ||
133 | /** If reply was received. */ |
133 | /** If reply was received. */ |
134 | bool done; |
134 | bool done; |
135 | 135 | ||
136 | /** Pointer to where the answer data is stored. */ |
136 | /** Pointer to where the answer data is stored. */ |
137 | ipc_call_t *dataptr; |
137 | ipc_call_t *dataptr; |
138 | 138 | ||
139 | ipcarg_t retval; |
139 | ipcarg_t retval; |
140 | } amsg_t; |
140 | } amsg_t; |
141 | 141 | ||
142 | /** |
142 | /** |
143 | * Structures of this type are used to group information about a call and a |
143 | * Structures of this type are used to group information about a call and a |
144 | * message queue link. |
144 | * message queue link. |
145 | */ |
145 | */ |
146 | typedef struct { |
146 | typedef struct { |
147 | link_t link; |
147 | link_t link; |
148 | ipc_callid_t callid; |
148 | ipc_callid_t callid; |
149 | ipc_call_t call; |
149 | ipc_call_t call; |
150 | } msg_t; |
150 | } msg_t; |
151 | 151 | ||
152 | typedef struct { |
152 | typedef struct { |
153 | awaiter_t wdata; |
153 | awaiter_t wdata; |
154 | 154 | ||
155 | /** Hash table link. */ |
155 | /** Hash table link. */ |
156 | link_t link; |
156 | link_t link; |
157 | 157 | ||
158 | /** Incoming phone hash. */ |
158 | /** Incoming phone hash. */ |
159 | ipcarg_t in_phone_hash; |
159 | ipcarg_t in_phone_hash; |
160 | 160 | ||
161 | /** Messages that should be delivered to this fibril. */ |
161 | /** Messages that should be delivered to this fibril. */ |
162 | link_t msg_queue; |
162 | link_t msg_queue; |
163 | 163 | ||
164 | /** Identification of the opening call. */ |
164 | /** Identification of the opening call. */ |
165 | ipc_callid_t callid; |
165 | ipc_callid_t callid; |
166 | /** Call data of the opening call. */ |
166 | /** Call data of the opening call. */ |
167 | ipc_call_t call; |
167 | ipc_call_t call; |
168 | 168 | ||
169 | /** Identification of the closing call. */ |
169 | /** Identification of the closing call. */ |
170 | ipc_callid_t close_callid; |
170 | ipc_callid_t close_callid; |
171 | 171 | ||
172 | /** Fibril function that will be used to handle the connection. */ |
172 | /** Fibril function that will be used to handle the connection. */ |
173 | void (*cfibril)(ipc_callid_t, ipc_call_t *); |
173 | void (*cfibril)(ipc_callid_t, ipc_call_t *); |
174 | } connection_t; |
174 | } connection_t; |
175 | 175 | ||
176 | /** Identifier of the incoming connection handled by the current fibril. */ |
176 | /** Identifier of the incoming connection handled by the current fibril. */ |
177 | __thread connection_t *FIBRIL_connection; |
177 | fibril_local connection_t *FIBRIL_connection; |
178 | 178 | ||
179 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call); |
179 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call); |
180 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call); |
180 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call); |
- | 181 | static void default_pending(void); |
|
181 | 182 | ||
182 | /** |
183 | /** |
183 | * Pointer to a fibril function that will be used to handle connections. |
184 | * Pointer to a fibril function that will be used to handle connections. |
184 | */ |
185 | */ |
185 | static async_client_conn_t client_connection = default_client_connection; |
186 | static async_client_conn_t client_connection = default_client_connection; |
186 | 187 | ||
187 | /** |
188 | /** |
188 | * Pointer to a fibril function that will be used to handle interrupt |
189 | * Pointer to a fibril function that will be used to handle interrupt |
189 | * notifications. |
190 | * notifications. |
190 | */ |
191 | */ |
191 | static async_client_conn_t interrupt_received = default_interrupt_received; |
192 | static async_client_conn_t interrupt_received = default_interrupt_received; |
192 | 193 | ||
- | 194 | /** |
|
- | 195 | * Pointer to a fibril function that will be used to handle pending |
|
- | 196 | * operations. |
|
- | 197 | */ |
|
- | 198 | static async_pending_t pending = default_pending; |
|
193 | 199 | ||
194 | static hash_table_t conn_hash_table; |
200 | static hash_table_t conn_hash_table; |
195 | static LIST_INITIALIZE(timeout_list); |
201 | static LIST_INITIALIZE(timeout_list); |
196 | 202 | ||
197 | - | ||
198 | #define CONN_HASH_TABLE_CHAINS 32 |
203 | #define CONN_HASH_TABLE_CHAINS 32 |
199 | 204 | ||
200 | /** Compute hash into the connection hash table based on the source phone hash. |
205 | /** Compute hash into the connection hash table based on the source phone hash. |
201 | * |
206 | * |
202 | * @param key Pointer to source phone hash. |
207 | * @param key Pointer to source phone hash. |
203 | * |
208 | * |
204 | * @return Index into the connection hash table. |
209 | * @return Index into the connection hash table. |
205 | * |
210 | * |
206 | */ |
211 | */ |
207 | static hash_index_t conn_hash(unsigned long *key) |
212 | static hash_index_t conn_hash(unsigned long *key) |
208 | { |
213 | { |
209 | assert(key); |
214 | assert(key); |
210 | return (((*key) >> 4) % CONN_HASH_TABLE_CHAINS); |
215 | return (((*key) >> 4) % CONN_HASH_TABLE_CHAINS); |
211 | } |
216 | } |
212 | 217 | ||
213 | /** Compare hash table item with a key. |
218 | /** Compare hash table item with a key. |
214 | * |
219 | * |
215 | * @param key Array containing the source phone hash as the only item. |
220 | * @param key Array containing the source phone hash as the only item. |
216 | * @param keys Expected 1 but ignored. |
221 | * @param keys Expected 1 but ignored. |
217 | * @param item Connection hash table item. |
222 | * @param item Connection hash table item. |
218 | * |
223 | * |
219 | * @return True on match, false otherwise. |
224 | * @return True on match, false otherwise. |
220 | * |
225 | * |
221 | */ |
226 | */ |
222 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item) |
227 | static int conn_compare(unsigned long key[], hash_count_t keys, link_t *item) |
223 | { |
228 | { |
224 | connection_t *hs = hash_table_get_instance(item, connection_t, link); |
229 | connection_t *hs = hash_table_get_instance(item, connection_t, link); |
225 | return (key[0] == hs->in_phone_hash); |
230 | return (key[0] == hs->in_phone_hash); |
226 | } |
231 | } |
227 | 232 | ||
228 | /** Connection hash table removal callback function. |
233 | /** Connection hash table removal callback function. |
229 | * |
234 | * |
230 | * This function is called whenever a connection is removed from the connection |
235 | * This function is called whenever a connection is removed from the connection |
231 | * hash table. |
236 | * hash table. |
232 | * |
237 | * |
233 | * @param item Connection hash table item being removed. |
238 | * @param item Connection hash table item being removed. |
234 | * |
239 | * |
235 | */ |
240 | */ |
236 | static void conn_remove(link_t *item) |
241 | static void conn_remove(link_t *item) |
237 | { |
242 | { |
238 | free(hash_table_get_instance(item, connection_t, link)); |
243 | free(hash_table_get_instance(item, connection_t, link)); |
239 | } |
244 | } |
240 | 245 | ||
241 | 246 | ||
242 | /** Operations for the connection hash table. */ |
247 | /** Operations for the connection hash table. */ |
243 | static hash_table_operations_t conn_hash_table_ops = { |
248 | static hash_table_operations_t conn_hash_table_ops = { |
244 | .hash = conn_hash, |
249 | .hash = conn_hash, |
245 | .compare = conn_compare, |
250 | .compare = conn_compare, |
246 | .remove_callback = conn_remove |
251 | .remove_callback = conn_remove |
247 | }; |
252 | }; |
248 | 253 | ||
249 | /** Sort in current fibril's timeout request. |
254 | /** Sort in current fibril's timeout request. |
250 | * |
255 | * |
251 | * @param wd Wait data of the current fibril. |
256 | * @param wd Wait data of the current fibril. |
252 | * |
257 | * |
253 | */ |
258 | */ |
254 | static void insert_timeout(awaiter_t *wd) |
259 | static void insert_timeout(awaiter_t *wd) |
255 | { |
260 | { |
256 | wd->timedout = false; |
261 | wd->timedout = false; |
257 | wd->inlist = true; |
262 | wd->inlist = true; |
258 | 263 | ||
259 | link_t *tmp = timeout_list.next; |
264 | link_t *tmp = timeout_list.next; |
260 | while (tmp != &timeout_list) { |
265 | while (tmp != &timeout_list) { |
261 | awaiter_t *cur = list_get_instance(tmp, awaiter_t, link); |
266 | awaiter_t *cur = list_get_instance(tmp, awaiter_t, link); |
262 | 267 | ||
263 | if (tv_gteq(&cur->expires, &wd->expires)) |
268 | if (tv_gteq(&cur->expires, &wd->expires)) |
264 | break; |
269 | break; |
265 | 270 | ||
266 | tmp = tmp->next; |
271 | tmp = tmp->next; |
267 | } |
272 | } |
268 | 273 | ||
269 | list_append(&wd->link, tmp); |
274 | list_append(&wd->link, tmp); |
270 | } |
275 | } |
271 | 276 | ||
272 | /** Try to route a call to an appropriate connection fibril. |
277 | /** Try to route a call to an appropriate connection fibril. |
273 | * |
278 | * |
274 | * If the proper connection fibril is found, a message with the call is added to |
279 | * If the proper connection fibril is found, a message with the call is added to |
275 | * its message queue. If the fibril was not active, it is activated and all |
280 | * its message queue. If the fibril was not active, it is activated and all |
276 | * timeouts are unregistered. |
281 | * timeouts are unregistered. |
277 | * |
282 | * |
278 | * @param callid Hash of the incoming call. |
283 | * @param callid Hash of the incoming call. |
279 | * @param call Data of the incoming call. |
284 | * @param call Data of the incoming call. |
280 | * |
285 | * |
281 | * @return False if the call doesn't match any connection. |
286 | * @return False if the call doesn't match any connection. |
282 | * True if the call was passed to the respective connection fibril. |
287 | * True if the call was passed to the respective connection fibril. |
283 | * |
288 | * |
284 | */ |
289 | */ |
285 | static bool route_call(ipc_callid_t callid, ipc_call_t *call) |
290 | static bool route_call(ipc_callid_t callid, ipc_call_t *call) |
286 | { |
291 | { |
287 | futex_down(&async_futex); |
292 | futex_down(&async_futex); |
288 | 293 | ||
289 | unsigned long key = call->in_phone_hash; |
294 | unsigned long key = call->in_phone_hash; |
290 | link_t *hlp = hash_table_find(&conn_hash_table, &key); |
295 | link_t *hlp = hash_table_find(&conn_hash_table, &key); |
291 | 296 | ||
292 | if (!hlp) { |
297 | if (!hlp) { |
293 | futex_up(&async_futex); |
298 | futex_up(&async_futex); |
294 | return false; |
299 | return false; |
295 | } |
300 | } |
296 | 301 | ||
297 | connection_t *conn = hash_table_get_instance(hlp, connection_t, link); |
302 | connection_t *conn = hash_table_get_instance(hlp, connection_t, link); |
298 | 303 | ||
299 | msg_t *msg = malloc(sizeof(*msg)); |
304 | msg_t *msg = malloc(sizeof(*msg)); |
300 | if (!msg) { |
305 | if (!msg) { |
301 | futex_up(&async_futex); |
306 | futex_up(&async_futex); |
302 | return false; |
307 | return false; |
303 | } |
308 | } |
304 | 309 | ||
305 | msg->callid = callid; |
310 | msg->callid = callid; |
306 | msg->call = *call; |
311 | msg->call = *call; |
307 | list_append(&msg->link, &conn->msg_queue); |
312 | list_append(&msg->link, &conn->msg_queue); |
308 | 313 | ||
309 | if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP) |
314 | if (IPC_GET_METHOD(*call) == IPC_M_PHONE_HUNGUP) |
310 | conn->close_callid = callid; |
315 | conn->close_callid = callid; |
311 | 316 | ||
312 | /* If the connection fibril is waiting for an event, activate it */ |
317 | /* If the connection fibril is waiting for an event, activate it */ |
313 | if (!conn->wdata.active) { |
318 | if (!conn->wdata.active) { |
314 | 319 | ||
315 | /* If in timeout list, remove it */ |
320 | /* If in timeout list, remove it */ |
316 | if (conn->wdata.inlist) { |
321 | if (conn->wdata.inlist) { |
317 | conn->wdata.inlist = false; |
322 | conn->wdata.inlist = false; |
318 | list_remove(&conn->wdata.link); |
323 | list_remove(&conn->wdata.link); |
319 | } |
324 | } |
320 | 325 | ||
321 | conn->wdata.active = true; |
326 | conn->wdata.active = true; |
322 | fibril_add_ready(conn->wdata.fid); |
327 | fibril_add_ready(conn->wdata.fid); |
323 | } |
328 | } |
324 | 329 | ||
325 | futex_up(&async_futex); |
330 | futex_up(&async_futex); |
326 | return true; |
331 | return true; |
327 | } |
332 | } |
328 | 333 | ||
329 | /** Notification fibril. |
334 | /** Notification fibril. |
330 | * |
335 | * |
331 | * When a notification arrives, a fibril with this implementing function is |
336 | * When a notification arrives, a fibril with this implementing function is |
332 | * created. It calls interrupt_received() and does the final cleanup. |
337 | * created. It calls interrupt_received() and does the final cleanup. |
333 | * |
338 | * |
334 | * @param arg Message structure pointer. |
339 | * @param arg Message structure pointer. |
335 | * |
340 | * |
336 | * @return Always zero. |
341 | * @return Always zero. |
337 | * |
342 | * |
338 | */ |
343 | */ |
339 | static int notification_fibril(void *arg) |
344 | static int notification_fibril(void *arg) |
340 | { |
345 | { |
341 | msg_t *msg = (msg_t *) arg; |
346 | msg_t *msg = (msg_t *) arg; |
342 | interrupt_received(msg->callid, &msg->call); |
347 | interrupt_received(msg->callid, &msg->call); |
343 | 348 | ||
344 | free(msg); |
349 | free(msg); |
345 | return 0; |
350 | return 0; |
346 | } |
351 | } |
347 | 352 | ||
348 | /** Process interrupt notification. |
353 | /** Process interrupt notification. |
349 | * |
354 | * |
350 | * A new fibril is created which would process the notification. |
355 | * A new fibril is created which would process the notification. |
351 | * |
356 | * |
352 | * @param callid Hash of the incoming call. |
357 | * @param callid Hash of the incoming call. |
353 | * @param call Data of the incoming call. |
358 | * @param call Data of the incoming call. |
354 | * |
359 | * |
355 | * @return False if an error occured. |
360 | * @return False if an error occured. |
356 | * True if the call was passed to the notification fibril. |
361 | * True if the call was passed to the notification fibril. |
357 | * |
362 | * |
358 | */ |
363 | */ |
359 | static bool process_notification(ipc_callid_t callid, ipc_call_t *call) |
364 | static bool process_notification(ipc_callid_t callid, ipc_call_t *call) |
360 | { |
365 | { |
361 | futex_down(&async_futex); |
366 | futex_down(&async_futex); |
362 | 367 | ||
363 | msg_t *msg = malloc(sizeof(*msg)); |
368 | msg_t *msg = malloc(sizeof(*msg)); |
364 | if (!msg) { |
369 | if (!msg) { |
365 | futex_up(&async_futex); |
370 | futex_up(&async_futex); |
366 | return false; |
371 | return false; |
367 | } |
372 | } |
368 | 373 | ||
369 | msg->callid = callid; |
374 | msg->callid = callid; |
370 | msg->call = *call; |
375 | msg->call = *call; |
371 | 376 | ||
372 | fid_t fid = fibril_create(notification_fibril, msg); |
377 | fid_t fid = fibril_create(notification_fibril, msg); |
373 | fibril_add_ready(fid); |
378 | fibril_add_ready(fid); |
374 | 379 | ||
375 | futex_up(&async_futex); |
380 | futex_up(&async_futex); |
376 | return true; |
381 | return true; |
377 | } |
382 | } |
378 | 383 | ||
- | 384 | /** Pending fibril. |
|
- | 385 | * |
|
- | 386 | * After each call the pending operations are executed in a separate |
|
- | 387 | * fibril. The function pending() is c. |
|
- | 388 | * |
|
- | 389 | * @param arg Unused. |
|
- | 390 | * |
|
- | 391 | * @return Always zero. |
|
- | 392 | * |
|
- | 393 | */ |
|
- | 394 | static int pending_fibril(void *arg) |
|
- | 395 | { |
|
- | 396 | pending(); |
|
- | 397 | ||
- | 398 | return 0; |
|
- | 399 | } |
|
- | 400 | ||
- | 401 | /** Process pending actions. |
|
- | 402 | * |
|
- | 403 | * A new fibril is created which would process the pending operations. |
|
- | 404 | * |
|
- | 405 | * @return False if an error occured. |
|
- | 406 | * True if the execution was passed to the pending fibril. |
|
- | 407 | * |
|
- | 408 | */ |
|
- | 409 | static bool process_pending(void) |
|
- | 410 | { |
|
- | 411 | futex_down(&async_futex); |
|
- | 412 | ||
- | 413 | fid_t fid = fibril_create(pending_fibril, NULL); |
|
- | 414 | fibril_add_ready(fid); |
|
- | 415 | ||
- | 416 | futex_up(&async_futex); |
|
- | 417 | return true; |
|
- | 418 | } |
|
- | 419 | ||
379 | /** Return new incoming message for the current (fibril-local) connection. |
420 | /** Return new incoming message for the current (fibril-local) connection. |
380 | * |
421 | * |
381 | * @param call Storage where the incoming call data will be stored. |
422 | * @param call Storage where the incoming call data will be stored. |
382 | * @param usecs Timeout in microseconds. Zero denotes no timeout. |
423 | * @param usecs Timeout in microseconds. Zero denotes no timeout. |
383 | * |
424 | * |
384 | * @return If no timeout was specified, then a hash of the |
425 | * @return If no timeout was specified, then a hash of the |
385 | * incoming call is returned. If a timeout is specified, |
426 | * incoming call is returned. If a timeout is specified, |
386 | * then a hash of the incoming call is returned unless |
427 | * then a hash of the incoming call is returned unless |
387 | * the timeout expires prior to receiving a message. In |
428 | * the timeout expires prior to receiving a message. In |
388 | * that case zero is returned. |
429 | * that case zero is returned. |
389 | * |
430 | * |
390 | */ |
431 | */ |
391 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs) |
432 | ipc_callid_t async_get_call_timeout(ipc_call_t *call, suseconds_t usecs) |
392 | { |
433 | { |
393 | assert(FIBRIL_connection); |
434 | assert(FIBRIL_connection); |
394 | 435 | ||
395 | /* Why doing this? |
436 | /* Why doing this? |
396 | * GCC 4.1.0 coughs on FIBRIL_connection-> dereference. |
437 | * GCC 4.1.0 coughs on FIBRIL_connection-> dereference. |
397 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot. |
438 | * GCC 4.1.1 happilly puts the rdhwr instruction in delay slot. |
398 | * I would never expect to find so many errors in |
439 | * I would never expect to find so many errors in |
399 | * a compiler. |
440 | * a compiler. |
400 | */ |
441 | */ |
401 | connection_t *conn = FIBRIL_connection; |
442 | connection_t *conn = FIBRIL_connection; |
402 | 443 | ||
403 | futex_down(&async_futex); |
444 | futex_down(&async_futex); |
404 | 445 | ||
405 | if (usecs) { |
446 | if (usecs) { |
406 | gettimeofday(&conn->wdata.expires, NULL); |
447 | gettimeofday(&conn->wdata.expires, NULL); |
407 | tv_add(&conn->wdata.expires, usecs); |
448 | tv_add(&conn->wdata.expires, usecs); |
408 | } else |
449 | } else |
409 | conn->wdata.inlist = false; |
450 | conn->wdata.inlist = false; |
410 | 451 | ||
411 | /* If nothing in queue, wait until something arrives */ |
452 | /* If nothing in queue, wait until something arrives */ |
412 | while (list_empty(&conn->msg_queue)) { |
453 | while (list_empty(&conn->msg_queue)) { |
413 | if (usecs) |
454 | if (usecs) |
414 | insert_timeout(&conn->wdata); |
455 | insert_timeout(&conn->wdata); |
415 | 456 | ||
416 | conn->wdata.active = false; |
457 | conn->wdata.active = false; |
417 | 458 | ||
418 | /* |
459 | /* |
419 | * Note: the current fibril will be rescheduled either due to a |
460 | * Note: the current fibril will be rescheduled either due to a |
420 | * timeout or due to an arriving message destined to it. In the |
461 | * timeout or due to an arriving message destined to it. In the |
421 | * former case, handle_expired_timeouts() and, in the latter |
462 | * former case, handle_expired_timeouts() and, in the latter |
422 | * case, route_call() will perform the wakeup. |
463 | * case, route_call() will perform the wakeup. |
423 | */ |
464 | */ |
424 | fibril_switch(FIBRIL_TO_MANAGER); |
465 | fibril_switch(FIBRIL_TO_MANAGER); |
425 | 466 | ||
426 | /* |
467 | /* |
427 | * Futex is up after getting back from async_manager. |
468 | * Futex is up after getting back from async_manager. |
428 | * Get it again. |
469 | * Get it again. |
429 | */ |
470 | */ |
430 | futex_down(&async_futex); |
471 | futex_down(&async_futex); |
431 | if ((usecs) && (conn->wdata.timedout) |
472 | if ((usecs) && (conn->wdata.timedout) |
432 | && (list_empty(&conn->msg_queue))) { |
473 | && (list_empty(&conn->msg_queue))) { |
433 | /* If we timed out -> exit */ |
474 | /* If we timed out -> exit */ |
434 | futex_up(&async_futex); |
475 | futex_up(&async_futex); |
435 | return 0; |
476 | return 0; |
436 | } |
477 | } |
437 | } |
478 | } |
438 | 479 | ||
439 | msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link); |
480 | msg_t *msg = list_get_instance(conn->msg_queue.next, msg_t, link); |
440 | list_remove(&msg->link); |
481 | list_remove(&msg->link); |
441 | 482 | ||
442 | ipc_callid_t callid = msg->callid; |
483 | ipc_callid_t callid = msg->callid; |
443 | *call = msg->call; |
484 | *call = msg->call; |
444 | free(msg); |
485 | free(msg); |
445 | 486 | ||
446 | futex_up(&async_futex); |
487 | futex_up(&async_futex); |
447 | return callid; |
488 | return callid; |
448 | } |
489 | } |
449 | 490 | ||
450 | /** Default fibril function that gets called to handle new connection. |
491 | /** Default fibril function that gets called to handle new connection. |
451 | * |
492 | * |
452 | * This function is defined as a weak symbol - to be redefined in user code. |
493 | * This function is defined as a weak symbol - to be redefined in user code. |
453 | * |
494 | * |
454 | * @param callid Hash of the incoming call. |
495 | * @param callid Hash of the incoming call. |
455 | * @param call Data of the incoming call. |
496 | * @param call Data of the incoming call. |
456 | * |
497 | * |
457 | */ |
498 | */ |
458 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call) |
499 | static void default_client_connection(ipc_callid_t callid, ipc_call_t *call) |
459 | { |
500 | { |
460 | ipc_answer_0(callid, ENOENT); |
501 | ipc_answer_0(callid, ENOENT); |
461 | } |
502 | } |
462 | 503 | ||
463 | /** Default fibril function that gets called to handle interrupt notifications. |
504 | /** Default fibril function that gets called to handle interrupt notifications. |
464 | * |
505 | * |
465 | * This function is defined as a weak symbol - to be redefined in user code. |
506 | * This function is defined as a weak symbol - to be redefined in user code. |
466 | * |
507 | * |
467 | * @param callid Hash of the incoming call. |
508 | * @param callid Hash of the incoming call. |
468 | * @param call Data of the incoming call. |
509 | * @param call Data of the incoming call. |
469 | * |
510 | * |
470 | */ |
511 | */ |
471 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call) |
512 | static void default_interrupt_received(ipc_callid_t callid, ipc_call_t *call) |
472 | { |
513 | { |
473 | } |
514 | } |
474 | 515 | ||
- | 516 | /** Default fibril function that gets called to handle pending operations. |
|
- | 517 | * |
|
- | 518 | * This function is defined as a weak symbol - to be redefined in user code. |
|
- | 519 | * |
|
- | 520 | */ |
|
- | 521 | static void default_pending(void) |
|
- | 522 | { |
|
- | 523 | } |
|
- | 524 | ||
475 | /** Wrapper for client connection fibril. |
525 | /** Wrapper for client connection fibril. |
476 | * |
526 | * |
477 | * When a new connection arrives, a fibril with this implementing function is |
527 | * When a new connection arrives, a fibril with this implementing function is |
478 | * created. It calls client_connection() and does the final cleanup. |
528 | * created. It calls client_connection() and does the final cleanup. |
479 | * |
529 | * |
480 | * @param arg Connection structure pointer. |
530 | * @param arg Connection structure pointer. |
481 | * |
531 | * |
482 | * @return Always zero. |
532 | * @return Always zero. |
483 | * |
533 | * |
484 | */ |
534 | */ |
485 | static int connection_fibril(void *arg) |
535 | static int connection_fibril(void *arg) |
486 | { |
536 | { |
487 | /* |
537 | /* |
488 | * Setup fibril-local connection pointer and call client_connection(). |
538 | * Setup fibril-local connection pointer and call client_connection(). |
489 | * |
539 | * |
490 | */ |
540 | */ |
491 | FIBRIL_connection = (connection_t *) arg; |
541 | FIBRIL_connection = (connection_t *) arg; |
492 | FIBRIL_connection->cfibril(FIBRIL_connection->callid, |
542 | FIBRIL_connection->cfibril(FIBRIL_connection->callid, |
493 | &FIBRIL_connection->call); |
543 | &FIBRIL_connection->call); |
494 | 544 | ||
495 | /* Remove myself from the connection hash table */ |
545 | /* Remove myself from the connection hash table */ |
496 | futex_down(&async_futex); |
546 | futex_down(&async_futex); |
497 | unsigned long key = FIBRIL_connection->in_phone_hash; |
547 | unsigned long key = FIBRIL_connection->in_phone_hash; |
498 | hash_table_remove(&conn_hash_table, &key, 1); |
548 | hash_table_remove(&conn_hash_table, &key, 1); |
499 | futex_up(&async_futex); |
549 | futex_up(&async_futex); |
500 | 550 | ||
501 | /* Answer all remaining messages with EHANGUP */ |
551 | /* Answer all remaining messages with EHANGUP */ |
502 | while (!list_empty(&FIBRIL_connection->msg_queue)) { |
552 | while (!list_empty(&FIBRIL_connection->msg_queue)) { |
503 | msg_t *msg |
553 | msg_t *msg; |
504 | = list_get_instance(FIBRIL_connection->msg_queue.next, msg_t, link); |
- | |
505 | 554 | ||
- | 555 | msg = list_get_instance(FIBRIL_connection->msg_queue.next, |
|
- | 556 | msg_t, link); |
|
506 | list_remove(&msg->link); |
557 | list_remove(&msg->link); |
507 | ipc_answer_0(msg->callid, EHANGUP); |
558 | ipc_answer_0(msg->callid, EHANGUP); |
508 | free(msg); |
559 | free(msg); |
509 | } |
560 | } |
510 | 561 | ||
511 | if (FIBRIL_connection->close_callid) |
562 | if (FIBRIL_connection->close_callid) |
512 | ipc_answer_0(FIBRIL_connection->close_callid, EOK); |
563 | ipc_answer_0(FIBRIL_connection->close_callid, EOK); |
513 | 564 | ||
514 | return 0; |
565 | return 0; |
515 | } |
566 | } |
516 | 567 | ||
517 | /** Create a new fibril for a new connection. |
568 | /** Create a new fibril for a new connection. |
518 | * |
569 | * |
519 | * Create new fibril for connection, fill in connection structures and inserts |
570 | * Create new fibril for connection, fill in connection structures and inserts |
520 | * it into the hash table, so that later we can easily do routing of messages to |
571 | * it into the hash table, so that later we can easily do routing of messages to |
521 | * particular fibrils. |
572 | * particular fibrils. |
522 | * |
573 | * |
523 | * @param in_phone_hash Identification of the incoming connection. |
574 | * @param in_phone_hash Identification of the incoming connection. |
524 | * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call. |
575 | * @param callid Hash of the opening IPC_M_CONNECT_ME_TO call. |
525 | * If callid is zero, the connection was opened by |
576 | * If callid is zero, the connection was opened by |
526 | * accepting the IPC_M_CONNECT_TO_ME call and this function |
577 | * accepting the IPC_M_CONNECT_TO_ME call and this function |
527 | * is called directly by the server. |
578 | * is called directly by the server. |
528 | * @param call Call data of the opening call. |
579 | * @param call Call data of the opening call. |
529 | * @param cfibril Fibril function that should be called upon opening the |
580 | * @param cfibril Fibril function that should be called upon opening the |
530 | * connection. |
581 | * connection. |
531 | * |
582 | * |
532 | * @return New fibril id or NULL on failure. |
583 | * @return New fibril id or NULL on failure. |
533 | * |
584 | * |
534 | */ |
585 | */ |
535 | fid_t async_new_connection(ipcarg_t in_phone_hash, ipc_callid_t callid, |
586 | fid_t async_new_connection(ipcarg_t in_phone_hash, ipc_callid_t callid, |
536 | ipc_call_t *call, void (*cfibril)(ipc_callid_t, ipc_call_t *)) |
587 | ipc_call_t *call, void (*cfibril)(ipc_callid_t, ipc_call_t *)) |
537 | { |
588 | { |
538 | connection_t *conn = malloc(sizeof(*conn)); |
589 | connection_t *conn = malloc(sizeof(*conn)); |
539 | if (!conn) { |
590 | if (!conn) { |
540 | if (callid) |
591 | if (callid) |
541 | ipc_answer_0(callid, ENOMEM); |
592 | ipc_answer_0(callid, ENOMEM); |
542 | return NULL; |
593 | return NULL; |
543 | } |
594 | } |
544 | 595 | ||
545 | conn->in_phone_hash = in_phone_hash; |
596 | conn->in_phone_hash = in_phone_hash; |
546 | list_initialize(&conn->msg_queue); |
597 | list_initialize(&conn->msg_queue); |
547 | conn->callid = callid; |
598 | conn->callid = callid; |
548 | conn->close_callid = false; |
599 | conn->close_callid = false; |
549 | 600 | ||
550 | if (call) |
601 | if (call) |
551 | conn->call = *call; |
602 | conn->call = *call; |
552 | 603 | ||
553 | /* We will activate the fibril ASAP */ |
604 | /* We will activate the fibril ASAP */ |
554 | conn->wdata.active = true; |
605 | conn->wdata.active = true; |
555 | conn->cfibril = cfibril; |
606 | conn->cfibril = cfibril; |
556 | conn->wdata.fid = fibril_create(connection_fibril, conn); |
607 | conn->wdata.fid = fibril_create(connection_fibril, conn); |
557 | 608 | ||
558 | if (!conn->wdata.fid) { |
609 | if (!conn->wdata.fid) { |
559 | free(conn); |
610 | free(conn); |
560 | if (callid) |
611 | if (callid) |
561 | ipc_answer_0(callid, ENOMEM); |
612 | ipc_answer_0(callid, ENOMEM); |
562 | return NULL; |
613 | return NULL; |
563 | } |
614 | } |
564 | 615 | ||
565 | /* Add connection to the connection hash table */ |
616 | /* Add connection to the connection hash table */ |
566 | ipcarg_t key = conn->in_phone_hash; |
617 | unsigned long key = conn->in_phone_hash; |
567 | 618 | ||
568 | futex_down(&async_futex); |
619 | futex_down(&async_futex); |
569 | hash_table_insert(&conn_hash_table, &key, &conn->link); |
620 | hash_table_insert(&conn_hash_table, &key, &conn->link); |
570 | futex_up(&async_futex); |
621 | futex_up(&async_futex); |
571 | 622 | ||
572 | fibril_add_ready(conn->wdata.fid); |
623 | fibril_add_ready(conn->wdata.fid); |
573 | 624 | ||
574 | return conn->wdata.fid; |
625 | return conn->wdata.fid; |
575 | } |
626 | } |
576 | 627 | ||
577 | /** Handle a call that was received. |
628 | /** Handle a call that was received. |
578 | * |
629 | * |
579 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created. |
630 | * If the call has the IPC_M_CONNECT_ME_TO method, a new connection is created. |
580 | * Otherwise the call is routed to its connection fibril. |
631 | * Otherwise the call is routed to its connection fibril. |
581 | * |
632 | * |
582 | * @param callid Hash of the incoming call. |
633 | * @param callid Hash of the incoming call. |
583 | * @param call Data of the incoming call. |
634 | * @param call Data of the incoming call. |
584 | * |
635 | * |
585 | */ |
636 | */ |
586 | static void handle_call(ipc_callid_t callid, ipc_call_t *call) |
637 | static void handle_call(ipc_callid_t callid, ipc_call_t *call) |
587 | { |
638 | { |
588 | /* Unrouted call - do some default behaviour */ |
639 | /* Unrouted call - do some default behaviour */ |
589 | if ((callid & IPC_CALLID_NOTIFICATION)) { |
640 | if ((callid & IPC_CALLID_NOTIFICATION)) { |
590 | process_notification(callid, call); |
641 | process_notification(callid, call); |
591 | return; |
642 | goto out; |
592 | } |
643 | } |
593 | 644 | ||
594 | switch (IPC_GET_METHOD(*call)) { |
645 | switch (IPC_GET_METHOD(*call)) { |
595 | case IPC_M_CONNECT_ME: |
646 | case IPC_M_CONNECT_ME: |
596 | case IPC_M_CONNECT_ME_TO: |
647 | case IPC_M_CONNECT_ME_TO: |
597 | /* Open new connection with fibril etc. */ |
648 | /* Open new connection with fibril etc. */ |
598 | async_new_connection(IPC_GET_ARG5(*call), callid, call, |
649 | async_new_connection(IPC_GET_ARG5(*call), callid, call, |
599 | client_connection); |
650 | client_connection); |
600 | return; |
651 | goto out; |
601 | } |
652 | } |
602 | 653 | ||
603 | /* Try to route the call through the connection hash table */ |
654 | /* Try to route the call through the connection hash table */ |
604 | if (route_call(callid, call)) |
655 | if (route_call(callid, call)) |
605 | return; |
656 | goto out; |
606 | 657 | ||
607 | /* Unknown call from unknown phone - hang it up */ |
658 | /* Unknown call from unknown phone - hang it up */ |
608 | ipc_answer_0(callid, EHANGUP); |
659 | ipc_answer_0(callid, EHANGUP); |
- | 660 | return; |
|
- | 661 | ||
- | 662 | out: |
|
- | 663 | process_pending(); |
|
609 | } |
664 | } |
610 | 665 | ||
611 | /** Fire all timeouts that expired. */ |
666 | /** Fire all timeouts that expired. */ |
612 | static void handle_expired_timeouts(void) |
667 | static void handle_expired_timeouts(void) |
613 | { |
668 | { |
614 | struct timeval tv; |
669 | struct timeval tv; |
615 | gettimeofday(&tv, NULL); |
670 | gettimeofday(&tv, NULL); |
616 | 671 | ||
617 | futex_down(&async_futex); |
672 | futex_down(&async_futex); |
618 | 673 | ||
619 | link_t *cur = timeout_list.next; |
674 | link_t *cur = timeout_list.next; |
620 | while (cur != &timeout_list) { |
675 | while (cur != &timeout_list) { |
621 | awaiter_t *waiter = list_get_instance(cur, awaiter_t, link); |
676 | awaiter_t *waiter = list_get_instance(cur, awaiter_t, link); |
622 | 677 | ||
623 | if (tv_gt(&waiter->expires, &tv)) |
678 | if (tv_gt(&waiter->expires, &tv)) |
624 | break; |
679 | break; |
625 | 680 | ||
626 | cur = cur->next; |
681 | cur = cur->next; |
627 | 682 | ||
628 | list_remove(&waiter->link); |
683 | list_remove(&waiter->link); |
629 | waiter->inlist = false; |
684 | waiter->inlist = false; |
630 | waiter->timedout = true; |
685 | waiter->timedout = true; |
631 | 686 | ||
632 | /* |
687 | /* |
633 | * Redundant condition? |
688 | * Redundant condition? |
634 | * The fibril should not be active when it gets here. |
689 | * The fibril should not be active when it gets here. |
635 | */ |
690 | */ |
636 | if (!waiter->active) { |
691 | if (!waiter->active) { |
637 | waiter->active = true; |
692 | waiter->active = true; |
638 | fibril_add_ready(waiter->fid); |
693 | fibril_add_ready(waiter->fid); |
639 | } |
694 | } |
640 | } |
695 | } |
641 | 696 | ||
642 | futex_up(&async_futex); |
697 | futex_up(&async_futex); |
643 | } |
698 | } |
644 | 699 | ||
645 | /** Endless loop dispatching incoming calls and answers. |
700 | /** Endless loop dispatching incoming calls and answers. |
646 | * |
701 | * |
647 | * @return Never returns. |
702 | * @return Never returns. |
648 | * |
703 | * |
649 | */ |
704 | */ |
650 | static int async_manager_worker(void) |
705 | static int async_manager_worker(void) |
651 | { |
706 | { |
652 | while (true) { |
707 | while (true) { |
653 | if (fibril_switch(FIBRIL_FROM_MANAGER)) { |
708 | if (fibril_switch(FIBRIL_FROM_MANAGER)) { |
654 | futex_up(&async_futex); |
709 | futex_up(&async_futex); |
655 | /* |
710 | /* |
656 | * async_futex is always held when entering a manager |
711 | * async_futex is always held when entering a manager |
657 | * fibril. |
712 | * fibril. |
658 | */ |
713 | */ |
659 | continue; |
714 | continue; |
660 | } |
715 | } |
661 | 716 | ||
662 | futex_down(&async_futex); |
717 | futex_down(&async_futex); |
663 | 718 | ||
664 | suseconds_t timeout; |
719 | suseconds_t timeout; |
665 | if (!list_empty(&timeout_list)) { |
720 | if (!list_empty(&timeout_list)) { |
666 | awaiter_t *waiter |
721 | awaiter_t *waiter = list_get_instance(timeout_list.next, |
667 | = list_get_instance(timeout_list.next, awaiter_t, link); |
722 | awaiter_t, link); |
668 | 723 | ||
669 | struct timeval tv; |
724 | struct timeval tv; |
670 | gettimeofday(&tv, NULL); |
725 | gettimeofday(&tv, NULL); |
671 | 726 | ||
672 | if (tv_gteq(&tv, &waiter->expires)) { |
727 | if (tv_gteq(&tv, &waiter->expires)) { |
673 | futex_up(&async_futex); |
728 | futex_up(&async_futex); |
674 | handle_expired_timeouts(); |
729 | handle_expired_timeouts(); |
675 | continue; |
730 | continue; |
676 | } else |
731 | } else |
677 | timeout = tv_sub(&waiter->expires, &tv); |
732 | timeout = tv_sub(&waiter->expires, &tv); |
678 | } else |
733 | } else |
679 | timeout = SYNCH_NO_TIMEOUT; |
734 | timeout = SYNCH_NO_TIMEOUT; |
680 | 735 | ||
681 | futex_up(&async_futex); |
736 | futex_up(&async_futex); |
682 | 737 | ||
683 | ipc_call_t call; |
738 | ipc_call_t call; |
684 | ipc_callid_t callid |
739 | ipc_callid_t callid = ipc_wait_cycle(&call, timeout, |
685 | = ipc_wait_cycle(&call, timeout, SYNCH_FLAGS_NONE); |
740 | SYNCH_FLAGS_NONE); |
686 | 741 | ||
687 | if (!callid) { |
742 | if (!callid) { |
688 | handle_expired_timeouts(); |
743 | handle_expired_timeouts(); |
689 | continue; |
744 | continue; |
690 | } |
745 | } |
691 | 746 | ||
692 | if (callid & IPC_CALLID_ANSWERED) |
747 | if (callid & IPC_CALLID_ANSWERED) |
693 | continue; |
748 | continue; |
694 | 749 | ||
695 | handle_call(callid, &call); |
750 | handle_call(callid, &call); |
696 | } |
751 | } |
697 | 752 | ||
698 | return 0; |
753 | return 0; |
699 | } |
754 | } |
700 | 755 | ||
701 | /** Function to start async_manager as a standalone fibril. |
756 | /** Function to start async_manager as a standalone fibril. |
702 | * |
757 | * |
703 | * When more kernel threads are used, one async manager should exist per thread. |
758 | * When more kernel threads are used, one async manager should exist per thread. |
704 | * |
759 | * |
705 | * @param arg Unused. |
760 | * @param arg Unused. |
706 | * @return Never returns. |
761 | * @return Never returns. |
707 | * |
762 | * |
708 | */ |
763 | */ |
709 | static int async_manager_fibril(void *arg) |
764 | static int async_manager_fibril(void *arg) |
710 | { |
765 | { |
711 | futex_up(&async_futex); |
766 | futex_up(&async_futex); |
712 | 767 | ||
713 | /* |
768 | /* |
714 | * async_futex is always locked when entering manager |
769 | * async_futex is always locked when entering manager |
715 | */ |
770 | */ |
716 | async_manager_worker(); |
771 | async_manager_worker(); |
717 | 772 | ||
718 | return 0; |
773 | return 0; |
719 | } |
774 | } |
720 | 775 | ||
721 | /** Add one manager to manager list. */ |
776 | /** Add one manager to manager list. */ |
722 | void async_create_manager(void) |
777 | void async_create_manager(void) |
723 | { |
778 | { |
724 | fid_t fid = fibril_create(async_manager_fibril, NULL); |
779 | fid_t fid = fibril_create(async_manager_fibril, NULL); |
725 | fibril_add_manager(fid); |
780 | fibril_add_manager(fid); |
726 | } |
781 | } |
727 | 782 | ||
728 | /** Remove one manager from manager list */ |
783 | /** Remove one manager from manager list */ |
729 | void async_destroy_manager(void) |
784 | void async_destroy_manager(void) |
730 | { |
785 | { |
731 | fibril_remove_manager(); |
786 | fibril_remove_manager(); |
732 | } |
787 | } |
733 | 788 | ||
734 | /** Initialize the async framework. |
789 | /** Initialize the async framework. |
735 | * |
790 | * |
736 | * @return Zero on success or an error code. |
791 | * @return Zero on success or an error code. |
737 | */ |
792 | */ |
738 | int _async_init(void) |
793 | int _async_init(void) |
739 | { |
794 | { |
740 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1, |
795 | if (!hash_table_create(&conn_hash_table, CONN_HASH_TABLE_CHAINS, 1, |
741 | &conn_hash_table_ops)) { |
796 | &conn_hash_table_ops)) { |
742 | printf("%s: cannot create hash table\n", "async"); |
797 | printf("%s: cannot create hash table\n", "async"); |
743 | return ENOMEM; |
798 | return ENOMEM; |
744 | } |
799 | } |
745 | 800 | ||
746 | return 0; |
801 | return 0; |
747 | } |
802 | } |
748 | 803 | ||
749 | /** Reply received callback. |
804 | /** Reply received callback. |
750 | * |
805 | * |
751 | * This function is called whenever a reply for an asynchronous message sent out |
806 | * This function is called whenever a reply for an asynchronous message sent out |
752 | * by the asynchronous framework is received. |
807 | * by the asynchronous framework is received. |
753 | * |
808 | * |
754 | * Notify the fibril which is waiting for this message that it has arrived. |
809 | * Notify the fibril which is waiting for this message that it has arrived. |
755 | * |
810 | * |
756 | * @param arg Pointer to the asynchronous message record. |
811 | * @param arg Pointer to the asynchronous message record. |
757 | * @param retval Value returned in the answer. |
812 | * @param retval Value returned in the answer. |
758 | * @param data Call data of the answer. |
813 | * @param data Call data of the answer. |
759 | */ |
814 | */ |
760 | static void reply_received(void *arg, int retval, ipc_call_t *data) |
815 | static void reply_received(void *arg, int retval, ipc_call_t *data) |
761 | { |
816 | { |
- | 817 | futex_down(&async_futex); |
|
- | 818 | ||
762 | amsg_t *msg = (amsg_t *) arg; |
819 | amsg_t *msg = (amsg_t *) arg; |
763 | msg->retval = retval; |
820 | msg->retval = retval; |
764 | 821 | ||
765 | futex_down(&async_futex); |
- | |
766 | - | ||
767 | /* Copy data after futex_down, just in case the call was detached */ |
822 | /* Copy data after futex_down, just in case the call was detached */ |
768 | if (msg->dataptr) |
823 | if ((msg->dataptr) && (data)) |
769 | *msg->dataptr = *data; |
824 | *msg->dataptr = *data; |
770 | 825 | ||
771 | write_barrier(); |
826 | write_barrier(); |
772 | 827 | ||
773 | /* Remove message from timeout list */ |
828 | /* Remove message from timeout list */ |
774 | if (msg->wdata.inlist) |
829 | if (msg->wdata.inlist) |
775 | list_remove(&msg->wdata.link); |
830 | list_remove(&msg->wdata.link); |
776 | 831 | ||
777 | msg->done = true; |
832 | msg->done = true; |
778 | if (!msg->wdata.active) { |
833 | if (!msg->wdata.active) { |
779 | msg->wdata.active = true; |
834 | msg->wdata.active = true; |
780 | fibril_add_ready(msg->wdata.fid); |
835 | fibril_add_ready(msg->wdata.fid); |
781 | } |
836 | } |
782 | 837 | ||
783 | futex_up(&async_futex); |
838 | futex_up(&async_futex); |
784 | } |
839 | } |
785 | 840 | ||
786 | /** Send message and return id of the sent message. |
841 | /** Send message and return id of the sent message. |
787 | * |
842 | * |
788 | * The return value can be used as input for async_wait() to wait for |
843 | * The return value can be used as input for async_wait() to wait for |
789 | * completion. |
844 | * completion. |
790 | * |
845 | * |
791 | * @param phoneid Handle of the phone that will be used for the send. |
846 | * @param phoneid Handle of the phone that will be used for the send. |
792 | * @param method Service-defined method. |
847 | * @param method Service-defined method. |
793 | * @param arg1 Service-defined payload argument. |
848 | * @param arg1 Service-defined payload argument. |
794 | * @param arg2 Service-defined payload argument. |
849 | * @param arg2 Service-defined payload argument. |
795 | * @param arg3 Service-defined payload argument. |
850 | * @param arg3 Service-defined payload argument. |
796 | * @param arg4 Service-defined payload argument. |
851 | * @param arg4 Service-defined payload argument. |
797 | * @param dataptr If non-NULL, storage where the reply data will be |
852 | * @param dataptr If non-NULL, storage where the reply data will be |
798 | * stored. |
853 | * stored. |
799 | * |
854 | * |
800 | * @return Hash of the sent message or 0 on error. |
855 | * @return Hash of the sent message or 0 on error. |
801 | * |
856 | * |
802 | */ |
857 | */ |
803 | aid_t async_send_fast(int phoneid, ipcarg_t method, ipcarg_t arg1, |
858 | aid_t async_send_fast(int phoneid, ipcarg_t method, ipcarg_t arg1, |
804 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr) |
859 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipc_call_t *dataptr) |
805 | { |
860 | { |
806 | amsg_t *msg = malloc(sizeof(*msg)); |
861 | amsg_t *msg = malloc(sizeof(*msg)); |
807 | 862 | ||
808 | if (!msg) |
863 | if (!msg) |
809 | return 0; |
864 | return 0; |
810 | 865 | ||
811 | msg->done = false; |
866 | msg->done = false; |
812 | msg->dataptr = dataptr; |
867 | msg->dataptr = dataptr; |
813 | 868 | ||
814 | msg->wdata.inlist = false; |
869 | msg->wdata.inlist = false; |
815 | /* We may sleep in the next method, but it will use its own mechanism */ |
870 | /* We may sleep in the next method, but it will use its own mechanism */ |
816 | msg->wdata.active = true; |
871 | msg->wdata.active = true; |
817 | 872 | ||
818 | ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg, |
873 | ipc_call_async_4(phoneid, method, arg1, arg2, arg3, arg4, msg, |
819 | reply_received, true); |
874 | reply_received, true); |
820 | 875 | ||
821 | return (aid_t) msg; |
876 | return (aid_t) msg; |
822 | } |
877 | } |
823 | 878 | ||
824 | /** Send message and return id of the sent message |
879 | /** Send message and return id of the sent message |
825 | * |
880 | * |
826 | * The return value can be used as input for async_wait() to wait for |
881 | * The return value can be used as input for async_wait() to wait for |
827 | * completion. |
882 | * completion. |
828 | * |
883 | * |
829 | * @param phoneid Handle of the phone that will be used for the send. |
884 | * @param phoneid Handle of the phone that will be used for the send. |
830 | * @param method Service-defined method. |
885 | * @param method Service-defined method. |
831 | * @param arg1 Service-defined payload argument. |
886 | * @param arg1 Service-defined payload argument. |
832 | * @param arg2 Service-defined payload argument. |
887 | * @param arg2 Service-defined payload argument. |
833 | * @param arg3 Service-defined payload argument. |
888 | * @param arg3 Service-defined payload argument. |
834 | * @param arg4 Service-defined payload argument. |
889 | * @param arg4 Service-defined payload argument. |
835 | * @param arg5 Service-defined payload argument. |
890 | * @param arg5 Service-defined payload argument. |
836 | * @param dataptr If non-NULL, storage where the reply data will be |
891 | * @param dataptr If non-NULL, storage where the reply data will be |
837 | * stored. |
892 | * stored. |
838 | * |
893 | * |
839 | * @return Hash of the sent message or 0 on error. |
894 | * @return Hash of the sent message or 0 on error. |
840 | * |
895 | * |
841 | */ |
896 | */ |
842 | aid_t async_send_slow(int phoneid, ipcarg_t method, ipcarg_t arg1, |
897 | aid_t async_send_slow(int phoneid, ipcarg_t method, ipcarg_t arg1, |
843 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, |
898 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, |
844 | ipc_call_t *dataptr) |
899 | ipc_call_t *dataptr) |
845 | { |
900 | { |
846 | amsg_t *msg = malloc(sizeof(*msg)); |
901 | amsg_t *msg = malloc(sizeof(*msg)); |
847 | 902 | ||
848 | if (!msg) |
903 | if (!msg) |
849 | return 0; |
904 | return 0; |
850 | 905 | ||
851 | msg->done = false; |
906 | msg->done = false; |
852 | msg->dataptr = dataptr; |
907 | msg->dataptr = dataptr; |
853 | 908 | ||
854 | msg->wdata.inlist = false; |
909 | msg->wdata.inlist = false; |
855 | /* We may sleep in next method, but it will use its own mechanism */ |
910 | /* We may sleep in next method, but it will use its own mechanism */ |
856 | msg->wdata.active = true; |
911 | msg->wdata.active = true; |
857 | 912 | ||
858 | ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg, |
913 | ipc_call_async_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, msg, |
859 | reply_received, true); |
914 | reply_received, true); |
860 | 915 | ||
861 | return (aid_t) msg; |
916 | return (aid_t) msg; |
862 | } |
917 | } |
863 | 918 | ||
864 | /** Wait for a message sent by the async framework. |
919 | /** Wait for a message sent by the async framework. |
865 | * |
920 | * |
866 | * @param amsgid Hash of the message to wait for. |
921 | * @param amsgid Hash of the message to wait for. |
867 | * @param retval Pointer to storage where the retval of the answer will |
922 | * @param retval Pointer to storage where the retval of the answer will |
868 | * be stored. |
923 | * be stored. |
869 | * |
924 | * |
870 | */ |
925 | */ |
871 | void async_wait_for(aid_t amsgid, ipcarg_t *retval) |
926 | void async_wait_for(aid_t amsgid, ipcarg_t *retval) |
872 | { |
927 | { |
873 | amsg_t *msg = (amsg_t *) amsgid; |
928 | amsg_t *msg = (amsg_t *) amsgid; |
874 | 929 | ||
875 | futex_down(&async_futex); |
930 | futex_down(&async_futex); |
876 | if (msg->done) { |
931 | if (msg->done) { |
877 | futex_up(&async_futex); |
932 | futex_up(&async_futex); |
878 | goto done; |
933 | goto done; |
879 | } |
934 | } |
880 | 935 | ||
881 | msg->wdata.fid = fibril_get_id(); |
936 | msg->wdata.fid = fibril_get_id(); |
882 | msg->wdata.active = false; |
937 | msg->wdata.active = false; |
883 | msg->wdata.inlist = false; |
938 | msg->wdata.inlist = false; |
884 | 939 | ||
885 | /* Leave the async_futex locked when entering this function */ |
940 | /* Leave the async_futex locked when entering this function */ |
886 | fibril_switch(FIBRIL_TO_MANAGER); |
941 | fibril_switch(FIBRIL_TO_MANAGER); |
887 | 942 | ||
888 | /* Futex is up automatically after fibril_switch */ |
943 | /* Futex is up automatically after fibril_switch */ |
889 | 944 | ||
890 | done: |
945 | done: |
891 | if (retval) |
946 | if (retval) |
892 | *retval = msg->retval; |
947 | *retval = msg->retval; |
893 | 948 | ||
894 | free(msg); |
949 | free(msg); |
895 | } |
950 | } |
896 | 951 | ||
897 | /** Wait for a message sent by the async framework, timeout variant. |
952 | /** Wait for a message sent by the async framework, timeout variant. |
898 | * |
953 | * |
899 | * @param amsgid Hash of the message to wait for. |
954 | * @param amsgid Hash of the message to wait for. |
900 | * @param retval Pointer to storage where the retval of the answer will |
955 | * @param retval Pointer to storage where the retval of the answer will |
901 | * be stored. |
956 | * be stored. |
902 | * @param timeout Timeout in microseconds. |
957 | * @param timeout Timeout in microseconds. |
903 | * |
958 | * |
904 | * @return Zero on success, ETIMEOUT if the timeout has expired. |
959 | * @return Zero on success, ETIMEOUT if the timeout has expired. |
905 | * |
960 | * |
906 | */ |
961 | */ |
907 | int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout) |
962 | int async_wait_timeout(aid_t amsgid, ipcarg_t *retval, suseconds_t timeout) |
908 | { |
963 | { |
909 | amsg_t *msg = (amsg_t *) amsgid; |
964 | amsg_t *msg = (amsg_t *) amsgid; |
910 | 965 | ||
911 | /* TODO: Let it go through the event read at least once */ |
966 | /* TODO: Let it go through the event read at least once */ |
912 | if (timeout < 0) |
967 | if (timeout < 0) |
913 | return ETIMEOUT; |
968 | return ETIMEOUT; |
914 | 969 | ||
915 | futex_down(&async_futex); |
970 | futex_down(&async_futex); |
916 | if (msg->done) { |
971 | if (msg->done) { |
917 | futex_up(&async_futex); |
972 | futex_up(&async_futex); |
918 | goto done; |
973 | goto done; |
919 | } |
974 | } |
920 | 975 | ||
921 | gettimeofday(&msg->wdata.expires, NULL); |
976 | gettimeofday(&msg->wdata.expires, NULL); |
922 | tv_add(&msg->wdata.expires, timeout); |
977 | tv_add(&msg->wdata.expires, timeout); |
923 | 978 | ||
924 | msg->wdata.fid = fibril_get_id(); |
979 | msg->wdata.fid = fibril_get_id(); |
925 | msg->wdata.active = false; |
980 | msg->wdata.active = false; |
926 | insert_timeout(&msg->wdata); |
981 | insert_timeout(&msg->wdata); |
927 | 982 | ||
928 | /* Leave the async_futex locked when entering this function */ |
983 | /* Leave the async_futex locked when entering this function */ |
929 | fibril_switch(FIBRIL_TO_MANAGER); |
984 | fibril_switch(FIBRIL_TO_MANAGER); |
930 | 985 | ||
931 | /* Futex is up automatically after fibril_switch */ |
986 | /* Futex is up automatically after fibril_switch */ |
932 | 987 | ||
933 | if (!msg->done) |
988 | if (!msg->done) |
934 | return ETIMEOUT; |
989 | return ETIMEOUT; |
935 | 990 | ||
936 | done: |
991 | done: |
937 | if (retval) |
992 | if (retval) |
938 | *retval = msg->retval; |
993 | *retval = msg->retval; |
939 | 994 | ||
940 | free(msg); |
995 | free(msg); |
941 | 996 | ||
942 | return 0; |
997 | return 0; |
943 | } |
998 | } |
944 | 999 | ||
945 | /** Wait for specified time. |
1000 | /** Wait for specified time. |
946 | * |
1001 | * |
947 | * The current fibril is suspended but the thread continues to execute. |
1002 | * The current fibril is suspended but the thread continues to execute. |
948 | * |
1003 | * |
949 | * @param timeout Duration of the wait in microseconds. |
1004 | * @param timeout Duration of the wait in microseconds. |
950 | * |
1005 | * |
951 | */ |
1006 | */ |
952 | void async_usleep(suseconds_t timeout) |
1007 | void async_usleep(suseconds_t timeout) |
953 | { |
1008 | { |
954 | amsg_t *msg = malloc(sizeof(*msg)); |
1009 | amsg_t *msg = malloc(sizeof(*msg)); |
955 | 1010 | ||
956 | if (!msg) |
1011 | if (!msg) |
957 | return; |
1012 | return; |
958 | 1013 | ||
959 | msg->wdata.fid = fibril_get_id(); |
1014 | msg->wdata.fid = fibril_get_id(); |
960 | msg->wdata.active = false; |
1015 | msg->wdata.active = false; |
961 | 1016 | ||
962 | gettimeofday(&msg->wdata.expires, NULL); |
1017 | gettimeofday(&msg->wdata.expires, NULL); |
963 | tv_add(&msg->wdata.expires, timeout); |
1018 | tv_add(&msg->wdata.expires, timeout); |
964 | 1019 | ||
965 | futex_down(&async_futex); |
1020 | futex_down(&async_futex); |
966 | 1021 | ||
967 | insert_timeout(&msg->wdata); |
1022 | insert_timeout(&msg->wdata); |
968 | 1023 | ||
969 | /* Leave the async_futex locked when entering this function */ |
1024 | /* Leave the async_futex locked when entering this function */ |
970 | fibril_switch(FIBRIL_TO_MANAGER); |
1025 | fibril_switch(FIBRIL_TO_MANAGER); |
971 | 1026 | ||
972 | /* Futex is up automatically after fibril_switch() */ |
1027 | /* Futex is up automatically after fibril_switch() */ |
973 | 1028 | ||
974 | free(msg); |
1029 | free(msg); |
975 | } |
1030 | } |
976 | 1031 | ||
977 | /** Setter for client_connection function pointer. |
1032 | /** Setter for client_connection function pointer. |
978 | * |
1033 | * |
979 | * @param conn Function that will implement a new connection fibril. |
1034 | * @param conn Function that will implement a new connection fibril. |
980 | * |
1035 | * |
981 | */ |
1036 | */ |
982 | void async_set_client_connection(async_client_conn_t conn) |
1037 | void async_set_client_connection(async_client_conn_t conn) |
983 | { |
1038 | { |
984 | client_connection = conn; |
1039 | client_connection = conn; |
985 | } |
1040 | } |
986 | 1041 | ||
987 | /** Setter for interrupt_received function pointer. |
1042 | /** Setter for interrupt_received function pointer. |
988 | * |
1043 | * |
989 | * @param intr Function that will implement a new interrupt |
1044 | * @param intr Function that will implement a new interrupt |
990 | * notification fibril. |
1045 | * notification fibril. |
991 | */ |
1046 | */ |
992 | void async_set_interrupt_received(async_client_conn_t intr) |
1047 | void async_set_interrupt_received(async_client_conn_t intr) |
993 | { |
1048 | { |
994 | interrupt_received = intr; |
1049 | interrupt_received = intr; |
995 | } |
1050 | } |
- | 1051 | ||
- | 1052 | /** Setter for pending function pointer. |
|
- | 1053 | * |
|
- | 1054 | * @param pend Function that will implement a new pending |
|
- | 1055 | * operations fibril. |
|
- | 1056 | */ |
|
- | 1057 | void async_set_pending(async_pending_t pend) |
|
- | 1058 | { |
|
- | 1059 | pending = pend; |
|
- | 1060 | } |
|
996 | 1061 | ||
997 | /** Pseudo-synchronous message sending - fast version. |
1062 | /** Pseudo-synchronous message sending - fast version. |
998 | * |
1063 | * |
999 | * Send message asynchronously and return only after the reply arrives. |
1064 | * Send message asynchronously and return only after the reply arrives. |
1000 | * |
1065 | * |
1001 | * This function can only transfer 4 register payload arguments. For |
1066 | * This function can only transfer 4 register payload arguments. For |
1002 | * transferring more arguments, see the slower async_req_slow(). |
1067 | * transferring more arguments, see the slower async_req_slow(). |
1003 | * |
1068 | * |
1004 | * @param phoneid Hash of the phone through which to make the call. |
1069 | * @param phoneid Hash of the phone through which to make the call. |
1005 | * @param method Method of the call. |
1070 | * @param method Method of the call. |
1006 | * @param arg1 Service-defined payload argument. |
1071 | * @param arg1 Service-defined payload argument. |
1007 | * @param arg2 Service-defined payload argument. |
1072 | * @param arg2 Service-defined payload argument. |
1008 | * @param arg3 Service-defined payload argument. |
1073 | * @param arg3 Service-defined payload argument. |
1009 | * @param arg4 Service-defined payload argument. |
1074 | * @param arg4 Service-defined payload argument. |
1010 | * @param r1 If non-NULL, storage for the 1st reply argument. |
1075 | * @param r1 If non-NULL, storage for the 1st reply argument. |
1011 | * @param r2 If non-NULL, storage for the 2nd reply argument. |
1076 | * @param r2 If non-NULL, storage for the 2nd reply argument. |
1012 | * @param r3 If non-NULL, storage for the 3rd reply argument. |
1077 | * @param r3 If non-NULL, storage for the 3rd reply argument. |
1013 | * @param r4 If non-NULL, storage for the 4th reply argument. |
1078 | * @param r4 If non-NULL, storage for the 4th reply argument. |
1014 | * @param r5 If non-NULL, storage for the 5th reply argument. |
1079 | * @param r5 If non-NULL, storage for the 5th reply argument. |
1015 | * |
1080 | * |
1016 | * @return Return code of the reply or a negative error code. |
1081 | * @return Return code of the reply or a negative error code. |
1017 | * |
1082 | * |
1018 | */ |
1083 | */ |
1019 | ipcarg_t async_req_fast(int phoneid, ipcarg_t method, ipcarg_t arg1, |
1084 | ipcarg_t async_req_fast(int phoneid, ipcarg_t method, ipcarg_t arg1, |
1020 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t *r1, ipcarg_t *r2, |
1085 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t *r1, ipcarg_t *r2, |
1021 | ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5) |
1086 | ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5) |
1022 | { |
1087 | { |
1023 | ipc_call_t result; |
1088 | ipc_call_t result; |
1024 | aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4, |
1089 | aid_t eid = async_send_4(phoneid, method, arg1, arg2, arg3, arg4, |
1025 | &result); |
1090 | &result); |
1026 | 1091 | ||
1027 | ipcarg_t rc; |
1092 | ipcarg_t rc; |
1028 | async_wait_for(eid, &rc); |
1093 | async_wait_for(eid, &rc); |
1029 | 1094 | ||
1030 | if (r1) |
1095 | if (r1) |
1031 | *r1 = IPC_GET_ARG1(result); |
1096 | *r1 = IPC_GET_ARG1(result); |
1032 | 1097 | ||
1033 | if (r2) |
1098 | if (r2) |
1034 | *r2 = IPC_GET_ARG2(result); |
1099 | *r2 = IPC_GET_ARG2(result); |
1035 | 1100 | ||
1036 | if (r3) |
1101 | if (r3) |
1037 | *r3 = IPC_GET_ARG3(result); |
1102 | *r3 = IPC_GET_ARG3(result); |
1038 | 1103 | ||
1039 | if (r4) |
1104 | if (r4) |
1040 | *r4 = IPC_GET_ARG4(result); |
1105 | *r4 = IPC_GET_ARG4(result); |
1041 | 1106 | ||
1042 | if (r5) |
1107 | if (r5) |
1043 | *r5 = IPC_GET_ARG5(result); |
1108 | *r5 = IPC_GET_ARG5(result); |
1044 | 1109 | ||
1045 | return rc; |
1110 | return rc; |
1046 | } |
1111 | } |
1047 | 1112 | ||
1048 | /** Pseudo-synchronous message sending - slow version. |
1113 | /** Pseudo-synchronous message sending - slow version. |
1049 | * |
1114 | * |
1050 | * Send message asynchronously and return only after the reply arrives. |
1115 | * Send message asynchronously and return only after the reply arrives. |
1051 | * |
1116 | * |
1052 | * @param phoneid Hash of the phone through which to make the call. |
1117 | * @param phoneid Hash of the phone through which to make the call. |
1053 | * @param method Method of the call. |
1118 | * @param method Method of the call. |
1054 | * @param arg1 Service-defined payload argument. |
1119 | * @param arg1 Service-defined payload argument. |
1055 | * @param arg2 Service-defined payload argument. |
1120 | * @param arg2 Service-defined payload argument. |
1056 | * @param arg3 Service-defined payload argument. |
1121 | * @param arg3 Service-defined payload argument. |
1057 | * @param arg4 Service-defined payload argument. |
1122 | * @param arg4 Service-defined payload argument. |
1058 | * @param arg5 Service-defined payload argument. |
1123 | * @param arg5 Service-defined payload argument. |
1059 | * @param r1 If non-NULL, storage for the 1st reply argument. |
1124 | * @param r1 If non-NULL, storage for the 1st reply argument. |
1060 | * @param r2 If non-NULL, storage for the 2nd reply argument. |
1125 | * @param r2 If non-NULL, storage for the 2nd reply argument. |
1061 | * @param r3 If non-NULL, storage for the 3rd reply argument. |
1126 | * @param r3 If non-NULL, storage for the 3rd reply argument. |
1062 | * @param r4 If non-NULL, storage for the 4th reply argument. |
1127 | * @param r4 If non-NULL, storage for the 4th reply argument. |
1063 | * @param r5 If non-NULL, storage for the 5th reply argument. |
1128 | * @param r5 If non-NULL, storage for the 5th reply argument. |
1064 | * |
1129 | * |
1065 | * @return Return code of the reply or a negative error code. |
1130 | * @return Return code of the reply or a negative error code. |
1066 | * |
1131 | * |
1067 | */ |
1132 | */ |
1068 | ipcarg_t async_req_slow(int phoneid, ipcarg_t method, ipcarg_t arg1, |
1133 | ipcarg_t async_req_slow(int phoneid, ipcarg_t method, ipcarg_t arg1, |
1069 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *r1, |
1134 | ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t *r1, |
1070 | ipcarg_t *r2, ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5) |
1135 | ipcarg_t *r2, ipcarg_t *r3, ipcarg_t *r4, ipcarg_t *r5) |
1071 | { |
1136 | { |
1072 | ipc_call_t result; |
1137 | ipc_call_t result; |
1073 | aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, |
1138 | aid_t eid = async_send_5(phoneid, method, arg1, arg2, arg3, arg4, arg5, |
1074 | &result); |
1139 | &result); |
1075 | 1140 | ||
1076 | ipcarg_t rc; |
1141 | ipcarg_t rc; |
1077 | async_wait_for(eid, &rc); |
1142 | async_wait_for(eid, &rc); |
1078 | 1143 | ||
1079 | if (r1) |
1144 | if (r1) |
1080 | *r1 = IPC_GET_ARG1(result); |
1145 | *r1 = IPC_GET_ARG1(result); |
1081 | 1146 | ||
1082 | if (r2) |
1147 | if (r2) |
1083 | *r2 = IPC_GET_ARG2(result); |
1148 | *r2 = IPC_GET_ARG2(result); |
1084 | 1149 | ||
1085 | if (r3) |
1150 | if (r3) |
1086 | *r3 = IPC_GET_ARG3(result); |
1151 | *r3 = IPC_GET_ARG3(result); |
1087 | 1152 | ||
1088 | if (r4) |
1153 | if (r4) |
1089 | *r4 = IPC_GET_ARG4(result); |
1154 | *r4 = IPC_GET_ARG4(result); |
1090 | 1155 | ||
1091 | if (r5) |
1156 | if (r5) |
1092 | *r5 = IPC_GET_ARG5(result); |
1157 | *r5 = IPC_GET_ARG5(result); |
1093 | 1158 | ||
1094 | return rc; |
1159 | return rc; |
1095 | } |
1160 | } |
1096 | 1161 | ||
1097 | /** @} |
1162 | /** @} |
1098 | */ |
1163 | */ |
1099 | 1164 |