Semaphore.H

Go to the documentation of this file.
00001 /*!@file Util/Semaphore.H */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005   //
00005 // by the University of Southern California (USC) and the iLab at USC.  //
00006 // See http://iLab.usc.edu for information about this project.          //
00007 // //////////////////////////////////////////////////////////////////// //
00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00010 // in Visual Environments, and Applications'' by Christof Koch and      //
00011 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00012 // pending; application number 09/912,225 filed July 23, 2001; see      //
00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00014 // //////////////////////////////////////////////////////////////////// //
00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00016 //                                                                      //
00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00018 // redistribute it and/or modify it under the terms of the GNU General  //
00019 // Public License as published by the Free Software Foundation; either  //
00020 // version 2 of the License, or (at your option) any later version.     //
00021 //                                                                      //
00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00025 // PURPOSE.  See the GNU General Public License for more details.       //
00026 //                                                                      //
00027 // You should have received a copy of the GNU General Public License    //
00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00030 // Boston, MA 02111-1307 USA.                                           //
00031 // //////////////////////////////////////////////////////////////////// //
00032 //
00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/Semaphore.H $
00035 // $Id: Semaphore.H 13883 2010-09-04 05:13:13Z dberg $
00036 //
00037 
00038 #ifndef UTIL_SEMAPHORE_H_DEFINED
00039 #define UTIL_SEMAPHORE_H_DEFINED
00040 
00041 #include "Util/log.H"
00042 
00043 #include <cerrno>
00044 #include <pthread.h>
00045 #include <semaphore.h> // for sem_t
00046 
00047 class PosixSemaphore;
00048 class CondVarSemaphore;
00049 
00050 #ifdef HAVE_WORKING_POSIX_SEMAPHORES
00051 typedef PosixSemaphore Semaphore;
00052 #else
00053 typedef CondVarSemaphore Semaphore;
00054 #endif
00055 
00056 /// Wrapper around posix sem_t semaphore
00057 class PosixSemaphore
00058 {
00059 public:
00060   PosixSemaphore(unsigned int initvalue = 0)
00061   {
00062     if (0 != sem_init(&itsSem, 0, initvalue))
00063       PLFATAL("sem_init() failed");
00064   }
00065 
00066   ~PosixSemaphore()
00067   {
00068     if (0 != sem_destroy(&itsSem))
00069       PLERROR("sem_destroy() failed");
00070   }
00071 
00072   /// Returns true on success
00073   bool post()
00074   {
00075     if (0 != sem_post(&itsSem))
00076       {
00077         // Don't PLFATAL() here because we may call post from
00078         // destructor chains:
00079         PLERROR("sem_post() failed");
00080         return false;
00081       }
00082     return true;
00083   }
00084 
00085   //return true if a lock was performed
00086   bool trywait()
00087   {
00088     errno = 0;
00089     if(0 == sem_trywait(&itsSem))
00090       return true;
00091     else
00092       {
00093         if(errno == EAGAIN)
00094           {
00095             errno = 0;
00096           }
00097         else
00098           {
00099             PLERROR("sem_trywait() failed");
00100           }
00101         return false;
00102       }
00103   }
00104 
00105   /// Returns true on success
00106   bool wait()
00107   {
00108     errno = 0;
00109     while (0 != sem_wait(&itsSem))
00110       {
00111         if (errno == EINTR)
00112           {
00113             PLERROR("sem_wait() failed; retrying... ");
00114             continue;
00115           }
00116         else
00117           {
00118             PLERROR("sem_wait() failed");
00119             return false;
00120           }
00121       }
00122     return true;
00123   }
00124 
00125 private:
00126   PosixSemaphore(const PosixSemaphore&); // not implemented
00127   PosixSemaphore& operator=(const PosixSemaphore&); // not implemented
00128 
00129   sem_t itsSem;
00130 };
00131 
00132 class CondVarSemaphore
00133 {
00134 public:
00135   CondVarSemaphore(unsigned int initvalue = 0)
00136     :
00137     itsCounter(initvalue)
00138   {
00139     if (0 != pthread_mutex_init(&itsMutex, NULL))
00140       PLFATAL("pthread_mutex_init() failed");
00141 
00142     if (0 != pthread_cond_init(&itsCond, NULL))
00143       PLFATAL("pthread_cond_init() failed");
00144   }
00145 
00146   ~CondVarSemaphore()
00147   {
00148     if (0 != pthread_cond_destroy(&itsCond))
00149       PLERROR("pthread_cond_destroy() failed");
00150 
00151     if (0 != pthread_mutex_destroy(&itsMutex))
00152       PLERROR("pthread_mutex_destroy() failed");
00153   }
00154 
00155   bool post()
00156   {
00157     bool retval = true;
00158 
00159     if (0 != pthread_mutex_lock(&itsMutex)) retval = false;
00160     ++itsCounter;
00161     if (0 != pthread_cond_signal(&itsCond)) retval = false;
00162     if (0 != pthread_mutex_unlock(&itsMutex)) retval = false;
00163 
00164     return retval;
00165   }
00166 
00167   bool trywait()
00168   {
00169     bool retval = true;
00170     if (0 != pthread_mutex_lock(&itsMutex)) retval = false;
00171     
00172     if (itsCounter > 0)
00173       --itsCounter;
00174     
00175     if (0 != pthread_mutex_unlock(&itsMutex)) retval = false;
00176     return retval;
00177   }
00178   
00179   bool wait()
00180   {
00181     bool retval = true;
00182 
00183     if (0 != pthread_mutex_lock(&itsMutex)) retval = false;
00184 
00185     while (itsCounter == 0)
00186       if (0 != pthread_cond_wait(&itsCond, &itsMutex)) retval = false;
00187 
00188     --itsCounter;
00189 
00190     if (0 != pthread_mutex_unlock(&itsMutex)) retval = false;
00191 
00192     return retval;
00193   }
00194 
00195 private:
00196   CondVarSemaphore(const CondVarSemaphore&); // not implemented
00197   CondVarSemaphore& operator=(const CondVarSemaphore&); // not implemented
00198 
00199   volatile unsigned int itsCounter;
00200   pthread_cond_t itsCond;
00201   pthread_mutex_t itsMutex;
00202 };
00203 
00204 // ######################################################################
00205 /* So things look consistent in everyone's emacs... */
00206 /* Local Variables: */
00207 /* mode: c++ */
00208 /* indent-tabs-mode: nil */
00209 /* End: */
00210 
00211 #endif // UTIL_SEMAPHORE_H_DEFINED
Generated on Sun May 8 08:06:59 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3