Subversion Repositories HelenOS

Rev

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

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