34
34
35
35
#include < vector>
36
36
#include < iostream>
37
+
37
38
#include " tbb/tbb.h"
39
+
38
40
#include " IECore/SharedSceneInterfaces.h"
41
+
39
42
#include " SceneCacheThreadingTest.h"
40
43
41
44
using namespace boost ;
@@ -52,61 +55,72 @@ struct SceneCacheThreadingTest
52
55
{
53
56
public :
54
57
55
- TestSceneCache () : m_errors( 0 )
58
+ TestSceneCache ( const char *attribute ) : m_errors( 0 ), m_attribute( attribute )
56
59
{
57
60
}
58
-
59
- void operator () ( int instance ) const
61
+
62
+ TestSceneCache ( TestSceneCache &that, tbb::split ) : m_errors( 0 ), m_attribute( that.m_attribute )
60
63
{
61
- for ( size_t i = 0 ; i < 1000 ; i++ )
64
+ }
65
+
66
+ void operator ()( const blocked_range<size_t > &r ) const
67
+ {
68
+ for ( size_t i = r.begin (); i != r.end (); ++i )
62
69
{
63
- if ( (instance + i) % 7 == 0 )
64
- {
65
- SharedSceneInterfaces::clear ();
66
- }
67
- ConstSceneInterfacePtr scene = SharedSceneInterfaces::get (" test/IECore/data/sccFiles/attributeAtRoot.scc" );
68
- try
70
+ for ( size_t j = 0 ; j < 1000 ; j++ )
69
71
{
70
- scene->readAttribute (" w" , 0 );
71
- }
72
- catch ( Exception e )
73
- {
74
- m_errors++;
72
+ if ( ( i + j ) % 7 == 0 )
73
+ {
74
+ SharedSceneInterfaces::clear ();
75
+ }
76
+
77
+ ConstSceneInterfacePtr scene = SharedSceneInterfaces::get (" test/IECore/data/sccFiles/attributeAtRoot.scc" );
78
+
79
+ try
80
+ {
81
+ scene->readAttribute ( m_attribute, 0 );
82
+ }
83
+ catch ( Exception &e )
84
+ {
85
+ m_errors++;
86
+ }
75
87
}
76
88
}
77
89
}
78
-
90
+
91
+ void join ( const TestSceneCache &that )
92
+ {
93
+ m_errors += that.m_errors ;
94
+ }
95
+
79
96
size_t errors () const
80
97
{
81
98
return m_errors;
82
99
}
83
100
84
101
private :
85
102
mutable size_t m_errors;
86
- SceneInterface::Path m_attributePath ;
103
+ SceneInterface::Name m_attribute ;
87
104
};
88
105
89
106
void testAttributeRead ()
90
107
{
91
- const int numThreads = 100 ;
92
-
93
- TestSceneCache task;
94
- std::vector< tbb::tbb_thread *> threads;
95
-
96
- for ( int i = 0 ; i < numThreads; i++ )
97
- {
98
- threads.push_back ( new tbb::tbb_thread ( task, i ) );
99
- }
100
- for ( int i = 0 ; i < numThreads; i++ )
101
- {
102
- threads[i]->join ();
103
- }
104
- for ( int i = 0 ; i < numThreads; i++ )
105
- {
106
- delete threads[i];
107
- }
108
- BOOST_CHECK ( task.errors () == 0 );
109
-
108
+ task_scheduler_init scheduler ( 100 );
109
+
110
+ TestSceneCache task ( " w" );
111
+
112
+ parallel_reduce ( blocked_range<size_t >( 0 , 100 ), task );
113
+ BOOST_CHECK ( task.errors () == 0 );
114
+ }
115
+
116
+ void testFakeAttributeRead ()
117
+ {
118
+ task_scheduler_init scheduler ( 100 );
119
+
120
+ TestSceneCache task ( " fake" );
121
+
122
+ parallel_reduce ( blocked_range<size_t >( 0 , 100 ), task );
123
+ BOOST_CHECK ( task.errors () == 100000 );
110
124
}
111
125
112
126
};
@@ -119,6 +133,7 @@ struct SceneCacheThreadingTestSuite : public boost::unit_test::test_suite
119
133
boost::shared_ptr<SceneCacheThreadingTest> instance ( new SceneCacheThreadingTest () );
120
134
121
135
add ( BOOST_CLASS_TEST_CASE ( &SceneCacheThreadingTest::testAttributeRead, instance ) );
136
+ add ( BOOST_CLASS_TEST_CASE ( &SceneCacheThreadingTest::testFakeAttributeRead, instance ) );
122
137
}
123
138
};
124
139
0 commit comments