PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Mutex.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_MUTEX_H__ 00024 #define __PLCORE_MUTEX_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 MutexImpl; 00044 00045 00046 //[-------------------------------------------------------] 00047 //[ Classes ] 00048 //[-------------------------------------------------------] 00049 /** 00050 * @brief 00051 * Mutex (Mutual Exclusion, binary semaphore) class 00052 * 00053 * @note 00054 * - Implementation of the bridge design pattern, this class is the abstraction 00055 * - The mutex is non-recursive, meaning that you have to be careful to avoid creating a "self-deadlock" by calling the lock method 00056 * multiple times directly or indirectly by other method calls 00057 * - It's a good idea to lock/unlock a mutex by using the MutexGuard helper class on the runtime stack so there's always an unlock for each lock! 00058 */ 00059 class Mutex { 00060 00061 00062 //[-------------------------------------------------------] 00063 //[ Public functions ] 00064 //[-------------------------------------------------------] 00065 public: 00066 /** 00067 * @brief 00068 * Constructor 00069 * 00070 * @note 00071 * - A new constructed mutex is unlocked by default 00072 */ 00073 PLCORE_API Mutex(); 00074 00075 /** 00076 * @brief 00077 * Constructor 00078 * 00079 * @param[in] bCriticalSection 00080 * If supported by the platform: 'true' to use "Critical Section" (lock/unlock only inside the same process) instead of "Mutex" (lock/unlock across multiple processes) implementation, else 'false' 00081 * 00082 * @remarks 00083 * Platforms handle mutual exclusion implementations differently. POSIX only offers "Mutex", while MS Windows 00084 * makes a difference between "Mutex" (lock/unlock across multiple processes) and "Critical Section" 00085 * (lock/unlock only inside the same process). 00086 * 00087 * @note 00088 * - A new constructed mutex is unlocked by default 00089 */ 00090 PLCORE_API Mutex(bool bCriticalSection); 00091 00092 /** 00093 * @brief 00094 * Destructor 00095 */ 00096 PLCORE_API ~Mutex(); 00097 00098 /** 00099 * @brief 00100 * Locks the mutex 00101 * 00102 * @return 00103 * 'true' if successful, 'false' on error 00104 * 00105 * @note 00106 * - Blocking if already locked 00107 * - Use the lock method with a timeout to avoid potential deadlocks 00108 * - In the literature, this operation is also known as "acquire" 00109 */ 00110 inline bool Lock(); 00111 00112 /** 00113 * @brief 00114 * Locks the mutex, but only wait until timeout 00115 * 00116 * @param[in] nTimeout 00117 * Timeout in milliseconds 00118 * 00119 * @return 00120 * 'true' if successful, 'false' on error 00121 * 00122 * @note 00123 * - 'nTimeout = 0' means: Return immediately if already locked 00124 * 00125 * @see 00126 * - Lock() 00127 */ 00128 inline bool TryLock(uint64 nTimeout); 00129 00130 /** 00131 * @brief 00132 * Unlocks the mutex 00133 * 00134 * @return 00135 * 'true' if successful, 'false' on error 00136 * 00137 * @note 00138 * - In the literature, this operation is also known as "release" 00139 */ 00140 inline bool Unlock(); 00141 00142 00143 //[-------------------------------------------------------] 00144 //[ Private functions ] 00145 //[-------------------------------------------------------] 00146 private: 00147 /** 00148 * @brief 00149 * Copy constructor 00150 * 00151 * @param[in] cSource 00152 * Source to copy from 00153 */ 00154 Mutex(const Mutex &cSource); 00155 00156 /** 00157 * @brief 00158 * Copy operator 00159 * 00160 * @param[in] cSource 00161 * Source to copy from 00162 * 00163 * @return 00164 * Reference to this instance 00165 */ 00166 Mutex &operator =(const Mutex &cSource); 00167 00168 00169 //[-------------------------------------------------------] 00170 //[ Private data ] 00171 //[-------------------------------------------------------] 00172 private: 00173 MutexImpl *m_pMutexImpl; /**< Pointer to the system specific implementation (assumed to be never a null pointer!) */ 00174 00175 00176 }; 00177 00178 00179 //[-------------------------------------------------------] 00180 //[ Namespace ] 00181 //[-------------------------------------------------------] 00182 } // PLCore 00183 00184 00185 //[-------------------------------------------------------] 00186 //[ Implementation ] 00187 //[-------------------------------------------------------] 00188 #include "PLCore/System/Mutex.inl" 00189 00190 00191 #endif // __PLCORE_MUTEX_H__
|