2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
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.
31 #ifndef CDSTEST_THREAD_H
32 #define CDSTEST_THREAD_H
34 #include <gtest/gtest.h>
37 #include <condition_variable>
40 #include <cds/threading/model.h>
53 protected: // thread_pool interface
54 thread( thread const& sample );
60 virtual thread * clone() = 0;
61 virtual void test() = 0;
65 cds::threading::Manager::attachThread();
68 virtual void TearDown()
70 cds::threading::Manager::detachThread();
74 explicit thread( thread_pool& master, int type = 0 );
76 thread_pool& pool() { return m_pool; }
77 int type() const { return m_type; }
78 size_t id() const { return m_id; }
79 bool time_elapsed() const;
82 friend class thread_pool;
89 // Pool of test threads
99 void reset( size_t count )
101 std::unique_lock< std::mutex > lock( m_mtx );
107 std::unique_lock< std::mutex > lock( m_mtx );
108 if ( --m_count == 0 ) {
113 while ( m_count != 0 )
122 std::condition_variable m_cv;
134 std::unique_lock< std::mutex > lock( m_mtx );
141 std::unique_lock< std::mutex > lock( m_mtx );
148 std::unique_lock< std::mutex > lock( m_mtx );
154 std::condition_variable m_cv;
159 explicit thread_pool( ::testing::Test& fixture )
160 : m_fixture( fixture )
161 , m_bTimeElapsed( false )
169 void add( thread * what )
171 m_workers.push_back( what );
174 void add( thread * what, size_t count )
177 for ( size_t i = 1; i < count; ++i ) {
178 thread * p = what->clone();
183 std::chrono::milliseconds run()
185 return run( std::chrono::seconds::zero());
188 std::chrono::milliseconds run( std::chrono::seconds duration )
190 m_startBarrier.reset( m_workers.size() + 1 );
191 m_stopBarrier.reset( m_workers.size() + 1 );
194 std::vector< std::thread > threads;
195 threads.reserve( m_workers.size());
196 for ( auto w : m_workers )
197 threads.emplace_back( &thread::run, w );
199 // The pool is intialized
200 m_startPoint.ready();
202 m_bTimeElapsed.store( false, std::memory_order_release );
204 auto native_duration = std::chrono::duration_cast<std::chrono::steady_clock::duration>(duration);
206 // The pool is ready to start all workers
207 m_startBarrier.wait();
209 auto time_start = std::chrono::steady_clock::now();
210 auto const expected_end = time_start + native_duration;
212 if ( duration != std::chrono::seconds::zero()) {
214 std::this_thread::sleep_for( native_duration );
215 auto time_now = std::chrono::steady_clock::now();
216 if ( time_now >= expected_end )
218 native_duration = expected_end - time_now;
221 m_bTimeElapsed.store( true, std::memory_order_release );
223 // Waiting for all workers done
224 m_stopBarrier.wait();
226 auto time_end = std::chrono::steady_clock::now();
228 for ( auto& t : threads )
231 return m_testDuration = std::chrono::duration_cast<std::chrono::milliseconds>(time_end - time_start);
234 size_t size() const { return m_workers.size(); }
235 thread& get( size_t idx ) const { return *m_workers.at( idx ); }
237 template <typename Fixture>
240 return static_cast<Fixture&>(m_fixture);
243 std::chrono::milliseconds duration() const { return m_testDuration; }
247 for ( auto t : m_workers )
250 m_startPoint.reset();
258 protected: // thread interface
261 return m_workers.size();
264 void ready_to_start( thread& /*who*/ )
266 // Called from test thread
268 // Wait until the pool is ready
271 // Wait until all thread ready
272 m_startBarrier.wait();
275 void thread_done( thread& /*who*/ )
277 // Called from test thread
278 m_stopBarrier.wait();
284 ::testing::Test& m_fixture;
285 std::vector<thread *> m_workers;
287 initial_gate m_startPoint;
288 barrier m_startBarrier;
289 barrier m_stopBarrier;
291 std::atomic<bool> m_bTimeElapsed;
292 std::chrono::milliseconds m_testDuration;
295 inline thread::thread( thread_pool& master, int type /*= 0*/ )
298 , m_id( master.get_next_id())
301 inline thread::thread( thread const& sample )
302 : m_pool( sample.m_pool )
303 , m_type( sample.m_type )
304 , m_id( m_pool.get_next_id())
307 inline void thread::run()
310 m_pool.ready_to_start( *this );
312 m_pool.thread_done( *this );
316 inline bool thread::time_elapsed() const
318 return m_pool.m_bTimeElapsed.load( std::memory_order_acquire );
321 } // namespace cds_test
323 #endif // CDSTEST_THREAD_H