]> www.ginac.de Git - cln.git/commitdiff
Replace CL_REQUIRE/CL_PROVIDE(cl_random_def) with portable code.
authorAlexei Sheplyakov <varg@theor.jinr.ru>
Thu, 21 Aug 2008 11:11:08 +0000 (15:11 +0400)
committerAlexei Sheplyakov <varg@theor.jinr.ru>
Wed, 27 Aug 2008 04:41:03 +0000 (08:41 +0400)
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
src/base/random/cl_random_def.cc

index 6ef2610f014674f313c97a233961c763d0decc03..06604e2db6c8a2d1a9ac31fa4fd00c0253f440c9 100644 (file)
@@ -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); }
index 59373a66fb0d58e0f2283d743ac1292d45f4ec1d..9728273adcd965693d20086abd052d8c3410e6c2 100644 (file)
@@ -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)