Subversion Repositories HelenOS-historic

Rev

Rev 1392 | Rev 1427 | 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
 *
1392 palkovsky 120
 * @param tomanager If true, we are switching to next ready manager thread
121
 *                  (if none is found, thread is exited)
122
 * @param frommanager If true, we are switching from manager thread
1128 jermar 123
 * @return 0 if there is no ready pseudo thread, 1 otherwise.
124
 */
1392 palkovsky 125
int psthread_schedule_next_adv(pschange_type ctype)
1113 palkovsky 126
{
127
    psthread_data_t *pt;
1392 palkovsky 128
    int retval = 0;
129
 
130
    futex_down(&psthread_futex);
1113 palkovsky 131
 
1392 palkovsky 132
    if (ctype == PS_PREEMPT && list_empty(&ready_list))
133
        goto ret_0;
1113 palkovsky 134
 
1392 palkovsky 135
    if (ctype == PS_FROM_MANAGER && list_empty(&ready_list)) {
136
        goto ret_0;
137
    }
1407 palkovsky 138
    /* If we are going to manager and none exists, create it */
139
    if (ctype == PS_TO_MANAGER && list_empty(&manager_list))
140
        async_create_manager();
1392 palkovsky 141
 
1129 palkovsky 142
    pt = __tcb_get()->pst_data;
1392 palkovsky 143
    if (!context_save(&pt->ctx))
144
        return 1; // futex_up already done here
145
 
146
    if (ctype == PS_PREEMPT)
147
        list_append(&pt->link, &ready_list);
148
    else if (ctype == PS_FROM_MANAGER)
149
        list_append(&pt->link, &manager_list);
1113 palkovsky 150
 
1392 palkovsky 151
    if (ctype == PS_TO_MANAGER)
152
        pt = list_get_instance(manager_list.next,psthread_data_t, link);
153
    else
154
        pt = list_get_instance(ready_list.next, psthread_data_t, link);
1128 jermar 155
    list_remove(&pt->link);
1113 palkovsky 156
 
1392 palkovsky 157
    futex_up(&psthread_futex);
1113 palkovsky 158
    context_restore(&pt->ctx);
1392 palkovsky 159
 
160
ret_0:
161
    futex_up(&psthread_futex);
162
    return retval;
1113 palkovsky 163
}
164
 
1128 jermar 165
/** Wait for uspace pseudo thread to finish.
166
 *
167
 * @param psthrid Pseudo thread to wait for.
168
 *
169
 * @return Value returned by the finished thread.
170
 */
171
int psthread_join(pstid_t psthrid)
1113 palkovsky 172
{
1125 jermar 173
    volatile psthread_data_t *pt, *mypt;
174
    volatile int retval;
1113 palkovsky 175
 
176
    /* Handle psthrid = Kernel address -> it is wait for call */
177
    pt = (psthread_data_t *) psthrid;
178
 
179
    if (!pt->finished) {
1129 palkovsky 180
        mypt = __tcb_get()->pst_data;
1125 jermar 181
        if (context_save(&((psthread_data_t *) mypt)->ctx)) {
182
            pt->waiter = (psthread_data_t *) mypt;
1128 jermar 183
            psthread_exit();
1113 palkovsky 184
        }
185
    }
186
    retval = pt->retval;
187
 
188
    free(pt->stack);
1129 palkovsky 189
    psthread_teardown((void *)pt);
1113 palkovsky 190
 
191
    return retval;
192
}
193
 
194
/**
1392 palkovsky 195
 * Create a userspace thread
1113 palkovsky 196
 *
1128 jermar 197
 * @param func Pseudo thread function.
198
 * @param arg Argument to pass to func.
199
 *
200
 * @return 0 on failure, TLS of the new pseudo thread.
1113 palkovsky 201
 */
202
pstid_t psthread_create(int (*func)(void *), void *arg)
203
{
204
    psthread_data_t *pt;
205
 
1392 palkovsky 206
    pt = psthread_setup();
207
    if (!pt)
1129 palkovsky 208
        return 0;
1155 vana 209
    pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize());
1113 palkovsky 210
 
211
    if (!pt->stack) {
1129 palkovsky 212
        psthread_teardown(pt);
1113 palkovsky 213
        return 0;
214
    }
215
 
216
    pt->arg= arg;
217
    pt->func = func;
218
    pt->finished = 0;
219
    pt->waiter = NULL;
220
 
221
    context_save(&pt->ctx);
1155 vana 222
    context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(),
1392 palkovsky 223
            pt->tcb);
1113 palkovsky 224
 
1392 palkovsky 225
    return (pstid_t )pt;
226
}
227
 
228
/** Add a thread to ready list */
229
void psthread_add_ready(pstid_t psthrid)
230
{
231
    psthread_data_t *pt;
232
 
233
    pt = (psthread_data_t *) psthrid;
234
    futex_down(&psthread_futex);
1128 jermar 235
    list_append(&pt->link, &ready_list);
1392 palkovsky 236
    futex_up(&psthread_futex);
237
}
1113 palkovsky 238
 
1392 palkovsky 239
/** Add a thread to manager list */
240
void psthread_add_manager(pstid_t psthrid)
241
{
242
    psthread_data_t *pt;
243
 
244
    pt = (psthread_data_t *) psthrid;
245
 
246
    futex_down(&psthread_futex);
247
    list_append(&pt->link, &manager_list);
248
    futex_up(&psthread_futex);
1113 palkovsky 249
}
1392 palkovsky 250
 
251
/** Remove one manager from manager list */
252
void psthread_remove_manager()
253
{
254
    futex_down(&psthread_futex);
255
    if (list_empty(&manager_list)) {
256
        futex_up(&psthread_futex);
257
        return;
258
    }
259
    list_remove(manager_list.next);
260
    futex_up(&psthread_futex);
261
}