1 ///////////////////////////////////////////////////////// 2 // brief: 3 // Implementation of multiple thread-safe containers. 4 // author: 5 // jK 6 // 7 // Copyright (C) 2009. 8 // Licensed under the terms of the GNU GPL, v2 or later. 9 // 10 11 #ifndef TSC_H 12 #define TSC_H 13 14 #include <list> 15 #include <vector> 16 17 #include "System/creg/creg_cond.h" 18 #include <set> 19 #include <map> 20 21 ///////////////////////////////////////////////////////// 22 // 23 24 //FIXME class ThreadVector<class T>; 25 //class ThreadListSimRender<class T>; 26 //class ThreadVectorSimRender<class T>; 27 28 ///////////////////////////////////////////////////////// 29 30 template <class C, class R, class T> 31 class ThreadListSimRender { 32 private: 33 typedef typename C::iterator ListIT; 34 typedef typename C::const_iterator constIT; 35 typedef typename std::vector<T>::const_iterator VecIT; 36 37 public: CR_DECLARE_STRUCT(ThreadListSimRender)38 CR_DECLARE_STRUCT(ThreadListSimRender) 39 40 void PostLoad() {}; 41 ~ThreadListSimRender()42 ~ThreadListSimRender() { 43 clear(); 44 } 45 clear()46 void clear() { 47 for(ListIT it = cont.begin(); it != cont.end(); ++it) { 48 delete *it; 49 } 50 cont.clear(); 51 delete_erased_synced(); 52 } 53 54 //! SIMULATION/SYNCED METHODS push(const T & x)55 void push(const T& x) { 56 cont.push_back(x); 57 } 58 insert(const T & x)59 void insert(const T& x) { 60 cont.insert(x); 61 } 62 insert(ListIT & it,const T & x)63 ListIT insert(ListIT& it, const T& x) { 64 return cont.insert(it, x); 65 } 66 67 // keep same deletion order in MT and non-MT version to reduce risk for desync erase_delete_synced(ListIT & it)68 ListIT erase_delete_synced(ListIT& it) { 69 delObj.push_back(*it); 70 return cont.erase(it); 71 } 72 erase_delete_set_synced(ListIT & it)73 ListIT erase_delete_set_synced(ListIT& it) { 74 delObj.push_back(*it); 75 return set_erase(cont, it); 76 } 77 can_delete_synced()78 bool can_delete_synced() const { 79 return !delObj.empty(); 80 } 81 delete_erased_synced()82 void delete_erased_synced() { 83 for (VecIT it = delObj.begin(); it != delObj.end(); ++it) { 84 delete *it; 85 } 86 delObj.clear(); 87 } 88 resize(const size_t & s)89 void resize(const size_t& s) { 90 cont.resize(s); 91 } 92 size()93 size_t size() const { 94 return cont.size(); 95 } 96 empty()97 bool empty() const { 98 return cont.empty(); 99 } 100 begin()101 ListIT begin() { 102 return cont.begin(); 103 } 104 end()105 ListIT end() { 106 return cont.end(); 107 } 108 109 public: 110 //! RENDER/UNSYNCED METHODS delay_add()111 void delay_add() const { 112 } 113 add_delayed()114 void add_delayed() const { 115 } 116 can_delay_add()117 bool can_delay_add() const { 118 return false; 119 } 120 erase_delete(ListIT & it)121 ListIT erase_delete(ListIT& it) { 122 delete *it; 123 return cont.erase(it); 124 } 125 erase_delete_set(ListIT & it)126 ListIT erase_delete_set(ListIT& it) { 127 delete *it; 128 return set_erase(cont, it); 129 } 130 can_delete()131 bool can_delete() const { 132 return false; 133 } 134 delete_erased()135 void delete_erased() const { 136 } 137 delay_delete()138 void delay_delete() const { 139 } 140 delete_delayed()141 void delete_delayed() const { 142 } 143 render_size()144 size_t render_size() const { 145 return cont.size(); 146 } 147 render_empty()148 bool render_empty() const { 149 return cont.empty(); 150 } 151 render_begin()152 constIT render_begin() const { 153 return cont.begin(); 154 } 155 render_end()156 constIT render_end() const { 157 return cont.end(); 158 } 159 160 public: 161 typedef ListIT iterator; 162 typedef constIT render_iterator; 163 164 public: 165 C cont; 166 std::vector<T> delObj; 167 }; 168 169 170 171 172 173 template <class C, class R, class T, class D> 174 class ThreadListRender { 175 private: 176 typedef typename std::set<T>::const_iterator constSetIT; 177 typedef typename std::set<T>::iterator SetIT; 178 typedef typename std::vector<T>::const_iterator VecIT; 179 180 public: CR_DECLARE_STRUCT(ThreadListRender)181 CR_DECLARE_STRUCT(ThreadListRender) 182 183 ~ThreadListRender() { 184 clear(); 185 } 186 clear()187 void clear() { 188 } 189 PostLoad()190 void PostLoad() { 191 } 192 193 //! SIMULATION/SYNCED METHODS push(const T & x)194 void push(const T& x) { 195 D::Add(x); 196 } insert(const T & x)197 void insert(const T& x) { 198 D::Add(x); 199 } 200 erase_remove_synced(const T & x)201 void erase_remove_synced(const T& x) { 202 D::Remove(x); 203 } 204 remove_erased_synced()205 void remove_erased_synced() { 206 } 207 208 public: 209 //! RENDER/UNSYNCED METHODS delay_add()210 void delay_add() { 211 } 212 add_delayed()213 void add_delayed() { 214 } 215 erase_delete(const T & x)216 void erase_delete(const T& x) { 217 D::Remove(x); 218 } 219 delay_delete()220 void delay_delete() { 221 } 222 delete_delayed()223 void delete_delayed() { 224 } 225 enqueue(const T & x)226 void enqueue(const T& x) { 227 D::Add(x); 228 } 229 delay()230 void delay() { 231 } 232 execute()233 void execute() { 234 } 235 clean()236 void clean() { 237 } 238 clean(std::vector<C> * delQueue)239 void clean(std::vector<C> *delQueue) { 240 } 241 to_destroy()242 std::vector<C> *to_destroy() { 243 return NULL; 244 } 245 dequeue(const C & x)246 void dequeue(const C& x) { 247 D::Remove(x); 248 } 249 dequeue_synced(const C & x)250 void dequeue_synced(const C& x) { 251 simDelQueue.push_back(x); 252 } 253 destroy()254 void destroy() { 255 } 256 destroy_synced()257 void destroy_synced() { 258 for (VecIT it = simDelQueue.begin(); it != simDelQueue.end(); ++it) { 259 D::Remove(*it); 260 } 261 simDelQueue.clear(); 262 } 263 264 private: 265 std::vector<T> simDelQueue; 266 }; 267 268 269 template <class C, class K, class V, class I> 270 class ThreadMapRender { 271 private: 272 typedef std::map<K,V> TMapC; 273 274 public: CR_DECLARE_STRUCT(ThreadMapRender)275 CR_DECLARE_STRUCT(ThreadMapRender) 276 277 ~ThreadMapRender() { 278 clear(); 279 } 280 get_render_map()281 const TMapC& get_render_map() const { return contRender; } 282 clear()283 void clear() { 284 } 285 PostLoad()286 void PostLoad() { 287 } 288 289 //! SIMULATION/SYNCED METHODS push(const C & x,const V & y)290 void push(const C& x, const V& y) { 291 contRender[I::Index(x)] = y; 292 } 293 294 public: 295 //! RENDER/UNSYNCED METHODS delay_add()296 void delay_add() { 297 } 298 add_delayed()299 void add_delayed() { 300 } 301 erase_delete(const C & x)302 void erase_delete(const C& x) { 303 contRender.erase(I::Index(x)); 304 } 305 delay_delete()306 void delay_delete() { 307 } 308 delete_delayed()309 void delete_delayed() { 310 } 311 312 private: 313 TMapC contRender; 314 }; 315 316 template <class C, class R, class T, class D> 317 class ThreadListSim { 318 private: 319 typedef typename C::iterator SimIT; 320 typedef typename std::set<T>::const_iterator constSetIT; 321 typedef typename std::set<T>::iterator SetIT; 322 typedef typename std::vector<T>::const_iterator VecIT; 323 324 public: CR_DECLARE_STRUCT(ThreadListSim)325 CR_DECLARE_STRUCT(ThreadListSim) 326 327 ~ThreadListSim() { 328 clear(); 329 } 330 clear()331 void clear() { 332 for(SimIT it = cont.begin(); it != cont.end(); ++it) { 333 delete *it; 334 } 335 cont.clear(); 336 delete_erased_synced(); 337 } 338 detach_all()339 void detach_all() { 340 for(SimIT it = cont.begin(); it != cont.end(); ++it) { 341 D::Detach(*it); 342 } 343 } 344 PostLoad()345 void PostLoad() { 346 for (SimIT it = cont.begin(); it != cont.end(); ++it) { 347 } 348 } 349 350 //! SIMULATION/SYNCED METHODS push(const T & x)351 void push(const T& x) { 352 cont.push_back(x); 353 } insert(const T & x)354 void insert(const T& x) { 355 cont.insert(x); 356 } insert(SimIT & it,const T & x)357 SimIT insert(SimIT& it, const T& x) { 358 return cont.insert(it, x); 359 } 360 361 // keep same deletion order in MT and non-MT version to reduce risk for desync erase_delete_synced(SimIT & it)362 SimIT erase_delete_synced(SimIT& it) { 363 del.push_back(*it); 364 return cont.erase(it); 365 } 366 can_delete_synced()367 bool can_delete_synced() { 368 return !del.empty(); 369 } 370 delete_erased_synced()371 void delete_erased_synced() { 372 for (VecIT it = del.begin(); it != del.end(); ++it) { 373 delete *it; 374 } 375 del.clear(); 376 } 377 detach_erased_synced()378 void detach_erased_synced() { 379 for (VecIT it = del.begin(); it != del.end(); ++it) { 380 D::Detach(*it); 381 delete *it; 382 } 383 del.clear(); 384 } 385 resize(const size_t & s)386 void resize(const size_t& s) { 387 cont.resize(s); 388 } 389 size()390 size_t size() const { 391 return cont.size(); 392 } 393 empty()394 bool empty() const { 395 return cont.empty(); 396 } 397 begin()398 SimIT begin() { 399 return cont.begin(); 400 } 401 end()402 SimIT end() { 403 return cont.end(); 404 } 405 erase_delete(SimIT & it)406 SimIT erase_delete(SimIT& it) { 407 delete *it; 408 return cont.erase(it); 409 } 410 erase_detach(SimIT & it)411 SimIT erase_detach(SimIT& it) { 412 delete *it; 413 return cont.erase(it); 414 } 415 416 public: 417 typedef SimIT iterator; 418 419 420 public: //!needed by CREG 421 C cont; 422 std::vector<T> del; 423 424 private: 425 }; 426 427 428 #endif // #ifndef TSC_H 429