LoUpdateLock.H

00001 /**
00002    \file  Robots/LoBot/misc/LoUpdateLock.H
00003    \brief An object to help coordinate read-write accesses to various
00004    sensor and other objects within the Robolocust system.
00005 
00006    This file defines a class that implements a reader-writer lock meant
00007    to be shared by the main thread and the different behaviours that make
00008    up the Robolocust controller. The main thread updates the video I/O
00009    objects, the laser range finder, the locust LGMD models, integration
00010    algorithm, etc. The behaviours simply retrieve the latest values from
00011    these objects.
00012 */
00013 
00014 // //////////////////////////////////////////////////////////////////// //
00015 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00016 // by the University of Southern California (USC) and the iLab at USC.  //
00017 // See http://iLab.usc.edu for information about this project.          //
00018 // //////////////////////////////////////////////////////////////////// //
00019 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00020 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00021 // in Visual Environments, and Applications'' by Christof Koch and      //
00022 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00023 // pending; application number 09/912,225 filed July 23, 2001; see      //
00024 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00025 // //////////////////////////////////////////////////////////////////// //
00026 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00027 //                                                                      //
00028 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00029 // redistribute it and/or modify it under the terms of the GNU General  //
00030 // Public License as published by the Free Software Foundation; either  //
00031 // version 2 of the License, or (at your option) any later version.     //
00032 //                                                                      //
00033 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00034 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00035 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00036 // PURPOSE.  See the GNU General Public License for more details.       //
00037 //                                                                      //
00038 // You should have received a copy of the GNU General Public License    //
00039 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00040 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00041 // Boston, MA 02111-1307 USA.                                           //
00042 // //////////////////////////////////////////////////////////////////// //
00043 //
00044 // Primary maintainer for this file: mviswana usc edu
00045 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/thread/LoUpdateLock.H $
00046 // $Id: LoUpdateLock.H 13521 2010-06-06 14:23:03Z mviswana $
00047 //
00048 
00049 #ifndef LOBOT_UPDATE_LOCK_DOT_H
00050 #define LOBOT_UPDATE_LOCK_DOT_H
00051 
00052 //------------------------------ HEADERS --------------------------------
00053 
00054 // lobot headers
00055 #include "Robots/LoBot/misc/singleton.hh"
00056 
00057 // POSIX threads
00058 #ifdef INVT_HAVE_LIBPTHREAD
00059 
00060 #include <pthread.h>
00061 
00062 #else // fake pthreads API to allow builds to succeed
00063 
00064 typedef int pthread_rwlock_t ;
00065 
00066 #endif
00067 
00068 //----------------------------- NAMESPACE -------------------------------
00069 
00070 namespace lobot {
00071 
00072 //------------------------- CLASS DEFINITION ----------------------------
00073 
00074 /**
00075    \class lobot::UpdateLock
00076    \brief An object to help coordinate read-write accesses to various
00077    shared objects within the Robolocust controller.
00078 
00079    This class implements a reader-writer lock meant to be shared by the
00080    Robolocust controller's main thread and each of its behaviours. The
00081    main thread is responsible for updating the different sensors (laser
00082    range finder, cameras, etc.) and other objects (LGMD models,
00083    integration algorithm, etc.). The individual behaviours simply
00084    retrieve the latest values from these objects.
00085 
00086    Each thread must take care of using this lock appropriately when
00087    required. That is, the objects themselves do not perform any locking
00088    (in other words, they are not thread-safe).
00089 */
00090 class UpdateLock : public singleton<UpdateLock> {
00091    // Prevent copy and assignment
00092    UpdateLock(const UpdateLock&) ;
00093    UpdateLock& operator=(const UpdateLock&) ;
00094 
00095    // Boilerplate code to make the generic singleton design pattern work
00096    friend class singleton<UpdateLock> ;
00097 
00098    /// The reader-writer lock used to synchronize access.
00099    pthread_rwlock_t m_update_lock ;
00100 
00101    /// A private constructor because this class is a singleton.
00102    UpdateLock() ;
00103 
00104 public:
00105    /// The locking functions.
00106    //@{
00107    void read_lock() ;
00108    void write_lock() ;
00109    void unlock() ;
00110    //@}
00111 
00112    /// Convenience functions.
00113    //@{
00114    static void begin_read()  {instance().read_lock() ;}
00115    static void end_read()    {instance().unlock() ;}
00116    static void begin_write() {instance().write_lock() ;}
00117    static void end_write()   {instance().unlock() ;}
00118    //@}
00119 
00120    /// Clean-up.
00121    ~UpdateLock() ;
00122 } ;
00123 
00124 //-----------------------------------------------------------------------
00125 
00126 } // end of namespace encapsulating this file's definitions
00127 
00128 #endif
00129 
00130 /* So things look consistent in everyone's emacs... */
00131 /* Local Variables: */
00132 /* indent-tabs-mode: nil */
00133 /* End: */
Generated on Sun May 8 08:41:31 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3