01bc5b6771148b806177ea3600b515778d81c63f
[libcds.git] / tests / cppunit / thread.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.     
29 */
30
31 #include <chrono>
32 #include <cds/details/defs.h> // TSan annotations
33 #include "cppunit/thread.h"
34
35 namespace CppUnitMini {
36
37     void TestThread::threadEntryPoint( TestThread * pInst )
38     {
39         pInst->run();
40     }
41
42     void TestThread::create()
43     {
44         m_pThread = new boost::thread( threadEntryPoint, this );
45     }
46
47     void TestThread::run()
48     {
49         try {
50             init();
51             m_Pool.onThreadInitDone( this );
52
53             test();
54             m_Pool.onThreadTestDone( this );
55
56             fini();
57             m_Pool.onThreadFiniDone( this );
58         }
59         catch ( std::exception& ex )
60         {
61             m_Pool.m_Test.message( "EXCEPTION in working thread: ");
62             m_Pool.m_Test.message( ex.what() );
63         }
64     }
65
66     void TestThread::error(const char *in_macroName, const char *in_macro, const char *in_file, int in_line)
67     {
68         m_Pool.m_Test.error( in_macroName, in_macro, in_file, in_line );
69     }
70
71
72     ThreadPool::~ThreadPool()
73     {
74         delete m_pBarrierStart;
75         delete m_pBarrierDone;
76
77         for ( size_t i = 0; i < m_arrThreads.size(); ++i )
78             delete m_arrThreads[i];
79         m_arrThreads.resize( 0 );
80     }
81
82     void    ThreadPool::add( TestThread * pThread, size_t nCount )
83     {
84         pThread->m_nThreadNo = m_arrThreads.size();
85         m_arrThreads.push_back( pThread );
86         while ( --nCount ) {
87             TestThread * p = pThread->clone();
88             if ( p ) {
89                 p->m_nThreadNo = m_arrThreads.size();
90                 m_arrThreads.push_back( p );
91             }
92         }
93     }
94
95     void    ThreadPool::run()
96     {
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) );
101
102         for ( size_t i = 0; i < nThreadCount; ++i )
103             m_arrThreads[i]->create();
104
105         // Wait while all threads is done
106         m_pBarrierDone->wait();
107         std::this_thread::sleep_for(std::chrono::milliseconds(500));
108     }
109
110     void ThreadPool::run( unsigned int nDuration )
111     {
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) );
115
116         for ( size_t i = 0; i < nThreadCount; ++i )
117             m_arrThreads[i]->create();
118
119         auto stEnd(std::chrono::steady_clock::now() + std::chrono::seconds( nDuration ));
120         do {
121             std::this_thread::sleep_until( stEnd );
122         } while ( std::chrono::steady_clock::now() < stEnd );
123
124         for ( size_t i = 0; i < nThreadCount; ++i )
125             m_arrThreads[i]->stop();
126
127         // Wait while all threads is done
128         m_pBarrierDone->wait();
129         std::this_thread::sleep_for(std::chrono::milliseconds(500));
130     }
131
132     void    ThreadPool::onThreadInitDone( TestThread * pThread )
133     {
134         // Calls in context of caller thread
135         // Wait while all threads started
136         m_pBarrierStart->wait();
137
138         pThread->m_Timer.reset();
139     }
140
141     void    ThreadPool::onThreadTestDone( TestThread * pThread )
142     {
143         // Calls in context of caller thread
144         pThread->m_nDuration = pThread->m_Timer.duration();
145     }
146
147     void    ThreadPool::onThreadFiniDone( TestThread * /*pThread*/ )
148     {
149         // Calls in context of caller thread
150         // Wait while all threads done
151         m_pBarrierDone->wait();
152     }
153 }