1 /* Copyright (c) 2015 Regents of the University of California
3 * Author: Brian Demsky <bdemsky@uci.edu>
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.
11 * @brief Thread functions.
19 #include "threads-model.h"
20 #include "mcschedule.h"
21 /* global "model" object */
24 /** Allocate a stack for a new thread. */
25 static void * stack_allocate(size_t size)
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.
31 return snapshot_malloc(size);
34 /** Free a stack for a terminated thread. */
35 static void stack_free(void *stack)
41 * @brief Get the current Thread
43 * Must be called from a user context
45 * @return The currently executing thread
47 Thread * thread_current(void)
50 return model->get_execution()->get_current_thread();
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
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();
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
72 int Thread::create_context()
76 ret = getcontext(&context);
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);
92 /** Terminate a thread and free its stack. */
93 void Thread::complete()
95 ASSERT(!is_complete());
96 DEBUG("completed thread %d\n", id_to_int(get_id()));
97 state = THREAD_COMPLETED;
101 * @brief Construct a new model-checker Thread
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.
106 * @param tid The thread ID to assign
108 Thread::Thread(thread_id_t tid) :
116 state(THREAD_READY), /* Thread is always ready? */
119 memset(&context, 0, sizeof(context));
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.
128 Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent, EPRecord *record) :
130 parentrecord(record),
136 state(THREAD_CREATED),
141 /* Initialize state */
142 ret = create_context();
144 model_print("Error in create_context\n");
146 user_thread->priv = this;
158 /** @return The thread_id_t corresponding to this Thread object. */
159 thread_id_t Thread::get_id() const
165 * Set a thread's THREAD_* state (@see thread_state)
166 * @param s The state to enter
168 void Thread::set_state(thread_state s)
170 ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
175 * Get the Thread that this Thread is immediately waiting on
176 * @return The thread we are waiting on, if any; otherwise NULL
178 Thread * Thread::waiting_on()
183 void Thread::set_waiting(Thread *th) {
188 * Check if this Thread is waiting (blocking) on a given Thread, directly or
189 * indirectly (via a chain of waiting threads)
191 * @param t The Thread on which we may be waiting
192 * @return True if we are waiting on Thread t; false otherwise
194 bool Thread::is_waiting_on(const Thread *t) {
196 for (wait = waiting_on();wait != NULL;wait = wait->waiting_on())