Subversion Repositories HelenOS-historic

Rev

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

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