#include <stdlib.h>
#include "libthreads.h"
+#include "common.h"
#define STACK_SIZE (1024 * 1024)
struct thread *old, *next;
DBG();
- if (current) {
- old = current;
- schedule_add_thread(old);
- } else {
- old = main_thread;
- }
+ old = current;
+ schedule_add_thread(old);
schedule_choose_next(&next);
current = next;
+ DEBUG("(%d, %d)\n", old->index, next->index);
return thread_swap(old, next);
}
static void thread_dispose(struct thread *t)
{
- DEBUG("completed thread %d\n", current->index);
+ DEBUG("completed thread %d\n", thread_current()->index);
t->completed = 1;
stack_free(t->stack);
}
thread_yield();
}
+struct thread *thread_current(void)
+{
+ return current;
+}
+
void a(int *parm)
{
int i;
for (i = 0; i < 10; i++) {
- printf("Thread %d, magic number %d, loop %d\n", current->index, *parm, i);
+ printf("Thread %d, magic number %d, loop %d\n", thread_current()->index, *parm, i);
if (i % 2)
thread_yield();
}