threads/model: move switch_to_master from class Thread to class ModelChecker
[model-checker.git] / model.cc
index 858b9198bccc2fea775d3870970c9f4a0e19ef7e..1015701d917803c75541fb2d3b830a0f223c301c 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -1,7 +1,8 @@
+#include <stdio.h>
+
 #include "model.h"
 #include "schedule.h"
-#include <stdlib.h>
-#include <string.h>
+#include "common.h"
 
 ModelChecker *model;
 
@@ -9,25 +10,97 @@ ModelChecker::ModelChecker()
 {
        /* First thread created (system_thread) will have id 1 */
        this->used_thread_id = 0;
+       /* Initialize default scheduler */
+       this->scheduler = new Scheduler();
 
-       scheduler_init(this);
+       this->current_action = NULL;
 }
 
 ModelChecker::~ModelChecker()
 {
-       struct scheduler *sched = model->scheduler;
+       delete this->scheduler;
+}
+
+void ModelChecker::assign_id(Thread *t)
+{
+       t->set_id(++used_thread_id);
+}
+
+void ModelChecker::add_system_thread(Thread *t)
+{
+       this->system_thread = t;
+}
+
+void ModelChecker::check_current_action(void)
+{
+       if (this->current_action)
+               this->action_trace.push_back(this->current_action);
+       else
+               DEBUG("trying to push NULL action...\n");
+}
+
+void ModelChecker::print_trace(void)
+{
+       std::list<class ModelAction *>::iterator it;
+
+       for (it = action_trace.begin(); it != action_trace.end(); it++) {
+               DBG();
+               (*it)->print();
+       }
+}
 
-       if (sched->exit)
-               sched->exit();
-       free(sched);
+int ModelChecker::add_thread(Thread *t)
+{
+       thread_map[t->get_id()] = t;
+       return 0;
 }
 
-void ModelChecker::assign_id(struct thread *t)
+int ModelChecker::switch_to_master(ModelAction *act)
 {
-       t->id = ++this->used_thread_id;
+       Thread *old, *next;
+
+       DBG();
+       old = thread_current();
+       set_current_action(act);
+       old->set_state(THREAD_READY);
+       next = system_thread;
+       return old->swap(next);
 }
 
-void ModelChecker::add_system_thread(struct thread *t)
+ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
 {
-       model->system_thread = t;
+       Thread *t = thread_current();
+       ModelAction *act = this;
+
+       act->type = type;
+       act->order = order;
+       act->location = loc;
+       act->tid = t->get_id();
+       act->value = value;
+}
+
+void ModelAction::print(void)
+{
+       const char *type_str;
+       switch (this->type) {
+       case THREAD_CREATE:
+               type_str = "thread create";
+               break;
+       case THREAD_YIELD:
+               type_str = "thread yield";
+               break;
+       case THREAD_JOIN:
+               type_str = "thread join";
+               break;
+       case ATOMIC_READ:
+               type_str = "atomic read";
+               break;
+       case ATOMIC_WRITE:
+               type_str = "atomic write";
+               break;
+       default:
+               type_str = "unknown type";
+       }
+
+       printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);
 }