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>
52 protected: // thread_pool interface
53 thread( thread const& sample );
58 void join() { m_impl.join(); }
61 virtual thread * clone() = 0;
62 virtual void test() = 0;
66 virtual void TearDown()
70 explicit thread( thread_pool& master, int type = 0 );
72 thread_pool& pool() { return m_pool; }
73 int type() const { return m_type; }
74 size_t id() const { return m_id; }
77 friend class thread_pool;
85 // Pool of test threads
89 explicit thread_pool( ::testing::Test& fixture )
90 : m_fixture( fixture )
98 for ( auto t : m_threads )
102 void add( thread& what )
104 m_threads.push_back( &what );
108 void add( thread& what, size_t count )
111 for ( size_t i = 1; i < count; ++i ) {
112 thread * p = what.clone();
117 std::chrono::milliseconds run()
122 auto time_start = std::chrono::steady_clock::now();
125 m_cvStart.notify_all();
128 scoped_lock l( m_cvMutex );
129 while ( m_doneCount != m_threads.size() )
133 auto time_end = std::chrono::steady_clock::now();
135 m_cvStop.notify_all();
137 for ( auto t : m_threads )
140 return m_testDuration = time_end - time_start;
143 size_t size() const { return m_threads.size(); }
144 thread& get( size_t idx ) const { return *m_threads.at( idx ); }
146 template <typename Fixture>
149 return static_cast<Fixture&>(m_fixture);
152 std::chrono::milliseconds duration() const { return m_testDuration; }
154 protected: // thread interface
157 return m_threads.size();
160 void ready_to_start( thread& /*who*/ )
162 // Called from test thread
164 // Wait for all thread created
165 scoped_lock l( m_cvMutex );
166 while ( !m_bRunning )
170 void thread_done( thread& /*who*/ )
172 // Called from test thread
175 scoped_lock l( m_cvMutex );
179 // Tell pool that the thread is done
180 m_cvDone.notify_all();
182 // Wait for all thread done
184 scoped_lock l( m_cvMutex );
185 while ( !m_bStopped )
193 ::testing::Test& m_fixture;
194 std::vector<thread *> m_threads;
196 typedef std::unique_lock<std::mutex> scoped_lock;
197 std::mutex m_cvMutex;
198 std::condition_variable m_cvStart;
199 std::condition_variable m_cvStop;
200 std::condition_variable m_cvDone;
202 volatile bool m_bRunning;
203 volatile bool m_bStopped;
204 volatile size_t m_doneCount;
206 std::chrono::milliseconds m_testDuration;
209 inline thread::thread( thread_pool& master, int type = 0 )
212 , m_id( master.get_next_id())
213 , m_impl( &run, this )
216 inline thread::thread( thread const& sample )
217 : m_pool( sample.m_pool )
218 , m_type( sample.m_type )
219 , m_id( m_pool.get_next_id() )
220 , m_impl( &run, this )
223 inline void thread::run()
226 m_pool.ready_to_start( *this );
228 m_pool.thread_done( *this );
232 } // namespace cds_test
234 #endif // CDSTEST_THREAD_H