Subversion Repositories HelenOS-historic

Rev

Rev 1128 | Rev 1155 | 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>
1113 palkovsky 36
 
37
static LIST_INITIALIZE(ready_list);
38
 
1128 jermar 39
static void psthread_exit(void) __attribute__ ((noinline));
40
static void psthread_main(void);
1125 jermar 41
 
1129 palkovsky 42
/** Setup PSthread information into TCB structure */
43
psthread_data_t * psthread_setup(tcb_t *tcb)
44
{
45
    psthread_data_t *pt;
46
 
47
    pt = malloc(sizeof(*pt));
48
    if (!pt) {
49
        return NULL;
50
    }
51
 
52
    tcb->pst_data = pt;
53
    pt->tcb = tcb;
54
 
55
    return pt;
56
}
57
 
58
void psthread_teardown(psthread_data_t *pt)
59
{
60
    free(pt);
61
}
62
 
1128 jermar 63
/** Function to preempt to other pseudo thread without adding
64
 * currently running pseudo thread to ready_list.
1113 palkovsky 65
 */
1128 jermar 66
void psthread_exit(void)
1113 palkovsky 67
{
68
    psthread_data_t *pt;
69
 
70
    if (list_empty(&ready_list)) {
71
        /* Wait on IPC queue etc... */
72
        printf("Cannot exit!!!\n");
73
        _exit(0);
74
    }
1128 jermar 75
    pt = list_get_instance(ready_list.next, psthread_data_t, link);
76
    list_remove(&pt->link);
1113 palkovsky 77
    context_restore(&pt->ctx);
78
}
79
 
80
/** Function that is called on entry to new uspace thread */
1128 jermar 81
void psthread_main(void)
1113 palkovsky 82
{
1129 palkovsky 83
    psthread_data_t *pt = __tcb_get()->pst_data;
84
 
1113 palkovsky 85
    pt->retval = pt->func(pt->arg);
86
 
87
    pt->finished = 1;
88
    if (pt->waiter)
1128 jermar 89
        list_append(&pt->waiter->link, &ready_list);
1113 palkovsky 90
 
1128 jermar 91
    psthread_exit();
1113 palkovsky 92
}
93
 
1128 jermar 94
/** Schedule next userspace pseudo thread.
95
 *
96
 * @return 0 if there is no ready pseudo thread, 1 otherwise.
97
 */
98
int psthread_schedule_next(void)
1113 palkovsky 99
{
100
    psthread_data_t *pt;
101
 
102
    if (list_empty(&ready_list))
103
        return 0;
104
 
1129 palkovsky 105
    pt = __tcb_get()->pst_data;
1128 jermar 106
    if (!context_save(&pt->ctx))
1113 palkovsky 107
        return 1;
108
 
1128 jermar 109
    list_append(&pt->link, &ready_list);
110
    pt = list_get_instance(ready_list.next, psthread_data_t, link);
111
    list_remove(&pt->link);
1113 palkovsky 112
 
113
    context_restore(&pt->ctx);
114
}
115
 
1128 jermar 116
/** Wait for uspace pseudo thread to finish.
117
 *
118
 * @param psthrid Pseudo thread to wait for.
119
 *
120
 * @return Value returned by the finished thread.
121
 */
122
int psthread_join(pstid_t psthrid)
1113 palkovsky 123
{
1125 jermar 124
    volatile psthread_data_t *pt, *mypt;
125
    volatile int retval;
1113 palkovsky 126
 
127
    /* Handle psthrid = Kernel address -> it is wait for call */
128
    pt = (psthread_data_t *) psthrid;
129
 
130
    if (!pt->finished) {
1129 palkovsky 131
        mypt = __tcb_get()->pst_data;
1125 jermar 132
        if (context_save(&((psthread_data_t *) mypt)->ctx)) {
133
            pt->waiter = (psthread_data_t *) mypt;
1128 jermar 134
            psthread_exit();
1113 palkovsky 135
        }
136
    }
137
    retval = pt->retval;
138
 
139
    free(pt->stack);
1129 palkovsky 140
    __free_tls(pt->tcb);
141
    psthread_teardown((void *)pt);
1113 palkovsky 142
 
143
    return retval;
144
}
145
 
146
/**
1128 jermar 147
 * Create a userspace thread and append it to ready list.
1113 palkovsky 148
 *
1128 jermar 149
 * @param func Pseudo thread function.
150
 * @param arg Argument to pass to func.
151
 *
152
 * @return 0 on failure, TLS of the new pseudo thread.
1113 palkovsky 153
 */
154
pstid_t psthread_create(int (*func)(void *), void *arg)
155
{
156
    psthread_data_t *pt;
1129 palkovsky 157
    tcb_t *tcb;
1113 palkovsky 158
 
1129 palkovsky 159
    tcb = __make_tls();
160
    if (!tcb)
161
        return 0;
162
 
163
    pt = psthread_setup(tcb);
164
    if (!pt) {
165
        __free_tls(tcb);
166
        return 0;
167
    }
1113 palkovsky 168
    pt->stack = (char *) malloc(getpagesize());
169
 
170
    if (!pt->stack) {
1129 palkovsky 171
        __free_tls(tcb);
172
        psthread_teardown(pt);
1113 palkovsky 173
        return 0;
174
    }
175
 
176
    pt->arg= arg;
177
    pt->func = func;
178
    pt->finished = 0;
179
    pt->waiter = NULL;
180
 
181
    context_save(&pt->ctx);
1129 palkovsky 182
    context_set(&pt->ctx, FADDR(psthread_main), pt->stack, getpagesize(),
183
            tcb);
1113 palkovsky 184
 
1128 jermar 185
    list_append(&pt->link, &ready_list);
1113 palkovsky 186
 
187
    return (pstid_t )pt;
188
}