check transfer  0.1
Check data transfer for SDAccell OpenCL application
tf_testthread.cpp
Go to the documentation of this file.
1 /*
2  * TF_TestThread.cpp
3  *
4  * Created on: Jan 29, 2017
5  * Author: Dmitry Smekhov
6  */
7 
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <time.h>
14 
15 #include "exceptinfo.h"
16 
17 #include "tf_testthread.h"
18 
19 //-----------------------------------------------------------------------------
20 
21 TF_TestThread::TF_TestThread( TableEngine *pTable, int argc, char **argv) : TF_Test( pTable )
22 {
23  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld\n", __FUNCTION__, pthread_self());
24 
26  m_isComplete=0;
27  m_isTerminate=0;
28  m_CycleCnt=0;
29  m_isException=0;
30 
31  pthread_mutex_init(&m_ResultStartMutex, 0);
32  pthread_mutex_init(&m_ResultCompleteMutex, 0);
33  pthread_mutex_init(&m_StartMutex, 0);
34  pthread_mutex_init(&m_ThreadExitMutex, 0);
35 
36  // Lock mutex for Start and GetResult
37  pthread_mutex_lock(&m_ResultStartMutex);
38  pthread_mutex_lock(&m_ResultCompleteMutex);
39  pthread_mutex_lock(&m_StartMutex);
40  //pthread_mutex_lock(&m_ThreadExitMutex);
41 
42 }
43 
44 
45 //-----------------------------------------------------------------------------
46 
48 {
49  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld\n", __FUNCTION__, pthread_self());
50  pthread_mutex_lock(&m_ThreadExitMutex);
51  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld (COMPLETE)\n", __FUNCTION__, pthread_self());
52 }
53 
54 //-----------------------------------------------------------------------------
55 
57 {
58  if( 0==cnt )
59  {
60  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld\n", __FUNCTION__, pthread_self());
61  int res = pthread_attr_init(&m_attrThread);
62  if(res != 0) {
63  fprintf(stderr, "%s\n", "Stream not started");
64  throw( "Stream not started" );
65  }
66 
67  res = pthread_attr_setdetachstate(&m_attrThread, PTHREAD_CREATE_JOINABLE);
68  if(res != 0) {
69  fprintf(stderr, "%s\n", "Stream not started");
70  throw( "Stream not started" );
71  }
72 
73  res = pthread_create(&m_hThread, &m_attrThread, ThreadFunc, this);
74  if(res != 0) {
75  fprintf(stderr, "%s\n", "Stream not started");
76  throw( "Stream not started" );
77  }
78  }
79 
80  int ret=m_isPrepareComplete;
81 
82  if( m_isException )
83  {
84  throw except_info( "%s - exception during execute", __FUNCTION__ );
85  }
86  return ret;
87 }
88 
89 //-----------------------------------------------------------------------------
90 
91 void* TF_TestThread::ThreadFunc(void* lpvThreadParm)
92 {
93  TF_TestThread *test=(TF_TestThread*)lpvThreadParm;
94  if(!test)
95  return 0;
96  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld\n", __FUNCTION__, pthread_self());
97  return test->Execute();
98 }
99 
100 //-----------------------------------------------------------------------------
101 
103 {
104  try
105  {
106  pthread_mutex_lock(&m_ThreadExitMutex);
107 
108  PrepareInThread();
109 
110 
112 
113  // Sleep here while TF_TestThread::Start() will be called in another thread
114  pthread_mutex_lock(&m_StartMutex);
115 
116  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld (START)\n", __FUNCTION__, pthread_self());
117 
118  // Working forever while m_isTerminate == 0
119  Run();
120 
121  m_isComplete = 1;
122 
123  // Wait while TF_TestThread::GetResult() will be called
124  pthread_mutex_lock( &m_ResultStartMutex );
125 
127 
128  // Unlock mutex for complete GetResult() execution
129  pthread_mutex_unlock(&m_ResultCompleteMutex);
130 
131  CleanupInThread();
132 
133  // Unlock mutex for complete TF_TestThread::~TF_TestThread() execution
134  pthread_mutex_unlock(&m_ThreadExitMutex);
135 
136  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld (COMPLETE)\n", __FUNCTION__, pthread_self());
137 
138  }
139  catch( except_info_t err )
140  {
141  printf( "\n\n\n\n\n\n\n \n Error in thread:\n%s\n \n", err.info.c_str());
142  m_isComplete = 1;
143  m_isException = 1;
144  pthread_mutex_unlock(&m_ThreadExitMutex);
145  }
146 
147  catch( ... )
148  {
149  printf( "\n\n\n \n Unknow error in thread\n \n" );
150  m_isComplete = 1;
151  m_isException = 1;
152  pthread_mutex_unlock(&m_ThreadExitMutex);
153 
154  }
155 
156  return 0;
157 }
158 
159 //-----------------------------------------------------------------------------
160 
162 {
163  // Unlock mutex for unblock TF_TestThread::Execute() function
164  pthread_mutex_unlock(&m_StartMutex);
165  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld\n", __FUNCTION__, pthread_self());
166 }
167 
168 //-----------------------------------------------------------------------------
169 
171 {
172  m_isTerminate = 1;
173  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld\n", __FUNCTION__, pthread_self());
174 }
175 
176 //-----------------------------------------------------------------------------
177 
179 {
180  //if(m_isComplete > 0)
181  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld\n", __FUNCTION__, pthread_self());
182 
183  return m_isComplete;
184 }
185 
186 //-----------------------------------------------------------------------------
187 
189 {
190  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld (START)\n", __FUNCTION__, pthread_self());
191 
192  // Unlock mutex for finish Execute()
193  pthread_mutex_unlock(&m_ResultStartMutex);
194 
195  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld (Unlock m_ResultStartMutex)\n", __FUNCTION__, pthread_self());
196 
197  // Waiting here while GetResultInThread() will be called and complete from Execute();
198  pthread_mutex_lock(&m_ResultCompleteMutex);
199 
200  //fprintf(stderr, "TF_TestThread::%s(): ID - %ld (COMPLETE)\n", __FUNCTION__, pthread_self());
201 }
202 
203 //-----------------------------------------------------------------------------
204 /*
205 void TF_TestThread::GetResultInThread()
206 {
207  fprintf(stderr, "TF_TestThread::%s(): ID - %ld (START)\n", __FUNCTION__, pthread_self());
208  fprintf(stderr, "TF_TestThread::%s(): ID - %ld (COMPLETE)\n", __FUNCTION__, pthread_self());
209 }
210 */
211 //-----------------------------------------------------------------------------
212 
213 
214 
215 //-----------------------------------------------------------------------------
216 
217 
pthread_mutex_t m_ResultStartMutex
Definition: tf_testthread.h:61
virtual void GetResultInThread()=0
Show result.
virtual void Start()
Start of test.
static void * ThreadFunc(void *lpvThreadParm)
TF_TestThread(TableEngine *pTable, int argc, char **argv)
virtual void PrepareInThread()=0
Prepare test.
pthread_mutex_t m_StartMutex
Definition: tf_testthread.h:63
int m_isPrepareComplete
Definition: tf_testthread.h:55
Base class for testing device.
Definition: tf_test.h:16
except_info_t except_info(const char *fmt,...)
Definition: exceptinfo.cpp:25
virtual int isComplete()
Return 1 when test is complete.
Base class for application with thread.
Definition: tf_testthread.h:23
virtual int Prepare(int cnt)
Prepare test.
std::string info
Definition: exceptinfo.h:8
virtual ~TF_TestThread()
pthread_mutex_t m_ResultCompleteMutex
Definition: tf_testthread.h:62
virtual void GetResult()
Show result of test.
pthread_mutex_t m_ThreadExitMutex
Definition: tf_testthread.h:64
virtual void CleanupInThread()=0
Free any resource.
virtual void Stop()
Stop of test.
pthread_t m_hThread
Definition: tf_testthread.h:66
pthread_attr_t m_attrThread
Definition: tf_testthread.h:67
virtual void Run()=0
Main body of user test.