LoRWLock.C

00001 /**
00002    \file  Robots/LoBot/misc/LoRWLock.C
00003    \brief This file defines the non-inline member functions of the
00004    lobot::RWLock, lobot::AutoReadLock and lobot::AutoWriteLock classes.
00005 */
00006 
00007 // //////////////////////////////////////////////////////////////////// //
00008 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00009 // by the University of Southern California (USC) and the iLab at USC.  //
00010 // See http://iLab.usc.edu for information about this project.          //
00011 // //////////////////////////////////////////////////////////////////// //
00012 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00013 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00014 // in Visual Environments, and Applications'' by Christof Koch and      //
00015 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00016 // pending; application number 09/912,225 filed July 23, 2001; see      //
00017 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00018 // //////////////////////////////////////////////////////////////////// //
00019 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00022 // redistribute it and/or modify it under the terms of the GNU General  //
00023 // Public License as published by the Free Software Foundation; either  //
00024 // version 2 of the License, or (at your option) any later version.     //
00025 //                                                                      //
00026 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00027 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00028 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00029 // PURPOSE.  See the GNU General Public License for more details.       //
00030 //                                                                      //
00031 // You should have received a copy of the GNU General Public License    //
00032 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00033 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00034 // Boston, MA 02111-1307 USA.                                           //
00035 // //////////////////////////////////////////////////////////////////// //
00036 //
00037 // Primary maintainer for this file: mviswana usc edu
00038 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/thread/LoRWLock.C $
00039 // $Id: LoRWLock.C 13613 2010-06-23 23:29:23Z mviswana $
00040 //
00041 
00042 //---------------------- ALTERNATIVE DEFINITION -------------------------
00043 
00044 // In case pthreads is missing
00045 #ifndef INVT_HAVE_LIBPTHREAD
00046 
00047 #include "Robots/LoBot/thread/LoRWLock.H"
00048 #include "Robots/LoBot/misc/LoExcept.H"
00049 
00050 namespace lobot {
00051 
00052 // Constructors
00053 RWLock::RWLock()
00054    : m_lock(0)
00055 {
00056    throw missing_libs(MISSING_PTHREAD) ;
00057 }
00058 
00059 AutoReadLock::AutoReadLock(RWLock& L)
00060    : m_lock(L)
00061 {
00062    throw missing_libs(MISSING_PTHREAD) ;
00063 }
00064 
00065 AutoReadLock::AutoReadLock(const RWLock& L)
00066    : m_lock(const_cast<RWLock&>(L))
00067 {
00068    throw missing_libs(MISSING_PTHREAD) ;
00069 }
00070 
00071 AutoWriteLock::AutoWriteLock(RWLock& L)
00072    : m_lock(L)
00073 {
00074    throw missing_libs(MISSING_PTHREAD) ;
00075 }
00076 
00077 AutoWriteLock::AutoWriteLock(const RWLock& L)
00078    : m_lock(const_cast<RWLock&>(L))
00079 {
00080    throw missing_libs(MISSING_PTHREAD) ;
00081 }
00082 
00083 // Empty API
00084 void RWLock::begin_read() {}
00085 void RWLock::end_read()   {}
00086 void RWLock::begin_write(){}
00087 void RWLock::end_write()  {}
00088 
00089 // Destructors
00090 RWLock::~RWLock(){}
00091 AutoReadLock::~AutoReadLock(){}
00092 AutoWriteLock::~AutoWriteLock(){}
00093 
00094 } // end of namespace encapsulating above empty definition
00095 
00096 #else // pthreads available ==> the real McCoy
00097 
00098 //------------------------------ HEADERS --------------------------------
00099 
00100 // lobot headers
00101 #include "Robots/LoBot/thread/LoRWLock.H"
00102 #include "Robots/LoBot/misc/LoExcept.H"
00103 
00104 //----------------------------- NAMESPACE -------------------------------
00105 
00106 namespace lobot {
00107 
00108 //-------------------------- INITIALIZATION -----------------------------
00109 
00110 RWLock::RWLock()
00111 {
00112    if (pthread_rwlock_init(& m_lock, 0) != 0)
00113       throw thread_error(RWLOCK_INIT_ERROR) ;
00114 }
00115 
00116 AutoReadLock::AutoReadLock(RWLock& L)
00117    : m_lock(L)
00118 {
00119    m_lock.begin_read() ;
00120 }
00121 
00122 AutoReadLock::AutoReadLock(const RWLock& L)
00123    : m_lock(const_cast<RWLock&>(L))
00124 {
00125    m_lock.begin_read() ;
00126 }
00127 
00128 AutoWriteLock::AutoWriteLock(RWLock& L)
00129    : m_lock(L)
00130 {
00131    m_lock.begin_write() ;
00132 }
00133 
00134 AutoWriteLock::AutoWriteLock(const RWLock& L)
00135    : m_lock(const_cast<RWLock&>(L))
00136 {
00137    m_lock.begin_write() ;
00138 }
00139 
00140 //-------------------- READ/WRITE LOCK OPERATIONS -----------------------
00141 
00142 void RWLock::begin_read()
00143 {
00144    if (pthread_rwlock_rdlock(& m_lock) != 0)
00145       throw thread_error(RWLOCK_RDLOCK_FAILED) ;
00146 }
00147 
00148 void RWLock::end_read()
00149 {
00150    pthread_rwlock_unlock(& m_lock) ;
00151 }
00152 
00153 void RWLock::begin_write()
00154 {
00155    if (pthread_rwlock_wrlock(& m_lock) != 0)
00156       throw thread_error(RWLOCK_WRLOCK_FAILED) ;
00157 }
00158 
00159 void RWLock::end_write()
00160 {
00161    pthread_rwlock_unlock(& m_lock) ;
00162 }
00163 
00164 //----------------------------- CLEAN-UP --------------------------------
00165 
00166 RWLock::~RWLock()
00167 {
00168    pthread_rwlock_destroy(& m_lock) ;
00169 }
00170 
00171 AutoReadLock::~AutoReadLock()
00172 {
00173    m_lock.end_read() ;
00174 }
00175 
00176 AutoWriteLock::~AutoWriteLock()
00177 {
00178    m_lock.end_write() ;
00179 }
00180 
00181 //-----------------------------------------------------------------------
00182 
00183 } // end of namespace encapsulating this file's definitions
00184 
00185 #endif // INVT_HAVE_PTHREAD
00186 
00187 /* So things look consistent in everyone's emacs... */
00188 /* Local Variables: */
00189 /* indent-tabs-mode: nil */
00190 /* End: */
Generated on Sun May 8 08:41:31 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3