LoPause.H

00001 /**
00002    \file  Robots/LoBot/misc/LoPause.H
00003    \brief An object to signal application pauses.
00004 
00005    This file defines a class that implements a pause signal for the
00006    Robolocust application. When this signal is set, all threads should
00007    cease their normal processing, i.e., stop reading sensors, issuing
00008    motor commands, updating the UI, etc. until the signal is cleared.
00009 
00010    Basically, this class just holds a Boolean flag protected with a
00011    read/write lock.
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/LoPause.H $
00046 // $Id: LoPause.H 13567 2010-06-13 15:58:59Z mviswana $
00047 //
00048 
00049 #ifndef LOBOT_PAUSE_DOT_H
00050 #define LOBOT_PAUSE_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::Pause
00076    \brief A Boolean flag to indicate whether threads should work as usual
00077    or wait until the application is unpaused.
00078 
00079    This class holds a Boolean flag indicating whether the different
00080    Robolocust threads should suspend their normal operations or proceed
00081    normally. When this flag is set, all threads are required to stop
00082    doing their normal tasks such as reading sensors, issuing motor
00083    commands, etc. and idle until the pause flag is cleared.
00084 
00085    Basically, this class serves to encapsulate an application-wide bool
00086    variable that is to be used in conjunction with a read/write lock.
00087 */
00088 class Pause : public singleton<Pause> {
00089    // Prevent copy and assignment
00090    Pause(const Pause&) ;
00091    Pause& operator=(const Pause&) ;
00092 
00093    // Boilerplate code to make the generic singleton design pattern work
00094    friend class singleton<Pause> ;
00095 
00096    /// The pause object's raison d'etre: indicate to all Robolocust
00097    /// threads whether they should quietly idle or carry on with their
00098    /// usual tasks.
00099    bool m_flag ;
00100 
00101    /// Since the above flag can be accessed by multiple threads, we need
00102    /// to synchronize accesses to it. More threads will read this
00103    /// variable than write to it. Therefore, a reader-writer lock (rather
00104    /// than a mutex) is an appropriate synchronization mechanism.
00105    pthread_rwlock_t m_lock ;
00106 
00107    /// A private constructor because this class is a singleton.
00108    Pause() ;
00109 
00110 public:
00111    /// Allow a thread to signal all other threads that they should pause,
00112    /// i.e., suspend normal operations and idle.
00113    static void set() ;
00114 
00115    /// Allow a thread to signal all other threads that they should stop
00116    /// idling and resume normal operations.
00117    static void clear() ;
00118 
00119    /// Allow a thread to toggle the application's current pause state.
00120    static void toggle() ;
00121 
00122    /// This function returns true if the application is currently paused.
00123    static bool is_set() ;
00124 
00125    /// This function returns true if the application is currently not
00126    /// paused.
00127    static bool is_clear() {return ! is_set() ;}
00128 
00129    /// Clean-up.
00130    ~Pause() ;
00131 } ;
00132 
00133 //-----------------------------------------------------------------------
00134 
00135 } // end of namespace encapsulating this file's definitions
00136 
00137 #endif
00138 
00139 /* So things look consistent in everyone's emacs... */
00140 /* Local Variables: */
00141 /* indent-tabs-mode: nil */
00142 /* End: */
Generated on Sun May 8 08:41:31 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3