Subversion Repositories HelenOS-historic

Rev

Rev 1407 | Rev 1610 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1113 palkovsky 1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
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
15
 *   derived from this software without specific prior written permission.
16
 *
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
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include <libadt/list.h>
30
#include <psthread.h>
31
#include <malloc.h>
32
#include <unistd.h>
33
#include <thread.h>
34
#include <stdio.h>
1125 jermar 35
#include <kernel/arch/faddr.h>
1392 palkovsky 36
#include <futex.h>
37
#include <assert.h>
1407 palkovsky 38
#include <async.h>
1113 palkovsky 39
 
1155 vana 40
#ifndef PSTHREAD_INITIAL_STACK_PAGES_NO
41
#define PSTHREAD_INITIAL_STACK_PAGES_NO 1
42
#endif
43
 
1113 palkovsky 44
static LIST_INITIALIZE(ready_list);
1392 palkovsky 45
static LIST_INITIALIZE(manager_list);
1113 palkovsky 46
 
1128 jermar 47
static void psthread_exit(void) __attribute__ ((noinline));
48
static void psthread_main(void);
1125 jermar 49
 
1392 palkovsky 50
static atomic_t psthread_futex = FUTEX_INITIALIZER;
51
 
1129 palkovsky 52
/** Setup PSthread information into TCB structure */
1392 palkovsky 53
psthread_data_t * psthread_setup()
1129 palkovsky 54
{
55
    psthread_data_t *pt;
1392 palkovsky 56
    tcb_t *tcb;
1129 palkovsky 57
 
1392 palkovsky 58
    tcb = __make_tls();
59
    if (!tcb)
60
        return NULL;
61
 
1129 palkovsky 62
    pt = malloc(sizeof(*pt));
63
    if (!pt) {
1392 palkovsky 64
        __free_tls(tcb);
1129 palkovsky 65
        return NULL;
66
    }
67
 
68
    tcb->pst_data = pt;
69
    pt->tcb = tcb;
70
 
71
    return pt;
72
}
73
 
74
void psthread_teardown(psthread_data_t *pt)
75
{
1392 palkovsky 76
    __free_tls(pt->tcb);
1129 palkovsky 77
    free(pt);
78
}
79
 
1128 jermar 80
/** Function to preempt to other pseudo thread without adding
81
 * currently running pseudo thread to ready_list.
1113 palkovsky 82
 */
1128 jermar 83
void psthread_exit(void)
1113 palkovsky 84
{
85
    psthread_data_t *pt;
86
 
1392 palkovsky 87
    futex_down(&psthread_futex);
88
 
89
    if (!list_empty(&ready_list))
90
        pt = list_get_instance(ready_list.next, psthread_data_t, link);
91
    else if (!list_empty(&manager_list))
92
        pt = list_get_instance(manager_list.next, psthread_data_t, link);
93
    else {
94
        printf("Cannot find suitable psthread to run.\n");
1113 palkovsky 95
        _exit(0);
96
    }
1128 jermar 97
    list_remove(&pt->link);
1392 palkovsky 98
    futex_up(&psthread_futex);
99
 
1113 palkovsky 100
    context_restore(&pt->ctx);
1392 palkovsky 101
    /* Never reached */
1113 palkovsky 102
}
103
 
104
/** Function that is called on entry to new uspace thread */
1128 jermar 105
void psthread_main(void)
1113 palkovsky 106
{
1129 palkovsky 107
    psthread_data_t *pt = __tcb_get()->pst_data;
108
 
1113 palkovsky 109
    pt->retval = pt->func(pt->arg);
110
 
111
    pt->finished = 1;
112
    if (pt->waiter)
1128 jermar 113
        list_append(&pt->waiter->link, &ready_list);
1113 palkovsky 114
 
1128 jermar 115
    psthread_exit();
1113 palkovsky 116
}
117
 
1128 jermar 118
/** Schedule next userspace pseudo thread.
119
 *
1427 palkovsky 120
 * If calling with PS_TO_MANAGER parameter, the async_futex should be
121
 * held.
122
 *
1392 palkovsky 123
 * @param tomanager If true, we are switching to next ready manager thread
124
 *                  (if none is found, thread is exited)
125
 * @param frommanager If true, we are switching from manager thread
1128 jermar 126
 * @return 0 if there is no ready pseudo thread, 1 otherwise.
127
 */
