LoShutdown.C

00001 /**
00002    \file  Robots/LoBot/misc/LoShutdown.C
00003    \brief This file defines the static data members and non-inline member
00004    functions of the lobot::Shutdown class.
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/LoShutdown.C $
00039 // $Id: LoShutdown.C 13521 2010-06-06 14:23:03Z mviswana $
00040 //
00041 
00042 //---------------------- ALTERNATIVE DEFINITION -------------------------
00043 
00044 // In case pthreads is missing
00045 #ifndef INVT_HAVE_LIBPTHREAD
00046 
00047 #include "Robots/LoBot/thread/LoShutdown.H"
00048 #include "Robots/LoBot/misc/LoExcept.H"
00049 
00050 namespace lobot {
00051 
00052 // Static data members
00053 sigset_t Shutdown::m_signals_mask ;
00054 bool     Shutdown::m_listening ;
00055 
00056 // Constructor
00057 Shutdown::Shutdown()
00058    : m_shutdown(false), m_shutdown_lock(0)
00059 {
00060    throw missing_libs(MISSING_PTHREAD) ;
00061 }
00062 
00063 // Empty API
00064 void Shutdown::run(){}
00065 void Shutdown::start_listening(){}
00066 bool Shutdown::signaled() {return false ;}
00067 void Shutdown::signal(){}
00068 
00069 // Destructor
00070 Shutdown::~Shutdown(){}
00071 
00072 } // end of namespace encapsulating above empty definition
00073 
00074 #else // pthreads available ==> the real McCoy
00075 
00076 //------------------------------ HEADERS --------------------------------
00077 
00078 // lobot headers
00079 #include "Robots/LoBot/thread/LoShutdown.H"
00080 #include "Robots/LoBot/misc/LoExcept.H"
00081 
00082 //----------------------------- NAMESPACE -------------------------------
00083 
00084 namespace lobot {
00085 
00086 //------------------------ STATIC DATA MEMBERS --------------------------
00087 
00088 // The signals to listen for (SIGINT, SIGHUP, SIGQUIT, SIGTERM, etc.)
00089 sigset_t Shutdown::m_signals_mask ;
00090 
00091 // Flag indicating whether listening for various termination signals has
00092 // begun or not.
00093 bool Shutdown::m_listening ;
00094 
00095 //-------------------------- INITIALIZATION -----------------------------
00096 
00097 void Shutdown::start_listening()
00098 {
00099    if (Thread::count() == 0 && ! m_listening) {
00100       sigemptyset(& m_signals_mask) ;
00101       sigaddset(& m_signals_mask, SIGINT) ;
00102       sigaddset(& m_signals_mask, SIGHUP) ;
00103       sigaddset(& m_signals_mask, SIGQUIT) ;
00104       sigaddset(& m_signals_mask, SIGTERM) ;
00105       if (pthread_sigmask(SIG_BLOCK, & m_signals_mask, 0) != 0)
00106          throw misc_error(SIGNALS_MASK_SETUP_FAILURE) ;
00107       instance() ; // force creation of shutdown thread
00108       m_listening = true ;
00109    }
00110 }
00111 
00112 Shutdown::Shutdown()
00113    : m_shutdown(false)
00114 {
00115    if (pthread_rwlock_init(& m_shutdown_lock, 0) != 0)
00116       throw thread_error(RWLOCK_INIT_ERROR) ;
00117    start("shutdown") ;
00118 }
00119 
00120 //------------------------ THE THREAD FUNCTION --------------------------
00121 
00122 void Shutdown::run()
00123 {
00124    for(;;)
00125    {
00126       int signum = 0 ;
00127       if (sigwait(& m_signals_mask, & signum) != 0)
00128          return ;
00129       switch (signum)
00130       {
00131          case SIGINT:
00132          case SIGHUP:
00133          case SIGQUIT:
00134          case SIGTERM:
00135             pthread_rwlock_wrlock(& m_shutdown_lock) ;
00136                m_shutdown = true ;
00137             pthread_rwlock_unlock(& m_shutdown_lock) ;
00138             return ;
00139          default:
00140             break ;
00141       }
00142    }
00143 }
00144 
00145 //---------------------- SHUTDOWN SIGNAL STATUS -------------------------
00146 
00147 bool Shutdown::signaled()
00148 {
00149    if (! instance().running())
00150       return true ; // dangerous if a thread calls before this one is up!
00151 
00152    bool shutdown_flag = false ;
00153    if (pthread_rwlock_rdlock(& instance().m_shutdown_lock) == 0) {
00154       shutdown_flag = instance().m_shutdown ;
00155       pthread_rwlock_unlock(& instance().m_shutdown_lock) ;
00156    }
00157    return shutdown_flag ;
00158 }
00159 
00160 void Shutdown::signal()
00161 {
00162    if (instance().running())
00163       pthread_kill(instance().id(), SIGTERM) ;
00164 }
00165 
00166 //----------------------------- CLEAN-UP --------------------------------
00167 
00168 Shutdown::~Shutdown()
00169 {
00170    pthread_rwlock_destroy(& m_shutdown_lock) ;
00171 }
00172 
00173 //-----------------------------------------------------------------------
00174 
00175 } // end of namespace encapsulating this file's definitions
00176 
00177 #endif // INVT_HAVE_PTHREAD
00178 
00179 /* So things look consistent in everyone's emacs... */
00180 /* Local Variables: */
00181 /* indent-tabs-mode: nil */
00182 /* End: */
Generated on Sun May 8 08:05:55 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3