PixelLightAPI  .
MutexGuard.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: MutexGuard.h                                   *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_MUTEXGUARD_H__
00024 #define __PLCORE_MUTEXGUARD_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/PLCore.h"
00032 
00033 
00034 //[-------------------------------------------------------]
00035 //[ Namespace                                             ]
00036 //[-------------------------------------------------------]
00037 namespace PLCore {
00038 
00039 
00040 //[-------------------------------------------------------]
00041 //[ Forward declarations                                  ]
00042 //[-------------------------------------------------------]
00043 class Mutex;
00044 
00045 
00046 //[-------------------------------------------------------]
00047 //[ Classes                                               ]
00048 //[-------------------------------------------------------]
00049 /**
00050 *  @brief
00051 *    Mutex guard class ("Scoped Locking"-idiom, also known as "synchronized block")
00052 *
00053 *  @remarks
00054 *    When using a mutex in more complex situations there's the risk that one forgets the unlock a locked mutex. To avoid this
00055 *    situation it's a good idea to use this mutex guard on the runtime stack (the runtime stack is your friend!) in order to
00056 *    lock the mutex and automatically unlock it as soon as the critical scope is left.
00057 */
00058 class MutexGuard {
00059 
00060 
00061     //[-------------------------------------------------------]
00062     //[ Public functions                                      ]
00063     //[-------------------------------------------------------]
00064     public:
00065         /**
00066         *  @brief
00067         *    Constructor
00068         *
00069         *  @param[in] cMutex
00070         *    Mutex to use, just referenced, must stay valid as long as this mutex guard exists!
00071         *
00072         *  @note
00073         *    - Blocking if the mutex is already locked
00074         *    - Use the constructor with a timeout to avoid potential deadlocks
00075         */
00076         inline MutexGuard(Mutex &cMutex);
00077 
00078         /**
00079         *  @brief
00080         *    Constructor
00081         *
00082         *  @param[in] cMutex
00083         *    Mutex to use, just referenced, must stay valid as long as this mutex guard exists!
00084         *  @param[in] nTimeout
00085         *    Timeout in milliseconds
00086         *
00087         *  @note
00088         *    - 'nTimeout = 0' means: Return immediately if the mutex is already locked
00089         */
00090         inline MutexGuard(Mutex &cMutex, uint64 nTimeout);
00091 
00092         /**
00093         *  @brief
00094         *    Destructor
00095         */
00096         inline ~MutexGuard();
00097 
00098         /**
00099         *  @brief
00100         *    Returns whether the used mutex was locked successfully
00101         *
00102         *  @return
00103         *    'true' if the used mutex was locked successfully, else 'false'
00104         */
00105         inline bool IsLocked() const;
00106 
00107         /**
00108         *  @brief
00109         *    Returns the used mutex
00110         *
00111         *  @return
00112         *    The used mutex
00113         */
00114         inline Mutex &GetMutex() const;
00115 
00116 
00117     //[-------------------------------------------------------]
00118     //[ Private functions                                     ]
00119     //[-------------------------------------------------------]
00120     private:
00121         /**
00122         *  @brief
00123         *    Copy constructor
00124         *
00125         *  @param[in] cSource
00126         *    Source to copy from
00127         */
00128         inline MutexGuard(const MutexGuard &cSource);
00129 
00130         /**
00131         *  @brief
00132         *    Copy operator
00133         *
00134         *  @param[in] cSource
00135         *    Source to copy from
00136         *
00137         *  @return
00138         *    Reference to this instance
00139         */
00140         inline MutexGuard &operator =(const MutexGuard &cSource);
00141 
00142 
00143     //[-------------------------------------------------------]
00144     //[ Private data                                          ]
00145     //[-------------------------------------------------------]
00146     private:
00147         Mutex *m_pMutex;    /**< Pointer to the used mutex, always valid! */
00148         bool   m_bLocked;   /**< 'true' if the used mutex was locked successfully, else 'false' */
00149 
00150 
00151 };
00152 
00153 
00154 //[-------------------------------------------------------]
00155 //[ Namespace                                             ]
00156 //[-------------------------------------------------------]
00157 } // PLCore
00158 
00159 
00160 //[-------------------------------------------------------]
00161 //[ Implementation                                        ]
00162 //[-------------------------------------------------------]
00163 #include "PLCore/System/MutexGuard.inl"
00164 
00165 
00166 #endif // __PLCORE_MUTEXGUARD_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:57
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported