Subversion Repositories HelenOS

Rev

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

Rev 2492 Rev 2568
1
/*
1
/*
2
 * Copyright (c) 2006 Ondrej Palkovsky
2
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * Copyright (c) 2007 Jakub Jermar
3
 * Copyright (c) 2007 Jakub Jermar
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup libc
30
/** @addtogroup libc
31
 * @{
31
 * @{
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <libadt/list.h>
36
#include <libadt/list.h>
37
#include <fibril.h>
37
#include <fibril.h>
38
#include <malloc.h>
38
#include <malloc.h>
39
#include <unistd.h>
39
#include <unistd.h>
40
#include <thread.h>
40
#include <thread.h>
41
#include <stdio.h>
41
#include <stdio.h>
42
#include <libarch/faddr.h>
42
#include <libarch/faddr.h>
43
#include <futex.h>
43
#include <futex.h>
44
#include <assert.h>
44
#include <assert.h>
45
#include <async.h>
45
#include <async.h>
46
 
46
 
47
#ifndef FIBRIL_INITIAL_STACK_PAGES_NO
47
#ifndef FIBRIL_INITIAL_STACK_PAGES_NO
48
#define FIBRIL_INITIAL_STACK_PAGES_NO   1
48
#define FIBRIL_INITIAL_STACK_PAGES_NO   1
49
#endif
49
#endif
50
 
50
 
51
/** This futex serializes access to ready_list, serialized_list and manage_list.
51
/** This futex serializes access to ready_list, serialized_list and manage_list.
52
 */
52
 */
53
static atomic_t fibril_futex = FUTEX_INITIALIZER;
53
static atomic_t fibril_futex = FUTEX_INITIALIZER;
54
 
54
 
55
static LIST_INITIALIZE(ready_list);
55
static LIST_INITIALIZE(ready_list);
56
static LIST_INITIALIZE(serialized_list);
56
static LIST_INITIALIZE(serialized_list);
57
static LIST_INITIALIZE(manager_list);
57
static LIST_INITIALIZE(manager_list);
58
 
58
 
59
static void fibril_main(void);
59
static void fibril_main(void);
60
 
60
 
61
/** Number of fibrils that are in async_serialized mode */
61
/** Number of fibrils that are in async_serialized mode */
62
static int serialized_fibrils;  /* Protected by async_futex */
62
static int serialized_fibrils;  /* Protected by async_futex */
63
/** Thread-local count of serialization. If >0, we must not preempt */
63
/** Thread-local count of serialization. If >0, we must not preempt */
64
static __thread int serialization_count;
64
static __thread int serialization_count;
65
/** Counter for fibrils residing in async_manager */
65
/** Counter for fibrils residing in async_manager */
66
static int fibrils_in_manager;
66
static int fibrils_in_manager;
67
 
67
 
68
/** Setup fibril information into TCB structure */
68
/** Setup fibril information into TCB structure */
69
fibril_t *fibril_setup(void)
69
fibril_t *fibril_setup(void)
70
{
70
{
71
    fibril_t *f;
71
    fibril_t *f;
72
    tcb_t *tcb;
72
    tcb_t *tcb;
73
 
73
 
74
    tcb = __make_tls();
74
    tcb = __make_tls();
75
    if (!tcb)
75
    if (!tcb)
76
        return NULL;
76
        return NULL;
77
 
77
 
78
    f = malloc(sizeof(*f));
78
    f = malloc(sizeof(fibril_t));
79
    if (!f) {
79
    if (!f) {
80
        __free_tls(tcb);
80
        __free_tls(tcb);
81
        return NULL;
81
        return NULL;
82
    }
82
    }
83
 
83
 
84
    tcb->fibril_data = f;
84
    tcb->fibril_data = f;
85
    f->tcb = tcb;
85
    f->tcb = tcb;
86
 
86
 
-
 
87
    f->func = NULL;
-
 
88
    f->arg = NULL;
-
 
89
    f->stack = NULL;
-
 
90
    f->clean_after_me = NULL;
-
 
91
    f->retval = 0;
-
 
92
    f->flags = 0;
-
 
93
 
87
    return f;
94
    return f;
88
}
95
}
89
 
