Subversion Repositories HelenOS-historic

Rev

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