Subversion Repositories HelenOS-historic

Rev

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

Rev 1614 Rev 1653
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
-
 
30
 * @{
-
 
31
 */
-
 
32
/** @file
-
 
33
 */
-
 
34
 
29
#include <libadt/list.h>
35
#include <libadt/list.h>
30
#include <psthread.h>
36
#include <psthread.h>
31
#include <malloc.h>
37
#include <malloc.h>
32
#include <unistd.h>
38
#include <unistd.h>
33
#include <thread.h>
39
#include <thread.h>
34
#include <stdio.h>
40
#include <stdio.h>
35
#include <kernel/arch/faddr.h>
41
#include <kernel/arch/faddr.h>
36
#include <futex.h>
42
#include <futex.h>
37
#include <assert.h>
43
#include <assert.h>
38
#include <async.h>
44
#include <async.h>
39
 
45
 
40
#ifndef PSTHREAD_INITIAL_STACK_PAGES_NO
46
#ifndef PSTHREAD_INITIAL_STACK_PAGES_NO
41
#define PSTHREAD_INITIAL_STACK_PAGES_NO 1
47
#define PSTHREAD_INITIAL_STACK_PAGES_NO 1
42
#endif
48
#endif
43
 
49
 
44
static LIST_INITIALIZE(ready_list);
50
static LIST_INITIALIZE(ready_list);
45
static LIST_INITIALIZE(serialized_list);
51
static LIST_INITIALIZE(serialized_list);
46
static LIST_INITIALIZE(manager_list);
52
static LIST_INITIALIZE(manager_list);
47
 
53
 
48
static void psthread_exit(void) __attribute__ ((noinline));
54
static void psthread_exit(void) __attribute__ ((noinline));
49
static void psthread_main(void);
55
static void psthread_main(void);
50
 
56
 
51
static atomic_t psthread_futex = FUTEX_INITIALIZER;
57
static atomic_t psthread_futex = FUTEX_INITIALIZER;
52
/** Count of real threads that are in async_serialized mode */
58
/** Count of real threads that are in async_serialized mode */
53
static int serialized_threads; /* Protected by async_futex */
59
static int serialized_threads; /* Protected by async_futex */
54
/** Thread-local count of serialization. If >0, we must not preempt */
60
/** Thread-local count of serialization. If >0, we must not preempt */
55
static __thread int serialization_count;
61
static __thread int serialization_count;
56
/** Counter of threads residing in async_manager */
62
/** Counter of threads residing in async_manager */
57
static int threads_in_manager;
63
static int threads_in_manager;
58
 
64
 
59
/** Setup PSthread information into TCB structure */
65
/** Setup PSthread information into TCB structure */
60
psthread_data_t * psthread_setup()
66
psthread_data_t * psthread_setup()
61
{
67
{
62
    psthread_data_t *pt;
68
    psthread_data_t *pt;
63
    tcb_t *tcb;
69
    tcb_t *tcb;
64
 
70
 
65
    tcb = __make_tls();
71
    tcb = __make_tls();
66
    if (!tcb)
72
    if (!tcb)
67
        return NULL;
73
        return NULL;
68
 
74
 
69
    pt = malloc(sizeof(*pt));
75
    pt = malloc(sizeof(*pt));
70
    if (!pt) {
76
    if (!pt) {
71
        __free_tls(tcb);
77
        __free_tls(tcb);
72
        return NULL;
78
        return NULL;
73
    }
79
    }
74
 
80
 
75
    tcb->pst_data = pt;
81
    tcb->pst_data = pt;
76
    pt->tcb = tcb;
82
    pt->tcb = tcb;
77
 
83
 
78
    return pt;
84
    return pt;
79
}
85
}
80
 
86
 
81
void psthread_teardown(psthread_data_t *pt)
87
void psthread_teardown(psthread_data_t *pt)
82
{
88
{
83
    __free_tls(pt->tcb);
89
    __free_tls(pt->tcb);
84
    free(pt);
90
    free(pt);
85
}
91
}
86
 
92
 
87
/** Function that is called on entry to new uspace thread */
93
/** Function that is called on entry to new uspace thread */
88
void psthread_main(void)
94
void psthread_main(void)
89
{
95
{
90
    psthread_data_t *pt = __tcb_get()->pst_data;
96
    psthread_data_t *pt = __tcb_get()->pst_data;
91
 
97
 
92
    pt->retval = pt->func(pt->arg);
98
    pt->retval = pt->func(pt->arg);
93
 
99
 
94
    pt->finished = 1;
100
    pt->finished = 1;
95
    if (pt->waiter)
101
    if (pt->waiter)
96
        list_append(&pt->waiter->link, &ready_list);
102
        list_append(&pt->waiter->link, &ready_list);
97
 
103
 
98
    psthread_schedule_next_adv(PS_FROM_DEAD);
104
    psthread_schedule_next_adv(PS_FROM_DEAD);
99
}
105
}
100
 
106
 
101
/** Schedule next userspace pseudo thread.
107
/** Schedule next userspace pseudo thread.
102
 *
108
 *
103
 * If calling with PS_TO_MANAGER parameter, the async_futex should be
109
 * If calling with PS_TO_MANAGER parameter, the async_futex should be
104
 * held.
110
 * held.
105
 *
111
 *
106
 * @param tomanager If true, we are switching to next ready manager thread
112
 * @param tomanager If true, we are switching to next ready manager thread
107
 *                  (if none is found, thread is exited)
113
 *                  (if none is found, thread is exited)
108
 * @param frommanager If true, we are switching from manager thread
114
 * @param frommanager If true, we are switching from manager thread
109
 * @return 0 if there is no ready pseudo thread, 1 otherwise.
115
 * @return 0 if there is no ready pseudo thread, 1 otherwise.
110
 */
116
 */
111
int psthread_schedule_next_adv(pschange_type ctype)
117
int psthread_schedule_next_adv(pschange_type ctype)
112
{
118
{
113
    psthread_data_t *srcpt, *dstpt;
119
    psthread_data_t *srcpt, *dstpt;
114
    int retval = 0;
120
    int retval = 0;
115
   
121
   
116
    futex_down(&psthread_futex);
122
    futex_down(&psthread_futex);
117
 
123
 
118
    if (ctype == PS_PREEMPT && list_empty(&ready_list))
124
    if (ctype == PS_PREEMPT && list_empty(&ready_list))
119
        goto ret_0;
125
        goto ret_0;
120
 
126
 
121
    if (ctype == PS_FROM_MANAGER) {
127
    if (ctype == PS_FROM_MANAGER) {
122
        if (list_empty(&ready_list) && list_empty(&serialized_list))
128
        if (list_empty(&ready_list) && list_empty(&serialized_list))
123
            goto ret_0;
129
            goto ret_0;
124
        /* Do not preempt if there is not sufficient count of thread managers */
130
        /* Do not preempt if there is not sufficient count of thread managers */
125
        if (list_empty(&serialized_list) && threads_in_manager <= serialized_threads) {
131
        if (list_empty(&serialized_list) && threads_in_manager <= serialized_threads) {
126
            goto ret_0;
132
            goto ret_0;
127
        }
133
        }
128
    }
134
    }
129
    /* If we are going to manager and none exists, create it */
135
    /* If we are going to manager and none exists, create it */
130
    if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
136
    if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
131
        while (list_empty(&manager_list)) {
137
        while (list_empty(&manager_list)) {
132
            futex_up(&psthread_futex);
138
            futex_up(&psthread_futex);
133
            async_create_manager();
139
            async_create_manager();
134
            futex_down(&psthread_futex);
140
            futex_down(&psthread_futex);
135
        }
141
        }
136
    }
142
    }
137
   
143
   
138
    if (ctype != PS_FROM_DEAD) {
144
    if (ctype != PS_FROM_DEAD) {
139
        /* Save current state */
145
        /* Save current state */
140
        srcpt = __tcb_get()->pst_data;
146
        srcpt = __tcb_get()->pst_data;
141
        if (!context_save(&srcpt->ctx)) {
147
        if (!context_save(&srcpt->ctx)) {
142
            if (serialization_count)
148
            if (serialization_count)
143
                srcpt->flags &= ~PSTHREAD_SERIALIZED;
149
                srcpt->flags &= ~PSTHREAD_SERIALIZED;
144
            return 1; // futex_up already done here
150
            return 1; // futex_up already done here
145
        }
151
        }
146
 
152
 
147
        /* Save myself to correct run list */
153
        /* Save myself to correct run list */
148
        if (ctype == PS_PREEMPT)
154
        if (ctype == PS_PREEMPT)
149
            list_append(&srcpt->link, &ready_list);
155
            list_append(&srcpt->link, &ready_list);
150
        else if (ctype == PS_FROM_MANAGER) {
156
        else if (ctype == PS_FROM_MANAGER) {
151
            list_append(&srcpt->link, &manager_list);
157
            list_append(&srcpt->link, &manager_list);
152
            threads_in_manager--;
158
            threads_in_manager--;
153
        } /* If ctype == PS_TO_MANAGER, don't save ourselves to any list, we should
159
        } /* If ctype == PS_TO_MANAGER, don't save ourselves to any list, we should
154
           * already be somewhere, or we will be lost */
160
           * already be somewhere, or we will be lost */
155
    }
161
    }
156
 
162
 
157
    /* Choose new thread to run */
163
    /* Choose new thread to run */
158
    if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
164
    if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) {
159
        dstpt = list_get_instance(manager_list.next,psthread_data_t, link);
165
        dstpt = list_get_instance(manager_list.next,psthread_data_t, link);
160
        if (serialization_count && ctype == PS_TO_MANAGER) {
166
        if (serialization_count && ctype == PS_TO_MANAGER) {
161
            serialized_threads++;
167
            serialized_threads++;
162
            srcpt->flags |= PSTHREAD_SERIALIZED;
168
            srcpt->flags |= PSTHREAD_SERIALIZED;
163
        }
169
        }
164
        threads_in_manager++;
170
        threads_in_manager++;
165
    } else {
171
    } else {
166
        if (!list_empty(&serialized_list)) {
172
        if (!list_empty(&serialized_list)) {
167
            dstpt = list_get_instance(serialized_list.next, psthread_data_t, link);
173
            dstpt = list_get_instance(serialized_list.next, psthread_data_t, link);
168
            serialized_threads--;
174
            serialized_threads--;
169
        } else
175
        } else
170
            dstpt = list_get_instance(ready_list.next, psthread_data_t, link);
176
            dstpt = list_get_instance(ready_list.next, psthread_data_t, link);
171
    }
177
    }