1392 palkovsky 128
int psthread_schedule_next_adv(pschange_type ctype)
1113 palkovsky 129
{
130
    psthread_data_t *pt;
1392 palkovsky 131
    int retval = 0;
132
 
133
    futex_down(&psthread_futex);
1113 palkovsky 134
 
1392 palkovsky 135
    if (ctype == PS_PREEMPT && list_empty(&ready_list))
136
        goto ret_0;
1113 palkovsky 137
 
1392 palkovsky 138
    if (ctype == PS_FROM_MANAGER && list_empty(&ready_list)) {
139
        goto ret_0;
140
    }
1407 palkovsky 141
    /* If we are going to manager and none exists, create it */
1427 palkovsky 142
    while (ctype == PS_TO_MANAGER && list_empty(&manager_list)) {
143
        futex_up(&psthread_futex);
1407 palkovsky 144
        async_create_manager();
1427 palkovsky 145
        futex_down(&psthread_futex);
146
    }
1392 palkovsky 147
 
1129 palkovsky 148
    pt = __tcb_get()->pst_data;
1392 palkovsky 149
    if (!context_save(&pt->ctx))
150
        return 1; // futex_up already done here
151
 
152
    if (ctype == PS_PREEMPT)
153
        list_append(&pt->link, &ready_list);
154
    else if (ctype == PS_FROM_MANAGER)
155
        list_append(&pt->link, &manager_list);
1113 palkovsky 156
 
1392 palkovsky 157
    if (ctype == PS_TO_MANAGER)
158
        pt = list_get_instance(manager_list.next,psthread_data_t, link);
159
    else
160
        pt = list_get_instance(ready_list.next, psthread_data_t, link);
1128 jermar 161
    list_remove(&pt->link);
1113 palkovsky 162
 
1392 palkovsky 163
    futex_up(&psthread_futex);
1113 palkovsky 164
    context_restore(&pt->ctx);
1392 palkovsky 165
 
166
ret_0:
167
    futex_up(&psthread_futex);
168
    return retval;
1113 palkovsky 169
}
170
 
1128 jermar 171
/** Wait for uspace pseudo thread to finish.
172
 *
173
 * @param psthrid Pseudo thread to wait for.
174
 *
175
 * @return Value returned by the finished thread.
176
 */
177
int psthread_join(pstid_t psthrid)
1113 palkovsky 178
{
1125 jermar 179
    volatile psthread_data_t *pt, *mypt;
180
    volatile int retval;
1113 palkovsky 181
 
182
    /* Handle psthrid = Kernel address -> it is wait for call */
183
    pt = (psthread_data_t *) psthrid;
184
 
185
    if (!pt->finished) {
1129 palkovsky 186
        mypt = __tcb_get()->pst_data;
1125 jermar 187
        if (context_save(&((psthread_data_t *) mypt)->ctx)) {
188
            pt->waiter = (psthread_data_t *) mypt;
1128 jermar 189
            psthread_exit();
1113 palkovsky 190
        }
191
    }
192
    retval = pt->retval;
193
 
194
    free(pt->stack);
1129 palkovsky 195
    psthread_teardown((void *)pt);
1113 palkovsky 196
 
197
    return retval;
198
}
199
 
200
/**
1392 palkovsky 201
 * Create a userspace thread
1113 palkovsky 202
 *
1128 jermar 203
 * @param func Pseudo thread function.
204
 * @param arg Argument to pass to func.
205
 *
206
 * @return 0 on failure, TLS of the new pseudo thread.
1113 palkovsky 207
 */
208
pstid_t psthread_create(int (*func)(void *), void *arg)
209
{
210
    psthread_data_t *pt;
211
 
1392 palkovsky 212
    pt = psthread_setup();
213
    if (!pt)
1129 palkovsky 214
        return 0;
1155 vana 215
    pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize());
1113 palkovsky 216
 
217
    if (!pt->stack) {
1129 palkovsky 218
        psthread_teardown(pt);
1113 palkovsky 219
        return 0;
220
    }
221
 
222
    pt->arg= arg;
223
    pt->func = func;
224
    pt->finished = 0;
225
    pt->waiter = NULL;
226
 
227
    context_save(&pt->ctx);
1155 vana 228
    context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(),
1392 palkovsky 229
            pt->tcb);
1113 palkovsky 230
 
1392 palkovsky 231
    return (pstid_t )pt;
232
}
233
 
234
/** Add a thread to ready list */
235
void psthread_add_ready(pstid_t psthrid)
236
{
237
    psthread_data_t *pt;
238
 
239
    pt = (psthread_data_t *) psthrid;
240
    futex_down(&psthread_futex);
1128 jermar 241
    list_append(&pt->link, &ready_list);
1392 palkovsky 242
    futex_up(&psthread_futex);
243
}
1113 palkovsky 244
 
1392 palkovsky 245
/** Add a thread to manager list */
246
void psthread_add_manager(pstid_t psthrid)
247
{
248
    psthread_data_t *pt;
249
 
250
    pt = (psthread_data_t *) psthrid;
251
 
252
    futex_down(&psthread_futex);
253
    list_append(&pt->link, &manager_list);
254
    futex_up(&psthread_futex);
1113 palkovsky 255
}
1392 palkovsky 256
 
257
/** Remove one manager from manager list */
258
void psthread_remove_manager()
259
{
260
    futex_down(&psthread_futex);
261
    if (list_empty(&manager_list)) {
262
        futex_up(&psthread_futex);
263
        return;
264
    }
265
    list_remove(manager_list.next);
266
    futex_up(&psthread_futex);
267
}
1427 palkovsky 268
 
269
/** Return thread id of current running thread */
270
pstid_t psthread_get_id(void)
271
{
272
    return (pstid_t)__tcb_get()->pst_data;
273
}