[420] | 1 | #include "SimHost.h" |
---|
| 2 | #include <osg/Notify> |
---|
| 3 | |
---|
| 4 | #include <OpenThreads/Thread> |
---|
| 5 | |
---|
| 6 | // System includes |
---|
| 7 | #include <cassert> |
---|
| 8 | |
---|
| 9 | #ifdef _WIN32 |
---|
| 10 | #include <process.h> |
---|
| 11 | #define getpid() _getpid() |
---|
| 12 | #else |
---|
| 13 | #include <unistd.h> |
---|
| 14 | #endif |
---|
| 15 | |
---|
| 16 | // App includes |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | SimHost::SimHost() |
---|
| 20 | { |
---|
| 21 | OSG_ALWAYS<<"SimHost instantiated."<<std::endl; |
---|
[430] | 22 | numberofChannels = 6; |
---|
[420] | 23 | } |
---|
| 24 | |
---|
| 25 | SimHost::~SimHost() |
---|
| 26 | { |
---|
[421] | 27 | //_channelWorker.clear(); |
---|
[420] | 28 | } |
---|
| 29 | |
---|
| 30 | void SimHost::initialize() |
---|
| 31 | { |
---|
[430] | 32 | done = false; |
---|
| 33 | int numThreads = numberofChannels+1; // #channels + one main thread |
---|
[420] | 34 | OpenThreads::Barrier syncBarrier; |
---|
| 35 | |
---|
[425] | 36 | // Create IG Connectors |
---|
| 37 | OSG_ALWAYS<<"Creating IG Connectors..."<<std::endl; |
---|
[430] | 38 | for(int i=0; i<numberofChannels; ++i) |
---|
[420] | 39 | { |
---|
[425] | 40 | osg::ref_ptr<ThreadedWorker> threadObject = new ThreadedWorker(); |
---|
[430] | 41 | osg::ref_ptr<IGConnector> IGCon = new IGConnector(numberofChannels, _frameBarrier); |
---|
[425] | 42 | threadObject->setThreadWorker(IGCon); |
---|
| 43 | _threadObjects.push_back(threadObject); |
---|
| 44 | _IGConnectors.push_back(IGCon); |
---|
[420] | 45 | } |
---|
| 46 | OSG_ALWAYS<<"...done."<<std::endl; |
---|
| 47 | |
---|
| 48 | // Setup Threading |
---|
| 49 | OSG_ALWAYS << "Root Thread ID: " << getpid() << std::endl; |
---|
| 50 | |
---|
| 51 | OSG_ALWAYS<<OpenThreads::GetNumberOfProcessors()<<" CPUs found, using "<<(numThreads)<<" of them concurrently."<<std::endl; |
---|
| 52 | OpenThreads::Thread::SetConcurrency( numThreads ); // Only supported by pthread implementation, otherwise ignored |
---|
| 53 | |
---|
| 54 | OpenThreads::Thread::Init(); |
---|
| 55 | |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | void SimHost::run() |
---|
| 59 | { |
---|
| 60 | // Launch threads |
---|
[425] | 61 | for(int i=0;i<static_cast<int>(_threadObjects.size());++i) |
---|
[420] | 62 | { |
---|
| 63 | int status; |
---|
[421] | 64 | |
---|
[420] | 65 | //thread->setStackSize(1024*256); |
---|
[425] | 66 | status = _threadObjects[i]->start(); |
---|
[420] | 67 | assert(status == 0); |
---|
| 68 | } |
---|
| 69 | |
---|
[430] | 70 | // Enter the SimHost main loop |
---|
| 71 | while(!done) |
---|
| 72 | { |
---|
| 73 | // Wait until all Channels have sent an SOF packet |
---|
| 74 | _frameBarrier.block(numberofChannels+1); // Sync the main and all IG threads. |
---|
| 75 | OSG_ALWAYS<<"All IGConnectors have recieved a SOF packet: Starting new Frame"<<std::endl; |
---|
| 76 | } |
---|
[420] | 77 | |
---|
| 78 | // Signal threads to finish |
---|
[425] | 79 | for(int i=0;i<static_cast<int>(_threadObjects.size());++i) |
---|
[420] | 80 | { |
---|
[425] | 81 | //_threadObjects[i]->setDone(true); // non blocking |
---|
| 82 | _threadObjects[i]->cancel(); // Blocking |
---|
[420] | 83 | } |
---|
| 84 | } |
---|