96
 
90
void fibril_teardown(fibril_t *f)
97
void fibril_teardown(fibril_t *f)
91
{
98
{
92
    __free_tls(f->tcb);
99
    __free_tls(f->tcb);
93
    free(f);
100
    free(f);
94
}
101
}
95
 
102
 
96
/** Function that spans the whole life-cycle of a fibril.
103
/** Function that spans the whole life-cycle of a fibril.
97
 *
104
 *
98
 * Each fibril begins execution in this function. Then the function implementing
105
 * Each fibril begins execution in this function. Then the function implementing
99
 * the fibril logic is called.  After its return, the return value is saved.
106
 * the fibril logic is called.  After its return, the return value is saved.
100
 * The fibril then switches to another fibril, which cleans up after it.
107
 * The fibril then switches to another fibril, which cleans up after it.
101
 */
108
 */
102
void fibril_main(void)
109
void fibril_main(void)
103
{
110
{
104
    fibril_t *f = __tcb_get()->fibril_data;
111
    fibril_t *f = __tcb_get()->fibril_data;
105
 
112
 
106
    /* Call the implementing function. */
113
    /* Call the implementing function. */
107
    f->retval = f->func(f->arg);
114
    f->retval = f->func(f->arg);
108
 
115
 
109
    fibril_schedule_next_adv(FIBRIL_FROM_DEAD);
116
    fibril_switch(FIBRIL_FROM_DEAD);
110
    /* not reached */
117
    /* not reached */
111
}
118
}
112
 
119
 
113
/** Schedule next fibril.
120
/** Switch from the current fibril.
114
 *
121
 *
115
 * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
122
 * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
116
 * held.
123
 * held.
117
 *
124
 *
118
 * @param stype     Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
125
 * @param stype     Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
119
 *          FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
126
 *          FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
120
 *          describes the circumstances of the switch.
127
 *          describes the circumstances of the switch.
121
 * @return      Return 0 if there is no ready fibril,
128
 * @return      Return 0 if there is no ready fibril,
122
 *          return 1 otherwise.
129
 *          return 1 otherwise.
123
 */
130
 */
124
int fibril_schedule_next_adv(fibril_switch_type_t stype)
131
int fibril_switch(fibril_switch_type_t stype)
125
{
132
{
126
    fibril_t *srcf, *dstf;
133
    fibril_t *srcf, *dstf;
127
    int retval = 0;
134
    int retval = 0;
128
   
135
   
129
    futex_down(&fibril_futex);
136
    futex_down(&fibril_futex);
130
 
137
 
131
    if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
138
    if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
132
        goto ret_0;
139
        goto ret_0;
133
 
140
 
134
    if (stype == FIBRIL_FROM_MANAGER) {
141
    if (stype == FIBRIL_FROM_MANAGER) {
135
        if (list_empty(&ready_list) && list_empty(&serialized_list))
142
        if (list_empty(&ready_list) && list_empty(&serialized_list))
136
            goto ret_0;
143
            goto ret_0;
137
        /*
144
        /*
138
         * Do not preempt if there is not sufficient count of fibril
145
         * Do not preempt if there is not sufficient count of fibril
139
         * managers.
146
         * managers.
140
         */
147
         */
141
        if (list_empty(&serialized_list) && fibrils_in_manager <=
148
        if (list_empty(&serialized_list) &&
142
            serialized_fibrils) {
149
            fibrils_in_manager <= serialized_fibrils) {
143
            goto ret_0;
150
            goto ret_0;
144
        }
151
        }
145
    }
152
    }
146
    /* If we are going to manager and none exists, create it */
153
    /* If we are going to manager and none exists, create it */
147
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
154
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
148
        while (list_empty(&manager_list)) {
155
        while (list_empty(&manager_list)) {
149
            futex_up(&fibril_futex);
156
            futex_up(&fibril_futex);
150
            async_create_manager();
157
            async_create_manager();
151
            futex_down(&fibril_futex);
158
            futex_down(&fibril_futex);
152
        }
159
        }
153
    }
160
    }
