ThreadHive.h 4.3 KB

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