Thread.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
3  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "Thread.h"
19 
20 #if defined(__LINUX__)
21 void* threadFunction(void* pvThread)
22 {
23  CThread* pclThread = (CThread*)pvThread;
24  (pclThread->m_pfuThreadFunction)(pclThread);
25  return NULL;
26 }
27 #endif
28 
29 #if defined(__QNX__)
30 void threadFunction(void* pvThread)
31 {
32  CThread* pclThread = (CThread*)pvThread;
33  (pclThread->m_pfuThreadFunction)(pclThread);
34 }
35 #endif
36 
37 #if defined(_WIN32)
38 unsigned int __stdcall threadFunction(void* pvThread)
39 {
40  CThread* pclThread = (CThread*)pvThread;
41  (pclThread->m_pfuThreadFunction)(pclThread);
42  return 0;
43 }
44 #endif
45 
46 // ========================================================================== ;
47 // ;
48 // ---- constructors / destructor ------------------------------------------- ;
49 // ;
50 // ========================================================================== ;
51 
53  m_uiStackSize(1228000),
54  m_pcStack(0),
55  m_hThreadHandle(0),
56  m_bThreadRunFlag(false),
57  m_bThreadStopFlag(false),
58  m_pvThreadObject(0),
59  m_pfuThreadFunction(0)
60 {
61 }
62 
63 CThread::CThread(const CThread& clThread)
64 {
65  error(-1, "copy contructor : method should no be called!");
66 }
67 
69 {
70  debug(1, "destructed");
71 }
72 
73 // ========================================================================== ;
74 // ;
75 // ---- operators ----------------------------------------------------------- ;
76 // ;
77 // ========================================================================== ;
78 
80 {
81  error(-1, "assignment operator : method should not be called!");
82  return *this;
83 }
84 
85 // ========================================================================== ;
86 // ;
87 // ---- query functions ----------------------------------------------------- ;
88 // ;
89 // ========================================================================== ;
90 
91 // ========================================================================== ;
92 // ;
93 // ---- modify functions ---------------------------------------------------- ;
94 // ;
95 // ========================================================================== ;
96 
97 void CThread::setThreadStackSize(unsigned int uiSize)
98 {
99  m_uiStackSize = uiSize;
100 }
101 
102 // ========================================================================== ;
103 // ;
104 // ---- I/O functions ------------------------------------------------------- ;
105 // ;
106 // ========================================================================== ;
107 
108 // ========================================================================== ;
109 // ;
110 // ---- exec functions ------------------------------------------------------ ;
111 // ;
112 // ========================================================================== ;
113 
114 int CThread::createThread(void (*pfuThreadFunction)(CThread*), void* pvThreadObject)
115 {
116  m_bThreadStopFlag = false;
117  m_pvThreadObject = pvThreadObject;
118  m_pfuThreadFunction = pfuThreadFunction;
119 #if defined(_WIN32)
120  unsigned int iThreadId;
121 
122  m_hThreadHandle = (HANDLE)_beginthreadex(NULL, 0, threadFunction, (void*)this, 0, &iThreadId);
123 
124  if(m_hThreadHandle == NULL)
125  {
126  warning("createThread : creating thread failed!");
127  m_bThreadRunFlag = false;
128  return -1;
129  }
130  else
131  {
132  m_bThreadRunFlag = true;
133  return 0;
134  }
135 #endif
136 #if defined(__LINUX__)
137  pthread_attr_t Thread_attr;
138 
139  int retVal = pthread_create(&m_hThreadHandle, NULL, threadFunction, (void*)this);
140  if(retVal != 0)
141  {
142  warning("createThread : creating thread failed!");
143  m_bThreadRunFlag = false;
144  return -1;
145  }
146  else
147  {
148  m_bThreadRunFlag = true;
149  return 0;
150  }
151 #endif
152 #if defined(__QNX__)
153 
154  if(m_pcStack == NULL)
155  m_pcStack = new char[m_uiStackSize];
156 
157  if(m_pcStack == NULL)
158  {
159  m_bThreadRunFlag = false;
160  warning("createThread : creating stack failed!");
161  return -1;
162  }
163  int iThreadId = _beginthread(threadFunction, m_pcStack, m_uiStackSize, (void*)this);
164  debug(1,"CThread: create stacksize=%d\n",m_uiStackSize);
165 
166  if( iThreadId == 0)
167  {
168  warning("createThread : creating thread failed!");
169  m_bThreadRunFlag = false;
170  delete [] m_pcStack;
171  return -1;
172  }
173  else
174  {
175  m_bThreadRunFlag = true;
176  return 0;
177  }
178 
179 #endif
180 }
181 
183 {
184  m_bThreadRunFlag = false;
185  #if defined(__WIN32)
186  _endthreadex(0);
187  #endif
188  #if defined(__QNX__)
189  _endthread();
190  #endif
191 }
192 
194 {
195  m_bThreadStopFlag = true;
196 }
197 
199 {
200  return m_bThreadRunFlag;
201 }
202 
204 {
205  return m_bThreadStopFlag;
206 }
bool m_bThreadRunFlag
Definition: Thread.h:57
CThread & operator=(const CThread &clThread)
Definition: Thread.cpp:79
int createThread(void(*fuThreadFunction)(CThread *), void *pThreadObject)
Definition: Thread.cpp:114
Definition: Thread.h:33
void error(const int iErrorCode, const char *pcErrorMessage,...) const
Definition: Message.cpp:204
void * m_pvThreadObject
Definition: Thread.h:66
void setThreadStackSize(unsigned int uiSize)
Definition: Thread.cpp:97
bool m_bThreadStopFlag
Definition: Thread.h:58
int g_iDebugLevel
Definition: Message.cpp:20
void exitThread()
called inside the thread function before leaving the thread
Definition: Thread.cpp:182
void terminateThread()
called outside the thread function to terminate the thread
Definition: Thread.cpp:193
unsigned int m_uiStackSize
Definition: Thread.h:46
void warning(const char *pcWarningMessage,...) const
Definition: Message.cpp:257
bool checkThreadRun()
check outside the thread function to check running
Definition: Thread.cpp:198
bool g_bDebug
Definition: Message.cpp:22
char * m_pcStack
Definition: Thread.h:47
bool g_bDebugFile
Definition: Message.cpp:21
void debug(const int iDebugLevel, const char *pcDebugMessage,...) const
Definition: Message.cpp:332
CThread()
Definition: Thread.cpp:52
bool checkThreadStop()
check inside the thread function to check termination
Definition: Thread.cpp:203
~CThread(void)
Definition: Thread.cpp:68
void(* m_pfuThreadFunction)(CThread *)
Definition: Thread.h:67


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:19