IlmThreadPool.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_ILM_THREAD_POOL_H
  6. #define INCLUDED_ILM_THREAD_POOL_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // class Task, class ThreadPool, class TaskGroup
  10. //
  11. // Class ThreadPool manages a set of worker threads and accepts
  12. // tasks for processing. Tasks added to the thread pool are
  13. // executed concurrently by the worker threads.
  14. //
  15. // Class Task provides an abstract interface for a task which
  16. // a ThreadPool works on. Derived classes need to implement the
  17. // execute() function which performs the actual task.
  18. //
  19. // Class TaskGroup allows synchronization on the completion of a set
  20. // of tasks. Every task that is added to a ThreadPool belongs to a
  21. // single TaskGroup. The destructor of the TaskGroup waits for all
  22. // tasks in the group to finish.
  23. //
  24. // Note: if you plan to use the ThreadPool interface in your own
  25. // applications note that the implementation of the ThreadPool calls
  26. // operator delete on tasks as they complete. If you define a custom
  27. // operator new for your tasks, for instance to use a custom heap,
  28. // then you must also write an appropriate operator delete.
  29. //
  30. //-----------------------------------------------------------------------------
  31. #include "IlmThreadNamespace.h"
  32. #include "IlmThreadExport.h"
  33. #include "IlmThreadConfig.h"
  34. ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER
  35. class TaskGroup;
  36. class Task;
  37. //-------------------------------------------------------
  38. // ThreadPoolProvider -- this is a pure virtual interface
  39. // enabling custom overloading of the threads used and how
  40. // the implementation of the processing of tasks
  41. // is implemented
  42. //-------------------------------------------------------
  43. class ILMTHREAD_EXPORT_TYPE ThreadPoolProvider
  44. {
  45. public:
  46. ILMTHREAD_EXPORT ThreadPoolProvider();
  47. ILMTHREAD_EXPORT virtual ~ThreadPoolProvider();
  48. // as in ThreadPool below
  49. virtual int numThreads () const = 0;
  50. // as in ThreadPool below
  51. virtual void setNumThreads (int count) = 0;
  52. // as in ThreadPool below
  53. virtual void addTask (Task* task) = 0;
  54. // Ensure that all tasks in this set are finished
  55. // and threads shutdown
  56. virtual void finish () = 0;
  57. // Make the provider non-copyable
  58. ThreadPoolProvider (const ThreadPoolProvider &) = delete;
  59. ThreadPoolProvider &operator= (const ThreadPoolProvider &) = delete;
  60. ThreadPoolProvider (ThreadPoolProvider &&) = delete;
  61. ThreadPoolProvider &operator= (ThreadPoolProvider &&) = delete;
  62. };
  63. class ILMTHREAD_EXPORT_TYPE ThreadPool
  64. {
  65. public:
  66. //-------------------------------------------------------
  67. // static routine to query how many processors should be
  68. // used for processing exr files. The user of ThreadPool
  69. // is free to use std::thread::hardware_concurrency or
  70. // whatever number of threads is appropriate based on the
  71. // application. However, this routine exists such that
  72. // in the future, if core counts expand faster than
  73. // memory bandwidth, or higher order NUMA machines are built
  74. // that we can query, this routine gives a place where we
  75. // can centralize that logic
  76. //-------------------------------------------------------
  77. ILMTHREAD_EXPORT
  78. static unsigned estimateThreadCountForFileIO ();
  79. //-------------------------------------------------------
  80. // Constructor -- creates numThreads worker threads which
  81. // wait until a task is available,
  82. // using a default ThreadPoolProvider
  83. //-------------------------------------------------------
  84. ILMTHREAD_EXPORT ThreadPool (unsigned numThreads = 0);
  85. //-----------------------------------------------------------
  86. // Destructor -- waits for all tasks to complete, joins all
  87. // the threads to the calling thread, and then destroys them.
  88. //-----------------------------------------------------------
  89. ILMTHREAD_EXPORT virtual ~ThreadPool ();
  90. ThreadPool (const ThreadPool&) = delete;
  91. ThreadPool& operator= (const ThreadPool&) = delete;
  92. ThreadPool (ThreadPool&&) = delete;
  93. ThreadPool& operator= (ThreadPool&&) = delete;
  94. //--------------------------------------------------------
  95. // Query and set the number of worker threads in the pool.
  96. //
  97. // Warning: never call setNumThreads from within a worker
  98. // thread as this will almost certainly cause a deadlock
  99. // or crash.
  100. //--------------------------------------------------------
  101. ILMTHREAD_EXPORT int numThreads () const;
  102. ILMTHREAD_EXPORT void setNumThreads (int count);
  103. //--------------------------------------------------------
  104. // Set the thread provider for the pool.
  105. //
  106. // The ThreadPool takes ownership of the ThreadPoolProvider
  107. // and will call delete on it when it is finished or when
  108. // it is changed
  109. //
  110. // Warning: never call setThreadProvider from within a worker
  111. // thread as this will almost certainly cause a deadlock
  112. // or crash.
  113. //--------------------------------------------------------
  114. ILMTHREAD_EXPORT void setThreadProvider (ThreadPoolProvider *provider);
  115. //------------------------------------------------------------
  116. // Add a task for processing. The ThreadPool can handle any
  117. // number of tasks regardless of the number of worker threads.
  118. // The tasks are first added onto a queue, and are executed
  119. // by threads as they become available, in FIFO order.
  120. //------------------------------------------------------------
  121. ILMTHREAD_EXPORT void addTask (Task* task);
  122. //-------------------------------------------
  123. // Access functions for the global threadpool
  124. //-------------------------------------------
  125. ILMTHREAD_EXPORT static ThreadPool& globalThreadPool ();
  126. ILMTHREAD_EXPORT static void addGlobalTask (Task* task);
  127. struct ILMTHREAD_HIDDEN Data;
  128. protected:
  129. Data * _data;
  130. };
  131. class ILMTHREAD_EXPORT_TYPE Task
  132. {
  133. public:
  134. ILMTHREAD_EXPORT Task (TaskGroup* g);
  135. ILMTHREAD_EXPORT virtual ~Task ();
  136. Task (const Task&) = delete;
  137. Task &operator= (const Task&) = delete;
  138. Task (Task&&) = delete;
  139. Task& operator= (Task&&) = delete;
  140. virtual void execute () = 0;
  141. ILMTHREAD_EXPORT
  142. TaskGroup * group();
  143. protected:
  144. TaskGroup * _group;
  145. };
  146. class ILMTHREAD_EXPORT_TYPE TaskGroup
  147. {
  148. public:
  149. ILMTHREAD_EXPORT TaskGroup();
  150. ILMTHREAD_EXPORT ~TaskGroup();
  151. TaskGroup (const TaskGroup& other) = delete;
  152. TaskGroup& operator = (const TaskGroup& other) = delete;
  153. TaskGroup (TaskGroup&& other) = delete;
  154. TaskGroup& operator = (TaskGroup&& other) = delete;
  155. // marks one task as finished
  156. // should be used by the thread pool provider to notify
  157. // as it finishes tasks
  158. ILMTHREAD_EXPORT void finishOneTask ();
  159. struct ILMTHREAD_HIDDEN Data;
  160. Data* const _data;
  161. };
  162. ILMTHREAD_INTERNAL_NAMESPACE_HEADER_EXIT
  163. #endif // INCLUDED_ILM_THREAD_POOL_H