#define STACK_SIZE (1024 * 1024)
-static struct thread *current, *main_thread;
+static struct thread *main_thread;
static void *stack_allocate(size_t size)
{
struct thread *old, *next;
DBG();
- old = current;
+ old = thread_current();
model->scheduler->add_thread(old);
next = model->scheduler->next_thread();
- current = next;
DEBUG("(%d, %d)\n", old->index, next->index);
return thread_swap(old, next);
}
static void thread_wait_finish(void)
{
- struct thread *next;
+ struct thread *curr, *next;
DBG();
do {
- if (current)
- thread_dispose(current);
+ if ((curr = thread_current()))
+ thread_dispose(curr);
next = model->scheduler->next_thread();
- current = next;
} while (next && !thread_swap(main_thread, next));
}
struct thread *thread_current(void)
{
- return current;
+ return model->scheduler->get_current_thread();
}
int main()
struct thread_list_node *head, *tail;
struct thread_list_node nodes[NUM_LIST_NODES];
+struct thread *current;
static void enqueue_thread(struct thread *t)
{
tail = node;
}
-struct thread *dequeue_thread(void)
+static struct thread *dequeue_thread(void)
{
struct thread *pop;
tail = NULL;
head = head->next;
+ /* Set new current thread */
+ current = pop;
+
return pop;
}
-void schedule_add_thread(struct thread *t)
+static void default_add_thread(struct thread *t)
{
DEBUG("thread %d\n", t->index);
enqueue_thread(t);
}
-struct thread *schedule_choose_next(void)
+static struct thread *default_choose_next(void)
{
return dequeue_thread();
}
+static struct thread *default_thread_current(void)
+{
+ return current;
+}
+
void scheduler_init(struct model_checker *mod)
{
struct scheduler *sched;
- /* Initialize FCFS scheduler */
+ /* Initialize default scheduler */
sched = malloc(sizeof(*sched));
sched->init = NULL;
sched->exit = NULL;
- sched->add_thread = schedule_add_thread;
- sched->next_thread = schedule_choose_next;
- sched->get_current_thread = thread_current;
+ sched->add_thread = default_add_thread;
+ sched->next_thread = default_choose_next;
+ sched->get_current_thread = default_thread_current;
mod->scheduler = sched;
}
};
void scheduler_init(struct model_checker *mod);
-void schedule_add_thread(struct thread *t);
-struct thread *schedule_choose_next(void);
#endif /* __SCHEDULE_H__ */