154
   
161
   
155
    srcf = __tcb_get()->fibril_data;
162
    srcf = __tcb_get()->fibril_data;
156
    if (stype != FIBRIL_FROM_DEAD) {
163
    if (stype != FIBRIL_FROM_DEAD) {
157
        /* Save current state */
164
        /* Save current state */
158
        if (!context_save(&srcf->ctx)) {
165
        if (!context_save(&srcf->ctx)) {
159
            if (serialization_count)
166
            if (serialization_count)
160
                srcf->flags &= ~FIBRIL_SERIALIZED;
167
                srcf->flags &= ~FIBRIL_SERIALIZED;
161
            if (srcf->clean_after_me) {
168
            if (srcf->clean_after_me) {
162
                /*
169
                /*
163
                 * Cleanup after the dead fibril from which we
170
                 * Cleanup after the dead fibril from which we
164
                 * restored context here.
171
                 * restored context here.
165
                 */
172
                 */
166
                free(srcf->clean_after_me->stack);
173
                void *stack = srcf->clean_after_me->stack;
-
 
174
                if (stack) {
-
 
175
                    /*
-
 
176
                     * This check is necessary because a
-
 
177
                     * thread could have exited like a
-
 
178
                     * normal fibril using the
-
 
179
                     * FIBRIL_FROM_DEAD switch type. In that
-
 
180
                     * case, its fibril will not have the
-
 
181
                     * stack member filled.
-
 
182
                     */
-
 
183
                    free(stack);
-
 
184
                }
167
                fibril_teardown(srcf->clean_after_me);
185
                fibril_teardown(srcf->clean_after_me);
168
                srcf->clean_after_me = NULL;
186
                srcf->clean_after_me = NULL;
169
            }
187
            }
170
            return 1;   /* futex_up already done here */
188
            return 1;   /* futex_up already done here */
171
        }
189
        }
172
 
190
 
173
        /* Save myself to the correct run list */
191
        /* Save myself to the correct run list */
174
        if (stype == FIBRIL_PREEMPT)
192
        if (stype == FIBRIL_PREEMPT)
175
            list_append(&srcf->link, &ready_list);
193
            list_append(&srcf->link, &ready_list);
176
        else if (stype == FIBRIL_FROM_MANAGER) {
194
        else if (stype == FIBRIL_FROM_MANAGER) {
177
            list_append(&srcf->link, &manager_list);
195
            list_append(&srcf->link, &manager_list);
178
            fibrils_in_manager--;
196
            fibrils_in_manager--;
179
        } else {   
197
        } else {   
180
            /*
198
            /*
181
             * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
199
             * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
182
             * any list, we should already be somewhere, or we will
200
             * any list, we should already be somewhere, or we will
183
             * be lost.
201
             * be lost.
184
             */
202
             */
185
        }
203
        }
186
    }
204
    }
187
 
205
   
188
    /* Choose a new fibril to run */
206
    /* Choose a new fibril to run */
189
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
207
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
190
        dstf = list_get_instance(manager_list.next, fibril_t, link);
208
        dstf = list_get_instance(manager_list.next, fibril_t, link);
191
        if (serialization_count && stype == FIBRIL_TO_MANAGER) {
209
        if (serialization_count && stype == FIBRIL_TO_MANAGER) {
192
            serialized_fibrils++;
210
            serialized_fibrils++;
193
            srcf->flags |= FIBRIL_SERIALIZED;
211
            srcf->flags |= FIBRIL_SERIALIZED;
194
        }
212
        }
195
        fibrils_in_manager++;
213
        fibrils_in_manager++;
196
 
214
 
197
        if (stype == FIBRIL_FROM_DEAD)
215
        if (stype == FIBRIL_FROM_DEAD)
198
            dstf->clean_after_me = srcf;
216
            dstf->clean_after_me = srcf;
