schedule, threads: update comments, const's
[c11tester.git] / threads.cc
1 /** @file threads.cc
2  *  @brief Thread functions.
3  */
4
5 #include "libthreads.h"
6 #include "common.h"
7 #include "threads.h"
8
9 /* global "model" object */
10 #include "model.h"
11
12 /** Allocate a stack for a new thread. */
13 static void * stack_allocate(size_t size)
14 {
15         return malloc(size);
16 }
17
18 /** Free a stack for a terminated thread. */
19 static void stack_free(void *stack)
20 {
21         free(stack);
22 }
23
24 /** Return the currently executing thread. */
25 Thread * thread_current(void)
26 {
27         ASSERT(model);
28         return model->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 void thread_startup()
39 {
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 /**
50  * Create a thread context for a new thread so we can use
51  * setcontext/getcontext/swapcontext to swap it out.
52  * @return 0 on success; otherwise, non-zero error condition
53  */
54 int Thread::create_context()
55 {
56         int ret;
57
58         ret = getcontext(&context);
59         if (ret)
60                 return ret;
61
62         /* Initialize new managed context */
63         stack = stack_allocate(STACK_SIZE);
64         context.uc_stack.ss_sp = stack;
65         context.uc_stack.ss_size = STACK_SIZE;
66         context.uc_stack.ss_flags = 0;
67         context.uc_link = model->get_system_context();
68         makecontext(&context, thread_startup, 0);
69
70         return 0;
71 }
72
73 /**
74  * Swaps the current context to another thread of execution. This form switches
75  * from a user Thread to a system context.
76  * @param t Thread representing the current context
77  * @param ctxt Context to switch to
78  * @return Does not return, unless we return to Thread t's context. See
79  * swapcontext(3) (returns 0 for success, -1 for failure).
80  */
81 int Thread::swap(Thread *t, ucontext_t *ctxt)
82 {
83         return swapcontext(&t->context, ctxt);
84 }
85
86 /**
87  * Swaps the current context to another thread of execution. This form switches
88  * from a system context to a user Thread.
89  * @param t Thread representing the current context
90  * @param ctxt Context to switch to
91  * @return Does not return, unless we return to Thread t's context. See
92  * swapcontext(3) (returns 0 for success, -1 for failure).
93  */
94 int Thread::swap(ucontext_t *ctxt, Thread *t)
95 {
96         return swapcontext(ctxt, &t->context);
97 }
98
99
100 /** Terminate a thread and free its stack. */
101 void Thread::complete()
102 {
103         if (state != THREAD_COMPLETED) {
104                 DEBUG("completed thread %d\n", get_id());
105                 state = THREAD_COMPLETED;
106                 if (stack)
107                         stack_free(stack);
108         }
109 }
110
111 /**
112  * Construct a new thread.
113  * @param t The thread identifier of the newly created thread.
114  * @param func The function that the thread will call.
115  * @param a The parameter to pass to this function.
116  */
117 Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
118         start_routine(func),
119         arg(a),
120         user_thread(t),
121         state(THREAD_CREATED),
122         last_action_val(VALUE_NONE)
123 {
124         int ret;
125
126         /* Initialize state */
127         ret = create_context();
128         if (ret)
129                 printf("Error in create_context\n");
130
131         id = model->get_next_id();
132         *user_thread = id;
133         parent = thread_current();
134 }
135
136 /** Destructor */
137 Thread::~Thread()
138 {
139         complete();
140         model->remove_thread(this);
141 }
142
143 /** @return The thread_id_t corresponding to this Thread object. */
144 thread_id_t Thread::get_id()
145 {
146         return id;
147 }