172
    list_remove(&dstpt->link);
178
    list_remove(&dstpt->link);
173
 
179
 
174
    futex_up(&psthread_futex);
180
    futex_up(&psthread_futex);
175
    context_restore(&dstpt->ctx);
181
    context_restore(&dstpt->ctx);
176
 
182
 
177
ret_0:
183
ret_0:
178
    futex_up(&psthread_futex);
184
    futex_up(&psthread_futex);
179
    return retval;
185
    return retval;
180
}
186
}
181
 
187
 
182
/** Wait for uspace pseudo thread to finish.
188
/** Wait for uspace pseudo thread to finish.
183
 *
189
 *
184
 * @param psthrid Pseudo thread to wait for.
190
 * @param psthrid Pseudo thread to wait for.
185
 *
191
 *
186
 * @return Value returned by the finished thread.
192
 * @return Value returned by the finished thread.
187
 */
193
 */
188
int psthread_join(pstid_t psthrid)
194
int psthread_join(pstid_t psthrid)
189
{
195
{
190
    volatile psthread_data_t *pt, *mypt;
196
    volatile psthread_data_t *pt, *mypt;
191
    volatile int retval;
197
    volatile int retval;
192
 
198
 
193
    /* Handle psthrid = Kernel address -> it is wait for call */
199
    /* Handle psthrid = Kernel address -> it is wait for call */
194
    pt = (psthread_data_t *) psthrid;
200
    pt = (psthread_data_t *) psthrid;
195
 
201
 
196
    /* TODO */
202
    /* TODO */
197
    printf("join unsupported\n");
203
    printf("join unsupported\n");
198
    _exit(1);
204
    _exit(1);
199
 
205
 
200
    retval = pt->retval;
206
    retval = pt->retval;
201
 
207
 
202
    free(pt->stack);
208
    free(pt->stack);
203
    psthread_teardown((void *)pt);
209
    psthread_teardown((void *)pt);
204
 
210
 
205
    return retval;
211
    return retval;
206
}
212
}
207
 
