task_scheduler_init.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_scheduler_init_H
  24. #define __TBB_task_scheduler_init_H
  25. #include "tbb_stddef.h"
  26. #include "limits.h"
  27. namespace tbb {
  28. typedef std::size_t stack_size_type;
  29. //! @cond INTERNAL
  30. namespace internal {
  31. //! Internal to library. Should not be used by clients.
  32. /** @ingroup task_scheduling */
  33. class scheduler;
  34. } // namespace internal
  35. //! @endcond
  36. //! Class delimiting the scope of task scheduler activity.
  37. /** A thread can construct a task_scheduler_init object and keep it alive
  38. while it uses TBB's tasking subsystem (including parallel algorithms).
  39. This class allows to customize properties of the TBB task pool to some extent.
  40. For example it can limit concurrency level of parallel work initiated by the
  41. given thread. It also can be used to specify stack size of the TBB worker threads,
  42. though this setting is not effective if the thread pool has already been created.
  43. If a parallel construct is used without task_scheduler_init object previously
  44. created, the scheduler will be initialized automatically with default settings,
  45. and will persist until this thread exits. Default concurrency level is defined
  46. as described in task_scheduler_init::initialize().
  47. @ingroup task_scheduling */
  48. class task_scheduler_init: internal::no_copy {
  49. enum ExceptionPropagationMode {
  50. propagation_mode_exact = 1u,
  51. propagation_mode_captured = 2u,
  52. propagation_mode_mask = propagation_mode_exact | propagation_mode_captured
  53. };
  54. #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
  55. enum {
  56. wait_workers_in_terminate_flag = 128u
  57. };
  58. #endif
  59. /** NULL if not currently initialized. */
  60. internal::scheduler* my_scheduler;
  61. public:
  62. //! Typedef for number of threads that is automatic.
  63. static const int automatic = -1;
  64. //! Argument to initialize() or constructor that causes initialization to be deferred.
  65. static const int deferred = -2;
  66. //! Ensure that scheduler exists for this thread
  67. /** A value of -1 lets TBB decide on the number of threads, which is usually
  68. maximal hardware concurrency for this process, that is the number of logical
  69. CPUs on the machine (possibly limited by the processor affinity mask of this
  70. process (Windows) or of this thread (Linux, FreeBSD). It is preferable option
  71. for production code because it helps to avoid nasty surprises when several
  72. TBB based components run side-by-side or in a nested fashion inside the same
  73. process.
  74. The number_of_threads is ignored if any other task_scheduler_inits
  75. currently exist. A thread may construct multiple task_scheduler_inits.
  76. Doing so does no harm because the underlying scheduler is reference counted. */
  77. void __TBB_EXPORTED_METHOD initialize( int number_of_threads=automatic );
  78. //! The overloaded method with stack size parameter
  79. /** Overloading is necessary to preserve ABI compatibility */
  80. void __TBB_EXPORTED_METHOD initialize( int number_of_threads, stack_size_type thread_stack_size );
  81. //! Inverse of method initialize.
  82. void __TBB_EXPORTED_METHOD terminate();
  83. //! Shorthand for default constructor followed by call to initialize(number_of_threads).
  84. #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
  85. task_scheduler_init( int number_of_threads=automatic, stack_size_type thread_stack_size=0, bool wait_workers_in_terminate = false )
  86. #else
  87. task_scheduler_init( int number_of_threads=automatic, stack_size_type thread_stack_size=0 )
  88. #endif
  89. : my_scheduler(NULL) {
  90. // Two lowest order bits of the stack size argument may be taken to communicate
  91. // default exception propagation mode of the client to be used when the
  92. // client manually creates tasks in the master thread and does not use
  93. // explicit task group context object. This is necessary because newer
  94. // TBB binaries with exact propagation enabled by default may be used
  95. // by older clients that expect tbb::captured_exception wrapper.
  96. // All zeros mean old client - no preference.
  97. __TBB_ASSERT( !(thread_stack_size & propagation_mode_mask), "Requested stack size is not aligned" );
  98. #if TBB_USE_EXCEPTIONS
  99. thread_stack_size |= TBB_USE_CAPTURED_EXCEPTION ? propagation_mode_captured : propagation_mode_exact;
  100. #endif /* TBB_USE_EXCEPTIONS */
  101. #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
  102. if (wait_workers_in_terminate)
  103. my_scheduler = (internal::scheduler*)wait_workers_in_terminate_flag;
  104. #endif
  105. initialize( number_of_threads, thread_stack_size );
  106. }
  107. //! Destroy scheduler for this thread if thread has no other live task_scheduler_inits.
  108. ~task_scheduler_init() {
  109. if( my_scheduler )
  110. terminate();
  111. internal::poison_pointer( my_scheduler );
  112. }
  113. //! Returns the number of threads TBB scheduler would create if initialized by default.
  114. /** Result returned by this method does not depend on whether the scheduler
  115. has already been initialized.
  116. Because tbb 2.0 does not support blocking tasks yet, you may use this method
  117. to boost the number of threads in the tbb's internal pool, if your tasks are
  118. doing I/O operations. The optimal number of additional threads depends on how
  119. much time your tasks spend in the blocked state.
  120. Before TBB 3.0 U4 this method returned the number of logical CPU in the
  121. system. Currently on Windows, Linux and FreeBSD it returns the number of
  122. logical CPUs available to the current process in accordance with its affinity
  123. mask.
  124. NOTE: The return value of this method never changes after its first invocation.
  125. This means that changes in the process affinity mask that took place after
  126. this method was first invoked will not affect the number of worker threads
  127. in the TBB worker threads pool. */
  128. static int __TBB_EXPORTED_FUNC default_num_threads ();
  129. //! Returns true if scheduler is active (initialized); false otherwise
  130. bool is_active() const { return my_scheduler != NULL; }
  131. };
  132. } // namespace tbb
  133. #endif /* __TBB_task_scheduler_init_H */