3 #include "libthreads.h"
8 /* global "model" object */
11 #define STACK_SIZE (1024 * 1024)
13 static void * stack_allocate(size_t size)
15 return userMalloc(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 /* Initialize new managed context */
37 stack = stack_allocate(STACK_SIZE);
38 context.uc_stack.ss_sp = stack;
39 context.uc_stack.ss_size = STACK_SIZE;
40 context.uc_stack.ss_flags = 0;
41 context.uc_link = model->get_system_context();
42 makecontext(&context, start_routine, 1, arg);
47 int Thread::swap(Thread *t, ucontext_t *ctxt)
49 return swapcontext(&t->context, ctxt);
52 int Thread::swap(ucontext_t *ctxt, Thread *t)
54 return swapcontext(ctxt, &t->context);
57 void Thread::complete()
59 if (state != THREAD_COMPLETED) {
60 DEBUG("completed thread %d\n", get_id());
61 state = THREAD_COMPLETED;
67 void * Thread::operator new(size_t size) {
68 return userMalloc(size);
71 void Thread::operator delete(void *ptr) {
75 Thread::Thread(thrd_t *t, void (*func)(), void *a) {
82 /* Initialize state */
83 ret = create_context();
85 printf("Error in create_context\n");
87 state = THREAD_CREATED;
88 id = model->get_next_id();
90 parent = thread_current();
96 model->remove_thread(this);
99 thread_id_t Thread::get_id()
105 * Return 1 if found next thread, 0 otherwise
107 static int thread_system_next(void)
111 curr = thread_current();
113 if (curr->get_state() == THREAD_READY) {
114 model->check_current_action();
115 model->scheduler->add_thread(curr);
116 } else if (curr->get_state() == THREAD_RUNNING)
117 /* Stopped while running; i.e., completed */
122 next = model->scheduler->next_thread();
124 next->set_state(THREAD_RUNNING);
125 DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
128 return Thread::swap(model->get_system_context(), next);
131 static void thread_wait_finish(void)
136 while (!thread_system_next());
140 * Main system function
145 ucontext_t main_context;
147 model = new ModelChecker();
149 if (getcontext(&main_context))
152 model->set_system_context(&main_context);
155 /* Start user program */
156 model->add_thread(new Thread(&user_thread, &user_main, NULL));
158 /* Wait for all threads to complete */
159 thread_wait_finish();
160 } while (model->next_execution());