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.

228 lines
8.2 KiB

  1. /*
  2. Copyright 2005-2013 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_TASK_ARENA
  28. namespace tbb {
  29. //! @cond INTERNAL
  30. namespace internal {
  31. //! Internal to library. Should not be used by clients.
  32. /** @ingroup task_scheduling */
  33. class arena;
  34. class task_scheduler_observer_v3;
  35. } // namespace internal
  36. //! @endcond
  37. namespace interface6 {
  38. //! @cond INTERNAL
  39. namespace internal {
  40. using namespace tbb::internal;
  41. template<typename F>
  42. class enqueued_function_task : public task { // TODO: reuse from task_group?
  43. F my_func;
  44. /*override*/ task* execute() {
  45. my_func();
  46. return NULL;
  47. }
  48. public:
  49. enqueued_function_task ( const F& f ) : my_func(f) {}
  50. };
  51. class delegate_base : no_assign {
  52. public:
  53. virtual void operator()() const = 0;
  54. virtual ~delegate_base() {}
  55. };
  56. template<typename F>
  57. class delegated_function : public delegate_base {
  58. F &my_func;
  59. /*override*/ void operator()() const {
  60. my_func();
  61. }
  62. public:
  63. delegated_function ( F& f ) : my_func(f) {}
  64. };
  65. } // namespace internal
  66. //! @endcond
  67. /** 1-to-1 proxy representation class of scheduler's arena
  68. * Constructors set up settings only, real construction is deferred till the first method invocation
  69. * TODO: A side effect of this is that it's impossible to create a const task_arena object. Rethink?
  70. * Destructor only removes one of the references to the inner arena representation.
  71. * Final destruction happens when all the references (and the work) are gone.
  72. */
  73. class task_arena {
  74. friend class internal::task_scheduler_observer_v3;
  75. //! Concurrency level for deferred initialization
  76. int my_max_concurrency;
  77. //! Reserved master slots
  78. unsigned my_master_slots;
  79. //! NULL if not currently initialized.
  80. internal::arena* my_arena;
  81. // Initialization flag enabling compiler to throw excessive lazy initialization checks
  82. bool my_initialized;
  83. // const methods help to optimize the !my_arena check TODO: check, IDEA: move to base-class?
  84. void __TBB_EXPORTED_METHOD internal_initialize( );
  85. void __TBB_EXPORTED_METHOD internal_terminate( );
  86. void __TBB_EXPORTED_METHOD internal_enqueue( task&, intptr_t ) const;
  87. void __TBB_EXPORTED_METHOD internal_execute( internal::delegate_base& ) const;
  88. void __TBB_EXPORTED_METHOD internal_wait() const;
  89. public:
  90. //! Typedef for number of threads that is automatic.
  91. static const int automatic = -1; // any value < 1 means 'automatic'
  92. //! Creates task_arena with certain concurrency limits
  93. /** @arg max_concurrency specifies total number of slots in arena where threads work
  94. * @arg reserved_for_masters specifies number of slots to be used by master threads only.
  95. * Value of 1 is default and reflects behavior of implicit arenas.
  96. **/
  97. task_arena(int max_concurrency = automatic, unsigned reserved_for_masters = 1)
  98. : my_max_concurrency(max_concurrency)
  99. , my_master_slots(reserved_for_masters)
  100. , my_arena(0)
  101. , my_initialized(false)
  102. {}
  103. //! Copies settings from another task_arena
  104. task_arena(const task_arena &s)
  105. : my_max_concurrency(s.my_max_concurrency) // copy settings
  106. , my_master_slots(s.my_master_slots)
  107. , my_arena(0) // but not the reference or instance
  108. , my_initialized(false)
  109. {}
  110. inline void initialize() {
  111. if( !my_initialized ) {
  112. internal_initialize();
  113. my_initialized = true;
  114. }
  115. }
  116. //! Overrides concurrency level and forces initialization of internal representation
  117. inline void initialize(int max_concurrency, unsigned reserved_for_masters = 1) {
  118. __TBB_ASSERT( !my_arena, "task_arena was initialized already");
  119. if( !my_initialized ) {
  120. my_max_concurrency = max_concurrency;
  121. my_master_slots = reserved_for_masters;
  122. initialize();
  123. } // TODO: else throw?
  124. }
  125. //! Removes the reference to the internal arena representation.
  126. //! Not thread safe wrt concurrent invocations of other methods.
  127. inline void terminate() {
  128. if( my_initialized ) {
  129. internal_terminate();
  130. my_initialized = false;
  131. }
  132. }
  133. //! Removes the reference to the internal arena representation, and destroys the external object.
  134. //! Not thread safe wrt concurrent invocations of other methods.
  135. ~task_arena() {
  136. terminate();
  137. }
  138. //! Returns true if the arena is active (initialized); false otherwise.
  139. //! The name was chosen to match a task_scheduler_init method with the same semantics.
  140. bool is_active() const { return my_initialized; }
  141. //! Enqueues a task into the arena to process a functor, and immediately returns.
  142. //! Does not require the calling thread to join the arena
  143. template<typename F>
  144. void enqueue( const F& f ) {
  145. initialize();
  146. internal_enqueue( *new( task::allocate_root() ) internal::enqueued_function_task<F>(f), 0 );
  147. }
  148. #if __TBB_TASK_PRIORITY
  149. //! Enqueues a task with priority p into the arena to process a functor f, and immediately returns.
  150. //! Does not require the calling thread to join the arena
  151. template<typename F>
  152. void enqueue( const F& f, priority_t p ) {
  153. __TBB_ASSERT( p == priority_low || p == priority_normal || p == priority_high, "Invalid priority level value" );
  154. initialize();
  155. internal_enqueue( *new( task::allocate_root() ) internal::enqueued_function_task<F>(f), (intptr_t)p );
  156. }
  157. #endif// __TBB_TASK_PRIORITY
  158. //! Joins the arena and executes a functor, then returns
  159. //! If not possible to join, wraps the functor into a task, enqueues it and waits for task completion
  160. //! Can decrement the arena demand for workers, causing a worker to leave and free a slot to the calling thread
  161. template<typename F>
  162. void execute(F& f) {
  163. initialize();
  164. internal::delegated_function<F> d(f);
  165. internal_execute( d );
  166. }
  167. //! Joins the arena and executes a functor, then returns
  168. //! If not possible to join, wraps the functor into a task, enqueues it and waits for task completion
  169. //! Can decrement the arena demand for workers, causing a worker to leave and free a slot to the calling thread
  170. template<typename F>
  171. void execute(const F& f) {
  172. initialize();
  173. internal::delegated_function<const F> d(f);
  174. internal_execute( d );
  175. }
  176. //! Wait for all work in the arena to be completed
  177. //! Even submitted by other application threads
  178. //! Joins arena if/when possible (in the same way as execute())
  179. void wait_until_empty() {
  180. initialize();
  181. internal_wait();
  182. }
  183. //! Returns the index, aka slot number, of the calling thread in its current arena
  184. static int __TBB_EXPORTED_FUNC current_slot();
  185. };
  186. } // namespace interfaceX
  187. using interface6::task_arena;
  188. } // namespace tbb
  189. #endif /* __TBB_TASK_ARENA */
  190. #endif /* __TBB_task_arena_H */