3 #include "libthreads.h"
6 #include "threads_internal.h"
8 /* global "model" object */
11 #define STACK_SIZE (1024 * 1024)
13 static void *stack_allocate(size_t size)
18 static void stack_free(void *stack)
23 Thread *thread_current(void)
25 return model->scheduler->get_current_thread();
28 int Thread::create_context()
32 ret = getcontext(&context);
36 /* start_routine == NULL means this is our initial context */
40 /* Initialize new managed context */
41 stack = stack_allocate(STACK_SIZE);
42 context.uc_stack.ss_sp = stack;
43 context.uc_stack.ss_size = STACK_SIZE;
44 context.uc_stack.ss_flags = 0;
45 context.uc_link = &model->system_thread->context;
46 makecontext(&context, start_routine, 1, arg);
51 int Thread::swap(Thread *t)
53 return swapcontext(&this->context, &t->context);
56 void Thread::dispose()
58 DEBUG("completed thread %d\n", thread_current()->get_id());
59 state = THREAD_COMPLETED;
63 int Thread::switch_to_master(ModelAction *act)
68 model->set_current_action(act);
70 next = model->system_thread;
74 Thread::Thread(thrd_t *t, void (*func)(), void *a) {
81 /* Initialize state */
82 ret = create_context();
84 printf("Error in create_context\n");
86 state = THREAD_CREATED;
87 model->assign_id(this);
88 model->scheduler->add_thread(this);
91 Thread::Thread(thrd_t *t) {
94 state = THREAD_CREATED;
95 model->assign_id(this);
97 model->add_system_thread(this);
100 thread_id_t Thread::get_id()
102 return thrd_to_id(*user_thread);
106 * Return 1 if found next thread, 0 otherwise
108 static int thread_system_next(void)
112 curr = thread_current();
113 model->check_current_action();
115 if (curr->get_state() == THREAD_READY)
116 model->scheduler->add_thread(curr);
117 else if (curr->get_state() == THREAD_RUNNING)
118 /* Stopped while running; i.e., completed */
121 DEBUG("ERROR: current thread in unexpected state??\n");
123 next = model->scheduler->next_thread();
125 next->set_state(THREAD_RUNNING);
126 DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
129 return model->system_thread->swap(next);
132 static void thread_wait_finish(void)
137 while (!thread_system_next());
141 * Main system function
145 thrd_t user_thread, main_thread;
148 model = new ModelChecker();
150 th = new Thread(&main_thread);
152 /* Start user program */
153 thrd_create(&user_thread, &user_main, NULL);
155 /* Wait for all threads to complete */
156 thread_wait_finish();
158 model->print_trace();