Subversion Repositories HelenOS-historic

Compare Revisions

Ignore whitespace Rev 1112 → Rev 1113

/uspace/trunk/libc/generic/thread.c
31,7 → 31,24
#include <stdlib.h>
#include <arch/faddr.h>
#include <kernel/proc/uarg.h>
#include <psthread.h>
 
#include <stdio.h>
void * __make_tls(void)
{
psthread_data_t *pt;
 
pt = malloc(sizeof(psthread_data_t));
pt->self = pt;
 
return pt;
}
 
void __free_tls(void *tls)
{
free(tls);
}
 
/** Main thread function.
*
* This function is called from __thread_entry() and is used
41,11 → 58,17
*
* @param uarg Pointer to userspace argument structure.
*/
void thread_main(uspace_arg_t *uarg)
void __thread_main(uspace_arg_t *uarg)
{
/* This should initialize the area according to TLS specicification */
__tls_set(__make_tls());
 
uarg->uspace_thread_function(uarg->uspace_thread_arg);
free(uarg->uspace_stack);
free(uarg);
 
__free_tls(__tls_get());
 
thread_exit(0);
}
 
/uspace/trunk/libc/generic/libc.c
29,6 → 29,8
#include <libc.h>
#include <unistd.h>
#include <thread.h>
#include <malloc.h>
#include <psthread.h>
 
void _exit(int status) {
thread_exit(status);
35,8 → 37,11
}
 
void __main(void) {
__tls_set(__make_tls());
}
 
void __exit(void) {
free(__tls_get());
_exit(0);
}
/uspace/trunk/libc/generic/psthread.c
0,0 → 1,139
/*
* Copyright (C) 2006 Ondrej Palkovsky
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
 
#include <libadt/list.h>
#include <psthread.h>
#include <malloc.h>
#include <unistd.h>
#include <thread.h>
#include <stdio.h>
 
static LIST_INITIALIZE(ready_list);
 
/** Function to preempt to other thread without adding
* currently running thread to runqueue
*/
static void ps_exit(void)
{
psthread_data_t *pt;
 
if (list_empty(&ready_list)) {
/* Wait on IPC queue etc... */
printf("Cannot exit!!!\n");
_exit(0);
}
pt = list_get_instance(ready_list.next, psthread_data_t, list);
list_remove(&pt->list);
context_restore(&pt->ctx);
}
 
/** Function that is called on entry to new uspace thread */
static int psthread_main(void)
{
psthread_data_t *pt = __tls_get();
pt->retval = pt->func(pt->arg);
 
pt->finished = 1;
if (pt->waiter)
list_append(&pt->waiter->list, &ready_list);
 
ps_exit();
}
 
/** Do a preemption of userpace threads */
int ps_preempt(void)
{
psthread_data_t *pt;
 
if (list_empty(&ready_list))
return 0;
 
pt = __tls_get();
if (! context_save(&pt->ctx))
return 1;
list_append(&pt->list, &ready_list);
pt = list_get_instance(ready_list.next, psthread_data_t, list);
list_remove(&pt->list);
 
context_restore(&pt->ctx);
}
 
/** Wait for uspace thread to finish */
int ps_join(pstid_t psthrid)
{
psthread_data_t *pt, *mypt;
int retval;
 
/* Handle psthrid = Kernel address -> it is wait for call */
 
pt = (psthread_data_t *) psthrid;
 
if (!pt->finished) {
mypt = __tls_get();
if (context_save(&mypt->ctx)) {
pt->waiter = mypt;
ps_exit();
}
}
retval = pt->retval;
 
free(pt->stack);
__free_tls(pt);
 
return retval;
}
 
/**
* Create a userspace thread
*
*/
pstid_t psthread_create(int (*func)(void *), void *arg)
{
psthread_data_t *pt;
 
pt = __make_tls();
pt->stack = (char *) malloc(getpagesize());
 
if (!pt->stack) {
return 0;
}
 
pt->arg= arg;
pt->func = func;
pt->finished = 0;
pt->waiter = NULL;
 
context_save(&pt->ctx);
context_set(&pt->ctx, psthread_main, pt->stack, getpagesize(), pt);
 
list_append(&pt->list, &ready_list);
 
return (pstid_t )pt;
}