2 * @brief Thread functions.
9 #include "threads-model.h"
11 /* global "model" object */
14 /** Allocate a stack for a new thread. */
15 static void * stack_allocate(size_t size)
17 return snapshot_malloc(size);
20 /** Free a stack for a terminated thread. */
21 static void stack_free(void *stack)
26 /** Return the currently executing thread. */
27 Thread * thread_current(void)
30 return model->get_current_thread();
34 * Provides a startup wrapper for each thread, allowing some initial
35 * model-checking data to be recorded. This method also gets around makecontext
36 * not being 64-bit clean
40 Thread * curr_thread = thread_current();
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));
45 /* Call the actual thread function */
46 curr_thread->start_routine(curr_thread->arg);
48 /* Finish thread properly */
49 model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, curr_thread));
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
57 int Thread::create_context()
61 ret = getcontext(&context);
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);
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).
85 int Thread::swap(Thread *t, ucontext_t *ctxt)
87 t->set_state(THREAD_READY);
88 return swapcontext(&t->context, ctxt);
92 * Swaps the current context to another thread of execution. This form switches
93 * from a system context to a user Thread.
94 * @param ctxt System context variable to which to save the current context.
95 * @param t Thread to which we will swap. Must hold a valid user context.
96 * @return Does not return, unless we return to the system context (ctxt). See
97 * swapcontext(3) (returns 0 for success, -1 for failure).
99 int Thread::swap(ucontext_t *ctxt, Thread *t)
101 t->set_state(THREAD_RUNNING);
102 return swapcontext(ctxt, &t->context);
106 /** Terminate a thread and free its stack. */
107 void Thread::complete()
109 ASSERT(!is_complete());
110 DEBUG("completed thread %d\n", id_to_int(get_id()));
111 state = THREAD_COMPLETED;
117 * @brief Construct a new model-checker Thread
119 * A model-checker Thread is used for accounting purposes only. It will never
120 * have its own stack, and it should never be inserted into the Scheduler.
122 * @param tid The thread ID to assign
124 Thread::Thread(thread_id_t tid) :
133 state(THREAD_READY), /* Thread is always ready? */
138 memset(&context, 0, sizeof(context));
142 * Construct a new thread.
143 * @param t The thread identifier of the newly created thread.
144 * @param func The function that the thread will call.
145 * @param a The parameter to pass to this function.
147 Thread::Thread(thrd_t *t, void (*func)(void *), void *a) :
153 state(THREAD_CREATED),
155 last_action_val(VALUE_NONE),
160 /* Initialize state */
161 ret = create_context();
163 model_print("Error in create_context\n");
165 id = model->get_next_id();
166 user_thread->priv = this;
167 parent = thread_current();
175 model->remove_thread(this);
178 /** @return The thread_id_t corresponding to this Thread object. */
179 thread_id_t Thread::get_id() const
185 * Set a thread's THREAD_* state (@see thread_state)
186 * @param s The state to enter
188 void Thread::set_state(thread_state s)
190 ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);