| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/Referenced> |
|---|
| 20 | #include <osgDB/ReadFile> |
|---|
| 21 | #include <osgDB/Registry> |
|---|
| 22 | |
|---|
| 23 | #include <OpenThreads/Thread> |
|---|
| 24 | #include <OpenThreads/ScopedLock> |
|---|
| 25 | |
|---|
| 26 | struct RefBarrier : public osg::Referenced, public OpenThreads::Barrier |
|---|
| 27 | { |
|---|
| 28 | RefBarrier(int numThreads): |
|---|
| 29 | OpenThreads::Barrier(numThreads) {} |
|---|
| 30 | }; |
|---|
| 31 | |
|---|
| 32 | class ReadThread : public osg::Referenced, public OpenThreads::Thread |
|---|
| 33 | { |
|---|
| 34 | public: |
|---|
| 35 | |
|---|
| 36 | ReadThread(): |
|---|
| 37 | _done(false) |
|---|
| 38 | { |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | virtual ~ReadThread() |
|---|
| 42 | { |
|---|
| 43 | _done = true; |
|---|
| 44 | |
|---|
| 45 | while(isRunning()) OpenThreads::Thread::YieldCurrentThread(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | void addFileName(const std::string& filename) |
|---|
| 49 | { |
|---|
| 50 | _fileNames.push_back(filename); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void setStartBarrier(RefBarrier* barrier) { _startBarrier = barrier; } |
|---|
| 54 | void setEndBarrier(RefBarrier* barrier) { _endBarrier = barrier; } |
|---|
| 55 | |
|---|
| 56 | virtual void run() |
|---|
| 57 | { |
|---|
| 58 | if (_startBarrier.valid()) |
|---|
| 59 | { |
|---|
| 60 | #if VERBOSE |
|---|
| 61 | std::cout<<"Waiting on start block "<<this<<std::endl; |
|---|
| 62 | #endif |
|---|
| 63 | _startBarrier->block(); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | #if VERBOSE |
|---|
| 67 | std::cout<<"Starting "<<this<<std::endl; |
|---|
| 68 | #endif |
|---|
| 69 | |
|---|
| 70 | do |
|---|
| 71 | { |
|---|
| 72 | if (!_fileNames.empty()) |
|---|
| 73 | { |
|---|
| 74 | |
|---|
| 75 | std::string filename = _fileNames.front(); |
|---|
| 76 | _fileNames.erase(_fileNames.begin()); |
|---|
| 77 | |
|---|
| 78 | #if VERBOSE |
|---|
| 79 | std::cout<<"Reading "<<filename; |
|---|
| 80 | #endif |
|---|
| 81 | osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(filename); |
|---|
| 82 | #if VERBOSE |
|---|
| 83 | if (node.valid()) std::cout<<".. OK"<<std::endl; |
|---|
| 84 | else std::cout<<".. FAILED"<<std::endl; |
|---|
| 85 | #endif |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | } while (!testCancel() && !_fileNames.empty() && !_done); |
|---|
| 89 | |
|---|
| 90 | if (_endBarrier.valid()) |
|---|
| 91 | { |
|---|
| 92 | #if VERBOSE |
|---|
| 93 | std::cout<<"Waiting on end block "<<this<<std::endl; |
|---|
| 94 | #endif |
|---|
| 95 | _endBarrier->block(); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | #if VERBOSE |
|---|
| 99 | std::cout<<"Completed"<<this<<std::endl; |
|---|
| 100 | #endif |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | typedef std::list<std::string> FileNames; |
|---|
| 104 | FileNames _fileNames; |
|---|
| 105 | bool _done; |
|---|
| 106 | osg::ref_ptr<RefBarrier> _startBarrier; |
|---|
| 107 | osg::ref_ptr<RefBarrier> _endBarrier; |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | class SerializerReadFileCallback : public osgDB::Registry::ReadFileCallback |
|---|
| 113 | { |
|---|
| 114 | public: |
|---|
| 115 | |
|---|
| 116 | virtual osgDB::ReaderWriter::ReadResult openArchive(const std::string& filename,osgDB::ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const osgDB::ReaderWriter::Options* useObjectCache) |
|---|
| 117 | { |
|---|
| 118 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 119 | return osgDB::Registry::instance()->openArchiveImplementation(filename, status, indexBlockSizeHint, useObjectCache); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | virtual osgDB::ReaderWriter::ReadResult readObject(const std::string& filename, const osgDB::ReaderWriter::Options* options) |
|---|
| 123 | { |
|---|
| 124 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 125 | return osgDB::Registry::instance()->readObjectImplementation(filename,options); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | virtual osgDB::ReaderWriter::ReadResult readImage(const std::string& filename, const osgDB::ReaderWriter::Options* options) |
|---|
| 129 | { |
|---|
| 130 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 131 | return osgDB::Registry::instance()->readImageImplementation(filename,options); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | virtual osgDB::ReaderWriter::ReadResult readHeightField(const std::string& filename, const osgDB::ReaderWriter::Options* options) |
|---|
| 135 | { |
|---|
| 136 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 137 | return osgDB::Registry::instance()->readHeightFieldImplementation(filename,options); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | virtual osgDB::ReaderWriter::ReadResult readNode(const std::string& filename, const osgDB::ReaderWriter::Options* options) |
|---|
| 141 | { |
|---|
| 142 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 143 | return osgDB::Registry::instance()->readNodeImplementation(filename,options); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | virtual osgDB::ReaderWriter::ReadResult readShader(const std::string& filename, const osgDB::ReaderWriter::Options* options) |
|---|
| 147 | { |
|---|
| 148 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 149 | return osgDB::Registry::instance()->readShaderImplementation(filename,options); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | protected: |
|---|
| 153 | virtual ~SerializerReadFileCallback() {} |
|---|
| 154 | |
|---|
| 155 | OpenThreads::Mutex _mutex; |
|---|
| 156 | }; |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | void runMultiThreadReadTests(int numThreads, osg::ArgumentParser& arguments) |
|---|
| 161 | { |
|---|
| 162 | #if VERBOSE |
|---|
| 163 | osg::notify(osg::NOTICE)<<"runMultiThreadReadTests() -- running"<<std::endl; |
|---|
| 164 | #endif |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | if (arguments.read("preload")) |
|---|
| 168 | { |
|---|
| 169 | osgDB::Registry::instance()->loadLibrary(osgDB::Registry::instance()->createLibraryNameForExtension("osg")); |
|---|
| 170 | osgDB::Registry::instance()->loadLibrary(osgDB::Registry::instance()->createLibraryNameForExtension("rgb")); |
|---|
| 171 | osgDB::Registry::instance()->loadLibrary(osgDB::Registry::instance()->createLibraryNameForExtension("jpeg")); |
|---|
| 172 | osgDB::Registry::instance()->loadLibrary(osgDB::Registry::instance()->createLibraryNameForExtension("ive")); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | if (arguments.read("serialize")) |
|---|
| 176 | { |
|---|
| 177 | osgDB::Registry::instance()->setReadFileCallback(new SerializerReadFileCallback()); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | osg::ref_ptr<RefBarrier> startBarrier = new RefBarrier(numThreads+1); |
|---|
| 181 | osg::ref_ptr<RefBarrier> endBarrier = new RefBarrier(numThreads+1); |
|---|
| 182 | |
|---|
| 183 | typedef std::list< osg::ref_ptr<ReadThread> > ReadThreads; |
|---|
| 184 | ReadThreads readThreads; |
|---|
| 185 | |
|---|
| 186 | for(int i=0; i<numThreads; ++i) |
|---|
| 187 | { |
|---|
| 188 | osg::ref_ptr<ReadThread> readThread = new ReadThread; |
|---|
| 189 | |
|---|
| 190 | readThread->setProcessorAffinity(numThreads % 4); |
|---|
| 191 | |
|---|
| 192 | readThread->setStartBarrier(startBarrier.get()); |
|---|
| 193 | readThread->setEndBarrier(endBarrier.get()); |
|---|
| 194 | |
|---|
| 195 | readThread->addFileName("cessna.osgt"); |
|---|
| 196 | readThread->addFileName("glider.osgt"); |
|---|
| 197 | readThread->addFileName("town.ive"); |
|---|
| 198 | |
|---|
| 199 | readThreads.push_back(readThread.get()); |
|---|
| 200 | |
|---|
| 201 | readThread->start(); |
|---|
| 202 | |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | startBarrier->block(); |
|---|
| 206 | endBarrier->block(); |
|---|
| 207 | |
|---|
| 208 | #if VERBOSE |
|---|
| 209 | osg::notify(osg::NOTICE)<<"runMultiThreadReadTests() -- completed."<<std::endl; |
|---|
| 210 | #endif |
|---|
| 211 | } |
|---|