Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11/src/libraries/core/Thread.cc @ 10443

Last change on this file since 10443 was 8399, checked in by rgrieder, 14 years ago

Build fix (surfaces without compilations): ORXONOX_PLATFORM_WINDOWS is defined in a header file → include that first.

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