2 * @brief Thread functions.
10 #include "threads-model.h"
13 /* global "model" object */
16 /** Allocate a stack for a new thread. */
17 static void * stack_allocate(size_t size)
19 return Thread_malloc(size);
22 /** Free a stack for a terminated thread. */
23 static void stack_free(void *stack)
29 * @brief Get the current Thread
31 * Must be called from a user context
33 * @return The currently executing thread
35 Thread * thread_current(void)
38 return model->get_current_thread();
42 * Provides a startup wrapper for each thread, allowing some initial
43 * model-checking data to be recorded. This method also gets around makecontext
44 * not being 64-bit clean
48 Thread * curr_thread = thread_current();
50 /* Add dummy "start" action, just to create a first clock vector */
51 model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
53 /* Call the actual thread function */
54 if (curr_thread->start_routine != NULL) {
55 curr_thread->start_routine(curr_thread->arg);
56 } else if (curr_thread->pstart_routine != NULL) {
57 curr_thread->pstart_routine(curr_thread->arg);
59 /* Finish thread properly */
60 model->switch_to_master(new ModelAction(THREAD_FINISH, std::memory_order_seq_cst, curr_thread));
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_system_context();
82 makecontext(&context, thread_startup, 0);
88 * Swaps the current context to another thread of execution. This form switches
89 * from a user Thread to a system context.
90 * @param t Thread representing the currently-running thread. The current
91 * context is saved here.
92 * @param ctxt Context to which we will swap. Must hold a valid system context.
93 * @return Does not return, unless we return to Thread t's context. See
94 * swapcontext(3) (returns 0 for success, -1 for failure).
96 int Thread::swap(Thread *t, ucontext_t *ctxt)
98 t->set_state(THREAD_READY);
99 return model_swapcontext(&t->context, ctxt);
103 * Swaps the current context to another thread of execution. This form switches
104 * from a system context to a user Thread.
105 * @param ctxt System context variable to which to save the current context.
106 * @param t Thread to which we will swap. Must hold a valid user context.
107 * @return Does not return, unless we return to the system context (ctxt). See
108 * swapcontext(3) (returns 0 for success, -1 for failure).
110 int Thread::swap(ucontext_t *ctxt, Thread *t)
112 t->set_state(THREAD_RUNNING);
113 return model_swapcontext(ctxt, &t->context);
117 /** Terminate a thread and free its stack. */
118 void Thread::complete()
120 ASSERT(!is_complete());
121 DEBUG("completed thread %d\n", id_to_int(get_id()));
122 state = THREAD_COMPLETED;
128 * @brief Construct a new model-checker Thread
130 * A model-checker Thread is used for accounting purposes only. It will never
131 * have its own stack, and it should never be inserted into the Scheduler.
133 * @param tid The thread ID to assign
135 Thread::Thread(thread_id_t tid) :
144 state(THREAD_READY), /* Thread is always ready? */
148 memset(&context, 0, sizeof(context));
152 * Construct a new thread.
153 * @param t The thread identifier of the newly created thread.
154 * @param func The function that the thread will call.
155 * @param a The parameter to pass to this function.
157 Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent) :
162 pstart_routine(NULL),
166 state(THREAD_CREATED),
167 last_action_val(VALUE_NONE),
172 /* Initialize state */
173 ret = create_context();
175 model_print("Error in create_context\n");
177 user_thread->priv = this; // WL
181 * Construct a new thread for pthread.
182 * @param t The thread identifier of the newly created thread.
183 * @param func The function that the thread will call.
184 * @param a The parameter to pass to this function.
186 Thread::Thread(thread_id_t tid, thrd_t *t, void *(*func)(void *), void *a, Thread *parent) :
191 pstart_routine(func),
195 state(THREAD_CREATED),
196 last_action_val(VALUE_NONE),
201 /* Initialize state */
202 ret = create_context();
204 model_print("Error in create_context\n");
215 /** @return The thread_id_t corresponding to this Thread object. */
216 thread_id_t Thread::get_id() const
222 * Set a thread's THREAD_* state (@see thread_state)
223 * @param s The state to enter
225 void Thread::set_state(thread_state s)
227 ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED);
232 * Get the Thread that this Thread is immediately waiting on
233 * @return The thread we are waiting on, if any; otherwise NULL
235 Thread * Thread::waiting_on() const
240 if (pending->get_type() == THREAD_JOIN)
241 return pending->get_thread_operand();
242 else if (pending->get_type() == PTHREAD_JOIN)
243 return pending->get_thread_operand();
244 else if (pending->is_lock())
245 return (Thread *)pending->get_mutex()->get_state()->locked;
250 * Check if this Thread is waiting (blocking) on a given Thread, directly or
251 * indirectly (via a chain of waiting threads)
253 * @param t The Thread on which we may be waiting
254 * @return True if we are waiting on Thread t; false otherwise
256 bool Thread::is_waiting_on(const Thread *t) const
259 for (wait = waiting_on(); wait != NULL; wait = wait->waiting_on())