ThreadHive.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/Thread.h>
  7. #include <AnKi/Util/WeakArray.h>
  8. #include <AnKi/Util/Allocator.h>
  9. namespace anki
  10. {
  11. // Forward
  12. class ThreadHive;
  13. /// @addtogroup util_thread
  14. /// @{
  15. /// Opaque handle that defines a ThreadHive depedency. @memberof ThreadHive
  16. class ThreadHiveSemaphore
  17. {
  18. friend class ThreadHive;
  19. public:
  20. /// Increase the value of the semaphore. It's easy to brake things with that.
  21. /// @note It's thread-safe.
  22. void increaseSemaphore(U32 increase)
  23. {
  24. m_atomic.fetchAdd(increase);
  25. }
  26. private:
  27. Atomic<U32> m_atomic;
  28. // No need to construct it or delete it
  29. ThreadHiveSemaphore() = delete;
  30. ~ThreadHiveSemaphore() = delete;
  31. };
  32. /// The callback that defines a ThreadHibe task.
  33. /// @memberof ThreadHive
  34. using ThreadHiveTaskCallback = void (*)(void* userData, U32 threadId, ThreadHive& hive,
  35. ThreadHiveSemaphore* signalSemaphore);
  36. /// Task for the ThreadHive. @memberof ThreadHive
  37. class ThreadHiveTask
  38. {
  39. public:
  40. /// What this task will do.
  41. ThreadHiveTaskCallback m_callback ANKI_DEBUG_CODE(= nullptr);
  42. /// Arguments to pass to the m_callback.
  43. void* m_argument ANKI_DEBUG_CODE(= nullptr);
  44. /// The task will start when that semaphore reaches zero.
  45. ThreadHiveSemaphore* m_waitSemaphore = nullptr;
  46. /// When the task is completed that semaphore will be decremented by one. Can be used to set dependencies to future
  47. /// tasks.
  48. ThreadHiveSemaphore* m_signalSemaphore = nullptr;
  49. };
  50. /// Initialize a ThreadHiveTask.
  51. #define ANKI_THREAD_HIVE_TASK(callback_, argument_, waitSemaphore_, signalSemaphore_) \
  52. { \
  53. [](void* ud, U32 threadId, ThreadHive& hive, ThreadHiveSemaphore* signalSemaphore) { \
  54. auto self = static_cast<decltype(argument_)>(ud); \
  55. (void)self; \
  56. callback_ \
  57. }, \
  58. argument_, waitSemaphore_, signalSemaphore_ \
  59. }
  60. /// A scheduler of small tasks. It takes a number of tasks and schedules them in one of the threads. The tasks can
  61. /// depend on previously submitted tasks or be completely independent.
  62. class ThreadHive
  63. {
  64. public:
  65. static const U32 MAX_THREADS = 32;
  66. /// Create the hive.
  67. ThreadHive(U32 threadCount, GenericMemoryPoolAllocator<U8> alloc, Bool pinToCores = false);
  68. ThreadHive(const ThreadHive&) = delete; // Non-copyable
  69. ~ThreadHive();
  70. ThreadHive& operator=(const ThreadHive&) = delete; // Non-copyable
  71. U32 getThreadCount() const
  72. {
  73. return m_threadCount;
  74. }
  75. /// Create a new semaphore with some initial value.
  76. /// @param initialValue Can't be zero.
  77. ThreadHiveSemaphore* newSemaphore(const U32 initialValue)
  78. {
  79. ANKI_ASSERT(initialValue > 0);
  80. PtrSize alignment = alignof(ThreadHiveSemaphore);
  81. ThreadHiveSemaphore* sem =
  82. reinterpret_cast<ThreadHiveSemaphore*>(m_alloc.allocate(sizeof(ThreadHiveSemaphore), &alignment));
  83. sem->m_atomic.setNonAtomically(initialValue);
  84. return sem;
  85. }
  86. /// Allocate some scratch memory. The memory becomes invalid after waitAllTasks() is called.
  87. void* allocateScratchMemory(PtrSize size, U32 alignment)
  88. {
  89. ANKI_ASSERT(size > 0 && alignment > 0);
  90. PtrSize align = alignment;
  91. void* out = m_alloc.allocate(size, &align);
  92. #if ANKI_ENABLE_ASSERTIONS
  93. memset(out, 0, size);
  94. #endif
  95. return out;
  96. }
  97. /// Submit tasks. The ThreadHiveTaskCallback callbacks can also call this.
  98. void submitTasks(ThreadHiveTask* tasks, const U32 taskCount);
  99. /// Submit a single task without dependencies. The ThreadHiveTaskCallback callbacks can also call this.
  100. void submitTask(ThreadHiveTaskCallback callback, void* arg)
  101. {
  102. ThreadHiveTask task;
  103. task.m_callback = callback;
  104. task.m_argument = arg;
  105. submitTasks(&task, 1);
  106. }
  107. /// Wait for all tasks to finish. Will block.
  108. void waitAllTasks();
  109. private:
  110. class Thread;
  111. /// Lightweight task.
  112. class Task;
  113. GenericMemoryPoolAllocator<U8> m_slowAlloc;
  114. StackAllocator<U8> m_alloc;
  115. Thread* m_threads = nullptr;
  116. U32 m_threadCount = 0;
  117. Task* m_head = nullptr; ///< Head of the task list.
  118. Task* m_tail = nullptr; ///< Tail of the task list.
  119. Bool m_quit = false;
  120. U32 m_pendingTasks = 0;
  121. Mutex m_mtx;
  122. ConditionVariable m_cvar;
  123. void threadRun(U32 threadId);
  124. /// Wait for more tasks.
  125. Bool waitForWork(U32 threadId, Task*& task);
  126. /// Get new work from the queue.
  127. Task* getNewTask();
  128. };
  129. /// @}
  130. } // end namespace anki