Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8353 was 8327, checked in by scheusso, 13 years ago

merging network6 into trunk

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