Subversion Repositories HelenOS-historic

Rev

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

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