Fix MC_Equals to handle NODEP MCIDs.
[satcheck.git] / threads.cc
1 /*      Copyright (c) 2015 Regents of the University of California
2  *
3  *      Author: Brian Demsky <bdemsky@uci.edu>
4  *
5  *      This program is free software; you can redistribute it and/or
6  *      modify it under the terms of the GNU General Public License
7  *      version 2 as published by the Free Software Foundation.
8  */
9
10 /** @file threads.cc
11  *  @brief Thread functions.
12  */
13
14 #include <string.h>
15
16 #include <threads.h>
17 #include <mutex>
18 #include "common.h"
19 #include "threads-model.h"
20 #include "mcschedule.h"
21 /* global "model" object */
22 #include "model.h"
23
24 /** Allocate a stack for a new thread. */
25 static void * stack_allocate(size_t size)
26 {
27         // TODO: This could be a bug if we have programs that start threads
28         // in different orders.  The solution is to use the same strategy
29         // the ALLOC action uses.
30
31         return snapshot_malloc(size);
32 }
33
34 /** Free a stack for a terminated thread. */
35 static void stack_free(void *stack)
36 {
37         snapshot_free(stack);
38 }
39
40 /**
41  * @brief Get the current Thread
42  *
43  * Must be called from a user context
44  *
45  * @return The currently executing thread
46  */
47 Thread * thread_current(void)
48 {
49         ASSERT(model);
50         return model->get_execution()->get_current_thread();
51 }
52
53 /**
54  * Provides a startup wrapper for each thread, allowing some initial
55  * model-checking data to be recorded. This method also gets around makecontext
56  * not being 64-bit clean
57  */
58 void thread_startup()
59 {
60         Thread * curr_thread = thread_current();
61         model->get_execution()->threadStart(curr_thread->getParentRecord());
62         /* Call the actual thread function */
63         curr_thread->start_routine(curr_thread->arg);
64         model->get_execution()->threadFinish();
65 }
66
67 /**
68  * Create a thread context for a new thread so we can use
69  * setcontext/getcontext/swapcontext to swap it out.
70  * @return 0 on success; otherwise, non-zero error condition
71  */
72 int Thread::create_context()
73 {
74         int ret;
75
76         ret = getcontext(&context);
77         if (ret)
78                 return ret;
79
80         /* Initialize new managed context */
81         stack = stack_allocate(STACK_SIZE);
82         context.uc_stack.ss_sp = stack;
83         context.uc_stack.ss_size = STACK_SIZE;
84         context.uc_stack.ss_flags = 0;
85         context.uc_link = model->get_scheduler()->get_system_context();
86         makecontext(&context, thread_startup, 0);
87
88         return 0;
89 }
90
91
92 /** Terminate a thread and free its stack. */
93 void Thread::complete()
94 {
95         ASSERT(!is_complete());
96         DEBUG("completed thread %d\n", id_to_int(get_id()));
97         state = THREAD_COMPLETED;
98 }
99
100 /**
101  * @brief Construct a new model-checker Thread
102  *
103  * A model-checker Thread is used for accounting purposes only. It will never
104  * have its own stack, and it should never be inserted into the Scheduler.
105  *
106  * @param tid The thread ID to assign
107  */
108 Thread::Thread(thread_id_t tid) :
109         parent(NULL),
110         start_routine(NULL),
111         arg(NULL),
112         stack(NULL),
113         user_thread(NULL),
114         waiting(NULL),
115         id(tid),
116         state(THREAD_READY),                    /* Thread is always ready? */
117         model_thread(true)
118 {
119         memset(&context, 0, sizeof(context));
120 }
121
122 /**
123  * Construct a new thread.
124  * @param t The thread identifier of the newly created thread.
125  * @param func The function that the thread will call.
126  * @param a The parameter to pass to this function.
127  */
128 Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent, EPRecord *record) :
129         parent(parent),
130         parentrecord(record),
131         start_routine(func),
132         arg(a),
133         user_thread(t),
134         waiting(NULL),
135         id(tid),
136         state(THREAD_CREATED),
137         model_thread(false)
138 {
139         int ret;
140
141         /* Initialize state */
142         ret = create_context();
143         if (ret)
144                 model_print("Error in create_context\n");
145
146         user_thread->priv = this;
147 }
148
149 /** Destructor */
150 Thread::~Thread()
151 {
152         if (!is_complete())
153                 complete();
154         if (stack)
155                 stack_free(stack);
156 }
157
158 /** @return The thread_id_t corresponding to this Thread object. */
159 thread_id_t Thread::get_id() const
160 {
161         return id;
162 }
163
164 /**
165  * Set a thread's THREAD_* state (@see thread_state)
166  * @param s The state to enter
167  */
168 void Thread::set_state(thread_state s)
169 {
170         ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
171         state = s;
172 }
173
174 /**
175  * Get the Thread that this Thread is immediately waiting on
176  * @return The thread we are waiting on, if any; otherwise NULL
177  */
178 Thread * Thread::waiting_on()
179 {
180         return waiting;
181 }
182
183 void Thread::set_waiting(Thread *th) {
184         waiting=th;
185 }
186
187 /**
188  * Check if this Thread is waiting (blocking) on a given Thread, directly or
189  * indirectly (via a chain of waiting threads)
190  *
191  * @param t The Thread on which we may be waiting
192  * @return True if we are waiting on Thread t; false otherwise
193  */
194 bool Thread::is_waiting_on(const Thread *t) {
195         Thread *wait;
196         for (wait = waiting_on();wait != NULL;wait = wait->waiting_on())
197                 if (wait == t)
198                         return true;
199         return false;
200 }