Thread.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
00003  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *   http://www.apache.org/licenses/LICENSE-2.0
00010 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "Thread.h"
00019 
00020 #if defined(__LINUX__)  
00021 void* threadFunction(void* pvThread)
00022 {
00023         CThread* pclThread = (CThread*)pvThread;
00024         (pclThread->m_pfuThreadFunction)(pclThread);
00025         return NULL;
00026 }
00027 #endif
00028 
00029 #if defined(__QNX__)    
00030 void threadFunction(void* pvThread)
00031 {
00032         CThread* pclThread = (CThread*)pvThread;
00033         (pclThread->m_pfuThreadFunction)(pclThread);
00034 }
00035 #endif
00036 
00037 #if defined(_WIN32)     
00038 unsigned int __stdcall threadFunction(void* pvThread)
00039 {
00040         CThread* pclThread = (CThread*)pvThread;
00041         (pclThread->m_pfuThreadFunction)(pclThread);
00042         return 0;
00043 }
00044 #endif
00045 
00046 // ========================================================================== ;
00047 //                                                                            ;
00048 // ---- constructors / destructor ------------------------------------------- ;
00049 //                                                                            ;
00050 // ========================================================================== ;
00051 
00052 CThread::CThread(void) : CMessage("CThread", g_iDebugLevel, g_bDebug, g_bDebugFile),
00053                                                  m_uiStackSize(1228000), 
00054                                                  m_pcStack(0),
00055                                                  m_hThreadHandle(0),
00056                                                  m_bThreadRunFlag(false),
00057                                                  m_bThreadStopFlag(false),
00058                                                  m_pvThreadObject(0),
00059                                                  m_pfuThreadFunction(0)
00060 {
00061 }
00062 
00063 CThread::CThread(const CThread& clThread)
00064 {
00065         error(-1, "copy contructor : method should no be called!");
00066 }
00067 
00068 CThread::~CThread(void)
00069 {
00070         debug(1, "destructed");
00071 }
00072 
00073 // ========================================================================== ;
00074 //                                                                            ;
00075 // ---- operators ----------------------------------------------------------- ;
00076 //                                                                            ;
00077 // ========================================================================== ;
00078 
00079 CThread& CThread::operator=(const CThread& clThread)
00080 {
00081         error(-1, "assignment operator : method should not be called!");
00082         return *this;
00083 }
00084 
00085 // ========================================================================== ;
00086 //                                                                            ;
00087 // ---- query functions ----------------------------------------------------- ;
00088 //                                                                            ;
00089 // ========================================================================== ;
00090 
00091 // ========================================================================== ;
00092 //                                                                            ;
00093 // ---- modify functions ---------------------------------------------------- ;
00094 //                                                                            ;
00095 // ========================================================================== ;
00096 
00097 void CThread::setThreadStackSize(unsigned int uiSize)
00098 {
00099         m_uiStackSize = uiSize;
00100 }
00101 
00102 // ========================================================================== ;
00103 //                                                                            ;
00104 // ---- I/O functions ------------------------------------------------------- ;
00105 //                                                                            ;
00106 // ========================================================================== ;
00107 
00108 // ========================================================================== ;
00109 //                                                                            ;
00110 // ---- exec functions ------------------------------------------------------ ;
00111 //                                                                            ;
00112 // ========================================================================== ;
00113 
00114 int CThread::createThread(void (*pfuThreadFunction)(CThread*), void* pvThreadObject)
00115 {
00116         m_bThreadStopFlag = false;
00117         m_pvThreadObject = pvThreadObject;
00118         m_pfuThreadFunction = pfuThreadFunction;
00119 #if defined(_WIN32)                                     
00120         unsigned int iThreadId;
00121 
00122         m_hThreadHandle = (HANDLE)_beginthreadex(NULL, 0, threadFunction, (void*)this, 0, &iThreadId);
00123 
00124         if(m_hThreadHandle == NULL)
00125         {
00126                 warning("createThread : creating thread failed!");
00127                 m_bThreadRunFlag = false;
00128                 return -1;
00129         }
00130         else
00131         {
00132                 m_bThreadRunFlag = true;
00133                 return 0;
00134         }
00135 #endif
00136 #if defined(__LINUX__)  
00137         pthread_attr_t Thread_attr;
00138         
00139     int retVal = pthread_create(&m_hThreadHandle, NULL, threadFunction, (void*)this);     
00140         if(retVal != 0)
00141         {
00142                 warning("createThread : creating thread failed!");
00143                 m_bThreadRunFlag = false; 
00144                 return -1;      
00145         }
00146         else
00147         {
00148                 m_bThreadRunFlag = true;
00149                 return 0;
00150         }
00151 #endif
00152 #if defined(__QNX__)
00153 
00154         if(m_pcStack == NULL)
00155                 m_pcStack = new char[m_uiStackSize];
00156 
00157         if(m_pcStack == NULL)
00158         {
00159                 m_bThreadRunFlag = false;
00160                 warning("createThread : creating stack failed!");
00161                 return -1;
00162         }
00163         int iThreadId = _beginthread(threadFunction, m_pcStack, m_uiStackSize, (void*)this);
00164         debug(1,"CThread: create stacksize=%d\n",m_uiStackSize);
00165 
00166         if( iThreadId == 0)
00167         {
00168                 warning("createThread : creating thread failed!");
00169                 m_bThreadRunFlag = false;
00170                 delete [] m_pcStack;
00171                 return -1;
00172         }
00173         else
00174         {
00175                 m_bThreadRunFlag = true;
00176                 return 0;
00177         }
00178 
00179 #endif
00180 }
00181 
00182 void CThread::exitThread()
00183 {
00184         m_bThreadRunFlag = false;
00185         #if defined(__WIN32)
00186                  _endthreadex(0);
00187         #endif
00188         #if defined(__QNX__)
00189                  _endthread();
00190         #endif
00191 }
00192 
00193 void CThread::terminateThread()
00194 {
00195         m_bThreadStopFlag = true;
00196 }
00197 
00198 bool CThread::checkThreadRun()
00199 {
00200         return m_bThreadRunFlag;
00201 }
00202 
00203 bool CThread::checkThreadStop()
00204 {
00205         return m_bThreadStopFlag;
00206 }


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Sat Jun 8 2019 20:25:13