ReferenceCounter.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: ReferenceCounter.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 #include "ReferenceCounter.hpp" 00045 #include "global.hpp" 00046 #include "debug.hpp" 00047 #include <stdio.h> 00048 00049 ReferenceCounter::ReferenceCounter() 00050 { 00051 usageCnt=0; 00052 mutex.initialize("ReferenceCounterMutex"); 00053 valid=true; 00054 }; 00055 00056 ReferenceCounter::~ReferenceCounter() 00057 { 00058 //dprintf_full("ReferenceCounter::~ReferenceCounter()\n"); 00059 mutex.lock(); 00060 mutex.destroy(); 00061 }; 00062 00063 void ReferenceCounter::reset() 00064 { 00065 lock(); 00066 usageCnt = 0; 00067 valid = true; 00068 unlock(); 00069 } 00070 00072 bool ReferenceCounter::increase() 00073 { 00074 bool ret=false; 00075 lock(); 00076 if(valid) { 00077 usageCnt++; 00078 ret=true; 00079 } 00080 unlock(); 00081 return ret; 00082 }; 00083 00085 bool ReferenceCounter::decrease() 00086 { 00087 bool ret=false; 00088 00089 lock(); 00090 if(valid && usageCnt>0) { 00091 usageCnt--; 00092 ret=true; 00093 } 00094 unlock(); 00095 return ret; 00096 }; 00097 00099 void ReferenceCounter::deactivate() 00100 { 00101 lock(); 00102 valid=false; usageCnt=0; 00103 unlock(); 00104 }; 00105 00106 unsigned int ReferenceCounter::getUsage() const 00107 { 00108 unsigned int res=0; 00109 lock(); 00110 res=usageCnt; 00111 unlock(); 00112 return res; 00113 }; 00114 00115 void ReferenceCounter::lock() const 00116 { 00117 mutex.lock(); 00118 }; 00119 00120 void ReferenceCounter::unlock() const 00121 { 00122 mutex.release(); 00123 }; 00124 00125 bool ReferenceCounter::deactivateWhenUsage(unsigned int i) 00126 { 00127 lock(); 00128 if(usageCnt<=i) { 00129 valid=false; 00130 usageCnt=0; 00131 } 00132 unlock(); 00133 return !valid; 00134 }; 00135 00136 bool ReferenceCounter::decreaseAndDeactivateWhenUsage(unsigned int i) 00137 { 00138 lock(); 00139 if(valid && usageCnt>0) { 00140 usageCnt--; 00141 } 00142 if(usageCnt<=i) { 00143 valid=false; 00144 } 00145 unlock(); 00146 return !valid; 00147 };