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 return snapshot_malloc(size);
30 /** Free a stack for a terminated thread. */
31 static void stack_free(void *stack)
37 * @brief Get the current Thread
39 * Must be called from a user context
41 * @return The currently executing thread
43 Thread * thread_current(void)
46 return model->get_execution()->get_current_thread();
50 * Provides a startup wrapper for each thread, allowing some initial
51 * model-checking data to be recorded. This method also gets around makecontext
52 * not being 64-bit clean
56 Thread * curr_thread = thread_current();
57 model->get_execution()->threadStart(curr_thread->getParentRecord());
58 /* Call the actual thread function */
59 curr_thread->start_routine(curr_thread->arg);
60 model->get_execution()->threadFinish();
64 * Create a thread context for a new thread so we can use
65 * setcontext/getcontext/swapcontext to swap it out.
66 * @return 0 on success; otherwise, non-zero error condition
68 int Thread::create_context()
72 ret = getcontext(&context);
76 /* Initialize new managed context */
77 stack = stack_allocate(STACK_SIZE);
78 context.uc_stack.ss_sp = stack;
79 context.uc_stack.ss_size = STACK_SIZE;
80 context.uc_stack.ss_flags = 0;
81 context.uc_link = model->get_scheduler()->get_system_context();
82 makecontext(&context, thread_startup, 0);
88 /** Terminate a thread and free its stack. */
89 void Thread::complete()
91 ASSERT(!is_complete());
92 DEBUG("completed thread %d\n", id_to_int(get_id()));
93 state = THREAD_COMPLETED;
97 * @brief Construct a new model-checker Thread
99 * A model-checker Thread is used for accounting purposes only. It will never
100 * have its own stack, and it should never be inserted into the Scheduler.
102 * @param tid The thread ID to assign
104 Thread::Thread(thread_id_t tid) :
112 state(THREAD_READY), /* Thread is always ready? */
115 memset(&context, 0, sizeof(context));
119 * Construct a new thread.
120 * @param t The thread identifier of the newly created thread.
121 * @param func The function that the thread will call.
122 * @param a The parameter to pass to this function.
124 Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent, EPRecord *record) :
126 parentrecord(record),
132 state(THREAD_CREATED),
137 /* Initialize state */
138 ret = create_context();
140 model_print("Error in create_context\n");
142 user_thread->priv = this;
154 /** @return The thread_id_t corresponding to this Thread object. */
155 thread_id_t Thread::get_id() const
161 * Set a thread's THREAD_* state (@see thread_state)
162 * @param s The state to enter
164 void Thread::set_state(thread_state s)
166 ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
171 * Get the Thread that this Thread is immediately waiting on
172 * @return The thread we are waiting on, if any; otherwise NULL
174 Thread * Thread::waiting_on()
179 void Thread::set_waiting(Thread *th) {
184 * Check if this Thread is waiting (blocking) on a given Thread, directly or
185 * indirectly (via a chain of waiting threads)
187 * @param t The Thread on which we may be waiting
188 * @return True if we are waiting on Thread t; false otherwise
190 bool Thread::is_waiting_on(const Thread *t) {
192 for (wait = waiting_on();wait != NULL;wait = wait->waiting_on())