ThreadHive.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (C) 2009-2020, 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 : public NonCopyable
  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();
  69. U32 getThreadCount() const
  70. {
  71. return m_threadCount;
  72. }
  73. /// Create a new semaphore with some initial value.
  74. /// @param initialValue Can't be zero.
  75. ThreadHiveSemaphore* newSemaphore(const U32 initialValue)
  76. {
  77. ANKI_ASSERT(initialValue > 0);
  78. PtrSize alignment = alignof(ThreadHiveSemaphore);
  79. ThreadHiveSemaphore* sem =
  80. reinterpret_cast<ThreadHiveSemaphore*>(m_alloc.allocate(sizeof(ThreadHiveSemaphore), &alignment));
  81. sem->m_atomic.setNonAtomically(initialValue);
  82. return sem;
  83. }
  84. /// Allocate some scratch memory. The memory becomes invalid after waitAllTasks() is called.
  85. void* allocateScratchMemory(PtrSize size, U32 alignment)
  86. {
  87. ANKI_ASSERT(size > 0 && alignment > 0);
  88. PtrSize align = alignment;
  89. void* out = m_alloc.allocate(size, &align);
  90. #if ANKI_ENABLE_ASSERTS
  91. memset(out, 0, size);
  92. #endif
  93. return out;
  94. }
  95. /// Submit tasks. The ThreadHiveTaskCallback callbacks can also call this.
  96. void submitTasks(ThreadHiveTask* tasks, const U32 taskCount);
  97. /// Submit a single task without dependencies. The ThreadHiveTaskCallback callbacks can also call this.
  98. void submitTask(ThreadHiveTaskCallback callback, void* arg)
  99. {
  100. ThreadHiveTask task;
  101. task.m_callback = callback;
  102. task.m_argument = arg;
  103. submitTasks(&task, 1);
  104. }
  105. /// Wait for all tasks to finish. Will block.
  106. void waitAllTasks();
  107. private:
  108. class Thread;
  109. /// Lightweight task.
  110. class Task;
  111. GenericMemoryPoolAllocator<U8> m_slowAlloc;
  112. StackAllocator<U8> m_alloc;
  113. Thread* m_threads = nullptr;
  114. U32 m_threadCount = 0;
  115. Task* m_head = nullptr; ///< Head of the task list.
  116. Task* m_tail = nullptr; ///< Tail of the task list.
  117. Bool m_quit = false;
  118. U32 m_pendingTasks = 0;
  119. Mutex m_mtx;
  120. ConditionVariable m_cvar;
  121. void threadRun(U32 threadId);
  122. /// Wait for more tasks.
  123. Bool waitForWork(U32 threadId, Task*& task);
  124. /// Get new work from the queue.
  125. Task* getNewTask();
  126. /// Complete a task.
  127. void completeTask(U32 taskId);
  128. };
  129. /// @}
  130. } // end namespace anki