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