Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/libraries/core/Thread.cc @ 7944

Last change on this file since 7944 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
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 "command/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( const ExecutorPtr& 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            ExecutorPtr executor = this->executor_;
95            //this->executorMutex_->unlock();
96            if( executor )
97            {
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();
105            }
106            else
107            {
108                this->workerThread_->yield();
109            }
110            //this->stopThreadMutex_->lock();
111            stopThread = this->stopThread_;
112            //this->stopThreadMutex_->unlock();
113        }
114    }
115
116    void Thread::waitUntilFinished()
117    {
118        bool stillWorking = true;
119        while( stillWorking )
120        {
121            this->isWorkingMutex_->lock();
122            stillWorking = this->isWorking_;
123            this->isWorkingMutex_->unlock();
124            if( stillWorking )
125                msleep( 1 );
126        }
127    }
128}
Note: See TracBrowser for help on using the repository browser.