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.
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 );
65 virtual thread * clone() = 0;
66 virtual void test() = 0;
70 cds::threading::Manager::attachThread();
73 virtual void TearDown()
75 cds::threading::Manager::detachThread();
79 explicit thread( thread_pool& master, int type = 0 );
81 thread_pool& pool() { return m_pool; }
82 int type() const { return m_type; }
83 size_t id() const { return m_id; }
84 bool time_elapsed() const;
87 friend class thread_pool;
95 // Pool of test threads
99 explicit thread_pool( ::testing::Test& fixture )
100 : m_fixture( fixture )
101 , m_bRunning( false )
102 , m_bStopped( false )
104 , m_bTimeElapsed( false )
113 void add( thread * what )
115 m_threads.push_back( what );
118 void add( thread * what, size_t count )
121 for ( size_t i = 1; i < count; ++i ) {
122 thread * p = what->clone();
127 std::chrono::milliseconds run()
129 return run( std::chrono::seconds::zero() );
132 std::chrono::milliseconds run( std::chrono::seconds duration )
137 while ( m_readyCount.load() != m_threads.size() )
138 std::this_thread::yield();
140 m_bTimeElapsed.store( false, std::memory_order_release );
141 auto time_start = std::chrono::steady_clock::now();
144 scoped_lock l( m_cvMutex );
146 m_cvStart.notify_all();
149 if ( duration != std::chrono::seconds::zero() )
150 std::this_thread::sleep_for( duration );
151 m_bTimeElapsed.store( true, std::memory_order_release );
154 scoped_lock l( m_cvMutex );
155 while ( m_doneCount != m_threads.size() )
159 auto time_end = std::chrono::steady_clock::now();
161 m_cvStop.notify_all();
163 for ( auto t : m_threads )
166 return m_testDuration = std::chrono::duration_cast<std::chrono::milliseconds>(time_end - time_start);
169 size_t size() const { return m_threads.size(); }
170 thread& get( size_t idx ) const { return *m_threads.at( idx ); }
172 template <typename Fixture>
175 return static_cast<Fixture&>(m_fixture);
178 std::chrono::milliseconds duration() const { return m_testDuration; }
182 for ( auto t : m_threads )
190 protected: // thread interface
193 return m_threads.size();
196 void ready_to_start( thread& /*who*/ )
198 // Called from test thread
200 // Wait for all thread created
201 scoped_lock l( m_cvMutex );
202 m_readyCount.fetch_add( 1 );
203 while ( !m_bRunning )
207 void thread_done( thread& /*who*/ )
209 // Called from test thread
212 scoped_lock l( m_cvMutex );
215 // Tell pool that the thread is done
216 m_cvDone.notify_all();
218 // Wait for all thread done
219 while ( !m_bStopped )
227 ::testing::Test& m_fixture;
228 std::vector<thread *> m_threads;
230 typedef std::unique_lock<std::mutex> scoped_lock;
231 std::mutex m_cvMutex;
232 std::condition_variable m_cvStart;
233 std::condition_variable m_cvStop;
234 std::condition_variable m_cvDone;
236 volatile bool m_bRunning;
237 volatile bool m_bStopped;
238 volatile size_t m_doneCount;
239 std::atomic<bool> m_bTimeElapsed;
240 std::atomic<size_t> m_readyCount;
242 std::chrono::milliseconds m_testDuration;
245 inline thread::thread( thread_pool& master, int type /*= 0*/ )
248 , m_id( master.get_next_id())
249 , m_impl( &thread::run, this )
252 inline thread::thread( thread const& sample )
253 : m_pool( sample.m_pool )
254 , m_type( sample.m_type )
255 , m_id( m_pool.get_next_id() )
256 , m_impl( &thread::run, this )
259 inline void thread::run()
262 m_pool.ready_to_start( *this );
264 m_pool.thread_done( *this );
268 inline bool thread::time_elapsed() const
270 return m_pool.m_bTimeElapsed.load( std::memory_order_acquire );
273 } // namespace cds_test
275 #endif // CDSTEST_THREAD_H