#include "schedule.h"
#include "common.h"
-#define STACK_SIZE (1024 * 1024)
+/* global "model" object */
+#include "model.h"
-static struct thread *current, *main_thread;
+#define STACK_SIZE (1024 * 1024)
static void *stack_allocate(size_t size)
{
t->context.uc_stack.ss_sp = t->stack;
t->context.uc_stack.ss_size = STACK_SIZE;
t->context.uc_stack.ss_flags = 0;
- t->context.uc_link = &main_thread->context;
+ t->context.uc_link = &model->system_thread->context;
makecontext(&t->context, t->start_routine, 1, t->arg);
return 0;
static int create_initial_thread(struct thread *t)
{
memset(t, 0, sizeof(*t));
+ model->assign_id(t);
return create_context(t);
}
-static int thread_swap(struct thread *old, struct thread *new)
+static int thread_swap(struct thread *t1, struct thread *t2)
{
- return swapcontext(&old->context, &new->context);
+ return swapcontext(&t1->context, &t2->context);
}
-static int thread_yield()
+static void thread_dispose(struct thread *t)
{
- struct thread *old, *next;
-
- DBG();
- 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);
+ DEBUG("completed thread %d\n", thread_current()->id);
+ t->state = THREAD_COMPLETED;
+ stack_free(t->stack);
}
-static void thread_dispose(struct thread *t)
+/*
+ * Return 1 if found next thread, 0 otherwise
+ */
+static int thread_system_next(void)
{
- DEBUG("completed thread %d\n", thread_current()->index);
- t->completed = 1;
- stack_free(t->stack);
+ struct thread *curr, *next;
+
+ curr = thread_current();
+ if (curr) {
+ if (curr->state == THREAD_READY)
+ model->scheduler->add_thread(curr);
+ else if (curr->state == THREAD_RUNNING)
+ /* Stopped while running; i.e., completed */
+ thread_dispose(curr);
+ else
+ DEBUG("ERROR: current thread in unexpected state??\n");
+ }
+ next = model->scheduler->next_thread();
+ if (next)
+ next->state = THREAD_RUNNING;
+ DEBUG("(%d, %d)\n", curr ? curr->id : -1, next ? next->id : -1);
+ if (!next)
+ return 1;
+ return thread_swap(model->system_thread, next);
}
-static void thread_wait_finish()
+static void thread_wait_finish(void)
{
- struct thread *next;
DBG();
- do {
- if (current)
- thread_dispose(current);
- schedule_choose_next(&next);
- current = next;
- } while (next && !thread_swap(main_thread, next));
+ while (!thread_system_next());
}
-int thread_create(struct thread *t, void (*start_routine), void *arg)
+/*
+ * User program API functions
+ */
+int thread_create(struct thread *t, void (*start_routine)(), void *arg)
{
- static int created = 1;
int ret = 0;
DBG();
memset(t, 0, sizeof(*t));
- t->index = created++;
- DEBUG("create thread %d\n", t->index);
+ model->assign_id(t);
+ DEBUG("create thread %d\n", t->id);
t->start_routine = start_routine;
t->arg = arg;
if (ret)
return ret;
- schedule_add_thread(t);
+ t->state = THREAD_CREATED;
+
+ model->scheduler->add_thread(t);
return 0;
}
void thread_join(struct thread *t)
{
- while (!t->completed)
+ while (t->state != THREAD_COMPLETED)
thread_yield();
}
-struct thread *thread_current(void)
-{
- return current;
-}
-
-void a(int *parm)
+int thread_yield(void)
{
- int i;
+ struct thread *old, *next;
- for (i = 0; i < 10; i++) {
- printf("Thread %d, magic number %d, loop %d\n", thread_current()->index, *parm, i);
- if (i % 2)
- thread_yield();
- }
+ DBG();
+ old = thread_current();
+ old->state = THREAD_READY;
+ next = model->system_thread;
+ return thread_swap(old, next);
}
-void user_main()
+struct thread *thread_current(void)
{
- struct thread t1, t2;
- int i = 17, j = 13;
-
- printf("%s() creating 2 threads\n", __func__);
- thread_create(&t1, &a, &i);
- thread_create(&t2, &a, &j);
-
- thread_join(&t1);
- thread_join(&t2);
- printf("%s() is finished\n", __func__);
+ return model->scheduler->get_current_thread();
}
+/*
+ * Main system function
+ */
int main()
{
struct thread user_thread;
+ struct thread *main_thread;
+
+ model = new ModelChecker();
- main_thread = malloc(sizeof(struct thread));
+ main_thread = (struct thread *)malloc(sizeof(*main_thread));
create_initial_thread(main_thread);
+ model->add_system_thread(main_thread);
/* Start user program */
thread_create(&user_thread, &user_main, NULL);