Subversion Repositories HelenOS

Rev

Rev 4528 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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