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();
95 model->remove_thread(this);
98 thread_id_t Thread::get_id()
104 * Return 1 if found next thread, 0 otherwise
106 static int thread_system_next(void)
110 curr = thread_current();
112 if (curr->get_state() == THREAD_READY) {
113 model->check_current_action();
114 model->scheduler->add_thread(curr);
115 } else if (curr->get_state() == THREAD_RUNNING)
116 /* Stopped while running; i.e., completed */
119 DEBUG("ERROR: current thread in unexpected state??\n");
121 next = model->scheduler->next_thread();
123 next->set_state(THREAD_RUNNING);
124 DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
127 return Thread::swap(model->get_system_context(), next);
130 static void thread_wait_finish(void)
135 while (!thread_system_next());
139 * Main system function
144 ucontext_t main_context;
146 model = new ModelChecker();
148 if (getcontext(&main_context))
151 model->set_system_context(&main_context);
154 /* Start user program */
155 thrd_create(&user_thread, &user_main, NULL);
157 /* Wait for all threads to complete */
158 thread_wait_finish();
159 } while (model->next_execution());