Subversion Repositories HelenOS-historic

Rev

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

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