00001 /** 00002 \file Robots/LoBot/misc/LoMutex.H 00003 \brief An object-oriented wrapper around pthreads mutexes. 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/LoMutex.H $ 00038 // $Id: LoMutex.H 13521 2010-06-06 14:23:03Z mviswana $ 00039 // 00040 00041 #ifndef LOBOT_MUTEX_DOT_H 00042 #define LOBOT_MUTEX_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_mutex_t ; 00054 00055 #endif 00056 00057 //----------------------------- NAMESPACE ------------------------------- 00058 00059 namespace lobot { 00060 00061 //------------------------- CLASS DEFINITION ---------------------------- 00062 00063 /** 00064 \class lobot::Mutex 00065 \brief A simple encapsulation of pthread mutexes. 00066 */ 00067 class Mutex { 00068 // Prevent copy and assignment 00069 Mutex(const Mutex&) ; 00070 Mutex& operator=(const Mutex&) ; 00071 00072 /// The underlying pthread mutex this class wraps around. 00073 pthread_mutex_t m_mutex ; 00074 00075 public: 00076 /// Default constructor: sets up the pthread mutex object. 00077 Mutex() ; 00078 00079 /// Mutex acquisition and release. 00080 //@{ 00081 void acquire() ; 00082 void release() ; 00083 00084 void lock() {acquire() ;} 00085 void unlock() {release() ;} 00086 //@} 00087 00088 /// Clean-up. 00089 ~Mutex() ; 00090 } ; 00091 00092 //------------------------- CLASS DEFINITION ---------------------------- 00093 00094 /** 00095 \class lobot::AutoMutex 00096 \brief Mutexes created on the stack that acquire an "underlying" mutex 00097 on initialization and release it when the object goes out of scope. 00098 */ 00099 class AutoMutex { 00100 // Prevent copy and assignment 00101 AutoMutex(const AutoMutex&) ; 00102 AutoMutex& operator=(const AutoMutex&) ; 00103 00104 /// The underlying mutex this object is "tied" to. 00105 Mutex& m_mutex ; 00106 00107 public: 00108 /// Constructor: will acquire the underlying mutex object. 00109 AutoMutex(Mutex&) ; 00110 00111 /// Constructor for const Mutex objects (e.g., when called from const 00112 /// member functions in client classes). 00113 AutoMutex(const Mutex&) ; 00114 00115 /// Clean-up: will release the underlying mutex object. 00116 ~AutoMutex() ; 00117 } ; 00118 00119 //----------------------------------------------------------------------- 00120 00121 } // end of namespace encapsulating this file's definitions 00122 00123 #endif 00124 00125 /* So things look consistent in everyone's emacs... */ 00126 /* Local Variables: */ 00127 /* indent-tabs-mode: nil */ 00128 /* End: */