PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Semaphore.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_SEMAPHORE_H__ 00024 #define __PLCORE_SEMAPHORE_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 SemaphoreImpl; 00044 00045 00046 //[-------------------------------------------------------] 00047 //[ Classes ] 00048 //[-------------------------------------------------------] 00049 /** 00050 * @brief 00051 * Counting semaphore class 00052 * 00053 * @note 00054 * - Implementation of the bridge design pattern, this class is the abstraction 00055 */ 00056 class Semaphore { 00057 00058 00059 //[-------------------------------------------------------] 00060 //[ Public functions ] 00061 //[-------------------------------------------------------] 00062 public: 00063 /** 00064 * @brief 00065 * Constructor 00066 * 00067 * @param[in] nValue 00068 * Initial value of the semaphore (usually >0) 00069 * @param[in] nMaxValue 00070 * Maximum value of the semaphore 00071 */ 00072 PLCORE_API Semaphore(uint32 nValue, uint32 nMaxValue); 00073 00074 /** 00075 * @brief 00076 * Destructor 00077 */ 00078 PLCORE_API ~Semaphore(); 00079 00080 /** 00081 * @brief 00082 * Locks the semaphore 00083 * 00084 * @return 00085 * 'true' if successful, 'false' on error 00086 * 00087 * @note 00088 * - Other known names for this operation: p, wait, acquire, down 00089 * - Blocking if no 'signal' left 00090 * - Use the lock method with a timeout to avoid potential deadlocks 00091 */ 00092 inline bool Lock(); 00093 00094 /** 00095 * @brief 00096 * Locks the semaphore, but only wait until timeout 00097 * 00098 * @param[in] nTimeout 00099 * Timeout in milliseconds 00100 * 00101 * @return 00102 * 'true' if successful, 'false' on error 00103 * 00104 * @note 00105 * - 'nTimeout = 0' means: Return immediately if no 'signal' left 00106 * 00107 * @see 00108 * - Lock() 00109 */ 00110 inline bool TryLock(uint64 nTimeout); 00111 00112 /** 00113 * @brief 00114 * Unlocks the semaphore 00115 * 00116 * @return 00117 * 'true' if successful, 'false' on error 00118 * 00119 * @note 00120 * - Other known names for this operation: v, signal, release, up 00121 */ 00122 inline bool Unlock(); 00123 00124 /** 00125 * @brief 00126 * Gets the current value of the semaphore 00127 * 00128 * @return 00129 * Current semaphore value (number of 'signals' left) 00130 */ 00131 inline uint32 GetValue() const; 00132 00133 00134 //[-------------------------------------------------------] 00135 //[ Private functions ] 00136 //[-------------------------------------------------------] 00137 private: 00138 /** 00139 * @brief 00140 * Copy constructor 00141 * 00142 * @param[in] cSource 00143 * Source to copy from 00144 */ 00145 Semaphore(const Semaphore &cSource); 00146 00147 /** 00148 * @brief 00149 * Copy operator 00150 * 00151 * @param[in] cSource 00152 * Source to copy from 00153 * 00154 * @return 00155 * Reference to this instance 00156 */ 00157 Semaphore &operator =(const Semaphore &cSource); 00158 00159 00160 //[-------------------------------------------------------] 00161 //[ Private data ] 00162 //[-------------------------------------------------------] 00163 private: 00164 SemaphoreImpl *m_pSemaphoreImpl; /**< Pointer to the system specific implementation (assumed to be never a null pointer!) */ 00165 uint32 m_nValue; /**< Current value of the semaphore */ 00166 00167 00168 }; 00169 00170 00171 //[-------------------------------------------------------] 00172 //[ Namespace ] 00173 //[-------------------------------------------------------] 00174 } // PLCore 00175 00176 00177 //[-------------------------------------------------------] 00178 //[ Implementation ] 00179 //[-------------------------------------------------------] 00180 #include "PLCore/System/Semaphore.inl" 00181 00182 00183 #endif // __PLCORE_SEMAPHORE_H__
|