Subversion Repositories HelenOS-historic

Rev

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

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