VMutex.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: VMutex.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 "VMutex.hpp" 00046 00047 00048 /*********************************************************************** 00049 Constructor-1 00050 ***********************************************************************/ 00051 VMutex::VMutex() 00052 { 00053 initialize(NULL); 00054 } 00055 00056 /*********************************************************************** 00057 Constructor-2 00058 ***********************************************************************/ 00059 VMutex::VMutex(char *name) 00060 { 00061 initialize(name); 00062 } 00063 00064 00065 /*********************************************************************** 00066 Destructor 00067 ***********************************************************************/ 00068 VMutex::~VMutex() 00069 { 00070 destroy(); 00071 #ifdef WINCE 00072 delete mutexname; 00073 #endif 00074 } 00075 00076 00077 /*********************************************************************** 00078 Initialize mutex variable 00079 ***********************************************************************/ 00080 void VMutex::initialize(char *name) 00081 { 00082 #ifndef DISABLE_MUTEXES 00083 #ifndef WINCE 00084 if (pthread_mutex_init(&mutex,0) != 0) { 00085 dprintf_err("\nVMutex::initialize: cannot create mutex_ \n"); 00086 } 00087 #else 00088 mutexname = new char[MAX_PATH]; 00089 sprintf(mutexname, "%s %d %d %d %d %d", name, Random(), Random(), Random(), Random(), Random()); 00090 00091 mutex = CreateMutex(NULL, false, (LPCTSTR) mutexname); 00092 if (mutex == NULL) { 00093 dprintf_err("\nVMutex::initialize: cannot create mutex %s : %d\n", mutexname, mutex); 00094 } 00095 #endif 00096 #endif 00097 } 00098 00099 00100 /*********************************************************************** 00101 Wait for lock on mutex variable 00102 ***********************************************************************/ 00103 void VMutex::lock() 00104 { 00105 #ifndef DISABLE_MUTEXES 00106 #ifndef WINCE 00107 pthread_mutex_lock(&mutex); 00108 #else 00109 WaitForSingleObject(mutex, INFINITE); 00110 #endif 00111 #endif 00112 } 00113 00114 /*********************************************************************** 00115 Try to get lock on mutex variable (if locked, return immediately) 00116 ***********************************************************************/ 00117 void VMutex::tryLock() 00118 { 00119 #ifndef DISABLE_MUTEXES 00120 #ifndef WINCE 00121 pthread_mutex_trylock(&mutex); 00122 #else 00123 WaitForSingleObject(mutex, 0); 00124 #endif 00125 #endif 00126 } 00127 00128 00129 /*********************************************************************** 00130 Release lock on mutex variable 00131 ***********************************************************************/ 00132 void VMutex::release() 00133 { 00134 #ifndef DISABLE_MUTEXES 00135 #ifndef WINCE 00136 pthread_mutex_unlock(&mutex); 00137 #else 00138 ReleaseMutex(mutex); 00139 #endif 00140 #endif 00141 } 00142 00143 00144 /*********************************************************************** 00145 Destroy lock object 00146 ***********************************************************************/ 00147 void VMutex::destroy() 00148 { 00149 #ifndef DISABLE_MUTEXES 00150 #ifndef WINCE 00151 pthread_mutex_destroy(&mutex); 00152 #else 00153 if (mutex != NULL) { 00154 CloseHandle(mutex); 00155 mutex = NULL; 00156 } 00157 #endif 00158 #endif 00159 } 00160 00161 /*********************************************************************** 00162 Return internally used mutex variable 00163 ***********************************************************************/ 00164 #ifndef WINCE 00165 //pthread_mutex_t &VMutex::getMutexObject() 00166 pthread_mutex_t *VMutex::getMutexObject() 00167 #else 00168 HANDLE VMutex::getMutexObject() 00169 #endif 00170 { 00171 #ifndef WINCE 00172 return &mutex; 00173 #else 00174 return mutex; 00175 #endif 00176 }