VThread.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: Vthread.cpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 #ifndef QT_THREAD_SUPPORT 00045 00046 00047 00048 #include "VThread.hpp" 00049 00050 00051 VThread::VThread() { 00052 is_running = is_finished = false; 00053 } 00054 00055 VThread::~VThread() { 00056 } 00057 00058 /* @PARAM time defines waittime 00059 */ 00060 bool VThread::wait( unsigned long time) { 00061 if (!is_running || is_finished) 00062 return true; 00063 else { 00064 #ifndef WINCE 00065 dprintf_full(" VThread::wait for pthread join\n"); 00066 bool ret=pthread_join(my_thread, NULL); 00067 // dprintf_full(" VThread::wait for pthread join DONE\n"); 00068 #else 00069 while (is_running && !is_finished) Sleep(10); 00070 bool ret = true; 00071 #endif 00072 is_finished = true; 00073 is_running = false; 00074 return ret; 00075 } 00076 } 00077 00078 /* @returns if this thread's execution is done */ 00079 bool VThread::finished() const { 00080 return is_finished; 00081 } 00082 00083 /* @returns if this thread's execution is still proceeding */ 00084 bool VThread::running() const { 00085 return is_running; 00086 } 00087 00088 /* exits the currently running thread and informs all other waiting threads */ 00089 void VThread::exit() { 00090 if (is_running) { 00091 is_finished = true; 00092 is_running = false; 00093 #ifndef WINCE 00094 pthread_exit(NULL); 00095 #else 00096 if (my_thread != NULL) 00097 ExitThread(*my_thread); 00098 #endif 00099 } 00100 } 00101 00103 int VThread::setRoundRobinScheduling(int val) 00104 { 00105 #ifndef WINCE 00106 struct sched_param sp; 00107 sp.sched_priority = val; 00108 return pthread_setschedparam(pthread_self(), SCHED_RR, &sp); 00109 #else 00110 return 0; 00111 #endif 00112 } 00113 00114 /* starts the overwritten run() method 00115 */ 00116 void VThread::start() { 00117 #ifndef WINCE 00118 dprintf_full(" VThread::start new thread\n"); 00119 #endif 00120 00121 if (running()) { 00122 wait(0); 00123 } 00124 00125 #ifndef WINCE 00126 if (pthread_create(&my_thread, NULL, VThread::HelperFunction, this) != 0) 00127 perror("pthread start failed:"); 00128 #else 00129 CreateThread(NULL, 00130 0, 00131 (LPTHREAD_START_ROUTINE) VThread::HelperFunction, 00132 this, 00133 0, //CREATE_SUSPENDED, 00134 my_thread); 00135 //SetThreadPriority(my_thread, THREAD_PRIORITY_TIME_CRITICAL); 00136 #endif //wince 00137 is_running = true; 00138 is_finished = false; 00139 } 00140 00141 00142 #ifndef WINCE 00143 void * VThread::HelperFunction (void *args) 00144 #else 00145 DWORD WINAPI VThread::HelperFunction (void *args) 00146 #endif 00147 { 00148 VThread *pThis = static_cast<VThread *> (args); 00149 00150 pThis->setRunning(true); 00151 pThis->setFinished(false); 00152 00153 pThis->run(); 00154 00155 pThis->setRunning(false); 00156 pThis->setFinished(true); 00157 00158 #ifndef WINCE 00159 return NULL; 00160 #else 00161 return 0; 00162 #endif 00163 } 00164 00165 00166 #endif //enable pthread threads 00167 00168 00169