return ++used_thread_id;
}
-void ModelChecker::add_system_thread(Thread *t)
-{
- this->system_thread = t;
-}
-
Thread * ModelChecker::schedule_next_thread()
{
Thread *t;
int ModelChecker::switch_to_master(ModelAction *act)
{
- Thread *old, *next;
+ Thread *old;
DBG();
old = thread_current();
set_current_action(act);
old->set_state(THREAD_READY);
- next = system_thread;
- return old->swap(next);
+ return Thread::swap(old, get_system_context());
}
ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
#include <list>
#include <map>
#include <cstddef>
+#include <ucontext.h>
#include "schedule.h"
#include "libthreads.h"
ModelChecker();
~ModelChecker();
class Scheduler *scheduler;
- Thread *system_thread;
- void add_system_thread(Thread *t);
+ void set_system_context(ucontext_t *ctxt) { system_context = ctxt; }
+ ucontext_t * get_system_context(void) { return system_context; }
void set_current_action(ModelAction *act) { current_action = act; }
void check_current_action(void);
Backtrack *exploring;
thread_id_t nextThread;
+ ucontext_t *system_context;
action_list_t *action_trace;
std::map<thread_id_t, class Thread *> thread_map;
class TreeNode *rootNode, *currentNode;
context.uc_stack.ss_sp = stack;
context.uc_stack.ss_size = STACK_SIZE;
context.uc_stack.ss_flags = 0;
- context.uc_link = &model->system_thread->context;
+ context.uc_link = model->get_system_context();
makecontext(&context, start_routine, 1, arg);
return 0;
}
-int Thread::swap(Thread *t)
+int Thread::swap(Thread *t, ucontext_t *ctxt)
{
- return swapcontext(&this->context, &t->context);
+ return swapcontext(&t->context, ctxt);
+}
+
+int Thread::swap(ucontext_t *ctxt, Thread *t)
+{
+ return swapcontext(ctxt, &t->context);
}
void Thread::complete()
state = THREAD_CREATED;
id = model->get_next_id();
*user_thread = id;
- model->add_system_thread(this);
+ model->set_system_context(&context);
}
Thread::~Thread()
DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
if (!next)
return 1;
- return model->system_thread->swap(next);
+ return Thread::swap(model->get_system_context(), next);
}
static void thread_wait_finish(void)
*/
int main()
{
- thrd_t user_thread, main_thread;
- Thread *th;
+ thrd_t user_thread;
+ ucontext_t main_context;
model = new ModelChecker();
- th = new Thread(&main_thread);
+ if (getcontext(&main_context))
+ return 1;
+
+ model->set_system_context(&main_context);
do {
/* Start user program */
thread_wait_finish();
} while (model->next_execution());
- delete th;
delete model;
DEBUG("Exiting\n");