213
 
208
/**
214
/**
209
 * Create a userspace thread
215
 * Create a userspace thread
210
 *
216
 *
211
 * @param func Pseudo thread function.
217
 * @param func Pseudo thread function.
212
 * @param arg Argument to pass to func.
218
 * @param arg Argument to pass to func.
213
 *
219
 *
214
 * @return 0 on failure, TLS of the new pseudo thread.
220
 * @return 0 on failure, TLS of the new pseudo thread.
215
 */
221
 */
216
pstid_t psthread_create(int (*func)(void *), void *arg)
222
pstid_t psthread_create(int (*func)(void *), void *arg)
217
{
223
{
218
    psthread_data_t *pt;
224
    psthread_data_t *pt;
219
 
225
 
220
    pt = psthread_setup();
226
    pt = psthread_setup();
221
    if (!pt)
227
    if (!pt)
222
        return 0;
228
        return 0;
223
    pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize());
229
    pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize());
224
 
230
 
225
    if (!pt->stack) {
231
    if (!pt->stack) {
226
        psthread_teardown(pt);
232
        psthread_teardown(pt);
227
        return 0;
233
        return 0;
228
    }
234
    }
229
 
235
 
230
    pt->arg= arg;
236
    pt->arg= arg;
231
    pt->func = func;
