00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 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 /** @defgroup synch_grp Synchronization, multi-threading synch tools 00041 * \ingroup mrpt_base_grp */ 00042 00043 00044 /** This namespace provides multitask, synchronization utilities. \ingroup synch_grp 00045 */ 00046 namespace synch 00047 { 00048 /** This class provides simple critical sections functionality. 00049 * \sa CCriticalSectionLocker 00050 * \ingroup synch_grp 00051 */ 00052 class BASE_IMPEXP CCriticalSection 00053 { 00054 private: 00055 utils::CReferencedMemBlock m_data; //!< The OS-dependent descriptors 00056 00057 std::string m_name; 00058 public: 00059 /** Constructor 00060 */ 00061 CCriticalSection(const char *name = NULL); 00062 00063 /** Destructor 00064 */ 00065 ~CCriticalSection(); 00066 00067 /** Enter. 00068 * \exception If the calling thread already possesses this critical section (it would be a dead-lock). 00069 */ 00070 void enter() const; 00071 00072 /** Leave 00073 * \exception If the calling thread is not the current owener of the critical section. 00074 */ 00075 void leave() const; 00076 00077 /** Returns the name used in the constructor. */ 00078 std::string getName() const { return m_name; } 00079 00080 /** If set to a non-NULL value, debug messages regarding the calling threads IDs will be output. 00081 */ 00082 utils::CStream *m_debugOut; 00083 }; 00084 00085 /** A class acquiring a CCriticalSection at its constructor, and releasing it at destructor. 00086 * 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. 00087 * Example: 00088 * \code 00089 * { // Code in this scope is protected by critical section 00090 * CCriticalSectionLocker myCSLocker( &myCS ); 00091 * ... 00092 * } // End of code protected by critical section 00093 * \endcode 00094 * \sa CCriticalSection, THREADSAFE_OPERATION 00095 */ 00096 class BASE_IMPEXP CCriticalSectionLocker 00097 { 00098 protected: 00099 const CCriticalSection *m_cs; 00100 00101 public: 00102 /** Constructor: enters the critical section. 00103 */ 00104 CCriticalSectionLocker( const CCriticalSection *cs ); 00105 00106 CCriticalSectionLocker(const CCriticalSectionLocker &o) : m_cs(o.m_cs) 00107 { 00108 } 00109 00110 CCriticalSectionLocker & operator = (const CCriticalSectionLocker&o) 00111 { 00112 m_cs = o.m_cs; 00113 return *this; 00114 } 00115 00116 /** Destructor: leaves the critical section. 00117 */ 00118 ~CCriticalSectionLocker(); 00119 00120 }; // end of CCriticalSectionLocker 00121 00122 00123 00124 /** A macro for protecting a given piece of code with a critical section; for example: 00125 * \code 00126 * CCriticalSection cs; 00127 * MyObject obj; 00128 * ... 00129 * 00130 * THREADSAFE_OPERATION(cs, obj.foo(); ) 00131 * ... 00132 * THREADSAFE_OPERATION(cs, obj.foo(); obj.bar(); } 00133 * 00134 * \endcode 00135 * 00136 * \sa CCriticalSectionLocker, CThreadSafeVariable 00137 */ 00138 #define THREADSAFE_OPERATION(_CRITSECT_OBJ, CODE_TO_EXECUTE ) \ 00139 { \ 00140 mrpt::synch::CCriticalSectionLocker lock(&_CRITSECT_OBJ); \ 00141 CODE_TO_EXECUTE \ 00142 } 00143 00144 00145 } // End of namespace 00146 } // End of namespace 00147 00148 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |