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.
// 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); }
// General includes.
#include "cl_sysdep.h"
-CL_PROVIDE(cl_random_def)
-
// Specification.
#include "cln/random.h"
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)