CmThreadDefines.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #define CM_AUTO_MUTEX_NAME mutex
  3. #if CM_THREAD_SUPPORT
  4. #include <thread>
  5. #include <chrono>
  6. #include <mutex>
  7. #include <condition_variable>
  8. #include "BsSpinLock.h"
  9. #define CM_AUTO_MUTEX mutable std::mutex CM_AUTO_MUTEX_NAME;
  10. #define CM_LOCK_AUTO_MUTEX std::unique_lock<std::mutex> cmAutoMutexLock(CM_AUTO_MUTEX_NAME);
  11. #define CM_MUTEX(name) mutable std::mutex name;
  12. #define CM_STATIC_MUTEX(name) static std::mutex name;
  13. #define CM_STATIC_MUTEX_INSTANCE(name) std::mutex name;
  14. #define CM_STATIC_MUTEX_CLASS_INSTANCE(name, classTypeName) std::mutex classTypeName##::name;
  15. #define CM_LOCK_MUTEX(name) std::unique_lock<std::mutex> cmnameLock(name);
  16. #define CM_LOCK_MUTEX_NAMED(mutexName, lockName) std::unique_lock<std::mutex> lockName(mutexName);
  17. #define CM_LOCK_TYPE std::unique_lock<std::mutex>
  18. // like CM_AUTO_MUTEX but mutex held by pointer
  19. #define CM_AUTO_SHARED_MUTEX mutable std::mutex *CM_AUTO_MUTEX_NAME;
  20. #define CM_LOCK_AUTO_SHARED_MUTEX assert(CM_AUTO_MUTEX_NAME); std::lock_guard<std::mutex> cmAutoMutexLock(*CM_AUTO_MUTEX_NAME);
  21. #define CM_COPY_AUTO_SHARED_MUTEX(from) assert(!CM_AUTO_MUTEX_NAME); CM_AUTO_MUTEX_NAME = from;
  22. #define CM_SET_AUTO_SHARED_MUTEX_NULL CM_AUTO_MUTEX_NAME = 0;
  23. #define CM_MUTEX_CONDITIONAL(mutex) if (mutex)
  24. #define CM_THREAD_SYNCHRONISER(sync) std::condition_variable sync;
  25. #define CM_STATIC_THREAD_SYNCHRONISER(sync) static std::condition_variable sync;
  26. #define CM_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(sync, classTypeName) std::condition_variable classTypeName##::sync;
  27. #define CM_THREAD_WAIT(sync, mutex, lock) sync.wait(lock);
  28. #define CM_THREAD_NOTIFY_ONE(sync) sync.notify_one();
  29. #define CM_THREAD_NOTIFY_ALL(sync) sync.notify_all();
  30. #define CM_THREAD_JOIN(thread) thread.join();
  31. // Recursive mutex
  32. #define CM_RECURSIVE_MUTEX(name) mutable std::recursive_mutex name
  33. #define CM_LOCK_RECURSIVE_MUTEX(name) std::unique_lock<std::recursive_mutex> cmnameLock(name);
  34. // Read-write mutex
  35. #define CM_RW_MUTEX(name) mutable std::mutex name
  36. #define CM_LOCK_RW_MUTEX_READ(name) std::unique_lock<std::mutex> cmnameLock(name)
  37. #define CM_LOCK_RW_MUTEX_WRITE(name) std::unique_lock<std::mutex> cmnameLock(name)
  38. // Thread objects and related functions
  39. #define CM_THREAD_TYPE std::thread
  40. #define CM_THREAD_CREATE(name, worker) std::thread* name = new (BansheeEngine::MemoryAllocator<BansheeEngine::GenAlloc>::allocate(sizeof(std::thread))) std::thread(worker);
  41. #define CM_THREAD_DESTROY(name) BansheeEngine::cm_delete<BansheeEngine::GenAlloc, std::thread>(name);
  42. #define CM_THREAD_HARDWARE_CONCURRENCY std::thread::hardware_concurrency()
  43. #define CM_THREAD_CURRENT_ID std::this_thread::get_id()
  44. #define CM_THREAD_ID_TYPE std::thread::id
  45. #define CM_DEFER_LOCK std::defer_lock
  46. #define CM_THREAD_WORKER_INHERIT
  47. // Utility
  48. #define CM_THREAD_SLEEP(ms) std::this_thread::sleep_for(std::chrono::milliseconds(ms));
  49. #else
  50. #define CM_AUTO_MUTEX
  51. #define CM_LOCK_AUTO_MUTEX
  52. #define CM_MUTEX(name)
  53. #define CM_STATIC_MUTEX(name)
  54. #define CM_STATIC_MUTEX_INSTANCE(name)
  55. #define CM_STATIC_MUTEX_CLASS_INSTANCE(name, classTypeName)
  56. #define CM_LOCK_MUTEX(name)
  57. #define CM_LOCK_MUTEX_NAMED(mutexName, lockName)
  58. #define CM_LOCK_TYPE UINT32
  59. #define CM_AUTO_SHARED_MUTEX
  60. #define CM_LOCK_AUTO_SHARED_MUTEX
  61. #define CM_COPY_AUTO_SHARED_MUTEX(from)
  62. #define CM_SET_AUTO_SHARED_MUTEX_NULL
  63. #define CM_MUTEX_CONDITIONAL(name) if(true)
  64. #define CM_RW_MUTEX(name)
  65. #define CM_LOCK_RW_MUTEX_READ(name)
  66. #define CM_LOCK_RW_MUTEX_WRITE(name)
  67. #define CM_THREAD_SYNCHRONISER(sync)
  68. #define CM_STATIC_THREAD_SYNCHRONISER(sync)
  69. #define CM_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(sync, classTypeName)
  70. #define CM_THREAD_WAIT(sync, lock)
  71. #define CM_THREAD_NOTIFY_ONE(sync)
  72. #define CM_THREAD_NOTIFY_ALL(sync)
  73. #define CM_THREAD_JOIN(thread)
  74. #define CM_THREAD_SLEEP(ms)
  75. #define CM_THREAD_ID_TYPE UINT32
  76. #define CM_THREAD_WORKER_INHERIT
  77. #define CM_DEFER_LOCK
  78. #endif