return swapcontext(&this->context, &t->context);
}
-void Thread::dispose()
+void Thread::complete()
{
- DEBUG("completed thread %d\n", thread_current()->get_id());
- state = THREAD_COMPLETED;
- stack_free(stack);
+ if (state != THREAD_COMPLETED) {
+ DEBUG("completed thread %d\n", get_id());
+ state = THREAD_COMPLETED;
+ stack_free(stack);
+ }
}
Thread::Thread(thrd_t *t, void (*func)(), void *a) {
model->add_system_thread(this);
}
+Thread::~Thread()
+{
+ complete();
+ model->remove_thread(this);
+}
+
thread_id_t Thread::get_id()
{
return id;
model->scheduler->add_thread(curr);
else if (curr->get_state() == THREAD_RUNNING)
/* Stopped while running; i.e., completed */
- curr->dispose();
+ curr->complete();
else
DEBUG("ERROR: current thread in unexpected state??\n");
}
public:
Thread(thrd_t *t, void (*func)(), void *a);
Thread(thrd_t *t);
+ ~Thread();
+ void complete();
int swap(Thread *t);
- void dispose();
thread_state get_state() { return state; }
void set_state(thread_state s) { state = s; }