fix my todo comments so they appear in documentation
[cdsspec-compiler.git] / threads.cc
1 /** @file threads.cc
2  *  @brief Thread functions.
3  */
4
5
6 #include "libthreads.h"
7 #include "common.h"
8 #include "threads.h"
9
10 /* global "model" object */
11 #include "model.h"
12
13 #define STACK_SIZE (1024 * 1024)
14
15 static void * stack_allocate(size_t size)
16 {
17         return malloc(size);
18 }
19
20 static void stack_free(void *stack)
21 {
22         free(stack);
23 }
24
25 Thread * thread_current(void)
26 {
27         ASSERT(model);
28         return model->scheduler->get_current_thread();
29 }
30
31 /**
32  * Provides a startup wrapper for each thread, allowing some initial
33  * model-checking data to be recorded. This method also gets around makecontext
34  * not being 64-bit clean
35  * @todo We should make the START event always immediately follow the
36  * CREATE event, so we don't get redundant traces...
37  */
38
39 void thread_startup() {
40         Thread * curr_thread = thread_current();
41
42         /* Add dummy "start" action, just to create a first clock vector */
43         model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
44
45         /* Call the actual thread function */
46         curr_thread->start_routine(curr_thread->arg);
47 }
48
49 int Thread::create_context()
50 {
51         int ret;
52
53         ret = getcontext(&context);
54         if (ret)
55                 return ret;
56
57         /* Initialize new managed context */
58         stack = stack_allocate(STACK_SIZE);
59         context.uc_stack.ss_sp = stack;
60         context.uc_stack.ss_size = STACK_SIZE;
61         context.uc_stack.ss_flags = 0;
62         context.uc_link = model->get_system_context();
63         makecontext(&context, thread_startup, 0);
64
65         return 0;
66 }
67
68 int Thread::swap(Thread *t, ucontext_t *ctxt)
69 {
70         return swapcontext(&t->context, ctxt);
71 }
72
73 int Thread::swap(ucontext_t *ctxt, Thread *t)
74 {
75         return swapcontext(ctxt, &t->context);
76 }
77
78 void Thread::complete()
79 {
80         if (state != THREAD_COMPLETED) {
81                 DEBUG("completed thread %d\n", get_id());
82                 state = THREAD_COMPLETED;
83                 if (stack)
84                         stack_free(stack);
85         }
86 }
87
88 Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
89         start_routine(func),
90         arg(a),
91         user_thread(t),
92         state(THREAD_CREATED),
93         last_action_val(VALUE_NONE)
94 {
95         int ret;
96
97         /* Initialize state */
98         ret = create_context();
99         if (ret)
100                 printf("Error in create_context\n");
101
102         id = model->get_next_id();
103         *user_thread = id;
104         parent = thread_current();
105 }
106
107 Thread::~Thread()
108 {
109         complete();
110         model->remove_thread(this);
111 }
112
113 thread_id_t Thread::get_id()
114 {
115         return id;
116 }