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