00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2011 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef mrpt_synch_criticalsection_H 00029 #define mrpt_synch_criticalsection_H 00030 00031 #include <mrpt/utils/utils_defs.h> 00032 #include <mrpt/utils/CStream.h> 00033 #include <mrpt/utils/CReferencedMemBlock.h> 00034 00035 /*--------------------------------------------------------------- 00036 Class 00037 ---------------------------------------------------------------*/ 00038 namespace mrpt 00039 { 00040 /** This namespace provides multitask, synchronization utilities. 00041 */ 00042 namespace synch 00043 { 00044 /** This class provides simple critical sections functionality. 00045 * \sa CCriticalSectionLocker 00046 */ 00047 class BASE_IMPEXP CCriticalSection 00048 { 00049 private: 00050 utils::CReferencedMemBlock m_data; //!< The OS-dependent descriptors 00051 00052 std::string m_name; 00053 public: 00054 /** Constructor 00055 */ 00056 CCriticalSection(const char *name = NULL); 00057 00058 /** Destructor 00059 */ 00060 ~CCriticalSection(); 00061 00062 /** Enter. 00063 * \exception If the calling thread already possesses this critical section (it would be a dead-lock). 00064 */ 00065 void enter() const; 00066 00067 /** Leave 00068 * \exception If the calling thread is not the current owener of the critical section. 00069 */ 00070 void leave() const; 00071 00072 /** Returns the name used in the constructor. */ 00073 std::string getName() const { return m_name; } 00074 00075 /** If set to a non-NULL value, debug messages regarding the calling threads IDs will be output. 00076 */ 00077 utils::CStream *m_debugOut; 00078 }; 00079 00080 /** A class acquiring a CCriticalSection at its constructor, and releasing it at destructor. 00081 * It is a better idea to always use CCriticalSectionLocker, since it is more secure in the case of possible exceptions, many different exit points from a function, etc.. : it will always release the critical section at the destructor. 00082 * Example: 00083 * \code 00084 * { // Code in this scope is protected by critical section 00085 * CCriticalSectionLocker myCSLocker( &myCS ); 00086 * ... 00087 * } // End of code protected by critical section 00088 * \endcode 00089 * \sa CCriticalSection, THREADSAFE_OPERATION 00090 */ 00091 class BASE_IMPEXP CCriticalSectionLocker 00092 { 00093 protected: 00094 const CCriticalSection *m_cs; 00095 00096 public: 00097 /** Constructor: enters the critical section. 00098 */ 00099 CCriticalSectionLocker( const CCriticalSection *cs ); 00100 00101 CCriticalSectionLocker(const CCriticalSectionLocker &o) : m_cs(o.m_cs) 00102 { 00103 } 00104 00105 CCriticalSectionLocker & operator = (const CCriticalSectionLocker&o) 00106 { 00107 m_cs = o.m_cs; 00108 return *this; 00109 } 00110 00111 /** Destructor: leaves the critical section. 00112 */ 00113 ~CCriticalSectionLocker(); 00114 00115 }; // end of CCriticalSectionLocker 00116 00117 00118 00119 /** A macro for protecting a given piece of code with a critical section; for example: 00120 * \code 00121 * CCriticalSection cs; 00122 * MyObject obj; 00123 * ... 00124 * 00125 * THREADSAFE_OPERATION(cs, obj.foo(); ) 00126 * ... 00127 * THREADSAFE_OPERATION(cs, obj.foo(); obj.bar(); } 00128 * 00129 * \endcode 00130 * 00131 * \sa CCriticalSectionLocker, CThreadSafeVariable 00132 */ 00133 #define THREADSAFE_OPERATION(_CRITSECT_OBJ, CODE_TO_EXECUTE ) \ 00134 { \ 00135 mrpt::synch::CCriticalSectionLocker lock(&_CRITSECT_OBJ); \ 00136 CODE_TO_EXECUTE \ 00137 } 00138 00139 00140 } // End of namespace 00141 } // End of namespace 00142 00143 #endif
Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011 |