4 #include "libthreads.h"
7 #include "threads_internal.h"
9 /* global "model" object */
12 #define STACK_SIZE (1024 * 1024)
14 static void *stack_allocate(size_t size)
19 static void stack_free(void *stack)
24 static int create_context(struct thread *t)
28 memset(&t->context, 0, sizeof(t->context));
29 ret = getcontext(&t->context);
33 /* t->start_routine == NULL means this is our initial context */
34 if (!t->start_routine)
37 /* Initialize new managed context */
38 t->stack = stack_allocate(STACK_SIZE);
39 t->context.uc_stack.ss_sp = t->stack;
40 t->context.uc_stack.ss_size = STACK_SIZE;
41 t->context.uc_stack.ss_flags = 0;
42 t->context.uc_link = &model->system_thread->context;
43 makecontext(&t->context, t->start_routine, 1, t->arg);
48 static int create_initial_thread(struct thread *t)
50 memset(t, 0, sizeof(*t));
52 return create_context(t);
55 static int thread_swap(struct thread *t1, struct thread *t2)
57 return swapcontext(&t1->context, &t2->context);
60 static void thread_dispose(struct thread *t)
62 DEBUG("completed thread %d\n", thread_current()->id);
63 t->state = THREAD_COMPLETED;
68 * Return 1 if found next thread, 0 otherwise
70 static int thread_system_next(void)
72 struct thread *curr, *next;
74 curr = thread_current();
76 if (curr->state == THREAD_READY)
77 model->scheduler->add_thread(curr);
78 else if (curr->state == THREAD_RUNNING)
79 /* Stopped while running; i.e., completed */
82 DEBUG("ERROR: current thread in unexpected state??\n");
84 next = model->scheduler->next_thread();
86 next->state = THREAD_RUNNING;
87 DEBUG("(%d, %d)\n", curr ? curr->id : -1, next ? next->id : -1);
90 return thread_swap(model->system_thread, next);
93 static void thread_wait_finish(void)
98 while (!thread_system_next());
101 int thread_switch_to_master(ModelAction *act)
103 struct thread *old, *next;
106 model->set_current_action(act);
107 old = thread_current();
108 old->state = THREAD_READY;
109 next = model->system_thread;
110 return thread_swap(old, next);
114 * User program API functions
116 int thread_create(struct thread *t, void (*start_routine)(), void *arg)
122 memset(t, 0, sizeof(*t));
124 DEBUG("create thread %d\n", t->id);
126 t->start_routine = start_routine;
129 /* Initialize state */
130 ret = create_context(t);
134 t->state = THREAD_CREATED;
136 model->scheduler->add_thread(t);
140 int thread_join(struct thread *t)
143 while (t->state != THREAD_COMPLETED && !ret)
144 /* seq_cst is just a 'don't care' parameter */
145 ret = thread_switch_to_master(new ModelAction(THREAD_JOIN, memory_order_seq_cst, NULL, VALUE_NONE));
149 int thread_yield(void)
151 /* seq_cst is just a 'don't care' parameter */
152 return thread_switch_to_master(new ModelAction(THREAD_YIELD, memory_order_seq_cst, NULL, VALUE_NONE));
155 struct thread *thread_current(void)
157 return model->scheduler->get_current_thread();
161 * Main system function
165 struct thread user_thread;
166 struct thread *main_thread;
168 model = new ModelChecker();
170 main_thread = (struct thread *)myMalloc(sizeof(*main_thread));
171 create_initial_thread(main_thread);
172 model->add_system_thread(main_thread);
174 /* Start user program */
175 thread_create(&user_thread, &user_main, NULL);
177 /* Wait for all threads to complete */
178 thread_wait_finish();