2 * @brief Thread functions.
9 #include "threads-model.h"
12 /* global "model" object */
15 /** Allocate a stack for a new thread. */
16 static void * stack_allocate(size_t size)
18 return snapshot_malloc(size);
21 /** Free a stack for a terminated thread. */
22 static void stack_free(void *stack)
28 * @brief Get the current Thread
30 * Must be called from a user context
32 * @return The currently executing thread
34 Thread * thread_current(void)
37 return model->get_current_thread();
41 * Provides a startup wrapper for each thread, allowing some initial
42 * model-checking data to be recorded. This method also gets around makecontext
43 * not being 64-bit clean
47 Thread * curr_thread = thread_current();
49 /* Add dummy "start" action, just to create a first clock vector */
50 model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
52 /* Call the actual thread function */
53 curr_thread->start_routine(curr_thread->arg);
55 /* Finish thread properly */
56 model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, curr_thread));
60 * Create a thread context for a new thread so we can use
61 * setcontext/getcontext/swapcontext to swap it out.
62 * @return 0 on success; otherwise, non-zero error condition
64 int Thread::create_context()
68 ret = getcontext(&context);
72 /* Initialize new managed context */
73 stack = stack_allocate(STACK_SIZE);
74 context.uc_stack.ss_sp = stack;
75 context.uc_stack.ss_size = STACK_SIZE;
76 context.uc_stack.ss_flags = 0;
77 context.uc_link = model->get_system_context();
78 makecontext(&context, thread_startup, 0);
84 * Swaps the current context to another thread of execution. This form switches
85 * from a user Thread to a system context.
86 * @param t Thread representing the currently-running thread. The current
87 * context is saved here.
88 * @param ctxt Context to which we will swap. Must hold a valid system context.
89 * @return Does not return, unless we return to Thread t's context. See
90 * swapcontext(3) (returns 0 for success, -1 for failure).
92 int Thread::swap(Thread *t, ucontext_t *ctxt)
94 t->set_state(THREAD_READY);
95 return swapcontext(&t->context, ctxt);
99 * Swaps the current context to another thread of execution. This form switches
100 * from a system context to a user Thread.
101 * @param ctxt System context variable to which to save the current context.
102 * @param t Thread to which we will swap. Must hold a valid user context.
103 * @return Does not return, unless we return to the system context (ctxt). See
104 * swapcontext(3) (returns 0 for success, -1 for failure).
106 int Thread::swap(ucontext_t *ctxt, Thread *t)
108 t->set_state(THREAD_RUNNING);
109 return swapcontext(ctxt, &t->context);
113 /** Terminate a thread and free its stack. */
114 void Thread::complete()
116 ASSERT(!is_complete());
117 DEBUG("completed thread %d\n", id_to_int(get_id()));
118 state = THREAD_COMPLETED;
124 * @brief Construct a new model-checker Thread
126 * A model-checker Thread is used for accounting purposes only. It will never
127 * have its own stack, and it should never be inserted into the Scheduler.
129 * @param tid The thread ID to assign
131 Thread::Thread(thread_id_t tid) :
140 state(THREAD_READY), /* Thread is always ready? */
145 memset(&context, 0, sizeof(context));
149 * Construct a new thread.
150 * @param t The thread identifier of the newly created thread.
151 * @param func The function that the thread will call.
152 * @param a The parameter to pass to this function.
154 Thread::Thread(thrd_t *t, void (*func)(void *), void *a, Thread *parent) :
161 state(THREAD_CREATED),
163 last_action_val(VALUE_NONE),
168 /* Initialize state */
169 ret = create_context();
171 model_print("Error in create_context\n");
173 id = model->get_next_id();
174 user_thread->priv = this;
182 model->remove_thread(this);
185 /** @return The thread_id_t corresponding to this Thread object. */
186 thread_id_t Thread::get_id() const
192 * Set a thread's THREAD_* state (@see thread_state)
193 * @param s The state to enter
195 void Thread::set_state(thread_state s)
197 ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);