Stop trolling, go back to a normal time.
This commit is contained in:
parent
30d0c291dc
commit
4829d28f4a
@ -12,16 +12,16 @@
|
|||||||
|
|
||||||
#define FINISHED 0x1
|
#define FINISHED 0x1
|
||||||
#define ALLOCATED 0x2
|
#define ALLOCATED 0x2
|
||||||
|
#define WAITING 0x4
|
||||||
|
|
||||||
#ifndef STACK_SIZE
|
#ifndef STACK_SIZE
|
||||||
#define STACK_SIZE 4096*4
|
#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;
|
||||||
thread_t parent;
|
|
||||||
void *retvalue;
|
void *retvalue;
|
||||||
int valgrind_id;
|
int valgrind_id;
|
||||||
char status;
|
char status;
|
||||||
@ -32,10 +32,11 @@ static struct context_entry *running = 0;
|
|||||||
static unsigned long long counter = 0;
|
static unsigned long long counter = 0;
|
||||||
|
|
||||||
int thread_yield(void) {
|
int thread_yield(void) {
|
||||||
|
TRACE("thread_yield");
|
||||||
if (counter <= 1) {
|
if (counter <= 1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
struct context_entry *first;
|
struct context_entry *first = NULL;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
do {
|
do {
|
||||||
if (count++ == counter) {
|
if (count++ == counter) {
|
||||||
@ -50,7 +51,9 @@ int thread_yield(void) {
|
|||||||
if (first->id == running->id) {
|
if (first->id == running->id) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} while (first->status & FINISHED);
|
} while ((first->status & FINISHED) ||
|
||||||
|
((first->status & WAITING) && !(((struct context_entry *)first->retvalue)->status & FINISHED)));
|
||||||
|
TRACE("PICKING %p", first);
|
||||||
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);
|
||||||
@ -64,21 +67,25 @@ thread_t thread_self(void) {
|
|||||||
return running->id;
|
return running->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void thread_function_wrapper(void *(*func)(void *), void *funcarg) {
|
||||||
|
TRACE("Wrapper for %p\n", func);
|
||||||
|
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);
|
||||||
if (counter == 0) {
|
|
||||||
struct context_entry *main = malloc(sizeof(*main));
|
|
||||||
memset(main, 0, sizeof(*main));
|
|
||||||
getcontext(&main->context);
|
|
||||||
main->id = 0;
|
|
||||||
main->parent = 0;
|
|
||||||
main->status = 0;
|
|
||||||
main->retvalue = 0;
|
|
||||||
TAILQ_INSERT_HEAD(&head, main, link);
|
|
||||||
running = main;
|
|
||||||
counter++;
|
|
||||||
}
|
|
||||||
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_HEAD(&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);
|
||||||
@ -88,18 +95,17 @@ int thread_create(thread_t *newthread, void *(*func)(void *), void *funcarg) {
|
|||||||
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;
|
||||||
new_entry->parent = running->id;
|
|
||||||
new_entry->status = ALLOCATED;
|
new_entry->status = ALLOCATED;
|
||||||
new_entry->retvalue = 0;
|
new_entry->retvalue = 0;
|
||||||
*newthread = new_entry;
|
*newthread = new_entry;
|
||||||
makecontext(&new_entry->context, (void (*)()) func, 1, funcarg);
|
makecontext(&new_entry->context, (void (*)(void)) thread_function_wrapper, 2, func, funcarg);
|
||||||
counter++;
|
counter++;
|
||||||
TAILQ_INSERT_HEAD(&head, new_entry, link);
|
TAILQ_INSERT_HEAD(&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, %p);\n", entry, entry->id, (entry->status & FINISHED) > 0, entry->parent);
|
TRACE("CONTEXT (%p, %p, %d);", entry, entry->id, (entry->status & FINISHED) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int thread_join(thread_t thread, void **retval) {
|
int thread_join(thread_t thread, void **retval) {
|
||||||
@ -109,12 +115,14 @@ int thread_join(thread_t thread, void **retval) {
|
|||||||
if (entry->id != thread) {
|
if (entry->id != thread) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
TRACE("FIND");
|
TRACE("FIND %d",entry->status);
|
||||||
print_entry(entry);
|
running->status |= WAITING;
|
||||||
while (!entry->status & FINISHED) {
|
running->retvalue = entry;
|
||||||
|
while (!(entry->status & FINISHED)) {
|
||||||
TRACE("NOT FINISHED");
|
TRACE("NOT FINISHED");
|
||||||
thread_yield();
|
thread_yield();
|
||||||
}
|
}
|
||||||
|
running->status &= ~WAITING;
|
||||||
TRACE("AFTER");
|
TRACE("AFTER");
|
||||||
if (retval)
|
if (retval)
|
||||||
*retval = entry->retvalue;
|
*retval = entry->retvalue;
|
||||||
@ -144,6 +152,8 @@ int thread_join(thread_t thread, void **retval) {
|
|||||||
|
|
||||||
void thread_exit(void *retval) {
|
void thread_exit(void *retval) {
|
||||||
TRACE("Exit thread %p", running);
|
TRACE("Exit thread %p", running);
|
||||||
|
if (running == 0)
|
||||||
|
exit(0);
|
||||||
running->status |= FINISHED;
|
running->status |= FINISHED;
|
||||||
running->retvalue = retval;
|
running->retvalue = retval;
|
||||||
thread_yield();
|
thread_yield();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user