TeMutex.h

Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright � 2001-2007 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00023 /*! \file TeMutex.h
00024   \brief This file contains definitions about a class to deal with critical 
00025   region locking.
00026 */
00027 
00028 
00029 #ifndef TEMUTEX_H
00030   #define TEMUTEX_H
00031   
00032   #include "TeAgnostic.h"
00033   #include "TeDefines.h"
00034   
00035   #if TePLATFORM == TePLATFORMCODE_MSWINDOWS
00036     #include <windows.h>
00037         #elif TePLATFORM == TePLATFORMCODE_LINUX || TePLATFORM == TePLATFORMCODE_AIX || TePLATFORM == TePLATFORMCODE_APPLE
00038     #include <pthread.h>
00039     #include <errno.h>
00040   #else
00041     #error "ERROR: Unsupported platform"
00042   #endif  
00043  
00044   /**
00045    * @brief A class to deal with critical region locking.
00046    * @author Emiliano F. Castejon <castejon@dpi.inpe.br>
00047    * @ingroup MultProgToolsGroup
00048    */
00049   class TL_DLL TeMutex
00050   {
00051     public :
00052     
00053       /**
00054        * @brief Default constructor.
00055        */    
00056       TeMutex();
00057 
00058       /**
00059        * @brief Default destructor.
00060        */       
00061       ~TeMutex();
00062       
00063       /**
00064        * @brief Lock the current object instance.
00065        * @note If section is already busy then the current thread will be 
00066        * blocked until it's ready again.
00067        */       
00068       inline void lock()
00069       {
00070         #if TePLATFORM == TePLATFORMCODE_MSWINDOWS
00071         
00072           DWORD return_value = 0;
00073           return_value = ::WaitForSingleObject( m_access_, INFINITE );
00074           TEAGN_DEBUG_CONDITION( ( ( return_value == WAIT_ABANDONED ) ||
00075             ( return_value == WAIT_OBJECT_0 ) ),
00076             "Unable to get mutex lock" );
00077         
00078                                 #elif TePLATFORM == TePLATFORMCODE_LINUX || TePLATFORM == TePLATFORMCODE_AIX || TePLATFORM == TePLATFORMCODE_APPLE
00079         
00080           pthread_mutex_lock( &m_access_ );
00081         
00082         #else
00083           #error "Unsuported plataform"
00084         #endif  
00085       };      
00086       
00087       /**
00088        * @brief Try to lock the current object instance.
00089        * @return true if OK, false if unable to lock.
00090        * @note If section is busy, this method will return false 
00091        * without blocking the current thread.
00092        */       
00093       inline bool tryLock()
00094       {
00095         #if TePLATFORM == TePLATFORMCODE_MSWINDOWS
00096         
00097           DWORD return_value = ::WaitForSingleObject( m_access_, 
00098             10 );
00099 
00100           if( ( return_value == WAIT_OBJECT_0 ) ||
00101             ( return_value == WAIT_ABANDONED ) ) {
00102 
00103             return true;
00104           } else {
00105             return false;
00106           }
00107         
00108                                 #elif TePLATFORM == TePLATFORMCODE_LINUX || TePLATFORM == TePLATFORMCODE_AIX || TePLATFORM == TePLATFORMCODE_APPLE
00109 
00110         
00111           if( pthread_mutex_trylock( &m_access_ ) == EBUSY ) {
00112             return false;
00113           } else {
00114             return true;
00115           }
00116         
00117         #else
00118           #error "Unsuported plataform"
00119         #endif  
00120       };      
00121       
00122       /**
00123        * @brief Unlock the current object instance.
00124        */       
00125       inline void unLock()
00126       {
00127         #if TePLATFORM == TePLATFORMCODE_MSWINDOWS
00128         
00129           ::ReleaseMutex( m_access_ );
00130         
00131                                 #elif TePLATFORM == TePLATFORMCODE_LINUX || TePLATFORM == TePLATFORMCODE_AIX || TePLATFORM == TePLATFORMCODE_APPLE
00132 
00133         
00134           pthread_mutex_unlock( &m_access_ );
00135         
00136         #else
00137           #error "Unsuported plataform"
00138         #endif 
00139       };      
00140             
00141     protected :
00142    
00143       #if TePLATFORM == TePLATFORMCODE_MSWINDOWS
00144     
00145         /**
00146         * @brief The mutex instance.
00147         */      
00148         HANDLE m_access_;
00149         
00150         /**
00151         * @brief The mutex instance attributes.
00152         */           
00153         SECURITY_ATTRIBUTES m_sa_;
00154       
00155                         #elif TePLATFORM == TePLATFORMCODE_LINUX || TePLATFORM == TePLATFORMCODE_AIX || TePLATFORM == TePLATFORMCODE_APPLE
00156 
00157       
00158         /**
00159         * @brief The mutex instance.
00160         */        
00161         pthread_mutex_t m_access_;
00162       
00163       #else
00164         #error "Unsuported plataform"
00165       #endif   
00166    
00167     private :
00168     
00169       /**
00170        * Alternative constructor.
00171        */    
00172       TeMutex(  const TeMutex& ) {};
00173     
00174    
00175       /**
00176        * operator= overload.
00177        * @return A const reference to the current instance.
00178        */      
00179       const TeMutex& operator=( const TeMutex& ) { return *this; };
00180  
00181   };
00182 
00183 #endif

Generated on Sun Jul 29 04:01:22 2012 for TerraLib - Development Source by  doxygen 1.5.3