Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:00

0001 /*
0002     Copyright (c) 2005-2020 Intel Corporation
0003 
0004     Licensed under the Apache License, Version 2.0 (the "License");
0005     you may not use this file except in compliance with the License.
0006     You may obtain a copy of the License at
0007 
0008         http://www.apache.org/licenses/LICENSE-2.0
0009 
0010     Unless required by applicable law or agreed to in writing, software
0011     distributed under the License is distributed on an "AS IS" BASIS,
0012     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013     See the License for the specific language governing permissions and
0014     limitations under the License.
0015 */
0016 
0017 #include "internal/_deprecated_header_message_guard.h"
0018 
0019 #if !defined(__TBB_show_deprecation_message_task_scheduler_init_H) && defined(__TBB_show_deprecated_header_message)
0020 #define  __TBB_show_deprecation_message_task_scheduler_init_H
0021 #pragma message("TBB Warning: tbb/task_scheduler_init.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
0022 #endif
0023 
0024 #if defined(__TBB_show_deprecated_header_message)
0025 #undef __TBB_show_deprecated_header_message
0026 #endif
0027 
0028 #ifndef __TBB_task_scheduler_init_H
0029 #define __TBB_task_scheduler_init_H
0030 
0031 #define __TBB_task_scheduler_init_H_include_area
0032 #include "internal/_warning_suppress_enable_notice.h"
0033 
0034 #include "tbb_stddef.h"
0035 #include "limits.h"
0036 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
0037 #include <new> // nothrow_t
0038 #endif
0039 
0040 namespace tbb {
0041 
0042 typedef std::size_t stack_size_type;
0043 
0044 //! @cond INTERNAL
0045 namespace internal {
0046     //! Internal to library. Should not be used by clients.
0047     /** @ingroup task_scheduling */
0048     class scheduler;
0049 } // namespace internal
0050 //! @endcond
0051 
0052 //! Class delimiting the scope of task scheduler activity.
0053 /** A thread can construct a task_scheduler_init object and keep it alive
0054     while it uses TBB's tasking subsystem (including parallel algorithms).
0055 
0056     This class allows to customize properties of the TBB task pool to some extent.
0057     For example it can limit concurrency level of parallel work initiated by the
0058     given thread. It also can be used to specify stack size of the TBB worker threads,
0059     though this setting is not effective if the thread pool has already been created.
0060 
0061     If a parallel construct is used without task_scheduler_init object previously
0062     created, the scheduler will be initialized automatically with default settings,
0063     and will persist until this thread exits. Default concurrency level is defined
0064     as described in task_scheduler_init::initialize().
0065     @ingroup task_scheduling */
0066 class __TBB_DEPRECATED_IN_VERBOSE_MODE task_scheduler_init: internal::no_copy {
0067     enum ExceptionPropagationMode {
0068         propagation_mode_exact = 1u,
0069         propagation_mode_captured = 2u,
0070         propagation_mode_mask = propagation_mode_exact | propagation_mode_captured
0071     };
0072 
0073     /** NULL if not currently initialized. */
0074     internal::scheduler* my_scheduler;
0075 
0076     bool internal_terminate( bool blocking );
0077 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
0078     bool __TBB_EXPORTED_METHOD internal_blocking_terminate( bool throwing );
0079 #endif
0080 public:
0081 
0082     //! Typedef for number of threads that is automatic.
0083     static const int automatic = -1;
0084 
0085     //! Argument to initialize() or constructor that causes initialization to be deferred.
0086     static const int deferred = -2;
0087 
0088     //! Ensure that scheduler exists for this thread
0089     /** A value of -1 lets TBB decide on the number of threads, which is usually
0090         maximal hardware concurrency for this process, that is the number of logical
0091         CPUs on the machine (possibly limited by the processor affinity mask of this
0092         process (Windows) or of this thread (Linux, FreeBSD). It is preferable option
0093         for production code because it helps to avoid nasty surprises when several
0094         TBB based components run side-by-side or in a nested fashion inside the same
0095         process.
0096 
0097         The number_of_threads is ignored if any other task_scheduler_inits
0098         currently exist.  A thread may construct multiple task_scheduler_inits.
0099         Doing so does no harm because the underlying scheduler is reference counted. */
0100     void __TBB_EXPORTED_METHOD initialize( int number_of_threads=automatic );
0101 
0102     //! The overloaded method with stack size parameter
0103     /** Overloading is necessary to preserve ABI compatibility */
0104     void __TBB_EXPORTED_METHOD initialize( int number_of_threads, stack_size_type thread_stack_size );
0105 
0106     //! Inverse of method initialize.
0107     void __TBB_EXPORTED_METHOD terminate();
0108 
0109 #if __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
0110 #if TBB_USE_EXCEPTIONS
0111     //! terminate() that waits for worker threads termination. Throws exception on error.
0112     void blocking_terminate() {
0113         internal_blocking_terminate( /*throwing=*/true );
0114     }
0115 #endif
0116     //! terminate() that waits for worker threads termination. Returns false on error.
0117     bool blocking_terminate(const std::nothrow_t&) __TBB_NOEXCEPT(true) {
0118         return internal_blocking_terminate( /*throwing=*/false );
0119     }
0120 #endif // __TBB_SUPPORTS_WORKERS_WAITING_IN_TERMINATE
0121 
0122     //! Shorthand for default constructor followed by call to initialize(number_of_threads).
0123     task_scheduler_init( int number_of_threads=automatic, stack_size_type thread_stack_size=0 ) : my_scheduler(NULL)
0124     {
0125         // Two lowest order bits of the stack size argument may be taken to communicate
0126         // default exception propagation mode of the client to be used when the
0127         // client manually creates tasks in the master thread and does not use
0128         // explicit task group context object. This is necessary because newer
0129         // TBB binaries with exact propagation enabled by default may be used
0130         // by older clients that expect tbb::captured_exception wrapper.
0131         // All zeros mean old client - no preference.
0132         __TBB_ASSERT( !(thread_stack_size & propagation_mode_mask), "Requested stack size is not aligned" );
0133 #if TBB_USE_EXCEPTIONS
0134         thread_stack_size |= TBB_USE_CAPTURED_EXCEPTION ? propagation_mode_captured : propagation_mode_exact;
0135 #endif /* TBB_USE_EXCEPTIONS */
0136         initialize( number_of_threads, thread_stack_size );
0137     }
0138 
0139     //! Destroy scheduler for this thread if thread has no other live task_scheduler_inits.
0140     ~task_scheduler_init() {
0141         if( my_scheduler )
0142             terminate();
0143         internal::poison_pointer( my_scheduler );
0144     }
0145     //! Returns the number of threads TBB scheduler would create if initialized by default.
0146     /** Result returned by this method does not depend on whether the scheduler
0147         has already been initialized.
0148 
0149         Because TBB 2.0 does not support blocking tasks yet, you may use this method
0150         to boost the number of threads in the TBB's internal pool, if your tasks are
0151         doing I/O operations. The optimal number of additional threads depends on how
0152         much time your tasks spend in the blocked state.
0153 
0154         Before TBB 3.0 U4 this method returned the number of logical CPU in the
0155         system. Currently on Windows, Linux and FreeBSD it returns the number of
0156         logical CPUs available to the current process in accordance with its affinity
0157         mask.
0158 
0159         NOTE: The return value of this method never changes after its first invocation.
0160         This means that changes in the process affinity mask that took place after
0161         this method was first invoked will not affect the number of worker threads
0162         in the TBB worker threads pool. */
0163     static int __TBB_EXPORTED_FUNC default_num_threads ();
0164 
0165     //! Returns true if scheduler is active (initialized); false otherwise
0166     bool is_active() const { return my_scheduler != NULL; }
0167 };
0168 
0169 } // namespace tbb
0170 
0171 #include "internal/_warning_suppress_disable_notice.h"
0172 #undef __TBB_task_scheduler_init_H_include_area
0173 
0174 #endif /* __TBB_task_scheduler_init_H */