add copyright message
[cdsspec-compiler.git] / threads.cc
1 /** @file threads.cc
2  *  @brief Thread functions.
3  */
4
5 #include <string.h>
6
7 #include <threads.h>
8 #include <mutex>
9 #include "common.h"
10 #include "threads-model.h"
11 #include "action.h"
12
13 /* global "model" object */
14 #include "model.h"
15
16 /** Allocate a stack for a new thread. */
17 static void * stack_allocate(size_t size)
18 {
19         return snapshot_malloc(size);
20 }
21
22 /** Free a stack for a terminated thread. */
23 static void stack_free(void *stack)
24 {
25         snapshot_free(stack);
26 }
27
28 /**
29  * @brief Get the current Thread
30  *
31  * Must be called from a user context
32  *
33  * @return The currently executing thread
34  */
35 Thread * thread_current(void)
36 {
37         ASSERT(model);
38         return model->get_current_thread();
39 }
40
41 /**
42  * Provides a startup wrapper for each thread, allowing some initial
43  * model-checking data to be recorded. This method also gets around makecontext
44  * not being 64-bit clean
45  */
46 void thread_startup()
47 {
48         Thread * curr_thread = thread_current();
49
50         /* Add dummy "start" action, just to create a first clock vector */
51         model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
52
53         /* Call the actual thread function */
54         curr_thread->start_routine(curr_thread->arg);
55
56         /* Finish thread properly */
57         model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, curr_thread));
58 }
59
60 /**
61  * Create a thread context for a new thread so we can use
62  * setcontext/getcontext/swapcontext to swap it out.
63  * @return 0 on success; otherwise, non-zero error condition
64  */
65 int Thread::create_context()
66 {
67         int ret;
68
69         ret = getcontext(&context);
70         if (ret)
71                 return ret;
72
73         /* Initialize new managed context */
74         stack = stack_allocate(STACK_SIZE);
75         context.uc_stack.ss_sp = stack;
76         context.uc_stack.ss_size = STACK_SIZE;
77         context.uc_stack.ss_flags = 0;
78         context.uc_link = model->get_system_context();
79         makecontext(&context, thread_startup, 0);
80
81         return 0;
82 }
83
84 /**
85  * Swaps the current context to another thread of execution. This form switches
86  * from a user Thread to a system context.
87  * @param t Thread representing the currently-running thread. The current
88  * context is saved here.
89  * @param ctxt Context to which we will swap. Must hold a valid system context.
90  * @return Does not return, unless we return to Thread t's context. See
91  * swapcontext(3) (returns 0 for success, -1 for failure).
92  */
93 int Thread::swap(Thread *t, ucontext_t *ctxt)
94 {
95         t->set_state(THREAD_READY);
96         return swapcontext(&t->context, ctxt);
97 }
98
99 /**
100  * Swaps the current context to another thread of execution. This form switches
101  * from a system context to a user Thread.
102  * @param ctxt System context variable to which to save the current context.
103  * @param t Thread to which we will swap. Must hold a valid user context.
104  * @return Does not return, unless we return to the system context (ctxt). See
105  * swapcontext(3) (returns 0 for success, -1 for failure).
106  */
107 int Thread::swap(ucontext_t *ctxt, Thread *t)
108 {
109         t->set_state(THREAD_RUNNING);
110         return swapcontext(ctxt, &t->context);
111 }
112
113
114 /** Terminate a thread and free its stack. */
115 void Thread::complete()
116 {
117         ASSERT(!is_complete());
118         DEBUG("completed thread %d\n", id_to_int(get_id()));
119         state = THREAD_COMPLETED;
120         if (stack)
121                 stack_free(stack);
122 }
123
124 /**
125  * @brief Construct a new model-checker Thread
126  *
127  * A model-checker Thread is used for accounting purposes only. It will never
128  * have its own stack, and it should never be inserted into the Scheduler.
129  *
130  * @param tid The thread ID to assign
131  */
132 Thread::Thread(thread_id_t tid) :
133         parent(NULL),
134         creation(NULL),
135         pending(NULL),
136         start_routine(NULL),
137         arg(NULL),
138         stack(NULL),
139         user_thread(NULL),
140         id(tid),
141         state(THREAD_READY), /* Thread is always ready? */
142         wait_list(),
143         last_action_val(0),
144         model_thread(true)
145 {
146         memset(&context, 0, sizeof(context));
147 }
148
149 /**
150  * Construct a new thread.
151  * @param t The thread identifier of the newly created thread.
152  * @param func The function that the thread will call.
153  * @param a The parameter to pass to this function.
154  */
155 Thread::Thread(thrd_t *t, void (*func)(void *), void *a, Thread *parent) :
156         parent(parent),
157         creation(NULL),
158         pending(NULL),
159         start_routine(func),
160         arg(a),
161         user_thread(t),
162         state(THREAD_CREATED),
163         wait_list(),
164         last_action_val(VALUE_NONE),
165         model_thread(false)
166 {
167         int ret;
168
169         /* Initialize state */
170         ret = create_context();
171         if (ret)
172                 model_print("Error in create_context\n");
173
174         id = model->get_next_id();
175         user_thread->priv = this;
176 }
177
178 /** Destructor */
179 Thread::~Thread()
180 {
181         if (!is_complete())
182                 complete();
183 }
184
185 /** @return The thread_id_t corresponding to this Thread object. */
186 thread_id_t Thread::get_id() const
187 {
188         return id;
189 }
190
191 /**
192  * Set a thread's THREAD_* state (@see thread_state)
193  * @param s The state to enter
194  */
195 void Thread::set_state(thread_state s)
196 {
197         ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
198         state = s;
199 }
200
201 /**
202  * Get the Thread that this Thread is waiting on
203  * @return The thread we are waiting on, if any; otherwise NULL
204  */
205 Thread * Thread::waiting_on() const
206 {
207         if (!pending)
208                 return NULL;
209
210         if (pending->get_type() == THREAD_JOIN)
211                 return pending->get_thread_operand();
212         else if (pending->is_lock())
213                 return (Thread *)pending->get_mutex()->get_state()->locked;
214         return NULL;
215 }