Subversion Repositories HelenOS

Rev

Rev 2481 | Rev 2483 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2481 Rev 2482
Line 32... Line 32...
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <libadt/list.h>
36
#include <libadt/list.h>
37
#include <psthread.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 PSTHREAD_INITIAL_STACK_PAGES_NO
47
#ifndef FIBRIL_INITIAL_STACK_PAGES_NO
48
#define PSTHREAD_INITIAL_STACK_PAGES_NO 1
48
#define FIBRIL_INITIAL_STACK_PAGES_NO   1
49
#endif
49
#endif
50
 
50
 
51
static LIST_INITIALIZE(ready_list);
51
static LIST_INITIALIZE(ready_list);
52
static LIST_INITIALIZE(serialized_list);
52
static LIST_INITIALIZE(serialized_list);
53
static LIST_INITIALIZE(manager_list);
53
static LIST_INITIALIZE(manager_list);
54
 
54
 
55
static void psthread_main(void);
55
static void fibril_main(void);
56
 
56
 
57
static atomic_t psthread_futex = FUTEX_INITIALIZER;
57
static atomic_t fibril_futex = FUTEX_INITIALIZER;
58
/** Count of real threads that are in async_serialized mode */
58
/** Number of threads that are in async_serialized mode */
59
static int serialized_threads;  /* Protected by async_futex */
59
static int serialized_threads;  /* Protected by async_futex */
60
/** Thread-local count of serialization. If >0, we must not preempt */
60
/** Thread-local count of serialization. If >0, we must not preempt */
61
static __thread int serialization_count;
61
static __thread int serialization_count;
62
/** Counter of threads residing in async_manager */
62
/** Counter for fibrils residing in async_manager */
63
static int threads_in_manager;
63
static int fibrils_in_manager;
64
 
64
 
65
/** Setup psthread information into TCB structure */
65
/** Setup fibril information into TCB structure */
66
psthread_data_t *psthread_setup(void)
66
fibril_t *fibril_setup(void)
67
{
67
{
68
    psthread_data_t *pt;
68
    fibril_t *f;
69
    tcb_t *tcb;
69
    tcb_t *tcb;
70
 
70
 
71
    tcb = __make_tls();
71
    tcb = __make_tls();
72
    if (!tcb)
72
    if (!tcb)
73
        return NULL;
73
        return NULL;
74
 
74
 
75
    pt = malloc(sizeof(*pt));
75
    f = malloc(sizeof(*f));
76
    if (!pt) {
76
    if (!f) {
77
        __free_tls(tcb);
77
        __free_tls(tcb);
78
        return NULL;
78
        return NULL;
79
    }
79
    }
80
 
80
 
81
    tcb->pst_data = pt;
81
    tcb->fibril_data = f;
82
    pt->tcb = tcb;
82
    f->tcb = tcb;
83
 
83
 
84
    return pt;
84
    return f;
85
}
85
}
86
 
86
 
87
void psthread_teardown(psthread_data_t *pt)
87
void fibril_teardown(fibril_t *f)
88
{
88
{
89
    __free_tls(pt->tcb);
89
    __free_tls(f->tcb);
90
    free(pt);
90
    free(f);
91
}
91
}
92
 
92
 
93
/** Function that spans the whole life-cycle of a pseudo thread.
93
/** Function that spans the whole life-cycle of a fibril.
94
 *
94
 *
95
 * Each pseudo thread begins execution in this function.
95
 * Each fibril begins execution in this function.  Then the function
96
 * Then the function implementing the pseudo thread logic is called.
-
 
97
 * After its return, the return value is saved for a potentional
96
 * implementing the fibril logic is called.  After its return, the return value
98
 * joiner. If the joiner exists, it is woken up. The pseudo thread
97
 * is saved for a potentional joiner. If the joiner exists, it is woken up. The
99
 * then switches to another pseudo thread, which cleans up after it.
98
 * fibril then switches to another fibril, which cleans up after it.
100
 */
99
 */
