cyclegraph: improve comments, use initializer list
[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
26 Thread * thread_current(void)
27 {
28         ASSERT(model);
29         return model->scheduler->get_current_thread();
30 }
31
32 /**
33  * Provides a startup wrapper for each thread, allowing some initial
34  * model-checking data to be recorded. This method also gets around makecontext
35  * not being 64-bit clean
36  * @todo We should make the START event always immediately follow the
37  * CREATE event, so we don't get redundant traces...
38  */
39 void thread_startup()
40 {
41         Thread * curr_thread = thread_current();
42
43         /* Add dummy "start" action, just to create a first clock vector */
44         model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
45
46         /* Call the actual thread function */
47         curr_thread->start_routine(curr_thread->arg);
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.
53  */
54
55 int Thread::create_context()
56 {
57         int ret;
58
59         ret = getcontext(&context);
60         if (ret)
61                 return ret;
62
63         /* Initialize new managed context */
64         stack = stack_allocate(STACK_SIZE);
65         context.uc_stack.ss_sp = stack;
66         context.uc_stack.ss_size = STACK_SIZE;
67         context.uc_stack.ss_flags = 0;
68         context.uc_link = model->get_system_context();
69         makecontext(&context, thread_startup, 0);
70
71         return 0;
72 }
73
74 /**
75  * Swaps the current context to another thread of execution. This form switches
76  * from a user Thread to a system context.
77  * @param t Thread representing the current context
78  * @param ctxt Context to switch to
79  * @return Does not return, unless we return to Thread t's context. See
80  * swapcontext(3) (returns 0 for success, -1 for failure).
81  */
82 int Thread::swap(Thread *t, ucontext_t *ctxt)
83 {
84         return swapcontext(&t->context, ctxt);
85 }
86
87 /**
88  * Swaps the current context to another thread of execution. This form switches
89  * from a system context to a user Thread.
90  * @param t Thread representing the current context
91  * @param ctxt Context to switch to
92  * @return Does not return, unless we return to Thread t's context. See
93  * swapcontext(3) (returns 0 for success, -1 for failure).
94  */
95 int Thread::swap(ucontext_t *ctxt, Thread *t)
96 {
97         return swapcontext(ctxt, &t->context);
98 }
99
100
101 /** Terminate a thread and free its stack. */
102 void Thread::complete()
103 {
104         if (state != THREAD_COMPLETED) {
105                 DEBUG("completed thread %d\n", get_id());
106                 state = THREAD_COMPLETED;
107                 if (stack)
108                         stack_free(stack);
109         }
110 }
111
112 /** Create a new thread.
113  *  Takes the following parameters:
114  *  @param t The thread identifier of the newly created thread.
115  *  @param func  The function that the thread will call.
116  *  @param a The parameter to pass to this function. */
117
118 Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
119         start_routine(func),
120         arg(a),
121         user_thread(t),
122         state(THREAD_CREATED),
123         last_action_val(VALUE_NONE)
124 {
125         int ret;
126
127         /* Initialize state */
128         ret = create_context();
129         if (ret)
130                 printf("Error in create_context\n");
131
132         id = model->get_next_id();
133         *user_thread = id;
134         parent = thread_current();
135 }
136
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
145 thread_id_t Thread::get_id()
146 {
147         return id;
148 }