X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=threads.cc;h=00e5c2fbd4a0f1287ac6b68b8be50a1a49b82bd8;hb=cfbcbb33437af392cea2a5092d89cfed47506b75;hp=9de580297871687e4529f2a67512aa6087a9cc1b;hpb=1cdcdc24156572a63fd8261a7a3dd2f04ca6648c;p=cdsspec-compiler.git diff --git a/threads.cc b/threads.cc index 9de5802..00e5c2f 100644 --- a/threads.cc +++ b/threads.cc @@ -5,8 +5,10 @@ #include #include +#include #include "common.h" #include "threads-model.h" +#include "action.h" /* global "model" object */ #include "model.h" @@ -23,7 +25,13 @@ static void stack_free(void *stack) snapshot_free(stack); } -/** Return the currently executing thread. */ +/** + * @brief Get the current Thread + * + * Must be called from a user context + * + * @return The currently executing thread + */ Thread * thread_current(void) { ASSERT(model); @@ -144,7 +152,8 @@ Thread::Thread(thread_id_t tid) : * @param func The function that the thread will call. * @param a The parameter to pass to this function. */ -Thread::Thread(thrd_t *t, void (*func)(void *), void *a) : +Thread::Thread(thrd_t *t, void (*func)(void *), void *a, Thread *parent) : + parent(parent), creation(NULL), pending(NULL), start_routine(func), @@ -164,7 +173,6 @@ Thread::Thread(thrd_t *t, void (*func)(void *), void *a) : id = model->get_next_id(); user_thread->priv = this; - parent = thread_current(); } /** Destructor */ @@ -172,7 +180,6 @@ Thread::~Thread() { if (!is_complete()) complete(); - model->remove_thread(this); } /** @return The thread_id_t corresponding to this Thread object. */ @@ -190,3 +197,19 @@ void Thread::set_state(thread_state s) ASSERT(s == THREAD_COMPLETED || state != THREAD_COMPLETED); state = s; } + +/** + * Get the Thread that this Thread is waiting on + * @return The thread we are waiting on, if any; otherwise NULL + */ +Thread * Thread::waiting_on() const +{ + if (!pending) + return NULL; + + if (pending->get_type() == THREAD_JOIN) + return pending->get_thread_operand(); + else if (pending->is_lock()) + return (Thread *)pending->get_mutex()->get_state()->locked; + return NULL; +}