BsThreadDefines.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #define BS_AUTO_MUTEX_NAME mutex
  5. #include <thread>
  6. #include <chrono>
  7. #include <mutex>
  8. #include <condition_variable>
  9. #include "BsSpinLock.h"
  10. /** @addtogroup Threading
  11. * @{
  12. */
  13. #define BS_AUTO_MUTEX mutable std::mutex BS_AUTO_MUTEX_NAME;
  14. #define BS_LOCK_AUTO_MUTEX std::unique_lock<std::mutex> bsAutoMutexLock(BS_AUTO_MUTEX_NAME);
  15. #define BS_MUTEX(name) mutable std::mutex name;
  16. #define BS_STATIC_MUTEX(name) static std::mutex name;
  17. #define BS_STATIC_MUTEX_INSTANCE(name) std::mutex name;
  18. #define BS_STATIC_MUTEX_CLASS_INSTANCE(name, classTypeName) std::mutex classTypeName##::name;
  19. #define BS_LOCK_MUTEX(name) std::unique_lock<std::mutex> bsnameLock(name);
  20. #define BS_LOCK_MUTEX_NAMED(mutexName, lockName) std::unique_lock<std::mutex> lockName(mutexName);
  21. #define BS_LOCK_TYPE std::unique_lock<std::mutex>
  22. // like BS_AUTO_MUTEX but mutex held by pointer
  23. #define BS_AUTO_SHARED_MUTEX mutable std::mutex *BS_AUTO_MUTEX_NAME;
  24. #define BS_LOCK_AUTO_SHARED_MUTEX assert(BS_AUTO_MUTEX_NAME); std::lock_guard<std::mutex> bsAutoMutexLock(*BS_AUTO_MUTEX_NAME);
  25. #define BS_COPY_AUTO_SHARED_MUTEX(from) assert(!BS_AUTO_MUTEX_NAME); BS_AUTO_MUTEX_NAME = from;
  26. #define BS_SET_AUTO_SHARED_MUTEX_NULL BS_AUTO_MUTEX_NAME = 0;
  27. #define BS_MUTEX_CONDITIONAL(mutex) if (mutex)
  28. #define BS_THREAD_SYNCHRONISER(sync) std::condition_variable sync;
  29. #define BS_STATIC_THREAD_SYNCHRONISER(sync) static std::condition_variable sync;
  30. #define BS_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(sync, classTypeName) std::condition_variable classTypeName##::sync;
  31. #define BS_THREAD_WAIT(sync, mutex, lock) sync.wait(lock);
  32. #define BS_THREAD_WAIT_FOR(sync, mutex, lock, ms) sync.wait_for(lock, std::chrono::milliseconds(ms));
  33. #define BS_THREAD_NOTIFY_ONE(sync) sync.notify_one();
  34. #define BS_THREAD_NOTIFY_ALL(sync) sync.notify_all();
  35. #define BS_THREAD_JOIN(thread) thread.join();
  36. // Recursive mutex
  37. #define BS_RECURSIVE_MUTEX(name) mutable std::recursive_mutex name
  38. #define BS_LOCK_RECURSIVE_MUTEX(name) std::unique_lock<std::recursive_mutex> cmnameLock(name);
  39. // Read-write mutex
  40. #define BS_RW_MUTEX(name) mutable std::mutex name
  41. #define BS_LOCK_RW_MUTEX_READ(name) std::unique_lock<std::mutex> cmnameLock(name)
  42. #define BS_LOCK_RW_MUTEX_WRITE(name) std::unique_lock<std::mutex> cmnameLock(name)
  43. // Thread objects and related functions
  44. #define BS_THREAD_TYPE std::thread
  45. #define BS_THREAD_CREATE(name, worker) std::thread* name = new (BansheeEngine::MemoryAllocator<BansheeEngine::GenAlloc>::allocate(sizeof(std::thread))) std::thread(worker);
  46. #define BS_THREAD_DESTROY(name) BansheeEngine::bs_delete<std::thread, BansheeEngine::GenAlloc>(name);
  47. #define BS_THREAD_HARDWARE_CONCURRENCY std::thread::hardware_concurrency()
  48. #define BS_THREAD_CURRENT_ID std::this_thread::get_id()
  49. #define BS_THREAD_ID_TYPE std::thread::id
  50. #define BS_DEFER_LOCK std::defer_lock
  51. #define BS_THREAD_WORKER_INHERIT
  52. // Utility
  53. #define BS_THREAD_SLEEP(ms) std::this_thread::sleep_for(std::chrono::milliseconds(ms));
  54. using Mutex = std::mutex;
  55. using RecursiveMutex = std::recursive_mutex;
  56. using Signal = std::condition_variable;
  57. using Thread = std::thread;
  58. template <typename T = Mutex>
  59. using Lock = std::unique_lock<T>;
  60. template <typename T = RecursiveMutex>
  61. using RecursiveLock = std::unique_lock<T>;
  62. /** @} */