101
void psthread_main(void)
100
void fibril_main(void)
102
{
101
{
103
    psthread_data_t *pt = __tcb_get()->pst_data;
102
    fibril_t *f = __tcb_get()->fibril_data;
104
 
103
 
105
    pt->retval = pt->func(pt->arg);
104
    f->retval = f->func(f->arg);
106
 
105
 
107
    /*
106
    /*
108
     * If there is a joiner, wake it up and save our return value.
107
     * If there is a joiner, wake it up and save our return value.
109
     */
108
     */
110
    if (pt->joiner) {
109
    if (f->joiner) {
111
        list_append(&pt->joiner->link, &ready_list);
110
        list_append(&f->joiner->link, &ready_list);
112
        pt->joiner->joinee_retval = pt->retval;
111
        f->joiner->joinee_retval = f->retval;
113
    }
112
    }
114
 
113
 
115
    psthread_schedule_next_adv(PS_FROM_DEAD);
114
    fibril_schedule_next_adv(FIBRIL_FROM_DEAD);
116
    /* not reached */
115
    /* not reached */
117
}
116
}
118
 
117
 
119
/** Schedule next userspace pseudo thread.
118
/** Schedule next fibril.
120
 *
119
 *
121
 * If calling with PS_TO_MANAGER parameter, the async_futex should be
120
 * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
122
 * held.
121
 * held.
123
 *
122
 *
124
 * @param ctype     One of PS_SLEEP, PS_PREEMPT, PS_TO_MANAGER,
123
 * @param stype     One of FIBRIL_SLEEP, FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
125
 *          PS_FROM_MANAGER, PS_FROM_DEAD. The parameter describes
124
 *          FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
126
 *          the circumstances of the switch.
125
 *          describes the circumstances of the switch.
127
 * @return      Return 0 if there is no ready pseudo thread,
126
 * @return      Return 0 if there is no ready fibril,
128
 *          return 1 otherwise.
127
 *          return 1 otherwise.
129
 */
128
 */
130
int psthread_schedule_next_adv(pschange_type ctype)
129
int fibril_schedule_next_adv(fibril_switch_type_t stype)
131
{
130
{
132
    psthread_data_t *srcpt, *dstpt;
131
    fibril_t *srcf, *dstf;
133
    int retval = 0;
132
    int retval = 0;
134
   
133
   
135
    futex_down(&psthread_futex);
134
    futex_down(&fibril_futex);
136
 
135
 
137
    if (ctype == PS_PREEMPT && list_empty(&ready_list))
136
    if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
138
        goto ret_0;
137
        goto ret_0;
139
    if (ctype == PS_SLEEP) {
138
    if (stype == FIBRIL_SLEEP) {
140
        if (list_empty(&ready_list) && list_empty(&serialized_list))
139
        if (list_empty(&ready_list) && list_empty(&serialized_list))
141
            goto ret_0;
140
            goto ret_0;
142
    }
141
    }
143
 
142
 
144
    if (ctype == PS_FROM_MANAGER) {
143
    if (stype == FIBRIL_FROM_MANAGER) {
145
        if (list_empty(&ready_list) && list_empty(&serialized_list))
144
        if (list_empty(&ready_list) && list_empty(&serialized_list))
146
            goto ret_0;
145
            goto ret_0;
147
        /*
146
        /*
148
         * Do not preempt if there is not sufficient count of thread
147
         * Do not preempt if there is not sufficient count of thread
149
         * managers.
148
         * managers.
150
         */
149
         */
151
        if (list_empty(&serialized_list) && threads_in_manager <=
150
        if (list_empty(&serialized_list) && fibrils_in_manager <=
152
            serialized_threads) {
151
            serialized_threads) {
153
            goto ret_0;
152
            goto ret_0;
154
        }
153
        }
155
    }
154
    }
156
    /* If we are going to manager and none exists, create it */
155
    /* If we are going to manager and none exists, create it */
157
    if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
156
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
158
        while (list_empty(&manager_list)) {
157
        while (list_empty(&manager_list)) {
159
            futex_up(&psthread_futex);
158
            futex_up(&fibril_futex);
160
            async_create_manager();
159
            async_create_manager();
161
            futex_down(&psthread_futex);
160
            futex_down(&fibril_futex);
162
        }
161
        }
163
    }
162
    }
164
   
163
   
165
    srcpt = __tcb_get()->pst_data;
164
    srcf = __tcb_get()->fibril_data;
