Subversion Repositories HelenOS-historic

Rev

Rev 1128 | Rev 1155 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1128 Rev 1129
Line 37... Line 37...
37
static LIST_INITIALIZE(ready_list);
37
static LIST_INITIALIZE(ready_list);
38
 
38
 
39
static void psthread_exit(void) __attribute__ ((noinline));
39
static void psthread_exit(void) __attribute__ ((noinline));
40
static void psthread_main(void);
40
static void psthread_main(void);
41
 
41
 
-
 
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
 
42
/** Function to preempt to other pseudo thread without adding
63
/** Function to preempt to other pseudo thread without adding
43
 * currently running pseudo thread to ready_list.
64
 * currently running pseudo thread to ready_list.
44
 */
65
 */
45
void psthread_exit(void)
66
void psthread_exit(void)
46
{
67
{
Line 57... Line 78...
57
}
78
}
58
 
79
 
59
/** Function that is called on entry to new uspace thread */
80
/** Function that is called on entry to new uspace thread */
60
void psthread_main(void)
81
void psthread_main(void)
61
{
82
{
62
    psthread_data_t *pt = __tls_get();
83
    psthread_data_t *pt = __tcb_get()->pst_data;
-
 
84
 
63
    pt->retval = pt->func(pt->arg);
85
    pt->retval = pt->func(pt->arg);
64
 
86
 
65
    pt->finished = 1;
87
    pt->finished = 1;
66
    if (pt->waiter)
88
    if (pt->waiter)
67
        list_append(&pt->waiter->link, &ready_list);
89
        list_append(&pt->waiter->link, &ready_list);
Line 78... Line 100...
78
    psthread_data_t *pt;
100
    psthread_data_t *pt;
79
 
101
 
80
    if (list_empty(&ready_list))
102
    if (list_empty(&ready_list))
81
        return 0;
103
        return 0;
82
 
104
 
83
    pt = __tls_get();
105
    pt = __tcb_get()->pst_data;
84
    if (!context_save(&pt->ctx))
106
    if (!context_save(&pt->ctx))
85
        return 1;
107
        return 1;
86
   
108
   
87
    list_append(&pt->link, &ready_list);
109
    list_append(&pt->link, &ready_list);
88
    pt = list_get_instance(ready_list.next, psthread_data_t, link);
110
    pt = list_get_instance(ready_list.next, psthread_data_t, link);
Line 101... Line 123...
101
{
123
{
102
    volatile psthread_data_t *pt, *mypt;
124
    volatile psthread_data_t *pt, *mypt;
103
    volatile int retval;
125
    volatile int retval;
104
 
126
 
105
    /* Handle psthrid = Kernel address -> it is wait for call */
127
    /* Handle psthrid = Kernel address -> it is wait for call */
106
 
-
 
107
    pt = (psthread_data_t *) psthrid;
128
    pt = (psthread_data_t *) psthrid;
108
 
129
 
109
    if (!pt->finished) {
130
    if (!pt->finished) {
110
        mypt = __tls_get();
131
        mypt = __tcb_get()->pst_data;
111
        if (context_save(&((psthread_data_t *) mypt)->ctx)) {
132
        if (context_save(&((psthread_data_t *) mypt)->ctx)) {
112
            pt->waiter = (psthread_data_t *) mypt;
133
            pt->waiter = (psthread_data_t *) mypt;
113
            psthread_exit();
134
            psthread_exit();
114
        }
135
        }
115
    }
136
    }
116
    retval = pt->retval;
137
    retval = pt->retval;
117
 
138
 
118
    free(pt->stack);
139
    free(pt->stack);
119
    __free_tls((psthread_data_t *) pt);
140
    __free_tls(pt->tcb);
-
 
141
    psthread_teardown((void *)pt);
120
 
142
 
121
    return retval;
143
    return retval;
122
}
144
}
123
 
145
 
124
/**
146
/**
Line 130... Line 152...
130
 * @return 0 on failure, TLS of the new pseudo thread.
152
 * @return 0 on failure, TLS of the new pseudo thread.
131
 */
153
 */
132
pstid_t psthread_create(int (*func)(void *), void *arg)
154
pstid_t psthread_create(int (*func)(void *), void *arg)
133
{
155
{
134
    psthread_data_t *pt;
156
    psthread_data_t *pt;
-
 
157
    tcb_t *tcb;
135
 
158
 
136
    pt = __make_tls();
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
    }
137
    pt->stack = (char *) malloc(getpagesize());
168
    pt->stack = (char *) malloc(getpagesize());
138
 
169
 
139
    if (!pt->stack) {
170
    if (!pt->stack) {
-
 
171
        __free_tls(tcb);
-
 
172
        psthread_teardown(pt);
140
        return 0;
173
        return 0;
141
    }
174
    }
142
 
175
 
143
    pt->arg= arg;
176
    pt->arg= arg;
144
    pt->func = func;
177
    pt->func = func;
145
    pt->finished = 0;
178
    pt->finished = 0;
146
    pt->waiter = NULL;
179
    pt->waiter = NULL;
147
 
180
 
148
    context_save(&pt->ctx);
181
    context_save(&pt->ctx);
149
    context_set(&pt->ctx, FADDR(psthread_main), pt->stack, getpagesize(), pt);
182
    context_set(&pt->ctx, FADDR(psthread_main), pt->stack, getpagesize(),
-
 
183
            tcb);
150
 
184
 
151
    list_append(&pt->link, &ready_list);
185
    list_append(&pt->link, &ready_list);
152
 
186
 
153
    return (pstid_t )pt;
187
    return (pstid_t )pt;
154
}
188
}