Subversion Repositories HelenOS

Rev

Rev 4055 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4055 Rev 4537
Line 31... Line 31...
31
 * @{
31
 * @{
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <libadt/list.h>
36
#include <adt/list.h>
37
#include <fibril.h>
37
#include <fibril.h>
38
#include <thread.h>
38
#include <thread.h>
39
#include <tls.h>
39
#include <tls.h>
40
#include <malloc.h>
40
#include <malloc.h>
41
#include <unistd.h>
41
#include <unistd.h>
Line 47... Line 47...
47
 
47
 
48
#ifndef FIBRIL_INITIAL_STACK_PAGES_NO
48
#ifndef FIBRIL_INITIAL_STACK_PAGES_NO
49
#define FIBRIL_INITIAL_STACK_PAGES_NO   1
49
#define FIBRIL_INITIAL_STACK_PAGES_NO   1
50
#endif
50
#endif
51
 
51
 
-
 
52
/**
52
/** This futex serializes access to ready_list, serialized_list and manage_list.
53
 * This futex serializes access to ready_list, serialized_list and manager_list.
53
 */
54
 */
54
static atomic_t fibril_futex = FUTEX_INITIALIZER;
55
static atomic_t fibril_futex = FUTEX_INITIALIZER;
55
 
56
 
56
static LIST_INITIALIZE(ready_list);
57
static LIST_INITIALIZE(ready_list);
57
static LIST_INITIALIZE(serialized_list);
58
static LIST_INITIALIZE(serialized_list);
58
static LIST_INITIALIZE(manager_list);
59
static LIST_INITIALIZE(manager_list);
59
 
60
 
60
static void fibril_main(void);
61
static void fibril_main(void);
61
 
62
 
62
/** Number of fibrils that are in async_serialized mode */
63
/** Number of threads that are executing a manager fibril. */
-
 
64
static int threads_in_manager;
-
 
65
/** Number of threads that are executing a manager fibril and are serialized. */
63
static int serialized_fibrils;  /* Protected by async_futex */
66
static int serialized_threads;  /* Protected by async_futex */
64
/** Thread-local count of serialization. If >0, we must not preempt */
67
/** Fibril-local count of serialization. If > 0, we must not preempt */
65
static __thread int serialization_count;
68
static fibril_local int serialization_count;
66
/** Counter for fibrils residing in async_manager */
-
 
67
static int fibrils_in_manager;
-
 
68
 
69
 
69
/** Setup fibril information into TCB structure */
70
/** Setup fibril information into TCB structure */
70
fibril_t *fibril_setup(void)
71
fibril_t *fibril_setup(void)
71
{
72
{
72
    fibril_t *f;
73
    fibril_t *f;
Line 141... Line 142...
141
 
142
 
142
    if (stype == FIBRIL_FROM_MANAGER) {
143
    if (stype == FIBRIL_FROM_MANAGER) {
143
        if (list_empty(&ready_list) && list_empty(&serialized_list))
144
        if (list_empty(&ready_list) && list_empty(&serialized_list))
144
            goto ret_0;
145
            goto ret_0;
145
        /*
146
        /*
146
         * Do not preempt if there is not sufficient count of fibril
147
         * Do not preempt if there is not enough threads to run the
147
         * managers.
148
         * ready fibrils which are not serialized.
148
         */
149
         */
149
        if (list_empty(&serialized_list) &&
150
        if (list_empty(&serialized_list) &&
150
            fibrils_in_manager <= serialized_fibrils) {
151
            threads_in_manager <= serialized_threads) {
151
            goto ret_0;
152
            goto ret_0;
152
        }
153
        }
153
    }
154
    }
154
    /* If we are going to manager and none exists, create it */
155
    /* If we are going to manager and none exists, create it */
155
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
156
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
Line 192... Line 193...
192
        /* Save myself to the correct run list */
193
        /* Save myself to the correct run list */
193
        if (stype == FIBRIL_PREEMPT)
194
        if (stype == FIBRIL_PREEMPT)
194
            list_append(&srcf->link, &ready_list);
195
            list_append(&srcf->link, &ready_list);
195
        else if (stype == FIBRIL_FROM_MANAGER) {
196
        else if (stype == FIBRIL_FROM_MANAGER) {
196
            list_append(&srcf->link, &manager_list);
197
            list_append(&srcf->link, &manager_list);
197
            fibrils_in_manager--;
198
            threads_in_manager--;
198
        } else {   
199
        } else {   
199
            /*
200
            /*
200
             * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
201
             * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
201
             * any list, we should already be somewhere, or we will
202
             * any list, we should already be somewhere, or we will
202
             * be lost.
203
             * be lost.
Line 206... Line 207...
206
   
207
   
207
    /* Choose a new fibril to run */
208
    /* Choose a new fibril to run */
208
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
209
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
209
        dstf = list_get_instance(manager_list.next, fibril_t, link);
210
        dstf = list_get_instance(manager_list.next, fibril_t, link);
210
        if (serialization_count && stype == FIBRIL_TO_MANAGER) {
211
        if (serialization_count && stype == FIBRIL_TO_MANAGER) {
211
            serialized_fibrils++;
212
            serialized_threads++;
212
            srcf->flags |= FIBRIL_SERIALIZED;
213
            srcf->flags |= FIBRIL_SERIALIZED;
213
        }
214
        }
214
        fibrils_in_manager++;
215
        threads_in_manager++;
215
 
216
 
216
        if (stype == FIBRIL_FROM_DEAD)
217
        if (stype == FIBRIL_FROM_DEAD)
217
            dstf->clean_after_me = srcf;
218
            dstf->clean_after_me = srcf;
218
    } else {
219
    } else {
219
        if (!list_empty(&serialized_list)) {
220
        if (!list_empty(&serialized_list)) {
220
            dstf = list_get_instance(serialized_list.next, fibril_t,
221
            dstf = list_get_instance(serialized_list.next, fibril_t,
221
                link);
222
                link);
222
            serialized_fibrils--;
223
            serialized_threads--;
223
        } else {
224
        } else {
224
            dstf = list_get_instance(ready_list.next, fibril_t,
225
            dstf = list_get_instance(ready_list.next, fibril_t,
225
                link);
226
                link);
226
        }
227
        }
227
    }
228
    }
Line 267... Line 268...
267
    return (fid_t) f;
268
    return (fid_t) f;
268
}
269
}
269
 
270
 
270
/** Add a fibril to the ready list.
271
/** Add a fibril to the ready list.
271
 *
272
 *
272
 * @param fid       Pinter to the fibril structure of the fibril to be
273
 * @param fid       Pointer to the fibril structure of the fibril to be
273
 *          added.
274
 *          added.
274
 */
275
 */
275
void fibril_add_ready(fid_t fid)
276
void fibril_add_ready(fid_t fid)
276
{
277
{
277
    fibril_t *f;
278
    fibril_t *f;
Line 285... Line 286...
285
    futex_up(&fibril_futex);
286
    futex_up(&fibril_futex);
286
}
287
}
287
 
288
 
288
/** Add a fibril to the manager list.
289
/** Add a fibril to the manager list.
289
 *
290
 *
290
 * @param fid       Pinter to the fibril structure of the fibril to be added.
291
 * @param fid       Pointer to the fibril structure of the fibril to be
-
 
292
 *          added.
291
 */
293
 */
292
void fibril_add_manager(fid_t fid)
294
void fibril_add_manager(fid_t fid)
293
{
295
{
294
    fibril_t *f;
296
    fibril_t *f;
295
 
297
 
Line 312... Line 314...
312
    futex_up(&fibril_futex);
314
    futex_up(&fibril_futex);
313
}
315
}
314
 
316
 
315
/** Return fibril id of the currently running fibril.
317
/** Return fibril id of the currently running fibril.
316
 *
318
 *
317
 * @return      Fibril ID of the currently running pseudo thread.
319
 * @return fibril ID of the currently running fibril.
-
 
320
 *
318
 */
321
 */
319
fid_t fibril_get_id(void)
322
fid_t fibril_get_id(void)
320
{
323
{
321
    return (fid_t) __tcb_get()->fibril_data;
324
    return (fid_t) __tcb_get()->fibril_data;
322
}
325
}
323
 
326
 
324
/** Disable preemption
327
/** Disable preemption
325
 *
328
 *
326
 * If the fibril wants to send several message in a row and does not want to be
329
 * If the fibril wants to send several message in a row and does not want to be
327
 * preempted, it should start async_serialize_start() in the beginning of
330
 * preempted, it should start async_serialize_start() in the beginning of
328
 * communication and async_serialize_end() in the end. If it is a true
331
 * communication and async_serialize_end() in the end. If it is a true
329
 * multithreaded application, it should protect the communication channel by a
332
 * multithreaded application, it should protect the communication channel by a
330
 * futex as well. Interrupt messages can still be preempted.
333
 * futex as well.
-
 
334
 *
331
 */
335
 */
332
void fibril_inc_sercount(void)
336
void fibril_inc_sercount(void)
333
{
337
{
334
    serialization_count++;
338
    serialization_count++;
335
}
339
}