You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
9.0 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_task_arena_H
  24. #define __TBB_task_arena_H
  25. #include "task.h"
  26. #include "tbb_exception.h"
  27. #if TBB_USE_THREADING_TOOLS
  28. #include "atomic.h" // for as_atomic
  29. #endif
  30. #if __TBB_TASK_ARENA
  31. namespace tbb {
  32. //! @cond INTERNAL
  33. namespace internal {
  34. //! Internal to library. Should not be used by clients.
  35. /** @ingroup task_scheduling */
  36. class arena;
  37. class task_scheduler_observer_v3;
  38. } // namespace internal
  39. //! @endcond
  40. namespace interface7 {
  41. //! @cond INTERNAL
  42. namespace internal {
  43. using namespace tbb::internal; //e.g. function_task from task.h
  44. class delegate_base : no_assign {
  45. public:
  46. virtual void operator()() const = 0;
  47. virtual ~delegate_base() {}
  48. };
  49. template<typename F>
  50. class delegated_function : public delegate_base {
  51. F &my_func;
  52. /*override*/ void operator()() const {
  53. my_func();
  54. }
  55. public:
  56. delegated_function ( F& f ) : my_func(f) {}
  57. };
  58. class task_arena_base {
  59. protected:
  60. //! NULL if not currently initialized.
  61. internal::arena* my_arena;
  62. #if __TBB_TASK_GROUP_CONTEXT
  63. //! default context of the arena
  64. task_group_context *my_context;
  65. #endif
  66. //! Concurrency level for deferred initialization
  67. int my_max_concurrency;
  68. //! Reserved master slots
  69. unsigned my_master_slots;
  70. //! Reserved for future use
  71. intptr_t my_reserved;
  72. task_arena_base(int max_concurrency, unsigned reserved_for_masters)
  73. : my_arena(0)
  74. #if __TBB_TASK_GROUP_CONTEXT
  75. , my_context(0)
  76. #endif
  77. , my_max_concurrency(max_concurrency)
  78. , my_master_slots(reserved_for_masters)
  79. , my_reserved(0)
  80. {}
  81. void __TBB_EXPORTED_METHOD internal_initialize( );
  82. void __TBB_EXPORTED_METHOD internal_terminate( );
  83. void __TBB_EXPORTED_METHOD internal_enqueue( task&, intptr_t ) const;
  84. void __TBB_EXPORTED_METHOD internal_execute( delegate_base& ) const;
  85. void __TBB_EXPORTED_METHOD internal_wait() const;
  86. static int __TBB_EXPORTED_FUNC internal_current_slot();
  87. public:
  88. //! Typedef for number of threads that is automatic.
  89. static const int automatic = -1; // any value < 1 means 'automatic'
  90. };
  91. } // namespace internal
  92. //! @endcond
  93. /** 1-to-1 proxy representation class of scheduler's arena
  94. * Constructors set up settings only, real construction is deferred till the first method invocation
  95. * Destructor only removes one of the references to the inner arena representation.
  96. * Final destruction happens when all the references (and the work) are gone.
  97. */
  98. class task_arena : public internal::task_arena_base {
  99. friend class tbb::internal::task_scheduler_observer_v3;
  100. bool my_initialized;
  101. public:
  102. //! Creates task_arena with certain concurrency limits
  103. /** Sets up settings only, real construction is deferred till the first method invocation
  104. * @arg max_concurrency specifies total number of slots in arena where threads work
  105. * @arg reserved_for_masters specifies number of slots to be used by master threads only.
  106. * Value of 1 is default and reflects behavior of implicit arenas.
  107. **/
  108. task_arena(int max_concurrency = automatic, unsigned reserved_for_masters = 1)
  109. : task_arena_base(max_concurrency, reserved_for_masters)
  110. , my_initialized(false)
  111. {}
  112. //! Copies settings from another task_arena
  113. task_arena(const task_arena &s) // copy settings but not the reference or instance
  114. : task_arena_base(s.my_max_concurrency, s.my_master_slots)
  115. , my_initialized(false)
  116. {}
  117. //! Forces allocation of the resources for the task_arena as specified in constructor arguments
  118. inline void initialize() {
  119. if( !my_initialized ) {
  120. internal_initialize();
  121. #if TBB_USE_THREADING_TOOLS
  122. // Threading tools respect lock prefix but report false-positive data-race via plain store
  123. internal::as_atomic(my_initialized).fetch_and_store<release>(true);
  124. #else
  125. my_initialized = true;
  126. #endif //TBB_USE_THREADING_TOOLS
  127. }
  128. }
  129. //! Overrides concurrency level and forces initialization of internal representation
  130. inline void initialize(int max_concurrency, unsigned reserved_for_masters = 1) {
  131. __TBB_ASSERT( !my_arena, "Impossible to modify settings of an already initialized task_arena");
  132. if( !my_initialized ) {
  133. my_max_concurrency = max_concurrency;
  134. my_master_slots = reserved_for_masters;
  135. initialize();
  136. }
  137. }
  138. //! Removes the reference to the internal arena representation.
  139. //! Not thread safe wrt concurrent invocations of other methods.
  140. inline void terminate() {
  141. if( my_initialized ) {
  142. internal_terminate();
  143. my_initialized = false;
  144. }
  145. }
  146. //! Removes the reference to the internal arena representation, and destroys the external object.
  147. //! Not thread safe wrt concurrent invocations of other methods.
  148. ~task_arena() {
  149. terminate();
  150. }
  151. //! Returns true if the arena is active (initialized); false otherwise.
  152. //! The name was chosen to match a task_scheduler_init method with the same semantics.
  153. bool is_active() const { return my_initialized; }
  154. //! Enqueues a task into the arena to process a functor, and immediately returns.
  155. //! Does not require the calling thread to join the arena
  156. template<typename F>
  157. void enqueue( const F& f ) {
  158. initialize();
  159. #if __TBB_TASK_GROUP_CONTEXT
  160. internal_enqueue( *new( task::allocate_root(*my_context) ) internal::function_task<F>(f), 0 );
  161. #else
  162. internal_enqueue( *new( task::allocate_root() ) internal::function_task<F>(f), 0 );
  163. #endif
  164. }
  165. #if __TBB_TASK_PRIORITY
  166. //! Enqueues a task with priority p into the arena to process a functor f, and immediately returns.
  167. //! Does not require the calling thread to join the arena
  168. template<typename F>
  169. void enqueue( const F& f, priority_t p ) {
  170. __TBB_ASSERT( p == priority_low || p == priority_normal || p == priority_high, "Invalid priority level value" );
  171. initialize();
  172. #if __TBB_TASK_GROUP_CONTEXT
  173. internal_enqueue( *new( task::allocate_root(*my_context) ) internal::function_task<F>(f), (intptr_t)p );
  174. #else
  175. internal_enqueue( *new( task::allocate_root() ) internal::function_task<F>(f), (intptr_t)p );
  176. #endif
  177. }
  178. #endif// __TBB_TASK_PRIORITY
  179. //! Joins the arena and executes a functor, then returns
  180. //! If not possible to join, wraps the functor into a task, enqueues it and waits for task completion
  181. //! Can decrement the arena demand for workers, causing a worker to leave and free a slot to the calling thread
  182. template<typename F>
  183. void execute(F& f) {
  184. initialize();
  185. internal::delegated_function<F> d(f);
  186. internal_execute( d );
  187. }
  188. //! Joins the arena and executes a functor, then returns
  189. //! If not possible to join, wraps the functor into a task, enqueues it and waits for task completion
  190. //! Can decrement the arena demand for workers, causing a worker to leave and free a slot to the calling thread
  191. template<typename F>
  192. void execute(const F& f) {
  193. initialize();
  194. internal::delegated_function<const F> d(f);
  195. internal_execute( d );
  196. }
  197. #if __TBB_EXTRA_DEBUG
  198. //! Wait for all work in the arena to be completed
  199. //! Even submitted by other application threads
  200. //! Joins arena if/when possible (in the same way as execute())
  201. void debug_wait_until_empty() {
  202. initialize();
  203. internal_wait();
  204. }
  205. #endif //__TBB_EXTRA_DEBUG
  206. //! Returns the index, aka slot number, of the calling thread in its current arena
  207. inline static int current_slot() {
  208. return internal_current_slot();
  209. }
  210. };
  211. } // namespace interfaceX
  212. using interface7::task_arena;
  213. } // namespace tbb
  214. #endif /* __TBB_TASK_ARENA */
  215. #endif /* __TBB_task_arena_H */