199
    } else {
217
    } else {
200
        if (!list_empty(&serialized_list)) {
218
        if (!list_empty(&serialized_list)) {
201
            dstf = list_get_instance(serialized_list.next, fibril_t,
219
            dstf = list_get_instance(serialized_list.next, fibril_t,
202
                link);
220
                link);
203
            serialized_fibrils--;
221
            serialized_fibrils--;
204
        } else {
222
        } else {
205
            dstf = list_get_instance(ready_list.next, fibril_t,
223
            dstf = list_get_instance(ready_list.next, fibril_t,
206
                link);
224
                link);
207
        }
225
        }
208
    }
226
    }
209
    list_remove(&dstf->link);
227
    list_remove(&dstf->link);
210
 
228
 
211
    futex_up(&fibril_futex);
229
    futex_up(&fibril_futex);
212
    context_restore(&dstf->ctx);
230
    context_restore(&dstf->ctx);
213
    /* not reached */
231
    /* not reached */
214
 
232
 
215
ret_0:
233
ret_0:
216
    futex_up(&fibril_futex);
234
    futex_up(&fibril_futex);
217
    return retval;
235
    return retval;
218
}
236
}
219
 
237
 
220
/** Create a new fibril.
238
/** Create a new fibril.
221
 *
239
 *
222
 * @param func      Implementing function of the new fibril.
240
 * @param func      Implementing function of the new fibril.
223
 * @param arg       Argument to pass to func.
241
 * @param arg       Argument to pass to func.
224
 *
242
 *
225
 * @return      Return 0 on failure or TLS of the new fibril.
243
 * @return      Return 0 on failure or TLS of the new fibril.
226
 */
244
 */
227
fid_t fibril_create(int (*func)(void *), void *arg)
245
fid_t fibril_create(int (*func)(void *), void *arg)
228
{
246
{
229
    fibril_t *f;
247
    fibril_t *f;
230
 
248
 
231
    f = fibril_setup();
249
    f = fibril_setup();
232
    if (!f)
250
    if (!f)
233
        return 0;
251
        return 0;
234
    f->stack = (char *) malloc(FIBRIL_INITIAL_STACK_PAGES_NO *
252
    f->stack = (char *) malloc(FIBRIL_INITIAL_STACK_PAGES_NO *
235
        getpagesize());
253
        getpagesize());
236
 
-
 
237
    if (!f->stack) {
254
    if (!f->stack) {
238
        fibril_teardown(f);
255
        fibril_teardown(f);
239
        return 0;
256
        return 0;
240
    }
257
    }
241
 
258
   
242
    f->arg = arg;
-
 
243
    f->func = func;
259
    f->func = func;
244
    f->clean_after_me = NULL;
-
 
245
    f->retval = 0;
-
 
246
    f->flags = 0;
260
    f->arg = arg;
247
 
261
 
248
    context_save(&f->ctx);
262
    context_save(&f->ctx);
249
    context_set(&f->ctx, FADDR(fibril_main), f->stack,
263
    context_set(&f->ctx, FADDR(fibril_main), f->stack,
250
        FIBRIL_INITIAL_STACK_PAGES_NO * getpagesize(), f->tcb);
264
        FIBRIL_INITIAL_STACK_PAGES_NO * getpagesize(), f->tcb);
251
 
265
 
252
    return (fid_t) f;
266
    return (fid_t) f;
253
}
267
}
254
 
268
 
255
/** Add a fibril to the ready list.
269
/** Add a fibril to the ready list.
256
 *
270
 *
257
 * @param fid       Pinter to the fibril structure of the fibril to be
271
 * @param fid       Pinter to the fibril structure of the fibril to be
258
 *          added.
272
 *          added.
259
 */
273
 */
