feat: begin rewrite
This commit is contained in:
parent
4bb1bb839a
commit
987e137377
@ -1,42 +1,50 @@
|
|||||||
#include <string.h>
|
|
||||||
#ifndef USE_PTHREAD
|
#ifndef USE_PTHREAD
|
||||||
|
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "pthread.h"
|
#include "pthread.h"
|
||||||
#include <sys/queue.h>
|
|
||||||
#include <bits/pthreadtypes.h>
|
#include <bits/pthreadtypes.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/queue.h>
|
||||||
#include <ucontext.h>
|
#include <ucontext.h>
|
||||||
#include <valgrind/valgrind.h>
|
#include <valgrind/valgrind.h>
|
||||||
|
|
||||||
#define FINISHED 0x1
|
#define FINISHED 0x1
|
||||||
|
#define IS_FINISHED(entry) (entry->status & FINISHED)
|
||||||
#define ALLOCATED 0x2
|
#define ALLOCATED 0x2
|
||||||
|
#define WAS_ALLOCATED(entry) (entry->status & ALLOCATED)
|
||||||
#define WAITING 0x4
|
#define WAITING 0x4
|
||||||
|
#define IS_WAITING(entry) (entry->status & WAITING)
|
||||||
|
#define IS_WAITING_THREAD_FINISHED(entry) (((struct context_entry*)entry->retvalue)->status & FINISHED)
|
||||||
|
#define WAITED 0x8
|
||||||
|
#define IS_WAITED(entry) (entry->status & WAITED)
|
||||||
|
|
||||||
#ifndef STACK_SIZE
|
#ifndef STACK_SIZE
|
||||||
#define STACK_SIZE 4096
|
#define STACK_SIZE 4096
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct context_entry {
|
struct context_entry {
|
||||||
TAILQ_ENTRY(context_entry) link;
|
TAILQ_ENTRY(context_entry)
|
||||||
|
link;
|
||||||
ucontext_t context;
|
ucontext_t context;
|
||||||
thread_t id;
|
thread_t id;
|
||||||
void *retvalue;
|
void* retvalue;
|
||||||
int valgrind_id;
|
int valgrind_id;
|
||||||
char status;
|
char status;
|
||||||
};
|
};
|
||||||
|
|
||||||
static TAILQ_HEAD(context_head, context_entry) head = TAILQ_HEAD_INITIALIZER(head);
|
static TAILQ_HEAD(context_head, context_entry) head = TAILQ_HEAD_INITIALIZER(head);
|
||||||
static struct context_entry *running = 0;
|
static struct context_entry* running = NULL;
|
||||||
static unsigned long long counter = 0;
|
static unsigned long long counter = 0;
|
||||||
|
|
||||||
int thread_yield(void) {
|
int thread_yield(void)
|
||||||
|
{
|
||||||
TRACE("thread_yield");
|
TRACE("thread_yield");
|
||||||
if (counter <= 1) {
|
if (counter <= 1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
struct context_entry *first = NULL;
|
struct context_entry* first = NULL;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
do {
|
do {
|
||||||
if (count++ == counter) {
|
if (count++ == counter) {
|
||||||
@ -48,127 +56,167 @@ int thread_yield(void) {
|
|||||||
}
|
}
|
||||||
TAILQ_REMOVE(&head, first, link);
|
TAILQ_REMOVE(&head, first, link);
|
||||||
TAILQ_INSERT_TAIL(&head, first, link);
|
TAILQ_INSERT_TAIL(&head, first, link);
|
||||||
} while ((first->status & FINISHED) ||
|
} while (IS_FINISHED(first) || IS_WAITING(first) && !IS_WAITING_THREAD_FINISHED(first) || (first->id == running->id));
|
||||||
((first->status & WAITING) && !(((struct context_entry *)first->retvalue)->status & FINISHED)) ||
|
|
||||||
(first->id == running->id));
|
|
||||||
TRACE("PICKING %p (previous was %p)", first->id, running->id);
|
TRACE("PICKING %p (previous was %p)", first->id, running->id);
|
||||||
struct context_entry *old_runner = running;
|
struct context_entry* old_runner = running;
|
||||||
running = first;
|
running = first;
|
||||||
swapcontext(&old_runner->context, &running->context);
|
swapcontext(&old_runner->context, &running->context);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_t thread_self(void) {
|
thread_t thread_self(void)
|
||||||
|
{
|
||||||
if (running == NULL) {
|
if (running == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return running->id;
|
return running->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void thread_function_wrapper(void *(*func)(void *), void *funcarg) {
|
void thread_function_wrapper(void* (*func)(void*), void* funcarg)
|
||||||
|
{
|
||||||
TRACE("Wrapper for %p\n", func);
|
TRACE("Wrapper for %p\n", func);
|
||||||
thread_exit(func(funcarg));
|
thread_exit(func(funcarg));
|
||||||
}
|
}
|
||||||
|
|
||||||
int thread_create(thread_t *newthread, void *(*func)(void *), void *funcarg) {
|
int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
|
||||||
|
{
|
||||||
TRACE("Create a new thread that execute function %p", func);
|
TRACE("Create a new thread that execute function %p", func);
|
||||||
struct context_entry *new_entry = malloc(sizeof(*new_entry));
|
struct context_entry* new_entry = malloc(sizeof(*new_entry));
|
||||||
if (counter == 0) {
|
|
||||||
memset(new_entry, 0, sizeof(*new_entry));
|
|
||||||
getcontext(&new_entry->context);
|
|
||||||
new_entry->id = 0;
|
|
||||||
new_entry->status = 0;
|
|
||||||
new_entry->retvalue = 0;
|
|
||||||
TAILQ_INSERT_TAIL(&head, new_entry, link);
|
|
||||||
running = new_entry;
|
|
||||||
counter++;
|
|
||||||
new_entry = malloc(sizeof(*new_entry));
|
|
||||||
}
|
|
||||||
memset(new_entry, 0, sizeof(*new_entry));
|
memset(new_entry, 0, sizeof(*new_entry));
|
||||||
getcontext(&new_entry->context);
|
getcontext(&new_entry->context);
|
||||||
new_entry->context.uc_stack.ss_sp = malloc(STACK_SIZE);
|
new_entry->context.uc_stack.ss_sp = malloc(STACK_SIZE);
|
||||||
new_entry->context.uc_stack.ss_size = STACK_SIZE;
|
new_entry->context.uc_stack.ss_size = STACK_SIZE;
|
||||||
new_entry->valgrind_id = VALGRIND_STACK_REGISTER(
|
new_entry->valgrind_id = VALGRIND_STACK_REGISTER(
|
||||||
new_entry->context.uc_stack.ss_sp,
|
new_entry->context.uc_stack.ss_sp,
|
||||||
new_entry->context.uc_stack.ss_sp + new_entry->context.uc_stack.ss_size
|
new_entry->context.uc_stack.ss_sp + new_entry->context.uc_stack.ss_size);
|
||||||
);
|
|
||||||
new_entry->id = new_entry;
|
new_entry->id = new_entry;
|
||||||
|
TRACE("ALLOCATED %p", new_entry);
|
||||||
new_entry->status = ALLOCATED;
|
new_entry->status = ALLOCATED;
|
||||||
new_entry->retvalue = 0;
|
new_entry->retvalue = 0;
|
||||||
*newthread = new_entry;
|
*newthread = new_entry->id;
|
||||||
makecontext(&new_entry->context, (void (*)(void)) thread_function_wrapper, 2, func, funcarg);
|
makecontext(&new_entry->context, (void (*)(void))thread_function_wrapper, 2, func, funcarg);
|
||||||
counter++;
|
counter++;
|
||||||
TAILQ_INSERT_TAIL(&head, new_entry, link);
|
TAILQ_INSERT_TAIL(&head, new_entry, link);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_entry(struct context_entry *entry) {
|
void print_entry(struct context_entry* entry)
|
||||||
TRACE("CONTEXT (%p, %p, %d);", entry, entry->id, (entry->status & FINISHED) > 0);
|
{
|
||||||
|
TRACE("CONTEXT (%p, %p, %d);", entry, entry->id, WAS_ALLOCATED(entry));
|
||||||
}
|
}
|
||||||
|
|
||||||
int thread_join(thread_t thread, void **retval) {
|
int thread_join(thread_t thread, void** retval)
|
||||||
|
{
|
||||||
TRACE("Join thread %p", thread);
|
TRACE("Join thread %p", thread);
|
||||||
struct context_entry *entry;
|
struct context_entry* entry = thread;
|
||||||
TAILQ_FOREACH(entry, &head, link) {
|
if (IS_WAITED(entry)) {
|
||||||
if (entry->id != thread) {
|
return -1;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
TRACE("FIND %d",entry->status);
|
|
||||||
running->status |= WAITING;
|
running->status |= WAITING;
|
||||||
running->retvalue = entry;
|
running->retvalue = entry;
|
||||||
while (!(entry->status & FINISHED)) {
|
entry->status |= WAITED;
|
||||||
TRACE("NOT FINISHED");
|
|
||||||
|
while (!IS_FINISHED(entry)) {
|
||||||
thread_yield();
|
thread_yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
running->status &= ~WAITING;
|
running->status &= ~WAITING;
|
||||||
TRACE("AFTER");
|
|
||||||
if (retval)
|
if (retval)
|
||||||
*retval = entry->retvalue;
|
*retval = entry->retvalue;
|
||||||
|
|
||||||
TAILQ_REMOVE(&head, entry, link);
|
TAILQ_REMOVE(&head, entry, link);
|
||||||
if (entry->status & ALLOCATED) {
|
|
||||||
TRACE("FREE\n");
|
if (WAS_ALLOCATED(entry)) {
|
||||||
VALGRIND_STACK_DEREGISTER(entry->valgrind_id);
|
VALGRIND_STACK_DEREGISTER(entry->valgrind_id);
|
||||||
free(entry->context.uc_stack.ss_sp);
|
free(entry->context.uc_stack.ss_sp);
|
||||||
} else {
|
|
||||||
TRACE("NOT ALLOCATED\n");
|
|
||||||
}
|
}
|
||||||
free(entry);
|
free(entry);
|
||||||
if (--counter == 1) {
|
--counter;
|
||||||
TAILQ_REMOVE(&head, running, link);
|
TRACE("DEBUG %p,%d", running, WAS_ALLOCATED(running));
|
||||||
if (running->status & ALLOCATED) {
|
|
||||||
VALGRIND_STACK_DEREGISTER(running->valgrind_id);
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void thread_exit(void* retval)
|
||||||
|
{
|
||||||
|
TRACE("Exit thread %p", running);
|
||||||
|
print_entry(running);
|
||||||
|
if (running == 0)
|
||||||
|
exit(0);
|
||||||
|
|
||||||
|
running->status |= FINISHED;
|
||||||
|
running->retvalue = retval;
|
||||||
|
|
||||||
|
if (counter > 1)
|
||||||
|
thread_yield();
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __attribute__((constructor)) setup_main_thread()
|
||||||
|
{
|
||||||
|
TRACE("premain");
|
||||||
|
struct context_entry* main = malloc(sizeof(*main));
|
||||||
|
memset(main, 0, sizeof(*main));
|
||||||
|
getcontext(&main->context);
|
||||||
|
main->id = main;
|
||||||
|
main->status = 0;
|
||||||
|
main->retvalue = 0;
|
||||||
|
TAILQ_INSERT_TAIL(&head, main, link);
|
||||||
|
running = main;
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_context(void)
|
||||||
|
{
|
||||||
|
struct context_entry *running;
|
||||||
|
while (TAILQ_EMPTY(&head)) {
|
||||||
|
TRACE("CONTEXT %p", running);
|
||||||
|
running = TAILQ_FIRST(&head);
|
||||||
|
if (WAS_ALLOCATED(running)) {
|
||||||
|
TRACE("ALLOCATED");
|
||||||
|
VALGRIND_STACK_DEREGISTER(running->id);
|
||||||
free(running->context.uc_stack.ss_sp);
|
free(running->context.uc_stack.ss_sp);
|
||||||
}
|
}
|
||||||
free(running);
|
free(running);
|
||||||
running = 0;
|
|
||||||
counter = 0;
|
|
||||||
}
|
}
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void thread_exit(void *retval) {
|
|
||||||
TRACE("Exit thread %p", running);
|
|
||||||
if (running == 0)
|
|
||||||
exit(0);
|
|
||||||
running->status |= FINISHED;
|
|
||||||
running->retvalue = retval;
|
|
||||||
thread_yield();
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int thread_mutex_init(thread_mutex_t *mutex) {
|
void __attribute__((destructor)) clear_last_thread()
|
||||||
return pthread_mutex_init((pthread_mutex_t *) mutex, NULL);
|
{
|
||||||
|
TRACE("POST");
|
||||||
|
ucontext_t old;
|
||||||
|
ucontext_t garbage_cleaner;
|
||||||
|
getcontext(&old);
|
||||||
|
getcontext(&garbage_cleaner);
|
||||||
|
char stack[STACK_SIZE] = { 0 };
|
||||||
|
memcpy(stack, old.uc_stack.ss_sp, STACK_SIZE);
|
||||||
|
garbage_cleaner.uc_stack.ss_sp = stack;
|
||||||
|
garbage_cleaner.uc_stack.ss_size = STACK_SIZE;
|
||||||
|
print_entry(running);
|
||||||
|
makecontext(&garbage_cleaner, (void (*)(void))clear_context, 0);
|
||||||
|
swapcontext(&old, &garbage_cleaner);
|
||||||
}
|
}
|
||||||
int thread_mutex_destroy(thread_mutex_t *mutex) {
|
|
||||||
return pthread_mutex_destroy((pthread_mutex_t *) mutex);
|
int thread_mutex_init(thread_mutex_t* mutex)
|
||||||
|
{
|
||||||
|
return pthread_mutex_init((pthread_mutex_t*)mutex, NULL);
|
||||||
}
|
}
|
||||||
int thread_mutex_lock(thread_mutex_t *mutex) {
|
|
||||||
return pthread_mutex_lock((pthread_mutex_t * )mutex);
|
int thread_mutex_destroy(thread_mutex_t* mutex)
|
||||||
|
{
|
||||||
|
return pthread_mutex_destroy((pthread_mutex_t*)mutex);
|
||||||
}
|
}
|
||||||
int thread_mutex_unlock(thread_mutex_t *mutex) {
|
|
||||||
return pthread_mutex_unlock((pthread_mutex_t *)mutex);
|
int thread_mutex_lock(thread_mutex_t* mutex)
|
||||||
|
{
|
||||||
|
return pthread_mutex_lock((pthread_mutex_t*)mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
int thread_mutex_unlock(thread_mutex_t* mutex)
|
||||||
|
{
|
||||||
|
return pthread_mutex_unlock((pthread_mutex_t*)mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user