Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7869 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 3.6 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"
[7284]38#include "command/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
[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}
Note: See TracBrowser for help on using the repository browser.