166
    if (ctype != PS_FROM_DEAD) {
165
    if (stype != FIBRIL_FROM_DEAD) {
167
        /* Save current state */
166
        /* Save current state */
168
        if (!context_save(&srcpt->ctx)) {
167
        if (!context_save(&srcf->ctx)) {
169
            if (serialization_count)
168
            if (serialization_count)
170
                srcpt->flags &= ~PSTHREAD_SERIALIZED;
169
                srcf->flags &= ~FIBRIL_SERIALIZED;
171
            if (srcpt->clean_after_me) {
170
            if (srcf->clean_after_me) {
172
                /*
171
                /*
173
                 * Cleanup after the dead pseudo thread from
172
                 * Cleanup after the dead fibril from which we
174
                 * which we restored context here.
173
                 * restored context here.
175
                 */
174
                 */
176
                free(srcpt->clean_after_me->stack);
175
                free(srcf->clean_after_me->stack);
177
                psthread_teardown(srcpt->clean_after_me);
176
                fibril_teardown(srcf->clean_after_me);
178
                srcpt->clean_after_me = NULL;
177
                srcf->clean_after_me = NULL;
179
            }
178
            }
180
            return 1;   /* futex_up already done here */
179
            return 1;   /* futex_up already done here */
181
        }
180
        }
182
 
181
 
183
        /* Save myself to the correct run list */
182
        /* Save myself to the correct run list */
184
        if (ctype == PS_PREEMPT)
183
        if (stype == FIBRIL_PREEMPT)
185
            list_append(&srcpt->link, &ready_list);
184
            list_append(&srcf->link, &ready_list);
186
        else if (ctype == PS_FROM_MANAGER) {
185
        else if (stype == FIBRIL_FROM_MANAGER) {
187
            list_append(&srcpt->link, &manager_list);
186
            list_append(&srcf->link, &manager_list);
188
            threads_in_manager--;
187
            fibrils_in_manager--;
189
        } else {   
188
        } else {   
190
            /*
189
            /*
191
             * If ctype == PS_TO_MANAGER, don't save ourselves to
190
             * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
192
             * any list, we should already be somewhere, or we will
191
             * any list, we should already be somewhere, or we will
193
             * be lost.
192
             * be lost.
194
             *
193
             *
195
             * The ctype == PS_SLEEP case is similar. The pseudo
194
             * The stype == FIBRIL_SLEEP case is similar. The fibril
196
             * thread has an external refernce which can be used to
195
             * has an external refernce which can be used to wake it
197
             * wake it up once that time has come.
196
             * up once that time has come.
198
             */
197
             */
199
        }
198
        }
200
    }
199
    }
201
 
200
 
202
    /* Choose new thread to run */
201
    /* Choose a new fibril to run */
203
    if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
202
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
204
        dstpt = list_get_instance(manager_list.next, psthread_data_t,
203
        dstf = list_get_instance(manager_list.next, fibril_t, link);
205
            link);
-
 
206
        if (serialization_count && ctype == PS_TO_MANAGER) {
204
        if (serialization_count && stype == FIBRIL_TO_MANAGER) {
207
            serialized_threads++;
205
            serialized_threads++;
208
            srcpt->flags |= PSTHREAD_SERIALIZED;
206
            srcf->flags |= FIBRIL_SERIALIZED;
209
        }
207
        }
210
        threads_in_manager++;
208
        fibrils_in_manager++;
211
 
209
 
212
        if (ctype == PS_FROM_DEAD)
210
        if (stype == FIBRIL_FROM_DEAD)
213
            dstpt->clean_after_me = srcpt;
211
            dstf->clean_after_me = srcf;
214
    } else {
212
    } else {
215
        if (!list_empty(&serialized_list)) {
213
        if (!list_empty(&serialized_list)) {
216
            dstpt = list_get_instance(serialized_list.next,
214
            dstf = list_get_instance(serialized_list.next, fibril_t,
217
                psthread_data_t, link);
215
                link);
218
            serialized_threads--;
216
            serialized_threads--;
219
        } else {
217
        } else {
220
            dstpt = list_get_instance(ready_list.next,
218
            dstf = list_get_instance(ready_list.next, fibril_t,
221
                psthread_data_t, link);
219
                link);
222
        }
220
        }
223
    }
221
    }
224
    list_remove(&dstpt->link);
222
    list_remove(&dstf->link);
225
 
223
 
226
    futex_up(&psthread_futex);
224
    futex_up(&fibril_futex);
227
    context_restore(&dstpt->ctx);
225
    context_restore(&dstf->ctx);
228
    /* not reached */
226
    /* not reached */
229
 
227
 
230
ret_0:
228
ret_0:
231
    futex_up(&psthread_futex);
229
    futex_up(&fibril_futex);
232
    return retval;
230
    return retval;
233
}
231
}
234
 
232
 
