Rev | Line | |
---|
[423] | 1 | #include "ThreadedWorker.h" |
---|
[420] | 2 | #include <osg/Notify> |
---|
[425] | 3 | #include <OpenThreads/ScopedLock> |
---|
[420] | 4 | |
---|
| 5 | |
---|
[423] | 6 | ThreadedWorker::ThreadedWorker() |
---|
[421] | 7 | : _done(false) |
---|
[420] | 8 | { |
---|
[423] | 9 | OSG_ALWAYS<<"ThreadedWorker instantiated."<<std::endl; |
---|
[420] | 10 | } |
---|
| 11 | |
---|
[423] | 12 | ThreadedWorker::~ThreadedWorker() |
---|
[420] | 13 | { |
---|
| 14 | cancel(); |
---|
| 15 | } |
---|
| 16 | |
---|
[423] | 17 | void ThreadedWorker::run() |
---|
[420] | 18 | { |
---|
[427] | 19 | OSG_ALWAYS<<"Starting Thread "<<getThreadId()<<"!"<<std::endl; |
---|
[420] | 20 | |
---|
| 21 | // Do the work |
---|
| 22 | do |
---|
| 23 | { |
---|
[425] | 24 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadWorkerMutex); |
---|
[420] | 25 | |
---|
[425] | 26 | if( _threadWorker.valid() ) |
---|
| 27 | (*_threadWorker)(this); |
---|
| 28 | |
---|
| 29 | } while (_threadWorker!=NULL && !testCancel() && !_done); // repeat as long as a functor is set and it is not canceled or set as Done |
---|
[420] | 30 | } |
---|
| 31 | |
---|
[423] | 32 | void ThreadedWorker::setDone(bool done) |
---|
[420] | 33 | { |
---|
| 34 | _done = done; |
---|
| 35 | } |
---|
| 36 | |
---|
[423] | 37 | int ThreadedWorker::cancel() |
---|
[420] | 38 | { |
---|
[423] | 39 | OSG_ALWAYS<<"Cancelling ThreadedWorker "<<this<<" isRunning()="<<isRunning()<<std::endl; |
---|
[420] | 40 | |
---|
| 41 | if( isRunning() ) |
---|
| 42 | { |
---|
| 43 | _done = true; |
---|
| 44 | |
---|
| 45 | OSG_ALWAYS<<" Doing cancel "<<this<<std::endl; |
---|
| 46 | |
---|
| 47 | // then wait for the the thread to stop running. |
---|
| 48 | while(isRunning()) |
---|
| 49 | { |
---|
| 50 | // commenting out debug info as it was cashing crash on exit, presumable |
---|
| 51 | // due to OSG_NOTIFY or std::cout destructing earlier than this destructor. |
---|
[423] | 52 | //OSG_ALWAYS<<" Waiting for ThreadedWorker to cancel "<<this<<std::endl; |
---|
[420] | 53 | OpenThreads::Thread::YieldCurrentThread(); |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
[423] | 57 | OSG_ALWAYS<<" ThreadedWorker::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl; |
---|
[420] | 58 | |
---|
| 59 | return 0; |
---|
[425] | 60 | } |
---|
| 61 | |
---|
| 62 | void ThreadedWorker::setThreadWorker( ThreadWorkerBase* tworker) |
---|
| 63 | { |
---|
| 64 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadWorkerMutex); |
---|
| 65 | |
---|
| 66 | _threadWorker = tworker; |
---|
[420] | 67 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.