Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Thread.cc @ 10458

Last change on this file since 10458 was 8399, checked in by rgrieder, 14 years ago

Build fix (surfaces without compilations): ORXONOX_PLATFORM_WINDOWS is defined in a header file → include that first.

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