Subversion Repositories HelenOS-historic

Rev

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

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