VCondition.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: VCondition.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 00045 #include "VCondition.hpp" 00046 00047 00048 /*********************************************************************** 00049 Constructor-1 00050 ***********************************************************************/ 00051 VCondition::VCondition() 00052 { 00053 #ifdef WINCE 00054 #ifndef DISABLE_CONDITIONS 00055 cond = CreateEvent(NULL, 00056 true, //manual control 00057 true, //initial state is signaled 00058 NULL); 00059 #endif 00060 #endif 00061 } 00062 00063 00064 /*********************************************************************** 00065 Destructor 00066 ***********************************************************************/ 00067 VCondition::~VCondition() 00068 { 00069 destroy(); 00070 } 00071 00072 00073 /*********************************************************************** 00074 Initialize condition variable 00075 ***********************************************************************/ 00076 void VCondition::initialize() 00077 { 00078 #ifndef DISABLE_CONDITIONS 00079 #ifndef WINCE 00080 pthread_cond_init(&cond, NULL); 00081 #else 00082 ResetEvent(cond); //set event state to nonsignaled 00083 #endif 00084 #endif 00085 } 00086 00087 00088 /*********************************************************************** 00089 Send a signal to all waiting threads 00090 ***********************************************************************/ 00091 void VCondition::signal() 00092 { 00093 #ifndef DISABLE_CONDITIONS 00094 #ifndef WINCE 00095 pthread_cond_signal(&cond); 00096 #else 00097 SetEvent(cond); //set event state to signaled 00098 #endif 00099 #endif 00100 } 00101 00102 /*********************************************************************** 00103 Broadcast signal to all waiting threads 00104 ***********************************************************************/ 00105 void VCondition::broadcast() 00106 { 00107 #ifndef DISABLE_CONDITIONS 00108 #ifndef WINCE 00109 pthread_cond_broadcast(&cond); 00110 #endif 00111 #endif 00112 } 00113 00114 00115 /*********************************************************************** 00116 Wait for condition variable 00117 ***********************************************************************/ 00118 void VCondition::condWait() 00119 { 00120 #ifndef DISABLE_CONDITIONS 00121 #ifndef WINCE 00122 pthread_cond_wait(&cond, NULL); 00123 #else 00124 //wait for condition variable to be signaled 00125 WaitForSingleObject(cond, INFINITE); 00126 #endif 00127 #endif 00128 } 00129 00130 /*********************************************************************** 00131 Wait for condition variable (2nd version) 00132 ***********************************************************************/ 00133 #ifndef WINCE 00134 void VCondition::condWait(pthread_mutex_t *mutex) 00135 { 00136 #ifndef DISABLE_CONDITIONS 00137 pthread_cond_wait(&cond, mutex); 00138 #endif 00139 } 00140 #else 00141 void VCondition::condWait(HANDLE mutex) 00142 { 00143 condWait(); 00144 } 00145 #endif 00146 00147 00148 /*********************************************************************** 00149 Destroy lock object 00150 ***********************************************************************/ 00151 void VCondition::destroy() 00152 { 00153 #ifndef DISABLE_CONDITIONS 00154 #ifndef WINCE 00155 //pthread_cond_destroy(&cond); 00156 #else 00157 if (cond != NULL) { 00158 CloseHandle(cond); 00159 cond = NULL; 00160 } 00161 #endif 00162 #endif 00163 } 00164 00165