34 static void read_arrayview_in_thread(RCP<ArrayView<int>> shared_arrayview,
35 int expectedValue, std::atomic<int> & countErrors) {
37 while (!ThreadTestManager::s_bAllowThreadsToRun) {}
38 for(
int n = 0;
n < 1000; ++
n) {
40 iter < shared_arrayview->end(); ++iter) {
41 int readAValueByIterator = *iter;
43 if (readAValueByIterator != expectedValue) {
76 const int setValue = 67359487;
77 const int arraySize = 10;
78 std::atomic<int> countErrors(0);
79 for (
int testCycle = 0; testCycle < numTests; ++testCycle) {
81 std::vector<std::thread> threads;
82 ThreadTestManager::s_bAllowThreadsToRun =
false;
83 Array<int> array(arraySize, setValue);
84 RCP<ArrayView<int>> arrayview_rcp =
rcp(
new ArrayView<int>(array));
86 for (
int i = 0; i < numThreads; ++i) {
87 threads.push_back( std::thread(read_arrayview_in_thread,
88 arrayview_rcp, setValue, std::ref(countErrors)) );
91 ThreadTestManager::s_bAllowThreadsToRun =
true;
92 for (
unsigned int i = 0; i < threads.size(); ++i) {
98 convenience_log_progress(testCycle, numTests);
115 static void scramble_memory(
int scrambleValue,
int testArraySize,
116 int finishWhenThisThreadCountCompletes) {
117 while (!ThreadTestManager::s_bAllowThreadsToRun) {}
122 #define ARRAY_SCRAMBLE_SIZE 100
123 std::vector<int> * tempPtrArray[ARRAY_SCRAMBLE_SIZE];
124 for (
int n = 0;
n < ARRAY_SCRAMBLE_SIZE; ++
n) {
128 tempPtrArray[
n] =
new std::vector<int>(testArraySize, scrambleValue);
130 for (
int n = 0;
n < ARRAY_SCRAMBLE_SIZE; ++
n) {
131 delete tempPtrArray[
n];
133 if (ThreadTestManager::s_countCompletedThreads >=
134 finishWhenThisThreadCountCompletes) {
144 static void share_arrayview_to_threads(ArrayView<int> shared_arrayview,
145 int theTestValue, Cycle_Index_Tracker & index_tracker) {
146 while (!ThreadTestManager::s_bAllowThreadsToRun) {}
150 bool bCheckStatus = ThreadTestManager::s_bMainThreadSetToNull;
153 int tryToReadAValue = shared_arrayview[0];
154 index_tracker.trackCycle = cycle;
155 if (tryToReadAValue != theTestValue) {
159 index_tracker.scambledMemory = cycle;
163 index_tracker.unknownError = cycle;
171 catch (DanglingReferenceError&) {
173 index_tracker.danglingReference = cycle;
176 std::cout << std::endl <<
"Unknown and unhandled exception!" << std::endl;
179 ++ThreadTestManager::s_countCompletedThreads;
216 const int theTestValue = 66387;
217 const int scrambleValue = 572778;
218 const int testArraySize = 3;
221 int countDanglingReferences = 0;
222 int scrambledMemoryEvents = 0;
223 int unknownErrors = 0;
226 int finishWhenThisThreadCountCompletes = numThreads - 1;
227 for (
int testCycle = 0; testCycle < numTests; ++testCycle) {
229 ThreadTestManager::s_countCompletedThreads = 0;
232 ArrayRCP<int> arrayrcp =
arcp(
rcp(
233 new std::vector<int>(testArraySize, theTestValue)));
235 ArrayView<int> shared_arrayview = arrayrcp();
237 ThreadTestManager::s_bAllowThreadsToRun =
false;
239 ThreadTestManager::s_bMainThreadSetToNull =
false;
241 Cycle_Index_Tracker index_tracker[numThreads];
243 std::vector<std::thread> threads;
244 for (
int i = 0; i < numThreads; ++i) {
254 threads.push_back(std::thread(scramble_memory, scrambleValue,
255 testArraySize, finishWhenThisThreadCountCompletes));
263 threads.push_back(std::thread(share_arrayview_to_threads,
264 shared_arrayview, theTestValue, std::ref(index_tracker[i])));
270 ThreadTestManager::s_bAllowThreadsToRun =
true;
272 while (index_tracker[1].trackCycle < 1) {}
276 ThreadTestManager::s_bMainThreadSetToNull =
true;
278 for (
unsigned int i = 0; i < threads.size(); ++i) {
282 for (
unsigned int i = 0; i < threads.size(); ++i) {
283 if (index_tracker[i].danglingReference != -1) {
284 ++countDanglingReferences;
286 if (index_tracker[i].scambledMemory != -1 ) {
291 ++scrambledMemoryEvents;
293 if (index_tracker[i].unknownError != -1 ) {
299 convenience_log_progress(testCycle, numTests);
305 int requiredDanglingReferenceCount = (numThreads-1) * numTests;
306 bool bDanglingReferenceDetectionCountIsOK = (countDanglingReferences ==
307 requiredDanglingReferenceCount);
310 if( !bDanglingReferenceDetectionCountIsOK ) {
311 std::cout << std::endl <<
"Detected " << countDanglingReferences <<
312 " Dangling References but should have found " <<
313 requiredDanglingReferenceCount <<
"." << std::endl;
319 std::cout <<
"Danglers: " << countDanglingReferences <<
" Scrambles: " <<
320 scrambledMemoryEvents <<
" ";
324 if (unknownErrors != 0) {
325 std::cout << std::endl <<
"Detected " << unknownErrors <<
326 " dangling references were missed which should have been detected."
330 TEST_ASSERT( bDanglingReferenceDetectionCountIsOK )
335 #endif // TEUCHOS_DEBUG
Dangling reference error exception class.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
#define TEST_ASSERT(v1)
Assert the given statement is true.
pointer iterator
Type of a nonconst iterator.
#define TEUCHOS_THREAD_SAFE_UNIT_TESTS_THREADS_USED
ArrayRCP< T > arcp(const RCP< Array< T > > &v)
Wrap an RCP<Array<T> > object as an ArrayRCP<T> object.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Templated array class derived from the STL std::vector.
Smart reference counting pointer class for automatic garbage collection.
Range error exception class.
Reference-counted smart pointer for managing arrays.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...