From 6e18082d677e9e027a0b3e78a0933146e09a7d07 Mon Sep 17 00:00:00 2001 From: Alexei Sheplyakov Date: Thu, 21 Aug 2008 15:11:08 +0400 Subject: [PATCH] Replace CL_REQUIRE/CL_PROVIDE(cl_random_def) with portable code. The order of initialization of non-local objects in different compilation units is not specified in C++. Hence special care should be taken to avoid static initialization order fiasco. CLN solved the problem with some evil (GCC specific, and even GCC-version-specific) hack. Replace it with a technique similar to one used in STL to initialize std::cout and friends. --- include/cln/random.h | 10 +++++++++- src/base/random/cl_random_def.cc | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/include/cln/random.h b/include/cln/random.h index 6ef2610..06604e2 100644 --- a/include/cln/random.h +++ b/include/cln/random.h @@ -33,7 +33,15 @@ inline uint64 random64 (random_state& randomstate) // Ein globaler Zufallszahlengenerator. extern random_state default_random_state; -CL_REQUIRE(cl_random_def) +class cl_random_def_init_helper +{ + static int count; +public: + cl_random_def_init_helper(); + ~cl_random_def_init_helper(); +}; +static cl_random_def_init_helper cl_random_def_init_helper_instance; + // Das ist der Default-Generator. inline uint32 random32 (void) { return random32(default_random_state); } diff --git a/src/base/random/cl_random_def.cc b/src/base/random/cl_random_def.cc index 59373a6..9728273 100644 --- a/src/base/random/cl_random_def.cc +++ b/src/base/random/cl_random_def.cc @@ -3,8 +3,6 @@ // General includes. #include "cl_sysdep.h" -CL_PROVIDE(cl_random_def) - // Specification. #include "cln/random.h" @@ -15,6 +13,20 @@ namespace cln { random_state default_random_state; +int cl_random_def_init_helper::count = 0; +cl_random_def_init_helper::cl_random_def_init_helper() +{ + if (count++ == 0) { + default_random_state = random_state(); + } +} + +cl_random_def_init_helper::~cl_random_def_init_helper() +{ + if (--count == 0) { + // Nothing to clean up? + } +} + } // namespace cln -CL_PROVIDE_END(cl_random_def)