| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Oliver Scheuss |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | #include "Thread.h" |
|---|
| 31 | |
|---|
| 32 | #include <cassert> |
|---|
| 33 | // #include <boost/thread/thread.hpp> |
|---|
| 34 | // #include <boost/bind.hpp> |
|---|
| 35 | // #include <boost/thread/mutex.hpp> |
|---|
| 36 | // #include <boost/thread/thread_time.hpp> |
|---|
| 37 | |
|---|
| 38 | // #include "util/Sleep.h" |
|---|
| 39 | #include "command/Executor.h" |
|---|
| 40 | |
|---|
| 41 | namespace orxonox |
|---|
| 42 | { |
|---|
| 43 | // boost::posix_time::millisec THREAD_WAIT_BEFORE_DETACH(1000); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | Thread::Thread(): |
|---|
| 47 | executor_(0), |
|---|
| 48 | isWorking_(false), |
|---|
| 49 | stopThread_(false) |
|---|
| 50 | { |
|---|
| 51 | // this->executorMutex_ = new boost::mutex; |
|---|
| 52 | // this->isWorkingMutex_ = new boost::mutex; |
|---|
| 53 | // this->stopThreadMutex_ = new boost::mutex; |
|---|
| 54 | // this->workerThread_ = new boost::thread( boost::bind(&Thread::threadLoop, this) ); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | Thread::~Thread() |
|---|
| 58 | { |
|---|
| 59 | // this->stopThreadMutex_->lock(); |
|---|
| 60 | // this->stopThread_ = true; |
|---|
| 61 | // this->stopThreadMutex_->unlock(); |
|---|
| 62 | // if( !this->workerThread_->timed_join( THREAD_WAIT_BEFORE_DETACH ) ) |
|---|
| 63 | // assert(0); // this should not happen |
|---|
| 64 | // delete this->workerThread_; |
|---|
| 65 | // delete this->executorMutex_; |
|---|
| 66 | // delete this->stopThreadMutex_; |
|---|
| 67 | // delete this->isWorkingMutex_; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | bool Thread::isWorking() |
|---|
| 71 | { |
|---|
| 72 | // this->isWorkingMutex_->lock(); |
|---|
| 73 | // bool isWorking = this->isWorking_; |
|---|
| 74 | // this->isWorkingMutex_->unlock(); |
|---|
| 75 | // return isWorking; |
|---|
| 76 | return false; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | bool Thread::evaluateExecutor( const ExecutorPtr& executor ) |
|---|
| 80 | { |
|---|
| 81 | // this->isWorkingMutex_->lock(); |
|---|
| 82 | // this->isWorking_=true; |
|---|
| 83 | // this->isWorkingMutex_->unlock(); |
|---|
| 84 | // this->executorMutex_->lock(); |
|---|
| 85 | // this->executor_ = executor; |
|---|
| 86 | // this->executorMutex_->unlock(); |
|---|
| 87 | (*executor)(); |
|---|
| 88 | return true; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void Thread::threadLoop() |
|---|
| 92 | { |
|---|
| 93 | // bool stopThread = false; |
|---|
| 94 | // while( !stopThread ) |
|---|
| 95 | // { |
|---|
| 96 | // this->executorMutex_->lock(); |
|---|
| 97 | // ExecutorPtr executor = this->executor_; |
|---|
| 98 | // this->executorMutex_->unlock(); |
|---|
| 99 | // if( executor ) |
|---|
| 100 | // { |
|---|
| 101 | // (*executor)(); |
|---|
| 102 | // this->executorMutex_->lock(); |
|---|
| 103 | // this->executor_ = 0; |
|---|
| 104 | // this->executorMutex_->unlock(); |
|---|
| 105 | // this->isWorkingMutex_->lock(); |
|---|
| 106 | // this->isWorking_=false; |
|---|
| 107 | // this->isWorkingMutex_->unlock(); |
|---|
| 108 | // } |
|---|
| 109 | // else |
|---|
| 110 | // { |
|---|
| 111 | // this->workerThread_->yield(); |
|---|
| 112 | // } |
|---|
| 113 | // this->stopThreadMutex_->lock(); |
|---|
| 114 | // stopThread = this->stopThread_; |
|---|
| 115 | // this->stopThreadMutex_->unlock(); |
|---|
| 116 | // } |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | void Thread::waitUntilFinished() |
|---|
| 120 | { |
|---|
| 121 | // bool stillWorking = true; |
|---|
| 122 | // while( stillWorking ) |
|---|
| 123 | // { |
|---|
| 124 | // this->isWorkingMutex_->lock(); |
|---|
| 125 | // stillWorking = this->isWorking_; |
|---|
| 126 | // this->isWorkingMutex_->unlock(); |
|---|
| 127 | // if( stillWorking ) |
|---|
| 128 | // msleep( 1 ); |
|---|
| 129 | // } |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|