Subversion Repositories HelenOS-historic

Rev

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

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