[3226] | 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 | #include "Thread.h" |
---|
| 30 | |
---|
| 31 | #include <cassert> |
---|
| 32 | #include <boost/thread/thread.hpp> |
---|
| 33 | #include <boost/bind.hpp> |
---|
| 34 | #include <boost/thread/mutex.hpp> |
---|
| 35 | #include <boost/thread/thread_time.hpp> |
---|
| 36 | |
---|
| 37 | #include "util/Sleep.h" |
---|
[7284] | 38 | #include "command/Executor.h" |
---|
[3226] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | boost::posix_time::millisec THREAD_WAIT_BEFORE_DETACH(1000); |
---|
[6417] | 43 | |
---|
| 44 | |
---|
[3226] | 45 | Thread::Thread(): |
---|
[3240] | 46 | executor_(0), |
---|
[3226] | 47 | isWorking_(false), |
---|
| 48 | stopThread_(false) |
---|
| 49 | { |
---|
[3240] | 50 | this->executorMutex_ = new boost::mutex; |
---|
| 51 | this->isWorkingMutex_ = new boost::mutex; |
---|
| 52 | this->stopThreadMutex_ = new boost::mutex; |
---|
[3226] | 53 | this->workerThread_ = new boost::thread( boost::bind(&Thread::threadLoop, this) ); |
---|
| 54 | } |
---|
[6417] | 55 | |
---|
[3226] | 56 | Thread::~Thread() |
---|
| 57 | { |
---|
[3240] | 58 | this->stopThreadMutex_->lock(); |
---|
[3226] | 59 | this->stopThread_ = true; |
---|
[3240] | 60 | this->stopThreadMutex_->unlock(); |
---|
[3226] | 61 | if( !this->workerThread_->timed_join( THREAD_WAIT_BEFORE_DETACH ) ) |
---|
| 62 | assert(0); // this should not happen |
---|
| 63 | delete this->workerThread_; |
---|
[3240] | 64 | delete this->executorMutex_; |
---|
| 65 | delete this->stopThreadMutex_; |
---|
| 66 | delete this->isWorkingMutex_; |
---|
[3226] | 67 | } |
---|
[6417] | 68 | |
---|
[3240] | 69 | bool Thread::isWorking() |
---|
[3226] | 70 | { |
---|
[3240] | 71 | this->isWorkingMutex_->lock(); |
---|
| 72 | bool isWorking = this->isWorking_; |
---|
| 73 | this->isWorkingMutex_->unlock(); |
---|
| 74 | return isWorking; |
---|
[3226] | 75 | } |
---|
[6417] | 76 | |
---|
[7284] | 77 | bool Thread::evaluateExecutor( const ExecutorPtr& executor ) |
---|
[3240] | 78 | { |
---|
| 79 | this->isWorkingMutex_->lock(); |
---|
| 80 | this->isWorking_=true; |
---|
| 81 | this->isWorkingMutex_->unlock(); |
---|
| 82 | this->executorMutex_->lock(); |
---|
| 83 | this->executor_ = executor; |
---|
| 84 | this->executorMutex_->unlock(); |
---|
| 85 | return true; |
---|
| 86 | } |
---|
[6417] | 87 | |
---|
[3226] | 88 | void Thread::threadLoop() |
---|
| 89 | { |
---|
[3240] | 90 | bool stopThread = false; |
---|
| 91 | while( !stopThread ) |
---|
[3226] | 92 | { |
---|
[3298] | 93 | //this->executorMutex_->lock(); |
---|
[7284] | 94 | ExecutorPtr executor = this->executor_; |
---|
[3298] | 95 | //this->executorMutex_->unlock(); |
---|
[3240] | 96 | if( executor ) |
---|
[3226] | 97 | { |
---|
[3240] | 98 | (*executor)(); |
---|
| 99 | this->executorMutex_->lock(); |
---|
| 100 | this->executor_ = 0; |
---|
| 101 | this->executorMutex_->unlock(); |
---|
| 102 | this->isWorkingMutex_->lock(); |
---|
| 103 | this->isWorking_=false; |
---|
| 104 | this->isWorkingMutex_->unlock(); |
---|
[3226] | 105 | } |
---|
| 106 | else |
---|
| 107 | { |
---|
| 108 | this->workerThread_->yield(); |
---|
| 109 | } |
---|
[3298] | 110 | //this->stopThreadMutex_->lock(); |
---|
[3240] | 111 | stopThread = this->stopThread_; |
---|
[3298] | 112 | //this->stopThreadMutex_->unlock(); |
---|
[3226] | 113 | } |
---|
| 114 | } |
---|
[6417] | 115 | |
---|
[3226] | 116 | void Thread::waitUntilFinished() |
---|
| 117 | { |
---|
| 118 | bool stillWorking = true; |
---|
| 119 | while( stillWorking ) |
---|
| 120 | { |
---|
[3240] | 121 | this->isWorkingMutex_->lock(); |
---|
[3226] | 122 | stillWorking = this->isWorking_; |
---|
[3240] | 123 | this->isWorkingMutex_->unlock(); |
---|
[3226] | 124 | if( stillWorking ) |
---|
| 125 | msleep( 1 ); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | } |
---|