1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef _TBB_tbb_main_H
18 #define _TBB_tbb_main_H
19 
20 #include "tbb/atomic.h"
21 #include "governor.h"
22 #include "tbb_environment.h"
23 
24 namespace tbb {
25 
26 namespace internal {
27 
28 void DoOneTimeInitializations ();
29 
30 //------------------------------------------------------------------------
31 // __TBB_InitOnce
32 //------------------------------------------------------------------------
33 
34 //! Class that supports TBB initialization.
35 /** It handles acquisition and release of global resources (e.g. TLS) during startup and shutdown,
36     as well as synchronization for DoOneTimeInitializations. */
37 class __TBB_InitOnce {
38     friend void DoOneTimeInitializations();
39     friend void ITT_DoUnsafeOneTimeInitialization ();
40 
41     static atomic<int> count;
42 
43     //! Platform specific code to acquire resources.
44     static void acquire_resources();
45 
46     //! Platform specific code to release resources.
47     static void release_resources();
48 
49     //! Specifies if the one-time initializations has been done.
50     static bool InitializationDone;
51 
52     //! Global initialization lock
53     /** Scenarios are possible when tools interop has to be initialized before the
54         TBB itself. This imposes a requirement that the global initialization lock
55         has to support valid static initialization, and does not issue any tool
56         notifications in any build mode. **/
57     static __TBB_atomic_flag InitializationLock;
58 
59 public:
lock()60     static void lock()   { __TBB_LockByte( InitializationLock ); }
61 
unlock()62     static void unlock() { __TBB_UnlockByte( InitializationLock ); }
63 
initialization_done()64     static bool initialization_done() { return __TBB_load_with_acquire(InitializationDone); }
65 
66     //! Add initial reference to resources.
67     /** We assume that dynamic loading of the library prevents any other threads
68         from entering the library until this constructor has finished running. **/
__TBB_InitOnce()69     __TBB_InitOnce() { add_ref(); }
70 
71     //! Remove the initial reference to resources.
72     /** This is not necessarily the last reference if other threads are still running. **/
~__TBB_InitOnce()73     ~__TBB_InitOnce() {
74         governor::terminate_auto_initialized_scheduler(); // TLS dtor not called for the main thread
75         remove_ref();
76         // We assume that InitializationDone is not set after file-scope destructors
77         // start running, and thus no race on InitializationDone is possible.
78         if( initialization_done() ) {
79             // Remove an extra reference that was added in DoOneTimeInitializations.
80             remove_ref();
81         }
82     }
83     //! Add reference to resources.  If first reference added, acquire the resources.
84     static void add_ref();
85 
86     //! Remove reference to resources.  If last reference removed, release the resources.
87     static void remove_ref();
88 }; // class __TBB_InitOnce
89 
90 
91 } // namespace internal
92 
93 } // namespace tbb
94 
95 #endif /* _TBB_tbb_main_H */
96