Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pickup3/src/libraries/core/Thread.cc @ 6497

Last change on this file since 6497 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
Line 
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"
38#include "Executor.h"
39
40namespace orxonox
41{
42    boost::posix_time::millisec THREAD_WAIT_BEFORE_DETACH(1000);
43
44
45    Thread::Thread():
46        executor_(0),
47        isWorking_(false),
48        stopThread_(false)
49    {
50        this->executorMutex_ = new boost::mutex;
51        this->isWorkingMutex_ = new boost::mutex;
52        this->stopThreadMutex_ = new boost::mutex;
53        this->workerThread_ = new boost::thread( boost::bind(&Thread::threadLoop, this) );
54    }
55
56    Thread::~Thread()
57    {
58        this->stopThreadMutex_->lock();
59        this->stopThread_ = true;
60        this->stopThreadMutex_->unlock();
61        if( !this->workerThread_->timed_join( THREAD_WAIT_BEFORE_DETACH ) )
62            assert(0); // this should not happen
63        delete this->workerThread_;
64        delete this->executorMutex_;
65        delete this->stopThreadMutex_;
66        delete this->isWorkingMutex_;
67    }
68
69    bool Thread::isWorking()
70    {
71      this->isWorkingMutex_->lock();
72      bool isWorking = this->isWorking_;
73      this->isWorkingMutex_->unlock();
74      return isWorking;
75    }
76
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    }
87
88    void Thread::threadLoop()
89    {
90        bool stopThread = false;
91        while( !stopThread )
92        {
93            //this->executorMutex_->lock();
94            Executor* executor = this->executor_;
95            //this->executorMutex_->unlock();
96            if( executor )
97            {
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();
106            }
107            else
108            {
109                this->workerThread_->yield();
110            }
111            //this->stopThreadMutex_->lock();
112            stopThread = this->stopThread_;
113            //this->stopThreadMutex_->unlock();
114        }
115    }
116
117    void Thread::waitUntilFinished()
118    {
119        bool stillWorking = true;
120        while( stillWorking )
121        {
122            this->isWorkingMutex_->lock();
123            stillWorking = this->isWorking_;
124            this->isWorkingMutex_->unlock();
125            if( stillWorking )
126                msleep( 1 );
127        }
128    }
129}
Note: See TracBrowser for help on using the repository browser.