09efcae619c06b78ed5c2668473c2b8143b9ea71
[model-checker.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 snapshot_malloc(size);
16 }
17
18 /** Free a stack for a terminated thread. */
19 static void stack_free(void *stack)
20 {
21         snapshot_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         /* Finish thread properly */
49         model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, curr_thread));
50 }
51
52 /**
53  * Create a thread context for a new thread so we can use
54  * setcontext/getcontext/swapcontext to swap it out.
55  * @return 0 on success; otherwise, non-zero error condition
56  */
57 int Thread::create_context()
58 {
59         int ret;
60
61         ret = getcontext(&context);
62         if (ret)
63                 return ret;
64
65         /* Initialize new managed context */
66         stack = stack_allocate(STACK_SIZE);
67         context.uc_stack.ss_sp = stack;
68         context.uc_stack.ss_size = STACK_SIZE;
69         context.uc_stack.ss_flags = 0;
70         context.uc_link = model->get_system_context();
71         makecontext(&context, thread_startup, 0);
72
73         return 0;
74 }
75
76 /**
77  * Swaps the current context to another thread of execution. This form switches
78  * from a user Thread to a system context.
79  * @param t Thread representing the currently-running thread. The current
80  * context is saved here.
81  * @param ctxt Context to which we will swap. Must hold a valid system context.
82  * @return Does not return, unless we return to Thread t's context. See
83  * swapcontext(3) (returns 0 for success, -1 for failure).
84  */
85 int Thread::swap(Thread *t, ucontext_t *ctxt)
86 {
87         return swapcontext(&t->context, ctxt);
88 }
89
90 /**
91  * Swaps the current context to another thread of execution. This form switches
92  * from a system context to a user Thread.
93  * @param ctxt System context variable to which to save the current context.
94  * @param t Thread to which we will swap. Must hold a valid user context.
95  * @return Does not return, unless we return to the system context (ctxt). See
96  * swapcontext(3) (returns 0 for success, -1 for failure).
97  */
98 int Thread::swap(ucontext_t *ctxt, Thread *t)
99 {
100         return swapcontext(ctxt, &t->context);
101 }
102
103
104 /** Terminate a thread and free its stack. */
105 void Thread::complete()
106 {
107         if (!is_complete()) {
108                 DEBUG("completed thread %d\n", get_id());
109                 state = THREAD_COMPLETED;
110                 if (stack)
111                         stack_free(stack);
112         }
113 }
114
115 /**
116  * Construct a new thread.
117  * @param t The thread identifier of the newly created thread.
118  * @param func The function that the thread will call.
119  * @param a The parameter to pass to this function.
120  */
121 Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
122         creation(NULL),
123         pending(NULL),
124         start_routine(func),
125         arg(a),
126         user_thread(t),
127         state(THREAD_CREATED),
128         wait_list(),
129         last_action_val(VALUE_NONE)
130 {
131         int ret;
132
133         /* Initialize state */
134         ret = create_context();
135         if (ret)
136                 printf("Error in create_context\n");
137
138         id = model->get_next_id();
139         *user_thread = id;
140         parent = thread_current();
141 }
142
143 /** Destructor */
144 Thread::~Thread()
145 {
146         complete();
147         model->remove_thread(this);
148 }
149
150 /** @return The thread_id_t corresponding to this Thread object. */
151 thread_id_t Thread::get_id()
152 {
153         return id;
154 }