BRE12
task_scheduler_init.h
1 /*
2  Copyright 2005-2016 Intel Corporation. All Rights Reserved.
3 
4  This file is part of Threading Building Blocks. Threading Building Blocks is free software;
5  you can redistribute it and/or modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation. Threading Building Blocks is
7  distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9  See the GNU General Public License for more details. You should have received a copy of
10  the GNU General Public License along with Threading Building Blocks; if not, write to the
11  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12 
13  As a special exception, you may use this file as part of a free software library without
14  restriction. Specifically, if other files instantiate templates or use macros or inline
15  functions from this file, or you compile this file and link it with other files to produce
16  an executable, this file does not by itself cause the resulting executable to be covered
17  by the GNU General Public License. This exception does not however invalidate any other
18  reasons why the executable file might be covered by the GNU General Public License.
19 */
20 
21 #ifndef __TBB_task_scheduler_init_H
22 #define __TBB_task_scheduler_init_H
23 
24 #include "tbb_stddef.h"
25 #include "limits.h"
26 
27 namespace tbb {
28 
29 typedef std::size_t stack_size_type;
30 
32 namespace internal {
34 
35  class scheduler;
36 } // namespace internal
38 
40 
53 class task_scheduler_init: internal::no_copy {
54  enum ExceptionPropagationMode {
55  propagation_mode_exact = 1u,
56  propagation_mode_captured = 2u,
57  propagation_mode_mask = propagation_mode_exact | propagation_mode_captured
58  };
59 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
60  enum {
61  wait_workers_in_terminate_flag = 128u
62  };
63 #endif
64 
66  internal::scheduler* my_scheduler;
67 public:
68 
70  static const int automatic = -1;
71 
73  static const int deferred = -2;
74 
76 
87  void __TBB_EXPORTED_METHOD initialize( int number_of_threads=automatic );
88 
90 
91  void __TBB_EXPORTED_METHOD initialize( int number_of_threads, stack_size_type thread_stack_size );
92 
94  void __TBB_EXPORTED_METHOD terminate();
95 
97 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
98  task_scheduler_init( int number_of_threads=automatic, stack_size_type thread_stack_size=0, bool wait_workers_in_terminate = false ) : my_scheduler(NULL)
99 #else
100  task_scheduler_init( int number_of_threads=automatic, stack_size_type thread_stack_size=0 ) : my_scheduler(NULL)
101 #endif
102  {
103  // Two lowest order bits of the stack size argument may be taken to communicate
104  // default exception propagation mode of the client to be used when the
105  // client manually creates tasks in the master thread and does not use
106  // explicit task group context object. This is necessary because newer
107  // TBB binaries with exact propagation enabled by default may be used
108  // by older clients that expect tbb::captured_exception wrapper.
109  // All zeros mean old client - no preference.
110  __TBB_ASSERT( !(thread_stack_size & propagation_mode_mask), "Requested stack size is not aligned" );
111 #if TBB_USE_EXCEPTIONS
112  thread_stack_size |= TBB_USE_CAPTURED_EXCEPTION ? propagation_mode_captured : propagation_mode_exact;
113 #endif /* TBB_USE_EXCEPTIONS */
114 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
115  if (wait_workers_in_terminate)
116  my_scheduler = (internal::scheduler*)wait_workers_in_terminate_flag;
117 #endif
118  initialize( number_of_threads, thread_stack_size );
119  }
120 
123  if( my_scheduler )
124  terminate();
125  internal::poison_pointer( my_scheduler );
126  }
128 
145  static int __TBB_EXPORTED_FUNC default_num_threads ();
146 
148  bool is_active() const { return my_scheduler != NULL; }
149 };
150 
151 } // namespace tbb
152 
153 #endif /* __TBB_task_scheduler_init_H */
Class delimiting the scope of task scheduler activity.
Definition: task_scheduler_init.h:53
bool is_active() const
Returns true if scheduler is active (initialized); false otherwise.
Definition: task_scheduler_init.h:148
*/
Definition: material.h:665
Definition: _flow_graph_async_msg_impl.h:32
~task_scheduler_init()
Destroy scheduler for this thread if thread has no other live task_scheduler_inits.
Definition: task_scheduler_init.h:122
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44