Subversion Repositories HelenOS-historic

Rev

Rev 1081 | Rev 1129 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
447 decky 1
/*
1065 jermar 2
 * Copyright (C) 2006 Jakub Jermar
447 decky 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
 
1065 jermar 29
#include <thread.h>
447 decky 30
#include <libc.h>
1079 jermar 31
#include <stdlib.h>
1065 jermar 32
#include <arch/faddr.h>
1079 jermar 33
#include <kernel/proc/uarg.h>
1113 palkovsky 34
#include <psthread.h>
447 decky 35
 
1113 palkovsky 36
#include <stdio.h>
37
void * __make_tls(void)
38
{
39
    psthread_data_t *pt;
40
 
41
    pt = malloc(sizeof(psthread_data_t));
42
    pt->self = pt;
43
 
44
    return pt;
45
}
46
 
47
void __free_tls(void *tls)
48
{
49
    free(tls);
50
}
51
 
1081 jermar 52
/** Main thread function.
53
 *
54
 * This function is called from __thread_entry() and is used
55
 * to call the thread's implementing function and perform cleanup
56
 * and exit when thread returns back. Do not call this function
57
 * directly.
58
 *
59
 * @param uarg Pointer to userspace argument structure.
60
 */
1113 palkovsky 61
void __thread_main(uspace_arg_t *uarg)
1079 jermar 62
{
1113 palkovsky 63
    /* This should initialize the area according to TLS specicification */
64
    __tls_set(__make_tls());
65
 
1079 jermar 66
    uarg->uspace_thread_function(uarg->uspace_thread_arg);
67
    free(uarg->uspace_stack);
68
    free(uarg);
1113 palkovsky 69
 
70
    __free_tls(__tls_get());
71
 
1079 jermar 72
    thread_exit(0);
73
}
1065 jermar 74
 
1081 jermar 75
/** Create userspace thread.
76
 *
77
 * This function creates new userspace thread and allocates userspace
78
 * stack and userspace argument structure for it.
79
 *
80
 * @param function Function implementing the thread.
81
 * @param arg Argument to be passed to thread.
82
 * @param name Symbolic name of the thread.
83
 *
84
 * @param TID of the new thread on success or -1 on failure.
85
 */
1079 jermar 86
int thread_create(void (* function)(void *), void *arg, char *name)
1065 jermar 87
{
1079 jermar 88
    char *stack;
89
    uspace_arg_t *uarg;
90
 
91
    stack = (char *) malloc(getpagesize());
92
    if (!stack)
93
        return -1;
94
 
95
    uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t));
96
    if (!uarg) {
97
        free(stack);
98
        return -1;
99
    }
1081 jermar 100
 
1079 jermar 101
    uarg->uspace_entry = (void *) FADDR(__thread_entry);
102
    uarg->uspace_stack = (void *) stack;
1081 jermar 103
    uarg->uspace_thread_function = function;
1079 jermar 104
    uarg->uspace_thread_arg = arg;
105
    uarg->uspace_uarg = uarg;
106
 
107
    return __SYSCALL2(SYS_THREAD_CREATE, (sysarg_t) uarg, (sysarg_t) name);
713 decky 108
}
109
 
1081 jermar 110
/** Terminate current thread.
111
 *
112
 * @param stat Exit status. Currently not used.
113
 */
1065 jermar 114
void thread_exit(int status)
115
{
116
    __SYSCALL1(SYS_THREAD_EXIT, (sysarg_t) status);
447 decky 117
}