46 defaults(defaults
const& other) =
delete;
47 defaults
const& operator=(defaults
const&) =
delete;
49 void resetEngine( HepRandomEngine * newEngine ) {
53 void resetEngine( HepRandomEngine & newEngine ) {
54 theEngine.reset( &newEngine, do_nothing_deleter() );
57 bool ensureInitialized() {
77 #ifdef CLHEP_USE_ATOMIC
83 class ThreadSafeDefaultsCache {
86 ThreadSafeDefaultsCache();
89 ~ThreadSafeDefaultsCache();
92 defaults* createNewDefaults();
100 DefaultsNode(DefaultsNode* iNext);
101 DefaultsNode
const* next()
const {
return next_; }
102 void setNext(DefaultsNode* v) { next_ = v; }
103 defaults* addressOfDefaults() {
return &defaults_; }
110 std::atomic<DefaultsNode*> front_;
113 ThreadSafeDefaultsCache::ThreadSafeDefaultsCache() :
117 defaults* ThreadSafeDefaultsCache::createNewDefaults() {
118 DefaultsNode* expected = front_.load();
119 DefaultsNode* newNode =
new DefaultsNode(expected);
120 while (!front_.compare_exchange_strong(expected, newNode)) {
122 newNode->setNext(expected);
124 return newNode->addressOfDefaults();
127 ThreadSafeDefaultsCache::DefaultsNode::DefaultsNode(DefaultsNode* iNext) :
132 ThreadSafeDefaultsCache::~ThreadSafeDefaultsCache() {
133 DefaultsNode
const* node = front_.load();
135 DefaultsNode
const* next = node->next();
141 defaults & theDefaults() {
153 static ThreadSafeDefaultsCache defaultsForAllThreads;
156 static CLHEP_THREAD_LOCAL defaults* theDefaults = defaultsForAllThreads.createNewDefaults();
164 defaults & theDefaults() {
165 static defaults theDefaults;
175 HepRandom::HepRandom()
178 HepRandom::HepRandom(
long seed)
185 theDefaults().resetEngine( algorithm );
190 theDefaults().resetEngine( algorithm );
193 HepRandom::~HepRandom()
198 return theDefaults().theEngine->flat();
201 void HepRandom::flatArray(
const int size,
double* vect)
203 theDefaults().theEngine->flatArray(size,vect);
206 double HepRandom::operator()() {
212 std::cerr <<
"HepRandom::engine() called -- there is no assigned engine!\n";
213 return *theDefaults().theEngine.get();
224 std::ostream & HepRandom::put(std::ostream & os)
const {
return os;}
225 std::istream & HepRandom::get(std::istream & is) {
return is;}
231 void HepRandom::setTheSeed(
long seed,
int lux)
233 theDefaults().theEngine->setSeed(seed,lux);
236 long HepRandom::getTheSeed()
238 return theDefaults().theEngine->getSeed();
241 void HepRandom::setTheSeeds(
const long* seeds,
int aux)
243 theDefaults().theEngine->setSeeds(seeds,aux);
246 const long* HepRandom::getTheSeeds ()
248 return theDefaults().theEngine->getSeeds();
251 void HepRandom::getTheTableSeeds(
long* seeds,
int index)
253 if ((index >= 0) && (index < 215)) {
254 seeds[0] = seedTable[index][0];
255 seeds[1] = seedTable[index][1];
262 return theDefaults().theGenerator.
get();
267 return theDefaults().theEngine.
get();
275 void HepRandom::saveEngineStatus(
const char filename[] )
277 theDefaults().theEngine->saveStatus( filename );
280 void HepRandom::restoreEngineStatus(
const char filename[] )
282 theDefaults().theEngine->restoreStatus( filename );
285 std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
286 os << *getTheEngine();
290 std::istream& HepRandom::restoreFullState ( std::istream & is ) {
291 is >> *getTheEngine();
295 std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
296 return StaticRandomStates::save(os);
299 std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
300 return StaticRandomStates::restore(is);
303 void HepRandom::showEngineStatus()
305 theDefaults().theEngine->showStatus();
308 int HepRandom::createInstance()
310 return static_cast<int>( theDefaults().ensureInitialized() );
std::shared_ptr< HepRandom > theGenerator
std::ostream & operator<<(std::ostream &, const BasicVector3D< float > &)
static constexpr double lux
std::shared_ptr< HepRandomEngine > theEngine
std::istream & operator>>(std::istream &, BasicVector3D< float > &)
static int engine(pchar, pchar, double &, pchar &, const dic_type &)
virtual std::istream & get(std::istream &is)
#define CLHEP_THREAD_LOCAL
virtual std::istream & get(std::istream &is)
HepRandom theDefaultGenerator
virtual std::ostream & put(std::ostream &os) const
MixMaxRng theDefaultEngine