Browse Source

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.
master
Alexei Sheplyakov 17 years ago
parent
commit
6e18082d67
  1. 10
      include/cln/random.h
  2. 18
      src/base/random/cl_random_def.cc

10
include/cln/random.h

@ -33,7 +33,15 @@ inline uint64 random64 (random_state& randomstate)
// Ein globaler Zufallszahlengenerator. // Ein globaler Zufallszahlengenerator.
extern random_state default_random_state; 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. // Das ist der Default-Generator.
inline uint32 random32 (void) inline uint32 random32 (void)
{ return random32(default_random_state); } { return random32(default_random_state); }

18
src/base/random/cl_random_def.cc

@ -3,8 +3,6 @@
// General includes. // General includes.
#include "cl_sysdep.h" #include "cl_sysdep.h"
CL_PROVIDE(cl_random_def)
// Specification. // Specification.
#include "cln/random.h" #include "cln/random.h"
@ -15,6 +13,20 @@ namespace cln {
random_state default_random_state; 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 } // namespace cln
CL_PROVIDE_END(cl_random_def)
Loading…
Cancel
Save