LoRWLock.H

00001 /**
00002    \file  Robots/LoBot/misc/LoRWLock.H
00003    \brief An object-oriented wrapper around pthreads read/write locks.
00004 */
00005 
00006 // //////////////////////////////////////////////////////////////////// //
00007 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00008 // by the University of Southern California (USC) and the iLab at USC.  //
00009 // See http://iLab.usc.edu for information about this project.          //
00010 // //////////////////////////////////////////////////////////////////// //
00011 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00012 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00013 // in Visual Environments, and Applications'' by Christof Koch and      //
00014 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00015 // pending; application number 09/912,225 filed July 23, 2001; see      //
00016 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00017 // //////////////////////////////////////////////////////////////////// //
00018 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00019 //                                                                      //
00020 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00021 // redistribute it and/or modify it under the terms of the GNU General  //
00022 // Public License as published by the Free Software Foundation; either  //
00023 // version 2 of the License, or (at your option) any later version.     //
00024 //                                                                      //
00025 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00026 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00027 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00028 // PURPOSE.  See the GNU General Public License for more details.       //
00029 //                                                                      //
00030 // You should have received a copy of the GNU General Public License    //
00031 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00032 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00033 // Boston, MA 02111-1307 USA.                                           //
00034 // //////////////////////////////////////////////////////////////////// //
00035 //
00036 // Primary maintainer for this file: mviswana usc edu
00037 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/thread/LoRWLock.H $
00038 // $Id: LoRWLock.H 13613 2010-06-23 23:29:23Z mviswana $
00039 //
00040 
00041 #ifndef LOBOT_RWLOCK_DOT_H
00042 #define LOBOT_RWLOCK_DOT_H
00043 
00044 //------------------------------ HEADERS --------------------------------
00045 
00046 // POSIX threads
00047 #ifdef INVT_HAVE_LIBPTHREAD
00048 
00049 #include <pthread.h>
00050 
00051 #else // fake pthreads API to allow builds to succeed
00052 
00053 typedef int pthread_rwlock_t ;
00054 
00055 #endif
00056 
00057 //----------------------------- NAMESPACE -------------------------------
00058 
00059 namespace lobot {
00060 
00061 //------------------------- CLASS DEFINITION ----------------------------
00062 
00063 /**
00064    \class lobot::RWLock
00065    \brief A simple encapsulation of pthread read/write locks.
00066 */
00067 class RWLock {
00068    // Prevent copy and assignment
00069    RWLock(const RWLock&) ;
00070    RWLock& operator=(const RWLock&) ;
00071 
00072    /// The underlying pthread lock this class wraps around.
00073    pthread_rwlock_t m_lock ;
00074 
00075 public:
00076    /// Default constructor: sets up the pthread read/write lock object.
00077    RWLock() ;
00078 
00079    /// RWLock acquisition and release.
00080    //@{
00081    void begin_read() ;
00082    void end_read() ;
00083    void begin_write() ;
00084    void end_write() ;
00085    //@}
00086 
00087    /// Clean-up.
00088    ~RWLock() ;
00089 } ;
00090 
00091 //------------------------- CLASS DEFINITION ----------------------------
00092 
00093 /**
00094    \class lobot::AutoReadLock
00095    \brief Read locks created on the stack that acquire an "underlying"
00096    read/write lock for reading on initialization and release it when the
00097    object goes out of scope.
00098 */
00099 class AutoReadLock {
00100    // Prevent copy and assignment
00101    AutoReadLock(const AutoReadLock&) ;
00102    AutoReadLock& operator=(const AutoReadLock&) ;
00103 
00104    /// The underlying lock this object is "tied" to.
00105    RWLock& m_lock ;
00106 
00107 public:
00108    /// Constructor: will acquire the underlying lock object for reading.
00109    AutoReadLock(RWLock&) ;
00110 
00111    /// Constructor for const RWLock objects (e.g., when called from const
00112    /// member functions in client classes).
00113    AutoReadLock(const RWLock&) ;
00114 
00115    /// Clean-up: will release the underlying lock object.
00116    ~AutoReadLock() ;
00117 } ;
00118 
00119 //------------------------- CLASS DEFINITION ----------------------------
00120 
00121 /**
00122    \class lobot::AutoWriteLock
00123    \brief Write locks created on the stack that acquire an "underlying"
00124    read/write lock for writing on initialization and release it when the
00125    object goes out of scope.
00126 */
00127 class AutoWriteLock {
00128    // Prevent copy and assignment
00129    AutoWriteLock(const AutoWriteLock&) ;
00130    AutoWriteLock& operator=(const AutoWriteLock&) ;
00131 
00132    /// The underlying lock this object is "tied" to.
00133    RWLock& m_lock ;
00134 
00135 public:
00136    /// Constructor: will acquire the underlying lock object for writing.
00137    AutoWriteLock(RWLock&) ;
00138 
00139    /// Constructor for const RWLock objects (e.g., when called from const
00140    /// member functions in client classes).
00141    AutoWriteLock(const RWLock&) ;
00142 
00143    /// Clean-up: will release the underlying lock object.
00144    ~AutoWriteLock() ;
00145 } ;
00146 
00147 //-----------------------------------------------------------------------
00148 
00149 } // end of namespace encapsulating this file's definitions
00150 
00151 #endif
00152 
00153 /* So things look consistent in everyone's emacs... */
00154 /* Local Variables: */
00155 /* indent-tabs-mode: nil */
00156 /* End: */
Generated on Sun May 8 08:41:31 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3