235
/** Wait for uspace pseudo thread to finish.
233
/** Wait for fibril to finish.
236
 *
234
 *
237
 * Each pseudo thread can be only joined by one other pseudo thread. Moreover,
235
 * Each fibril can be only joined by one other fibril. Moreover, the joiner must
238
 * the joiner must be from the same thread as the joinee.
236
 * be from the same thread as the joinee.
239
 *
237
 *
240
 * @param psthrid   Pseudo thread to join.
238
 * @param fid       Fibril to join.
241
 *
239
 *
242
 * @return      Value returned by the finished thread.
240
 * @return      Value returned by the completed fibril.
243
 */
241
 */
244
int psthread_join(pstid_t psthrid)
242
int fibril_join(fid_t fid)
245
{
243
{
246
    psthread_data_t *pt;
244
    fibril_t *f;
247
    psthread_data_t *cur;
245
    fibril_t *cur;
248
 
246
 
249
    /* Handle psthrid = Kernel address -> it is wait for call */
247
    /* Handle fid = Kernel address -> it is wait for call */
250
    pt = (psthread_data_t *) psthrid;
248
    f = (fibril_t *) fid;
251
 
249
 
252
    /*
250
    /*
253
     * The joiner is running so the joinee isn't.
251
     * The joiner is running so the joinee isn't.
254
     */
252
     */
255
    cur = __tcb_get()->pst_data;
253
    cur = __tcb_get()->fibril_data;
256
    pt->joiner = cur;
254
    f->joiner = cur;
257
    psthread_schedule_next_adv(PS_SLEEP);
255
    fibril_schedule_next_adv(FIBRIL_SLEEP);
258
 
256
 
259
    /*
257
    /*
260
     * The joinee fills in the return value.
258
     * The joinee fills in the return value.
261
     */
259
     */
262
    return cur->joinee_retval;
260
    return cur->joinee_retval;
263
}
261
}
264
 
262
 
265
/** Create a userspace pseudo thread.
263
/** Create a new fibril.
266
 *
264
 *
267
 * @param func      Pseudo thread function.
265
 * @param func      Implementing function of the new fibril.
268
 * @param arg       Argument to pass to func.
266
 * @param arg       Argument to pass to func.
269
 *
267
 *
270
 * @return      Return 0 on failure or TLS of the new pseudo thread.
268
 * @return      Return 0 on failure or TLS of the new fibril.
271
 */
269
 */
272
pstid_t psthread_create(int (*func)(void *), void *arg)
270
fid_t fibril_create(int (*func)(void *), void *arg)
273
{
271
{
274
    psthread_data_t *pt;
272
    fibril_t *f;
275
 
273
 
276
    pt = psthread_setup();
274
    f = fibril_setup();
277
    if (!pt)
275
    if (!f)
278
        return 0;
276
        return 0;
279
    pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO *
277
    f->stack = (char *) malloc(FIBRIL_INITIAL_STACK_PAGES_NO *
280
        getpagesize());
278
        getpagesize());
281
 
279
 
282
    if (!pt->stack) {
280
    if (!f->stack) {
283
        psthread_teardown(pt);
281
        fibril_teardown(f);
284
        return 0;
282
        return 0;
285
    }
283
    }
286
 
284
 
287
    pt->arg= arg;
285
    f->arg = arg;
288
    pt->func = func;
286
    f->func = func;
289
    pt->clean_after_me = NULL;
287
    f->clean_after_me = NULL;
290
    pt->joiner = NULL;
288
    f->joiner = NULL;
291
    pt->joinee_retval = 0;
289
    f->joinee_retval = 0;
292
    pt->retval = 0;
290
    f->retval = 0;
293
    pt->flags = 0;
291
    f->flags = 0;
294
 
292
 
295
    context_save(&pt->ctx);
293
    context_save(&f->ctx);
296
    context_set(&pt->ctx, FADDR(psthread_main), pt->stack,
294
    context_set(&f->ctx, FADDR(fibril_main), f->stack,
297
        PSTHREAD_INITIAL_STACK_PAGES_NO * getpagesize(), pt->tcb);
295
        FIBRIL_INITIAL_STACK_PAGES_NO * getpagesize(), f->tcb);
298
 
296
 
299
    return (pstid_t) pt;
297
    return (fid_t) f;
300
}
298
}
301
 
299
 
302
/** Add a thread to the ready list.
300
/** Add a fibril to the ready list.
303
 *
301
 *
304
 * @param psthrid       Pinter to the pseudo thread structure of the
302
 * @param fid       Pinter to the fibril structure of the fibril to be added.
305
 *              pseudo thread to be added.
-
 
306
 */
