Thread.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  * 
00004  * Copyright (c) 2012 
00005  * 
00006  * SCHUNK GmbH & Co. KG
00007  *  
00008  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00009  * 
00010  * Project name: Drivers for "Amtec M5 Protocol" Electronics V4
00011  *                                                                        
00012  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00013  * 
00014  * Email:robotics@schunk.com
00015  * 
00016  * ToDo: 
00017  * 
00018  * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
00019  * 
00020  * Redistribution and use in source and binary forms, with or without 
00021  * modification, are permitted provided that the following conditions are met: 
00022  * 
00023  *  * Redistributions of source code must retain the above copyright 
00024  *    notice, this list of conditions and the following disclaimer. 
00025  *  * Redistributions in binary form must reproduce the above copyright 
00026  *    notice, this list of conditions and the following disclaimer in the 
00027  *    documentation and/or other materials provided with the distribution. 
00028  *  * Neither the name of SCHUNK GmbH & Co. KG nor the names of its 
00029  *    contributors may be used to endorse or promote products derived from 
00030  *    this software without specific prior written permission. 
00031  * 
00032  * This program is free software: you can redistribute it and/or modify 
00033  * it under the terms of the GNU Lesser General Public License LGPL as 
00034  * published by the Free Software Foundation, either version 3 of the 
00035  * License, or (at your option) any later version. 
00036  * 
00037  * This program is distributed in the hope that it will be useful, 
00038  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00039  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00040  * GNU Lesser General Public License LGPL for more details. 
00041  * 
00042  * You should have received a copy of the GNU Lesser General Public 
00043  * License LGPL along with this program. 
00044  * If not, see <http://www.gnu.org/licenses/>.
00045  * 
00046  ******************************************************************************/
00047 
00048 
00049 #include "Thread.h"
00050 
00051 #if defined(__LINUX__)  
00052 void* threadFunction(void* pvThread)
00053 {
00054         CThread* pclThread = (CThread*)pvThread;
00055         (pclThread->m_pfuThreadFunction)(pclThread);
00056         return NULL;
00057 }
00058 #endif
00059 
00060 #if defined(__QNX__)    
00061 void threadFunction(void* pvThread)
00062 {
00063         CThread* pclThread = (CThread*)pvThread;
00064         (pclThread->m_pfuThreadFunction)(pclThread);
00065 }
00066 #endif
00067 
00068 #if defined(_WIN32)     
00069 unsigned int __stdcall threadFunction(void* pvThread)
00070 {
00071         CThread* pclThread = (CThread*)pvThread;
00072         (pclThread->m_pfuThreadFunction)(pclThread);
00073         return 0;
00074 }
00075 #endif
00076 
00077 // ========================================================================== ;
00078 //                                                                            ;
00079 // ---- constructors / destructor ------------------------------------------- ;
00080 //                                                                            ;
00081 // ========================================================================== ;
00082 
00083 CThread::CThread(void) : CMessage("CThread", g_iDebugLevel, g_bDebug, g_bDebugFile),
00084                                                  m_uiStackSize(1228000), 
00085                                                  m_pcStack(0),
00086                                                  m_hThreadHandle(0),
00087                                                  m_bThreadRunFlag(false),
00088                                                  m_bThreadStopFlag(false),
00089                                                  m_pvThreadObject(0),
00090                                                  m_pfuThreadFunction(0)
00091 {
00092 }
00093 
00094 CThread::CThread(const CThread& clThread)
00095 {
00096         error(-1, "copy contructor : method should no be called!");
00097 }
00098 
00099 CThread::~CThread(void)
00100 {
00101         debug(1, "destructed");
00102 }
00103 
00104 // ========================================================================== ;
00105 //                                                                            ;
00106 // ---- operators ----------------------------------------------------------- ;
00107 //                                                                            ;
00108 // ========================================================================== ;
00109 
00110 CThread& CThread::operator=(const CThread& clThread)
00111 {
00112         error(-1, "assignment operator : method should not be called!");
00113         return *this;
00114 }
00115 
00116 // ========================================================================== ;
00117 //                                                                            ;
00118 // ---- query functions ----------------------------------------------------- ;
00119 //                                                                            ;
00120 // ========================================================================== ;
00121 
00122 // ========================================================================== ;
00123 //                                                                            ;
00124 // ---- modify functions ---------------------------------------------------- ;
00125 //                                                                            ;
00126 // ========================================================================== ;
00127 
00128 void CThread::setThreadStackSize(unsigned int uiSize)
00129 {
00130         m_uiStackSize = uiSize;
00131 }
00132 
00133 // ========================================================================== ;
00134 //                                                                            ;
00135 // ---- I/O functions ------------------------------------------------------- ;
00136 //                                                                            ;
00137 // ========================================================================== ;
00138 
00139 // ========================================================================== ;
00140 //                                                                            ;
00141 // ---- exec functions ------------------------------------------------------ ;
00142 //                                                                            ;
00143 // ========================================================================== ;
00144 
00145 int CThread::createThread(void (*pfuThreadFunction)(CThread*), void* pvThreadObject)
00146 {
00147         m_bThreadStopFlag = false;
00148         m_pvThreadObject = pvThreadObject;
00149         m_pfuThreadFunction = pfuThreadFunction;
00150 #if defined(_WIN32)                                     
00151         unsigned int iThreadId;
00152 
00153         m_hThreadHandle = (HANDLE)_beginthreadex(NULL, 0, threadFunction, (void*)this, 0, &iThreadId);
00154 
00155         if(m_hThreadHandle == NULL)
00156         {
00157                 warning("createThread : creating thread failed!");
00158                 m_bThreadRunFlag = false;
00159                 return -1;
00160         }
00161         else
00162         {
00163                 m_bThreadRunFlag = true;
00164                 return 0;
00165         }
00166 #endif
00167 #if defined(__LINUX__)  
00168         pthread_attr_t Thread_attr;
00169         
00170     int retVal = pthread_create(&m_hThreadHandle, NULL, threadFunction, (void*)this);     
00171         if(retVal != 0)
00172         {
00173                 warning("createThread : creating thread failed!");
00174                 m_bThreadRunFlag = false; 
00175                 return -1;      
00176         }
00177         else
00178         {
00179                 m_bThreadRunFlag = true;
00180                 return 0;
00181         }
00182 #endif
00183 #if defined(__QNX__)
00184 
00185         if(m_pcStack == NULL)
00186                 m_pcStack = new char[m_uiStackSize];
00187 
00188         if(m_pcStack == NULL)
00189         {
00190                 m_bThreadRunFlag = false;
00191                 warning("createThread : creating stack failed!");
00192                 return -1;
00193         }
00194         int iThreadId = _beginthread(threadFunction, m_pcStack, m_uiStackSize, (void*)this);
00195         debug(1,"CThread: create stacksize=%d\n",m_uiStackSize);
00196 
00197         if( iThreadId == 0)
00198         {
00199                 warning("createThread : creating thread failed!");
00200                 m_bThreadRunFlag = false;
00201                 delete [] m_pcStack;
00202                 return -1;
00203         }
00204         else
00205         {
00206                 m_bThreadRunFlag = true;
00207                 return 0;
00208         }
00209 
00210 #endif
00211 }
00212 
00213 void CThread::exitThread()
00214 {
00215         m_bThreadRunFlag = false;
00216         #if defined(__WIN32)
00217                  _endthreadex(0);
00218         #endif
00219         #if defined(__QNX__)
00220                  _endthread();
00221         #endif
00222 }
00223 
00224 void CThread::terminateThread()
00225 {
00226         m_bThreadStopFlag = true;
00227 }
00228 
00229 bool CThread::checkThreadRun()
00230 {
00231         return m_bThreadRunFlag;
00232 }
00233 
00234 bool CThread::checkThreadStop()
00235 {
00236         return m_bThreadStopFlag;
00237 }


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Thu Aug 27 2015 15:06:52