260
void fibril_add_ready(fid_t fid)
274
void fibril_add_ready(fid_t fid)
261
{
275
{
262
    fibril_t *f;
276
    fibril_t *f;
263
 
277
 
264
    f = (fibril_t *) fid;
278
    f = (fibril_t *) fid;
265
    futex_down(&fibril_futex);
279
    futex_down(&fibril_futex);
266
    if ((f->flags & FIBRIL_SERIALIZED))
280
    if ((f->flags & FIBRIL_SERIALIZED))
267
        list_append(&f->link, &serialized_list);
281
        list_append(&f->link, &serialized_list);
268
    else
282
    else
269
        list_append(&f->link, &ready_list);
283
        list_append(&f->link, &ready_list);
270
    futex_up(&fibril_futex);
284
    futex_up(&fibril_futex);
271
}
285
}
272
 
286
 
273
/** Add a fibril to the manager list.
287
/** Add a fibril to the manager list.
274
 *
288
 *
275
 * @param fid       Pinter to the fibril structure of the fibril to be added.
289
 * @param fid       Pinter to the fibril structure of the fibril to be added.
276
 */
290
 */
277
void fibril_add_manager(fid_t fid)
291
void fibril_add_manager(fid_t fid)
278
{
292
{
279
    fibril_t *f;
293
    fibril_t *f;
280
 
294
 
281
    f = (fibril_t *) fid;
295
    f = (fibril_t *) fid;
282
 
296
 
283
    futex_down(&fibril_futex);
297
    futex_down(&fibril_futex);
284
    list_append(&f->link, &manager_list);
298
    list_append(&f->link, &manager_list);
285
    futex_up(&fibril_futex);
299
    futex_up(&fibril_futex);
286
}
300
}
287
 
301
 
288
/** Remove one manager from the manager list. */
302
/** Remove one manager from the manager list. */
289
void fibril_remove_manager(void)
303
void fibril_remove_manager(void)
290
{
304
{
291
    futex_down(&fibril_futex);
305
    futex_down(&fibril_futex);
292
    if (list_empty(&manager_list)) {
306
    if (list_empty(&manager_list)) {
293
        futex_up(&fibril_futex);
307
        futex_up(&fibril_futex);
294
        return;
308
        return;
295
    }
309
    }
296
    list_remove(manager_list.next);
310
    list_remove(manager_list.next);
297
    futex_up(&fibril_futex);
311
    futex_up(&fibril_futex);
298
}
312
}
299
 
313
 
300
/** Return fibril id of the currently running fibril.
314
/** Return fibril id of the currently running fibril.
301
 *
315
 *
302
 * @return      Fibril ID of the currently running pseudo thread.
316
 * @return      Fibril ID of the currently running pseudo thread.
303
 */
317
 */
304
fid_t fibril_get_id(void)
318
fid_t fibril_get_id(void)
305
{
319
{
306
    return (fid_t) __tcb_get()->fibril_data;
320
    return (fid_t) __tcb_get()->fibril_data;
307
}
321
}
308
 
322
 
309
/** Disable preemption
323
/** Disable preemption
310
 *
324
 *
311
 * If the fibril wants to send several message in a row and does not want to be
325
 * If the fibril wants to send several message in a row and does not want to be
312
 * preempted, it should start async_serialize_start() in the beginning of
326
 * preempted, it should start async_serialize_start() in the beginning of
313
 * communication and async_serialize_end() in the end. If it is a true
327
 * communication and async_serialize_end() in the end. If it is a true
314
 * multithreaded application, it should protect the communication channel by a
328
 * multithreaded application, it should protect the communication channel by a
315
 * futex as well. Interrupt messages can still be preempted.
329
 * futex as well. Interrupt messages can still be preempted.
316
 */
330
 */
317
void fibril_inc_sercount(void)
331
void fibril_inc_sercount(void)
318
{
332
{
319
    serialization_count++;
333
    serialization_count++;
320
}
334
}
321
 
335
 
322
/** Restore the preemption counter to the previous state. */
336
/** Restore the preemption counter to the previous state. */
323
void fibril_dec_sercount(void)
337
void fibril_dec_sercount(void)
324
{
338
{
325
    serialization_count--;
339
    serialization_count--;
326
}
340
}
327
 
341
 
328
/** @}
342
/** @}
329
 */
343
 */
330
 
344
 
331
 
345