1 /* 2 ================================================================================ 3 PROJECT: 4 5 Eddy C++ Thread Safety Project 6 7 CONTENTS: 8 9 Definition of class mutex_lock. 10 11 NOTES: 12 13 See notes under Class Definition section of this file. 14 15 PROGRAMMERS: 16 17 John Eddy (jpeddy@sandia.gov) (JE) 18 19 ORGANIZATION: 20 21 Sandia National Laboratories 22 23 COPYRIGHT: 24 25 See the LICENSE file in the top level JEGA directory. 26 27 VERSION: 28 29 1.0.0 30 31 CHANGES: 32 33 Sat Sep 13 14:02:10 2003 - Original Version (JE) 34 35 ================================================================================ 36 */ 37 38 39 40 41 /* 42 ================================================================================ 43 Document This File 44 ================================================================================ 45 */ 46 /** \file 47 * \brief Contains the definition of the mutex_lock class. 48 */ 49 50 51 52 53 /* 54 ================================================================================ 55 Prevent Multiple Inclusions 56 ================================================================================ 57 */ 58 #ifndef EDDY_THREADS_MUTEX_LOCK_HPP 59 #define EDDY_THREADS_MUTEX_LOCK_HPP 60 61 62 63 64 65 66 67 /* 68 ================================================================================ 69 Includes 70 ================================================================================ 71 */ 72 // config.hpp should be the first include. 73 #include "../include/config.hpp" 74 75 #include "mutex.hpp" 76 #include "thread_exceptions.hpp" 77 78 79 80 /* 81 ================================================================================ 82 Pre-Namespace Forward Declares 83 ================================================================================ 84 */ 85 86 87 88 89 90 91 92 93 /* 94 ================================================================================ 95 Namespace Aliases 96 ================================================================================ 97 */ 98 99 100 101 102 103 104 105 106 /* 107 ================================================================================ 108 Begin Namespace 109 ================================================================================ 110 */ 111 namespace eddy { 112 namespace threads { 113 114 115 116 117 118 /* 119 ================================================================================ 120 In-Namespace Forward Declares 121 ================================================================================ 122 */ 123 class mutex_lock; 124 125 126 127 128 129 130 131 /* 132 ================================================================================ 133 In-Namespace File Scope Typedefs 134 ================================================================================ 135 */ 136 137 138 139 140 141 142 143 /* 144 ================================================================================ 145 Class Definition 146 ================================================================================ 147 */ 148 /// This is meant to provide scoped locking of mutexes. 149 /** 150 * The constructor of this class takes a mutex and immediately locks it. 151 * The destructor unlocks that mutex. 152 */ 153 class EDDY_SL_IEDECL mutex_lock 154 { 155 /* 156 ============================================================================ 157 Friend Declarations 158 ============================================================================ 159 */ 160 friend class condition; 161 162 163 /* 164 ============================================================================ 165 Member Data Declarations 166 ============================================================================ 167 */ 168 private: 169 170 171 /// A pointer to the mutex to lock. 172 mutex* _mutex; 173 174 175 176 /* 177 ============================================================================ 178 Mutators 179 ============================================================================ 180 */ 181 public: 182 183 184 protected: 185 186 187 private: 188 189 190 /* 191 ============================================================================ 192 Accessors 193 ============================================================================ 194 */ 195 public: 196 197 198 protected: 199 200 201 private: 202 203 204 /* 205 ============================================================================ 206 Public Methods 207 ============================================================================ 208 */ 209 public: 210 211 212 213 214 /* 215 ============================================================================ 216 Subclass Visible Methods 217 ============================================================================ 218 */ 219 protected: 220 221 222 223 224 225 /* 226 ============================================================================ 227 Subclass Overridable Methods 228 ============================================================================ 229 */ 230 public: 231 232 233 protected: 234 235 236 private: 237 238 239 240 241 242 /* 243 ============================================================================ 244 Private Methods 245 ============================================================================ 246 */ 247 private: 248 249 /// Locks the mutex held in this lock. 250 inline 251 void 252 lock( 253 ); 254 255 /// Unlocks the mutex held in this lock. 256 inline 257 void 258 unlock( 259 ); 260 261 /// Allows access to the mutex held in this lock to friends. 262 /** 263 * Only friends can get at this mutex and they should not lock or 264 * unlock it unless they really know what they are doing since this 265 * object will attempt to unlock it in the destructor. There is the 266 * potential for an unbalance of lock and unlock calls. 267 * 268 * \return A reference to the mutex that is locked by this lock object. 269 */ 270 inline 271 mutex& 272 get_mutex( 273 ); 274 275 /* 276 ============================================================================ 277 Structors 278 ============================================================================ 279 */ 280 public: 281 282 283 /// Constructs a mutex_lock 284 /** 285 * Construction causes locking of the supplied \a mutex. 286 * 287 * \param mutex The mutex to lock in this constructor and unlock in 288 * the destructor. 289 * \throw lock_error If \a mutex cannot be locked. 290 */ 291 inline 292 mutex_lock( 293 mutex& mutex 294 ); 295 296 /// Destructs a mutex_lock 297 /** 298 * Destruction causes unlocking of _mutex. 299 */ 300 inline 301 ~mutex_lock( 302 ) throw(); 303 304 305 306 }; // class mutex_lock 307 308 309 310 /* 311 ================================================================================ 312 End Namespace 313 ================================================================================ 314 */ 315 } // namespace threads 316 } // namespace eddy 317 318 319 320 321 322 323 324 /* 325 ================================================================================ 326 Include Inlined Functions File 327 ================================================================================ 328 */ 329 #include "./inline/mutex_lock.hpp.inl" 330 331 332 333 /* 334 ================================================================================ 335 End of Multiple Inclusion Check 336 ================================================================================ 337 */ 338 #endif // EDDY_THREADS_MUTEX_LOCK_HPP 339