00001 /** @file rutz/mutex.h */ 00002 00003 /////////////////////////////////////////////////////////////////////// 00004 // 00005 // Copyright (c) 2005-2007 University of Southern California 00006 // Rob Peters <rjpeters at usc dot edu> 00007 // 00008 // created: Tue Sep 13 09:15:02 2005 00009 // commit: $Id: mutex.h 8249 2007-04-12 06:03:40Z rjpeters $ 00010 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/mutex.h $ 00011 // 00012 // -------------------------------------------------------------------- 00013 // 00014 // This file is part of GroovX. 00015 // [http://www.klab.caltech.edu/rjpeters/groovx/] 00016 // 00017 // GroovX is free software; you can redistribute it and/or modify it 00018 // under the terms of the GNU General Public License as published by 00019 // the Free Software Foundation; either version 2 of the License, or 00020 // (at your option) any later version. 00021 // 00022 // GroovX is distributed in the hope that it will be useful, but 00023 // WITHOUT ANY WARRANTY; without even the implied warranty of 00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00025 // General Public License for more details. 00026 // 00027 // You should have received a copy of the GNU General Public License 00028 // along with GroovX; if not, write to the Free Software Foundation, 00029 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00030 // 00031 /////////////////////////////////////////////////////////////////////// 00032 00033 #ifndef GROOVX_RUTZ_MUTEX_H_UTC20050913161502_DEFINED 00034 #define GROOVX_RUTZ_MUTEX_H_UTC20050913161502_DEFINED 00035 00036 #include <pthread.h> 00037 00038 namespace rutz 00039 { 00040 class mutex_lock_class; 00041 class mutex_unlocker; 00042 } 00043 00044 //! Quick mutex locking class 00045 /** Use this when we don't want to use bare pthread_mutex_lock() and 00046 pthread_mutex_unlock() calls, because they aren't 00047 exception-safe. Use the GVX_MUTEX_LOCK() macro to create a local 00048 temporary lock object. */ 00049 class rutz::mutex_lock_class 00050 { 00051 public: 00052 /// Throws an exception if pthread_mutex_lock() fails 00053 mutex_lock_class(pthread_mutex_t* mut = 0); 00054 00055 ~mutex_lock_class() throw() 00056 { 00057 this->unlock(); 00058 } 00059 00060 bool is_locked() const throw() { return m_mutex != 0; } 00061 00062 void unlock() throw(); 00063 00064 void swap(mutex_lock_class& that) throw() 00065 { 00066 pthread_mutex_t* const this_mutex = this->m_mutex; 00067 this->m_mutex = that.m_mutex; 00068 that.m_mutex = this_mutex; 00069 } 00070 00071 private: 00072 pthread_mutex_t* m_mutex; 00073 00074 mutex_lock_class(const mutex_lock_class&); // not implemented 00075 mutex_lock_class& operator=(const mutex_lock_class&); // not implemented 00076 }; 00077 00078 #define GVX_MUTEX_CONCAT2(x,y) x##y 00079 #define GVX_MUTEX_CONCAT(x,y) GVX_MUTEX_CONCAT2(x,y) 00080 00081 #define GVX_MUTEX_LOCK(x) \ 00082 ::rutz::mutex_lock_class GVX_MUTEX_CONCAT(anonymous_lock,__LINE__) (x) 00083 00084 static const char __attribute__((used)) vcid_groovx_rutz_mutex_h_utc20050913161502[] = "$Id: mutex.h 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/rutz/mutex.h $"; 00085 #endif // !GROOVX_RUTZ_MUTEX_H_UTC20050913161502DEFINED