Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
General_MT_UnitTests.hpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #ifndef TEUCHOS_GENERAL_MT_UNITTESTS_HPP
45 #define TEUCHOS_GENERAL_MT_UNITTESTS_HPP
46 
48 #include "Teuchos_ConfigDefs.hpp"
49 
50 // NUM_TOTAL_CORES_USED = 4 set in CMake
51 #define TEUCHOS_THREAD_SAFE_UNIT_TESTS_THREADS_USED 4
52 
53 #include <atomic>
54 #include <iostream>
55 
56 namespace {
57 
58 // this is a convenience function for outputting percent complete information
59 // for long tests designed to find race conditions
60 static void convenience_log_progress(int cycle, int totalCycles) {
61  if (cycle==0) {
62  std::cout << "Percent complete: "; // begin the log line
63  }
64  // log every 10% percent complete - using mod % to output at regular intervals
65  int mod = (totalCycles/10);
66  // sometimes quick testing so make sure mod is not 0
67  if((cycle % (mod == 0 ? 1 : mod) == 0) || (cycle == totalCycles-1)) {
68  std::cout <<
69  (int)( 100.0f * (float) cycle / (float) (totalCycles-1) ) << "% ";
70  // not necessary on some setups but for Xcode this would not work without
71  // the flush command - it waits for an endl
72  std::flush( std::cout );
73  }
74 }
75 
76 // This class is a utility class which tracks constructor/destructor calls
77 // (for this test) or counts times a dealloc or deallocHandle was implemented
78 // (for later tests)
79 class CatchMemoryLeak
80 {
81 public:
82  CatchMemoryLeak() { ++s_countAllocated; }
83  ~CatchMemoryLeak() { --s_countAllocated; }
84  static std::atomic<int> s_countAllocated;
85  static std::atomic<int> s_countDeallocs;
86 };
87 // counts constructor calls (+1) and destructor calls (-1) which may include
88 // double delete events
89 std::atomic<int> CatchMemoryLeak::s_countAllocated(0);
90 // counts dealloc or dellocHandle calls - used for test 4 and test 5
91 std::atomic<int> CatchMemoryLeak::s_countDeallocs(0);
92 
93 // manages a bool for spin locking unit test threads - the idea is to hold all
94 // threads until ready and then release simultaneously to maximize race
95 // condition probability.
96 class ThreadTestManager
97 {
98 public:
99  // used to spin lock the sub threads until all released simultaneously
100  static std::atomic<bool> s_bAllowThreadsToRun;
101  // this lets the sub threads know the event happened so they can quit or give
102  // errors if they don't detect something after this occurs
103  static std::atomic<bool> s_bMainThreadSetToNull;
104  // utility to tell special threads when main threads have finished work
105  static std::atomic<int> s_countCompletedThreads;
106  // utility to count how many times a working thread has run - using this to
107  // debug other threads if they are supposed to trigger an event once this
108  // thread completes a cycle
109  static std::atomic<int> s_countWritingThreadCycles;
110 };
111 std::atomic<bool> ThreadTestManager::s_bAllowThreadsToRun(false);
112 std::atomic<bool> ThreadTestManager::s_bMainThreadSetToNull(false);
113 std::atomic<int> ThreadTestManager::s_countCompletedThreads(0);
114 std::atomic<int> ThreadTestManager::s_countWritingThreadCycles(0);
115 
116 // utility define used below: -1 means it was never set
117 #define UNSET_CYCLE_INDEX -1
118 
119 // this convenience struct is used to track the various errors which can
120 // happen with the RCP classes, which uses weak references. The unit test
121 // description below describes in more detail what this test is doing.
122 struct Cycle_Index_Tracker
123 {
124  Cycle_Index_Tracker()
125  {
126  missedDanglingOnFirstCycle = UNSET_CYCLE_INDEX;
127  danglingReference = UNSET_CYCLE_INDEX;
128  scambledMemory = UNSET_CYCLE_INDEX;
129  outOfRangeError = UNSET_CYCLE_INDEX;
130  unknownError = UNSET_CYCLE_INDEX;
131  }
132  // for special test with detection designed to hit on the first cycle,
133  // this tracks if it actually missed that cycle
134  // this test has less random behavior so was useful for validation
135  int missedDanglingOnFirstCycle;
136  // tracks when a dangling reference was hit
137  int danglingReference;
138  // tracks when scrambled memory was detected
139  int scambledMemory;
140  // tracks when an out of range error was found
141  int outOfRangeError;
142  // tracks unknown exception - not expected
143  int unknownError;
144  // feedback to indicate how many times the thread has actually done a loop
145  std::atomic<int> trackCycle;
146 };
147 
148 } // end namespace
149 
150 #endif // end #ifdef TEUCHOS_GENERAL_MT_UNITTESTS_HPP
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
#define UNSET_CYCLE_INDEX