http://stxxl.sourceforge.net
<beckmann@cs.uni-frankfurt.de>
<dementiev@ira.uka.de>
http://www.boost.org/LICENSE_1_0.txt
#include <cassert>
#include <stxxl/bits/config.h>
#include <stxxl/bits/common/mutex.h>
#include <stxxl/bits/namespace.h>
#if STXXL_WINDOWS
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <io.h>
#include <windows.h>
#else
#include <unistd.h>
#include <sys/time.h>
#endif
STXXL_BEGIN_NAMESPACE
inline unsigned initial_seed();
struct seed_generator_t {
unsigned seed;
mutex mtx;
seed_generator_t(unsigned s) : seed(s)
{ }
};
seed_generator_t & seed_generator()
{
static seed_generator_t sg(initial_seed());
return sg;
}
inline unsigned initial_seed()
{
#ifndef NDEBUG
static bool initialized = false;
assert(!initialized);
initialized = true;
#endif
#if STXXL_WINDOWS
return GetTickCount() ^ GetCurrentProcessId();
#else
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec ^ tv.tv_usec ^ (getpid() << 16);
#endif
}
void set_seed(unsigned seed)
{
scoped_mutex_lock Lock(seed_generator().mtx);
seed_generator().seed = seed;
}
unsigned get_next_seed()
{
scoped_mutex_lock Lock(seed_generator().mtx);
return seed_generator().seed++;
}
STXXL_END_NAMESPACE