Subversion Repositories HelenOS-historic

Rev

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

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