Subversion Repositories HelenOS-historic

Rev

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

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