237
    pt->func = func;
232
    pt->finished = 0;
238
    pt->finished = 0;
233
    pt->waiter = NULL;
239
    pt->waiter = NULL;
234
    pt->flags = 0;
240
    pt->flags = 0;
235
 
241
 
236
    context_save(&pt->ctx);
242
    context_save(&pt->ctx);
237
    context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(),
243
    context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(),
238
            pt->tcb);
244
            pt->tcb);
239
 
245
 
240
    return (pstid_t )pt;
246
    return (pstid_t )pt;
241
}
247
}
242
 
248
 
243
/** Add a thread to ready list */
249
/** Add a thread to ready list */
244
void psthread_add_ready(pstid_t psthrid)
250
void psthread_add_ready(pstid_t psthrid)
245
{
251
{
246
    psthread_data_t *pt;
252
    psthread_data_t *pt;
247
 
253
 
248
    pt = (psthread_data_t *) psthrid;
254
    pt = (psthread_data_t *) psthrid;
249
    futex_down(&psthread_futex);
255
    futex_down(&psthread_futex);
250
    if ((pt->flags & PSTHREAD_SERIALIZED))
256
    if ((pt->flags & PSTHREAD_SERIALIZED))
251
        list_append(&pt->link, &serialized_list);
257
        list_append(&pt->link, &serialized_list);
252
    else
258
    else
253
        list_append(&pt->link, &ready_list);
259
        list_append(&pt->link, &ready_list);
254
    futex_up(&psthread_futex);
260
    futex_up(&psthread_futex);
255
}
261
}
256
 
262
 
257
/** Add a thread to manager list */
263
/** Add a thread to manager list */
258
void psthread_add_manager(pstid_t psthrid)
264
void psthread_add_manager(pstid_t psthrid)
259
{
265
{
260
    psthread_data_t *pt;
266
    psthread_data_t *pt;
261
 
267
 
262
    pt = (psthread_data_t *) psthrid;
268
    pt = (psthread_data_t *) psthrid;
263
 
269
 
264
    futex_down(&psthread_futex);
270
    futex_down(&psthread_futex);
265
    list_append(&pt->link, &manager_list);
271
    list_append(&pt->link, &manager_list);
266
    futex_up(&psthread_futex);
272
    futex_up(&psthread_futex);
267
}
273
}
268
 
274
 
269
/** Remove one manager from manager list */
275
/** Remove one manager from manager list */
270
void psthread_remove_manager()
276
void psthread_remove_manager()
271
{
277
{
272
    futex_down(&psthread_futex);
278
    futex_down(&psthread_futex);
273
    if (list_empty(&manager_list)) {
279
    if (list_empty(&manager_list)) {
274
        futex_up(&psthread_futex);
280
        futex_up(&psthread_futex);
275
        return;
281
        return;
276
    }
282
    }
277
    list_remove(manager_list.next);
283
    list_remove(manager_list.next);
278
    futex_up(&psthread_futex);
284
    futex_up(&psthread_futex);
279
}
285
}
280
 
286
 
281
/** Return thread id of current running thread */
287
/** Return thread id of current running thread */
282
pstid_t psthread_get_id(void)
288
pstid_t psthread_get_id(void)
283
{
289
{
284
    return (pstid_t)__tcb_get()->pst_data;
290
    return (pstid_t)__tcb_get()->pst_data;
285
}
291
}
286
 
292
 
287
/** Disable preemption
293
/** Disable preemption
288
 *
294
 *
289
 * If the thread wants to send several message in row and does not want
295
 * If the thread wants to send several message in row and does not want
290
 * to be preempted, it should start async_serialize_start() in the beginning
296
 * to be preempted, it should start async_serialize_start() in the beginning
291
 * of communication and async_serialize_end() in the end. If it is a
297
 * of communication and async_serialize_end() in the end. If it is a
292
 * true multithreaded application, it should protect the communication channel
298
 * true multithreaded application, it should protect the communication channel
293
 * by a futex as well. Interrupt messages will can still be preempted.
299
 * by a futex as well. Interrupt messages will can still be preempted.
294
 */
300
 */
295
void psthread_inc_sercount(void)
301
void psthread_inc_sercount(void)
296
{
302
{
297
    serialization_count++;
303
    serialization_count++;
298
}
304
}
299
 
305
 
300
void psthread_dec_sercount(void)
306
void psthread_dec_sercount(void)
301
{
307
{
302
    serialization_count--;
308
    serialization_count--;
303
}
309
}
-
 
310
 
-
 
311
 
-
 
312
 /** @}
-
 
313
 */
-
 
314
 
-
 
315
 
304
 
316