00001
00002
00003
00004
00005
00006
00007
00008 #include <thread.h>
00009 #include <scheduler_disabler.h>
00010 #include <semaphore.h>
00011
00012 __BEGIN_SYS
00013
00014
00015 Thread::~Thread() {
00016 DISABLE_SCHED {
00017 if (state() != FINISHING) {
00018 if (!_waitingThread) Scheduler::getInstance().cacheThreadStack(_stack);
00019 releaseSemaphores();
00020 Scheduler::getInstance().exit(this, 0);
00021 }
00022 }
00023 }
00024
00025
00026 void Thread::releaseSemaphores() {
00027 db<Thread>(TRC) << "Thread::releaseSemaphores()\n";
00028 for (CircularVector<Semaphore *>::Iterator it = _semaphores.begin(); it != _semaphores.end(); ++it) {
00029 db<Thread>(TRC) << "Thread::releaseSemaphores [Semaphore=" << *it << "]\n";
00030 (*it)->v();
00031 }
00032 }
00033
00034
00035 void Thread::header(Log_Addr entry, unsigned int stack_size) {
00036 db<Thread>(TRC) << "Thread(this=" << this
00037 << ",entry=" << (void *)entry
00038 << ",state=" << _state
00039 << ",priority=" << _priority
00040 << ",stack={b=" << _stack
00041 << ",s=" << stack_size
00042 << ",context={b=" << _context
00043 << "," << *_context << "})\n";
00044 }
00045
00046 void Thread::body() {
00047 db<Thread>(TRC) << "Inserted thread: " << this << "\n";
00048 Scheduler::getInstance().addThread(this);
00049 }
00050
00051 int Thread::join() {
00052 db<Thread>(TRC) << "Thread::join(this=" << this
00053 << ",state=" << _state << ")\n";
00054
00055 int value = -1;
00056
00057 //TODO -- assert(!_waitingThread);
00058 DISABLE_SCHED {
00059 bool isFinished = state() == FINISHING;
00060 if (!isFinished) {
00061 _waitingThread = &Scheduler::getInstance().getRunning();
00062 Scheduler::getInstance().suspend(&Scheduler::getInstance().getRunning());
00063 }
00064
00065 value = (state() == Thread::DESTROYED) ? 0 : *((int *)_stack);
00066
00067 if (!isFinished) Scheduler::getInstance().cacheThreadStack(this->_stack);
00068 }
00069
00070 return value;
00071 }
00072
00073 void Thread::idle()
00074 {
00075 db<Thread>(TRC) << "Thread::idle()\n";
00076
00077 db<Thread>(WRN) << "There are no runnable threads at the moment!\n";
00078 db<Thread>(WRN) << "Halting the CPU ...\n";
00079
00080 CPU::int_enable();
00081 CPU::halt();
00082 }
00083
00084 __END_SYS