Subversion Repositories HelenOS

Rev

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

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