ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
bootstrap.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !DOXYGEN
10# include "alib/alib.hpp"
14# if ALIB_BOXING
15# include "alib/boxing/boxing.hpp"
16# endif
17# if ALIB_CONFIGURATION
21# endif
23# if ALIB_TIME
24# include "alib/time/time.hpp"
25# endif
26# if ALIB_THREADMODEL
28# endif
29# if ALIB_THREADS
31# endif
32# if ALIB_ALOX
33# include "alib/alox/aloxcamp.hpp"
34# endif
35# if ALIB_CLI
36# include "alib/cli/clicamp.hpp"
37# endif
38# if ALIB_EXPRESSIONS
40# endif
41# if ALIB_FILES
43# endif
46#endif // !DOXYGEN
47
48//#include <iostream>
49
50using namespace alib::lang;
51
52namespace alib {
53
54#if !DOXYGEN
57#endif
58
60{
61 // if the global allocator was not initialized from outside, then we have to do it.
62 if (!monomem::GLOBAL_ALLOCATOR.IsInitialized())
64
65 ALIB_ASSERT_ERROR( CAMPS.IsEmpty(), "CAMPS", "List CAMPS already set." )
66
67 IF_ALIB_CAMP( CAMPS.PushBack( &BASECAMP ); )
68 IF_ALIB_CONFIGURATION( CAMPS.PushBack( &CONFIG ); )
69 IF_ALIB_ALOX( CAMPS.PushBack( &ALOX ); )
70 IF_ALIB_CLI( CAMPS.PushBack( &CLI ); )
72 IF_ALIB_FILES( CAMPS.PushBack( &FILES ); )
73}
74
75void Bootstrap( BootstrapPhases targetPhase,
76 Camp* targetCamp,
77 int alibVersion, int alibRevision, TCompilationFlags compilationFlags )
78{
79 // verify ALib
80 AssertALibVersionAndFlags( alibVersion, alibRevision, compilationFlags );
81
82 #if ALIB_MONOMEM
83 // if the global allocator was not initialized from outside, then we have to do it.
84 if (!monomem::GLOBAL_ALLOCATOR.IsInitialized())
86 #endif
87
88 // if not customized, create default module list
89 if( CAMPS.IsEmpty() )
91
92 if( targetCamp == nullptr )
93 targetCamp= CAMPS.Back();
94
95 //std::cout << "Camp::Bootstrap called on: '" << this->ResourceCategory << "', target phase: " << int(targetPhase) << std::endl;
96
97 // Initialize non-camp modules once
99 {
100 IF_ALIB_TIME (time ::Bootstrap ( );)
102
103 // now we add the base-camps' boxing vtables already. This is primarily needed
104 // for assertions in debug-compilation ( vt_alib_report_types )
105 ALIB_BOXING_BOOTSTRAP_VTABLE_DBG_REGISTER( vt_system_exceptions )
106 ALIB_BOXING_BOOTSTRAP_VTABLE_DBG_REGISTER( vt_system_systemerrors )
108 ALIB_BOXING_BOOTSTRAP_VTABLE_DBG_REGISTER( vt_system_fmtexceptions)
110 ALIB_BOXING_BOOTSTRAP_VTABLE_DBG_REGISTER( vt_alib_report_types )
111
112 IF_ALIB_THREADS (threads ::Bootstrap ( );)
113 IF_ALIB_ENUMS (enums ::Bootstrap ( );)
116 }
117
118 // find target camp in the list of camps
119 auto targetCampIt= CAMPS.rbegin();
120 while(targetCampIt != CAMPS.rend() &&
121 *targetCampIt != targetCamp )
122 ++targetCampIt;
123
124 ALIB_ASSERT_ERROR(targetCampIt != CAMPS.rend(), "CAMPS",
125 "Target camp given to function alib::Bootstrap() is not included in list alib::CAMPS.\n"
126 "Resource category of the target camp: ", targetCamp->ResourceCategory )
127
128
129 // loop over all phases
130 for ( int phaseIntegral= targetCamp->bootstrapState + 1 ;
131 phaseIntegral <= UnderlyingIntegral( targetPhase ) ;
132 ++phaseIntegral )
133 {
134 BootstrapPhases actualPhase = BootstrapPhases( phaseIntegral );
135
136 // phase 1: distribute a resource pool
137 if ( actualPhase == BootstrapPhases::PrepareResources )
138 {
139 // create a resource pool?
140 if ( targetCamp->resourcePool == nullptr )
141 {
142 targetCamp->isResourceOwner = true;
145
146 targetCamp->resourcePool= pool;
147
148 // \releasetask{update resource numbers numbers}
149 integer expectedSize= 97 // ALIB distribution resources
150 IF_ALIB_ALOX ( + 42 )
152 IF_ALIB_CLI ( + 17 )
153 IF_ALIB_EXPRESSIONS ( + 256 )
154 IF_ALIB_FILES ( + 43 ) ;
155
156 auto& hashMap= pool->BootstrapGetInternalHashMap();
157 hashMap.BaseLoadFactor( 2.0 );
158 hashMap.MaxLoadFactor ( 5.0 );
159 hashMap.Reserve ( expectedSize, ValueReference::Absolute );
160 }
161
162
163 // loop in reverse order over modules, start with this module
164 auto* actPool = targetCamp->resourcePool;
165 for(auto campIt= targetCampIt ; campIt != CAMPS.rend() ; ++campIt )
166 {
167
168 // if a different resources object is set, then use that one from now on
169 if((*campIt)->resourcePool != nullptr && (*campIt)->resourcePool != actPool)
170 {
171 actPool= (*campIt)->resourcePool;
172 continue;
173 }
174
175 (*campIt)->resourcePool= actPool;
176
177 } // resources distribution loop
178 }
179
180 // phase 2: create and distribute a configuration
181 #if ALIB_CONFIGURATION
182 else if ( actualPhase == BootstrapPhases::PrepareConfig)
183 {
184 // create a configuration?
185 if ( targetCamp->config == nullptr )
186 {
187 targetCamp->isConfigOwner= true;
188 auto* monoAllocator = monomem::GLOBAL_ALLOCATOR().New<MonoAllocator>(
189 ALIB_DBG("Configuration",) 16u);
190IF_ALIB_THREADS(targetCamp->configLock= (*monoAllocator)().New<SharedLock>();)
191 targetCamp->config = (*monoAllocator)().New<Configuration>(*monoAllocator);
192 #if ALIB_DEBUG_CRITICAL_SECTIONS
193 targetCamp->config->NodeTable().dcs.DCSName= "ALib-Camp-Configuration";
194 #endif
195 }
196
197 // loop in reverse order over modules, start with this module
198 auto* actConfig = targetCamp->config;
199IF_ALIB_THREADS(auto* actConfigLock = targetCamp->configLock; )
200 for(auto module= targetCampIt ; module != CAMPS.rend() ; ++module )
201 {
202 // if a different resources object is set, then use that one from now on
203 if( (*module)->config != nullptr && (*module)->config != actConfig)
204 {
205 actConfig = (*module)->config;
206IF_ALIB_THREADS( actConfigLock= (*module)->configLock;
207 ALIB_DBG( (*module)->configLock->Dbg.Name= "CampConfig";) )
208 }
209 else
210 {
211 (*module)->config = actConfig;
212IF_ALIB_THREADS( (*module)->configLock= actConfigLock; )
213 }
214
215 } // resources distribution loop
216 }
217 #endif
218
219 // initialize modules on this phase
220 ALIB_DBG( bool foundThisModuleInList = false; )
221 for ( auto* camp : CAMPS )
222 {
223 // bootstrap camp
224 if(camp->bootstrapState >= UnderlyingIntegral(actualPhase ) )
225 continue;
226
227 //std::cout << "Camp::Bootstrap '" << module->ResourceCategory << "', phase: " << int(actualPhase) << std::endl;
228
229 ALIB_ASSERT_ERROR( camp->bootstrapState == phaseIntegral - 1,
230 "With this invocation of Bootstrap() a camp skips a bootstrap phase.\n"
231 "Resource category of the target camp: ", camp->ResourceCategory )
232 camp->bootstrap(actualPhase );
233
234 camp->bootstrapState= UnderlyingIntegral(actualPhase );
235
236 // stop if this is us
237 if (camp == targetCamp )
238 {
239 ALIB_DBG( foundThisModuleInList = true );
240 break;
241 }
242 }
243 ALIB_ASSERT_ERROR( foundThisModuleInList, "CAMPS",
244 "The target camp of function Bootstrap is not included in list alib::CAMPS "
245 "or was already bootstrapped for this phase!\n"
246 "Resource category of the target camp: ", targetCamp->ResourceCategory )
247 }
248
249 // Are all camps finalized?
250 if ( targetPhase == BootstrapPhases::Final && targetCamp == CAMPS.Back() )
251 {
252 #if ALIB_DEBUG_CRITICAL_SECTIONS
253 # if ALIB_MONOMEM
255 monomem::GLOBAL_ALLOCATOR_LOCK.Dbg.Name= "GlobalAllocator";
256 # endif
257 # if ALIB_CONFIGURATION
258 targetCamp->config->NodeTable().dcs.DCSLock= targetCamp->configLock;
259 # endif
260 #endif
261
262
263 }
264}
265
266void Shutdown( ShutdownPhases targetPhase,
267 Camp* targetCamp )
268{
269 #if ALIB_DEBUG_CRITICAL_SECTIONS && ALIB_MONOMEM
270 monomem::GLOBAL_ALLOCATOR.DbgCriticalSectionsPH.Get()->DCSLock= nullptr;
271 #endif
272
273
274 ALIB_ASSERT_ERROR( CAMPS.IsNotEmpty(), "CAMPS", "Empty camp list on shutdown. Shutdown invoked twice?" )
275 if( targetCamp == nullptr )
276 targetCamp= CAMPS.Front();
277
278 //std::cout << "Camp::Shutdown called on'" << targetCamp->ResourceCategory << "', target phase: " << int(targetPhase) << std::endl;
279
280 // find target camp in the list of camps
281 auto targetCampIt= CAMPS.begin();
282 while( targetCampIt != CAMPS.end()
283 && *targetCampIt != targetCamp )
284 ++targetCampIt;
285 ALIB_ASSERT_ERROR(targetCampIt != CAMPS.end(), "CAMPS",
286 "Target camp given to function alib::Shutdown() is not included in list alib::CAMPS.\n"
287 "Resource category of the target camp: ", targetCamp->ResourceCategory )
288
289
290 ALIB_DBG( bool foundThisModuleInList= false );
291 // loop over all (both) termination levels
292 for( auto phaseIntegral= UnderlyingIntegral( ShutdownPhases::Announce )
293 ; phaseIntegral<= UnderlyingIntegral( targetPhase )
294 ; ++phaseIntegral )
295 {
296 ShutdownPhases actualPhase = ShutdownPhases( phaseIntegral );
297
298 // shutdown in reverse order
299 for(auto campIt= CAMPS.rbegin() ; campIt != CAMPS.rend() ; ++campIt )
300 {
301 ALIB_ASSERT_ERROR( ( *campIt )->bootstrapState < 0
302 || ( *campIt )->bootstrapState == UnderlyingIntegral(BootstrapPhases::Final ),
303 "CAMPS", "Trying to terminate a not (fully) initialized module. "
304 "Module Name (resource category): ", targetCamp->ResourceCategory )
305
306 // shutdown module
307 if (( *campIt )->bootstrapState > -UnderlyingIntegral(actualPhase) )
308 {
309 //std::cout << "Camp::Shutdown '" << (*campIt)->ResourceCategory << "', phase: " << int(actualPhase) << std::endl;
310
311 ALIB_ASSERT_ERROR( ( ( *campIt )->bootstrapState == 3 && phaseIntegral == 1 )
312 || ( ( *campIt )->bootstrapState == -1 && phaseIntegral == 2 ),
313 "CAMPS", "With this invocation of Bootstrap(), a camp skips a bootstrap phase \n"
314 "Resource category of the target camp: ", ( *campIt )->ResourceCategory )
315
316
317 ( *campIt )->shutdown(actualPhase );
318 ( *campIt )->bootstrapState = -UnderlyingIntegral(actualPhase );
319
320 ALIB_DBG( if(( *campIt ) == targetCamp )
321 foundThisModuleInList= true; )
322 }
323
324 if(( *campIt ) == targetCamp )
325 break;
326 }
327 }
328 ALIB_ASSERT_ERROR( foundThisModuleInList, "CAMPS",
329 "The target camp of function Shutdown is not included in list alib::CAMPS "
330 "or was already shutdown for this phase!\n"
331 "Resource category of the target camp: ", targetCamp->ResourceCategory )
332
333
334 #if ALIB_DEBUG_CRITICAL_SECTIONS && ALIB_CONFIGURATION
335 // deactivate assertions for non-locked access to configuration
336 if( targetPhase == ShutdownPhases::Announce)
337 for(auto campIt= CAMPS.rbegin() ; campIt != CAMPS.rend() ; ++campIt )
338 if ( ( *campIt )->isConfigOwner )
339 (*campIt)->config->NodeTable().dcs.DCSLock= nullptr;
340 #endif
341
342 // delete resources/config
343 if( targetPhase == ShutdownPhases::Destruct)
344 for(auto campIt= CAMPS.rbegin() ; campIt != CAMPS.rend() ; ++campIt )
345 {
346 if ( ( *campIt )->isResourceOwner ) lang::Destruct( *(*campIt)->resourcePool );
347 #if ALIB_CONFIGURATION
348 if ( ( *campIt )->isConfigOwner )
349 {
350 auto& cfgAllocator= (*campIt)->config->GetAllocator();
351
352 #if ALIB_DEBUG_CRITICAL_SECTIONS
353 (*campIt)->config->NodeTable().dcs.DCSLock= nullptr;
354 #endif
355 lang::Destruct( *(*campIt)->config );
356IF_ALIB_THREADS(lang::Destruct( *(*campIt)->configLock ); )
357 lang::Destruct( cfgAllocator );
358 }
359 #endif
360 if(( *campIt ) == targetCamp )
361 break;
362 }
363
364 if( targetPhase == ShutdownPhases::Destruct
365 && targetCamp == CAMPS.Front() )
366 {
367 IF_ALIB_THREADS( threads ::Shutdown(); )
368 IF_ALIB_TIME( time ::Shutdown(); )
369 IF_ALIB_ENUMS( enums ::Shutdown(); )
370 IF_ALIB_BOXING( boxing ::Shutdown(); )
371 IF_ALIB_SINGLETONS( singletons ::Shutdown(); )
372 CAMPS.Reset();
373 }
374
375}
377
378} // namespace [alib]
void BaseLoadFactor(float newBaseLoadFactor) noexcept
int bootstrapState
Definition camp.hpp:68
bool isResourceOwner
Definition camp.hpp:80
bool isConfigOwner
Definition camp.hpp:108
SharedLock * configLock
A shared lock associated to member config.
Definition camp.hpp:100
NCString ResourceCategory
Definition camp.hpp:119
lang::resources::ResourcePool * resourcePool
Definition camp.hpp:75
config::Configuration * config
Definition camp.hpp:96
detail::StaticResourceMap & BootstrapGetInternalHashMap()
lang::Placeholder< lang::DbgCriticalSections > DbgCriticalSectionsPH
DbgLockAsserter Dbg
The debug tool instance.
#define IF_ALIB_FILES(...)
Definition alib.hpp:304
#define IF_ALIB_BOXING(...)
Definition alib.hpp:248
#define IF_ALIB_CLI(...)
Definition alib.hpp:264
#define IF_ALIB_THREADS(...)
Definition alib.hpp:352
#define IF_ALIB_CONFIGURATION(...)
Definition alib.hpp:272
#define IF_ALIB_ALOX(...)
Definition alib.hpp:232
#define IF_ALIB_SINGLETONS(...)
Definition alib.hpp:320
#define IF_ALIB_THREADMODEL(...)
Definition alib.hpp:344
#define ALIB_BOXING_BOOTSTRAP_VTABLE_DBG_REGISTER(Identifier)
Definition vtable.inl:489
#define IF_ALIB_TIME(...)
Definition alib.hpp:360
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
#define IF_ALIB_ENUMS(...)
Definition alib.hpp:288
#define IF_ALIB_CAMP(...)
Definition alib.hpp:336
#define ALIB_DBG(...)
Definition alib.hpp:390
#define IF_ALIB_EXPRESSIONS(...)
Definition alib.hpp:296
void Bootstrap()
Definition boxing.cpp:708
static ALIB_FORCE_INLINE void Destruct(T &object)
ALIB_API MonoAllocator GLOBAL_ALLOCATOR
ALIB_API RecursiveLock GLOBAL_ALLOCATOR_LOCK
ALIB_API void Bootstrap()
Definition alib.cpp:69
ShutdownPhases
Termination levels usable with Bootstrapping ALib Camps.
Definition alib.hpp:1418
@ Destruct
The main phase of termination that destructs everything.
lox::ALoxCamp ALOX
The singleton instance of ALib Camp class ALoxCamp.
Definition aloxcamp.cpp:33
ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE void AssertALibVersionAndFlags(int alibVersion, int alibRevision, TCompilationFlags compilationFlags)
Definition alib.cpp:184
config::ConfigCamp CONFIG
The singleton instance of ALib Camp class ConfigCamp.
lang::basecamp::BaseCamp BASECAMP
The singleton instance of ALib Camp class BaseCamp.
Definition basecamp.cpp:70
expressions::ExpressionsCamp EXPRESSIONS
The singleton instance of ALib Camp class ExpressionsCamp.
BootstrapPhases
Initialization levels usable with Bootstrapping ALib Camps.
Definition alib.hpp:1393
@ Final
The final initialization phase. Here, camps are initializing their custom setup.
bool NonCampModulesInitialized
Definition alib.cpp:75
ALIB_API List< MonoAllocator, lang::Camp * > CAMPS
cli::CliCamp CLI
The singleton instance of ALib Camp class CliCamp.
Definition clicamp.cpp:22
files::FilesCamp FILES
The singleton instance of ALib Camp class FilesCamp.
Definition filescamp.cpp:23
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
void BootstrapAddDefaultCamps()
Definition bootstrap.cpp:59
ALIB_WARNINGS_RESTORE void Bootstrap(int alibVersion, int alibRevision, TCompilationFlags compilationFlags)
Definition alib.cpp:84
void Shutdown()
Definition alib.cpp:122
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273