2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <cds/details/defs.h> // TSan annotations
33 #include "cppunit/thread.h"
35 namespace CppUnitMini {
37 void TestThread::threadEntryPoint( TestThread * pInst )
42 void TestThread::create()
44 m_pThread = new boost::thread( threadEntryPoint, this );
47 void TestThread::run()
51 m_Pool.onThreadInitDone( this );
54 m_Pool.onThreadTestDone( this );
57 m_Pool.onThreadFiniDone( this );
59 catch ( std::exception& ex )
61 m_Pool.m_Test.message( "EXCEPTION in working thread: ");
62 m_Pool.m_Test.message( ex.what() );
66 void TestThread::error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line)
68 m_Pool.m_Test.error( in_macroName, in_macro, in_file, in_line );
72 ThreadPool::~ThreadPool()
74 delete m_pBarrierStart;
75 delete m_pBarrierDone;
77 for ( size_t i = 0; i < m_arrThreads.size(); ++i )
78 delete m_arrThreads[i];
79 m_arrThreads.resize( 0 );
82 void ThreadPool::add( TestThread * pThread, size_t nCount )
84 pThread->m_nThreadNo = m_arrThreads.size();
85 m_arrThreads.push_back( pThread );
87 TestThread * p = pThread->clone();
89 p->m_nThreadNo = m_arrThreads.size();
90 m_arrThreads.push_back( p );
95 void ThreadPool::run()
97 const size_t nThreadCount = m_arrThreads.size();
98 m_pBarrierStart = new boost::barrier( (unsigned int) nThreadCount );
99 // nThreadCount threads + current thread
100 m_pBarrierDone = new boost::barrier( (unsigned int) (nThreadCount + 1) );
102 for ( size_t i = 0; i < nThreadCount; ++i )
103 m_arrThreads[i]->create();
105 // Wait while all threads is done
106 m_pBarrierDone->wait();
107 std::this_thread::sleep_for(std::chrono::milliseconds(500));
110 void ThreadPool::run( unsigned int nDuration )
112 const size_t nThreadCount = m_arrThreads.size();
113 m_pBarrierStart = new boost::barrier( (unsigned int) nThreadCount );
114 m_pBarrierDone = new boost::barrier( (unsigned int) (nThreadCount + 1) );
116 for ( size_t i = 0; i < nThreadCount; ++i )
117 m_arrThreads[i]->create();
119 auto stEnd(std::chrono::steady_clock::now() + std::chrono::seconds( nDuration ));
121 std::this_thread::sleep_until( stEnd );
122 } while ( std::chrono::steady_clock::now() < stEnd );
124 for ( size_t i = 0; i < nThreadCount; ++i )
125 m_arrThreads[i]->stop();
127 // Wait while all threads is done
128 m_pBarrierDone->wait();
129 std::this_thread::sleep_for(std::chrono::milliseconds(500));
132 void ThreadPool::onThreadInitDone( TestThread * pThread )
134 // Calls in context of caller thread
135 // Wait while all threads started
136 m_pBarrierStart->wait();
138 pThread->m_Timer.reset();
141 void ThreadPool::onThreadTestDone( TestThread * pThread )
143 // Calls in context of caller thread
144 pThread->m_nDuration = pThread->m_Timer.duration();
147 void ThreadPool::onThreadFiniDone( TestThread * /*pThread*/ )
149 // Calls in context of caller thread
150 // Wait while all threads done
151 m_pBarrierDone->wait();