303
 */
307
void psthread_add_ready(pstid_t psthrid)
304
void fibril_add_ready(fid_t fid)
308
{
305
{
309
    psthread_data_t *pt;
306
    fibril_t *f;
310
 
307
 
311
    pt = (psthread_data_t *) psthrid;
308
    f = (fibril_t *) fid;
312
    futex_down(&psthread_futex);
309
    futex_down(&fibril_futex);
313
    if ((pt->flags & PSTHREAD_SERIALIZED))
310
    if ((f->flags & FIBRIL_SERIALIZED))
314
        list_append(&pt->link, &serialized_list);
311
        list_append(&f->link, &serialized_list);
315
    else
312
    else
316
        list_append(&pt->link, &ready_list);
313
        list_append(&f->link, &ready_list);
317
    futex_up(&psthread_futex);
314
    futex_up(&fibril_futex);
318
}
315
}
319
 
316
 
320
/** Add a pseudo thread to the manager list.
317
/** Add a fibril to the manager list.
321
 *
318
 *
322
 * @param psthrid       Pinter to the pseudo thread structure of the
319
 * @param fid       Pinter to the fibril structure of the fibril to be added.
323
 *              pseudo thread to be added.
-
 
324
 */
320
 */
325
void psthread_add_manager(pstid_t psthrid)
321
void fibril_add_manager(fid_t fid)
326
{
322
{
327
    psthread_data_t *pt;
323
    fibril_t *f;
328
 
324
 
329
    pt = (psthread_data_t *) psthrid;
325
    f = (fibril_t *) fid;
330
 
326
 
331
    futex_down(&psthread_futex);
327
    futex_down(&fibril_futex);
332
    list_append(&pt->link, &manager_list);
328
    list_append(&f->link, &manager_list);
333
    futex_up(&psthread_futex);
329
    futex_up(&fibril_futex);
334
}
330
}
335
 
331
 
336
/** Remove one manager from manager list */
332
/** Remove one manager from the manager list. */
337
void psthread_remove_manager(void)
333
void fibril_remove_manager(void)
338
{
334
{
339
    futex_down(&psthread_futex);
335
    futex_down(&fibril_futex);
340
    if (list_empty(&manager_list)) {
336
    if (list_empty(&manager_list)) {
341
        futex_up(&psthread_futex);
337
        futex_up(&fibril_futex);
342
        return;
338
        return;
343
    }
339
    }
344
    list_remove(manager_list.next);
340
    list_remove(manager_list.next);
345
    futex_up(&psthread_futex);
341
    futex_up(&fibril_futex);
346
}
342
}
347
 
343
 
348
/** Return thread id of the currently running thread.
344
/** Return fibril id of the currently running fibril.
349
 *
345
 *
350
 * @return      Pseudo thread ID of the currently running pseudo thread.
346
 * @return      Fibril ID of the currently running pseudo thread.
351
 */
347
 */
352
pstid_t psthread_get_id(void)
348
fid_t fibril_get_id(void)
353
{
349
{
354
    return (pstid_t) __tcb_get()->pst_data;
350
    return (fid_t) __tcb_get()->fibril_data;
355
}
351
}
356
 
352
 
357
/** Disable preemption
353
/** Disable preemption
358
 *
354
 *
359
 * If the thread wants to send several message in a row and does not want to be
355
 * If the fibril wants to send several message in a row and does not want to be
360
 * preempted, it should start async_serialize_start() in the beginning of
356
 * preempted, it should start async_serialize_start() in the beginning of
361
 * communication and async_serialize_end() in the end. If it is a true
357
 * communication and async_serialize_end() in the end. If it is a true
362
 * multithreaded application, it should protect the communication channel by a
358
 * multithreaded application, it should protect the communication channel by a
363
 * futex as well. Interrupt messages can still be preempted.
359
 * futex as well. Interrupt messages can still be preempted.
364
 */
360
 */
365
void psthread_inc_sercount(void)
361
void fibril_inc_sercount(void)
366
{
362
{
367
    serialization_count++;
363
    serialization_count++;
368
}
364
}
369
 
365
 
370
/** Restore the preemption counter to the previous state. */
366
/** Restore the preemption counter to the previous state. */
371
void psthread_dec_sercount(void)
367
void fibril_dec_sercount(void)
372
{
368
{
373
    serialization_count--;
369
    serialization_count--;
374
}
370
}
375
 
371
 
376
/** @}
372
/** @}