root/OpenSceneGraph/trunk/examples/osgunittests/MultiThreadRead.cpp @ 12292

Revision 12292, 7.0 kB (checked in by robert, 3 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

  • Property svn:eol-style set to native
Line 
1/* OpenSceneGraph example, osgunittests.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Referenced>
20#include <osgDB/ReadFile>
21#include <osgDB/Registry>
22
23#include <OpenThreads/Thread>
24#include <OpenThreads/ScopedLock>
25
26struct RefBarrier : public osg::Referenced, public OpenThreads::Barrier
27{
28    RefBarrier(int numThreads):
29        OpenThreads::Barrier(numThreads) {}
30};
31
32class ReadThread : public osg::Referenced, public OpenThreads::Thread
33{
34public:
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                // take front filename;
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
112class SerializerReadFileCallback : public osgDB::Registry::ReadFileCallback
113{
114public:
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
152protected:
153       virtual ~SerializerReadFileCallback() {}
154       
155       OpenThreads::Mutex _mutex;
156};
157
158
159
160void 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.osg");
196        readThread->addFileName("glider.osg");
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}
Note: See TracBrowser for help on using the browser.