OpenVideo Documentation

   Main Page       Modules       Class Hierarchy       Alphabetical List       Compound List       File List       Compound Members       Related Pages   

State.cxx

Go to the documentation of this file.
00001 /* ========================================================================
00002  * Copyright (C) 2005  Graz University of Technology
00003  *
00004  * This framework is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This framework is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this framework; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  * For further information please contact Denis Kalkofen under
00019  * <kalkofen@icg.tu-graz.ac.at> or write to Denis Kalkofen,
00020  * Graz University of Technology, Inffeldgasse 16a, A8010 Graz,
00021  * Austria.
00022  * ========================================================================
00023  * PROJECT: OpenVideo
00024  * ======================================================================== */
00034 #include <openvideo/State.h>
00035 #include <ace/Thread_Mutex.h>
00036 
00037 
00038 namespace openvideo {
00039 
00040 
00041 Buffer::Buffer() : buffer(NULL)
00042 {
00043     lockCtr = 0;
00044     updateCtr = 0;
00045     mutex = new ACE_Thread_Mutex;
00046     width = height = 0;
00047     format = FORMAT_UNKNOWN;
00048 }
00049 
00050 
00051 Buffer::~Buffer()
00052 {
00053     delete mutex;
00054     mutex = NULL;
00055 }
00056 
00057 
00058 void
00059 Buffer::lock()
00060 {
00061     mutex->acquire();
00062     lockCtr++;
00063     mutex->release();
00064 }
00065 
00066 
00067 void
00068 Buffer::unlock()
00069 {
00070     mutex->acquire();
00071     lockCtr--;
00072     assert(lockCtr>=0);
00073     mutex->release();
00074 }
00075 
00076 
00077 void
00078 State::unlockAllBuffers()
00079 {
00080     for(size_t i=0; i<buffers.size(); i++)
00081         buffers[i]->unlock();
00082 }
00083 
00084 
00085 Buffer*
00086 State::findFreeBuffer()
00087 {
00088     for(size_t i=0; i<buffers.size(); i++)
00089         if(!buffers[i]->isLocked())
00090             return buffers[i];
00091 
00092     return NULL;
00093 }
00094 
00095 
00096 int
00097 State::getNumLockedBuffers() const
00098 {
00099     int c=0;
00100 
00101     for(size_t i=0; i<buffers.size(); i++)
00102         if(buffers[i]->isLocked())
00103             c++;
00104 
00105     return c;
00106 }
00107 
00108 
00109 BufferSynchronizer::BufferSynchronizer()
00110 {
00111     buffer = NULL;
00112     mutex = new ACE_Thread_Mutex;
00113 }
00114 
00115 
00116 BufferSynchronizer::~BufferSynchronizer()
00117 {
00118     if(buffer)
00119         buffer->unlock();
00120     buffer = NULL;
00121 
00122     delete mutex;
00123     mutex = NULL;
00124 }
00125 
00126 
00127 void
00128 BufferSynchronizer::assign(Buffer* newBuffer)
00129 {
00130     if(buffer == newBuffer)
00131         return;
00132 
00133     mutex->acquire();
00134 
00135         Buffer* oldBuffer = buffer;
00136         if(newBuffer)
00137             newBuffer->lock();
00138         buffer = newBuffer;
00139         if(oldBuffer)
00140             oldBuffer->unlock();
00141 
00142     mutex->release();
00143 }
00144 
00145 
00146 Buffer*
00147 BufferSynchronizer::getLocked()
00148 {
00149     if(!buffer)
00150         return NULL;
00151 
00152     Buffer* retBuffer;
00153 
00154     mutex->acquire();
00155 
00156         retBuffer = buffer;
00157         retBuffer->lock();
00158 
00159     mutex->release();
00160 
00161     return retBuffer;
00162 }
00163 
00164 
00165 }  // namespace openvideo
 This page was generated at Wed May 31 13:04:16 2006 for OpenVideo by Doxygen.
 If you have any comments, please send a message to schmalstieg@icg.tu-graz.ac.at.
www.studierstube.org