move 'current thread' details
authorBrian Norris <banorris@uci.edu>
Mon, 12 Mar 2012 23:06:14 +0000 (16:06 -0700)
committerBrian Norris <banorris@uci.edu>
Mon, 12 Mar 2012 23:06:14 +0000 (16:06 -0700)
The low level details regarding the 'current thread' should be encapsulated in
the scheduler (i.e., 'get_current_thread()'). So we move the 'current' variable
to scheduler.c.

libthreads.c
schedule.c
schedule.h

index 3697c0851e035109ddd49329ea640f8a154b08ca..c164d6779610198efe03a16d0c3d3816ba6fd90d 100644 (file)
@@ -10,7 +10,7 @@
 
 #define STACK_SIZE (1024 * 1024)
 
-static struct thread *current, *main_thread;
+static struct thread *main_thread;
 
 static void *stack_allocate(size_t size)
 {
@@ -62,10 +62,9 @@ int thread_yield(void)
        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);
 }
@@ -79,15 +78,14 @@ static void thread_dispose(struct thread *t)
 
 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));
 }
 
@@ -122,7 +120,7 @@ void thread_join(struct thread *t)
 
 struct thread *thread_current(void)
 {
-       return current;
+       return model->scheduler->get_current_thread();
 }
 
 int main()
index 4ac8b4991b37a70993a10485923c367bc847d4d2..c8f54e84e493b2c591d53a3b33bafdc850d64022 100644 (file)
@@ -15,6 +15,7 @@ struct thread_list_node {
 
 struct thread_list_node *head, *tail;
 struct thread_list_node nodes[NUM_LIST_NODES];
+struct thread *current;
 
 static void enqueue_thread(struct thread *t)
 {
@@ -35,7 +36,7 @@ static void enqueue_thread(struct thread *t)
        tail = node;
 }
 
-struct thread *dequeue_thread(void)
+static struct thread *dequeue_thread(void)
 {
        struct thread *pop;
 
@@ -48,30 +49,38 @@ struct thread *dequeue_thread(void)
                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;
 }
index 360b390512b4776d0af54eb331aa91682c4906c0..bae8c1f8e2ce136206d42325dfbba6954e81c7c5 100644 (file)
@@ -15,7 +15,5 @@ struct scheduler {
 };
 
 void scheduler_init(struct model_checker *mod);
-void schedule_add_thread(struct thread *t);
-struct thread *schedule_choose_next(void);
 
 #endif /* __SCHEDULE_H__ */