Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6804 was 6417, checked in by rgrieder, 15 years ago

Merged presentation2 branch back to trunk.
Major new features:

  • Actual GUI with settings, etc.
  • Improved space ship steering (human interaction)
  • Rocket fire and more particle effects
  • Advanced sound framework
  • 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
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"
[3240]38#include "Executor.h"
[3226]39
40namespace 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
[3240]77    bool Thread::evaluateExecutor( Executor* executor )
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();
[3240]94            Executor* executor = this->executor_;
[3298]95            //this->executorMutex_->unlock();
[3240]96            if( executor )
[3226]97            {
[3240]98                (*executor)();
99                this->executorMutex_->lock();
100                delete this->executor_;
101                this->executor_ = 0;
102                this->executorMutex_->unlock();
103                this->isWorkingMutex_->lock();
104                this->isWorking_=false;
105                this->isWorkingMutex_->unlock();
[3226]106            }
107            else
108            {
109                this->workerThread_->yield();
110            }
[3298]111            //this->stopThreadMutex_->lock();
[3240]112            stopThread = this->stopThread_;
[3298]113            //this->stopThreadMutex_->unlock();
[3226]114        }
115    }
[6417]116
[3226]117    void Thread::waitUntilFinished()
118    {
119        bool stillWorking = true;
120        while( stillWorking )
121        {
[3240]122            this->isWorkingMutex_->lock();
[3226]123            stillWorking = this->isWorking_;
[3240]124            this->isWorkingMutex_->unlock();
[3226]125            if( stillWorking )
126                msleep( 1 );
127        }
128    }
129}
Note: See TracBrowser for help on using the repository browser.