Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8373 was 8327, checked in by scheusso, 14 years ago

merging network6 into trunk

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