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