Subversion Repositories HelenOS-historic

Rev

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