Subversion Repositories HelenOS

Rev

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

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