1 // The MIT License (MIT) 2 // 3 // Copyright (c) 2015 Sergey Makeev, Vadim Slyusarev 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 #pragma once 24 25 26 27 namespace MT 28 { 29 30 namespace TaskID 31 { 32 //unused_id is any odd number, valid_id should be always only even numbers 33 static const int UNUSED = 1; 34 } 35 36 37 //forward declaration 38 class TaskHandle; 39 40 41 /// \class PoolElementHeader 42 /// \brief 43 ////////////////////////////////////////////////////////////////////////// 44 struct PoolElementHeader 45 { 46 //Task id (timestamp) 47 AtomicInt id; 48 49 internal::TaskDesc desc; 50 51 public: 52 53 PoolElementHeader(int _id) 54 : id(_id) 55 { 56 } 57 58 static bool DestoryByHandle(const MT::TaskHandle & handle); 59 }; 60 61 62 /// \class TaskPoolElement 63 /// \brief 64 ////////////////////////////////////////////////////////////////////////// 65 template<typename T> 66 class PoolElement : public PoolElementHeader 67 { 68 public: 69 70 // Storage for task 71 T task; 72 73 PoolElement(int _id, T && _task) 74 : PoolElementHeader(_id) 75 , task( std::move(_task) ) 76 { 77 MT_ASSERT( offsetof(PoolElement<T>, task) == sizeof(PoolElementHeader), "Invalid offset for task in PoolElement"); 78 79 desc.poolDestroyFunc = T::PoolTaskDestroy; 80 desc.taskFunc = T::TaskEntryPoint; 81 desc.userData = &task; 82 83 #ifdef MT_INSTRUMENTED_BUILD 84 desc.debugID = T::GetDebugID(); 85 desc.colorIndex = T::GetDebugColorIndex(); 86 #endif 87 } 88 89 }; 90 91 92 /// \class TaskHandle 93 /// \brief 94 ////////////////////////////////////////////////////////////////////////// 95 class TaskHandle 96 { 97 98 int check_id; 99 100 protected: 101 102 friend struct PoolElementHeader; 103 104 PoolElementHeader * task; 105 106 public: 107 108 //default ctor 109 TaskHandle() 110 : check_id(TaskID::UNUSED) 111 , task(nullptr) 112 { 113 114 } 115 116 //ctor 117 TaskHandle(int _id, PoolElementHeader* _task) 118 : check_id(_id) 119 , task(_task) 120 { 121 } 122 123 //copy ctor 124 TaskHandle(const TaskHandle & other) 125 : check_id(other.check_id) 126 , task(other.task) 127 { 128 } 129 130 //move ctor 131 TaskHandle(TaskHandle && other) 132 : check_id(other.check_id) 133 , task(other.task) 134 { 135 other.check_id = TaskID::UNUSED; 136 other.task = nullptr; 137 } 138 139 ~TaskHandle() 140 { 141 } 142 143 bool IsValid() const 144 { 145 if (task == nullptr) 146 { 147 return false; 148 } 149 150 if (check_id != task->id.Get()) 151 { 152 return false; 153 } 154 155 return true; 156 } 157 158 const internal::TaskDesc & GetDesc() 159 { 160 MT_ASSERT(IsValid(), "Task handle is invalid"); 161 return task->desc; 162 } 163 164 165 }; 166 167 168 169 170 ////////////////////////////////////////////////////////////////////////// 171 inline bool PoolElementHeader::DestoryByHandle(const MT::TaskHandle & handle) 172 { 173 if (!handle.IsValid()) 174 { 175 return false; 176 } 177 178 if (handle.task->desc.poolDestroyFunc == nullptr) 179 { 180 return false; 181 } 182 183 if (handle.task->desc.userData == nullptr) 184 { 185 return false; 186 } 187 188 //call destroy func 189 handle.task->desc.poolDestroyFunc(handle.task->desc.userData); 190 return true; 191 } 192 193 194 195 196 197 /// \class TaskPool 198 /// \brief 199 ////////////////////////////////////////////////////////////////////////// 200 template<typename T, size_t N> 201 class TaskPool 202 { 203 typedef PoolElement<T> PoolItem; 204 205 // 206 static const size_t MASK = (N - 1); 207 208 void* data; 209 AtomicInt idGenerator; 210 AtomicInt index; 211 212 inline PoolItem* Buffer() 213 { 214 return (PoolItem*)(data); 215 } 216 217 inline void MoveCtor(PoolItem* element, int id, T && val) 218 { 219 new(element) PoolItem(id, std::move(val)); 220 } 221 222 private: 223 224 TaskPool(const TaskPool &) {} 225 void operator=(const TaskPool &) {} 226 227 public: 228 229 TaskPool() 230 : idGenerator(0) 231 , index(0) 232 { 233 static_assert( MT::StaticIsPow2<N>::result, "Task pool capacity must be power of 2"); 234 235 size_t bytesCount = sizeof(PoolItem) * N; 236 data = Memory::Alloc(bytesCount); 237 238 for(size_t idx = 0; idx < N; idx++) 239 { 240 PoolItem* pElement = Buffer() + idx; 241 pElement->id.Set(TaskID::UNUSED); 242 } 243 } 244 245 ~TaskPool() 246 { 247 if (data != nullptr) 248 { 249 250 for(size_t idx = 0; idx < N; idx++) 251 { 252 PoolItem* pElement = Buffer() + idx; 253 254 int preValue = pElement->id.Set(TaskID::UNUSED); 255 if (preValue != TaskID::UNUSED) 256 { 257 pElement->task.~T(); 258 } 259 } 260 261 Memory::Free(data); 262 data = nullptr; 263 } 264 } 265 266 267 TaskHandle Alloc(T && task) 268 { 269 int idx = index.Inc() - 1; 270 271 int clampedIdx = (idx & MASK); 272 273 PoolItem* pElement = Buffer() + clampedIdx; 274 275 bool isUnused = ((pElement->id.Get() & 1 ) != 0); 276 if (isUnused == false) 277 { 278 //Can't allocate more, next element in circular buffer is already used 279 return TaskHandle(); 280 } 281 282 //generate next even number for id 283 int id = idGenerator.Add(2); 284 MoveCtor( pElement, id, std::move(task) ); 285 return TaskHandle(id, pElement); 286 } 287 288 }; 289 290 } 291