45 defaults(defaults
const& other) =
delete;
46 defaults
const& operator=(defaults
const&) =
delete;
48 void resetEngine( HepRandomEngine * newEngine ) {
52 void resetEngine( HepRandomEngine & newEngine ) {
53 theEngine.reset( &newEngine, do_nothing_deleter() );
56 bool ensureInitialized() {
76 #ifdef CLHEP_USE_ATOMIC
82 class ThreadSafeDefaultsCache {
85 ThreadSafeDefaultsCache();
88 ~ThreadSafeDefaultsCache();
91 defaults* createNewDefaults();
99 DefaultsNode(DefaultsNode* iNext);
100 DefaultsNode
const* next()
const {
return next_; }
101 void setNext(DefaultsNode*
v) { next_ =
v; }
102 defaults* addressOfDefaults() {
return &defaults_; }
109 std::atomic<DefaultsNode*> front_;
112 ThreadSafeDefaultsCache::ThreadSafeDefaultsCache() :
116 defaults* ThreadSafeDefaultsCache::createNewDefaults() {
117 DefaultsNode* expected = front_.load();
118 DefaultsNode* newNode =
new DefaultsNode(expected);
119 while (!front_.compare_exchange_strong(expected, newNode)) {
121 newNode->setNext(expected);
123 return newNode->addressOfDefaults();
126 ThreadSafeDefaultsCache::DefaultsNode::DefaultsNode(DefaultsNode* iNext) :
131 ThreadSafeDefaultsCache::~ThreadSafeDefaultsCache() {
132 DefaultsNode
const* node = front_.load();
134 DefaultsNode
const* next = node->next();
140 defaults & theDefaults() {
152 static ThreadSafeDefaultsCache defaultsForAllThreads;
155 static CLHEP_THREAD_LOCAL defaults* theDefaults = defaultsForAllThreads.createNewDefaults();
163 defaults & theDefaults() {
164 static defaults theDefaults;
174 HepRandom::HepRandom()
177 HepRandom::HepRandom(
long seed)
184 theDefaults().resetEngine( algorithm );
189 theDefaults().resetEngine( algorithm );
192 HepRandom::~HepRandom()
197 return theDefaults().theEngine->flat();
200 void HepRandom::flatArray(
const int size,
double* vect)
202 theDefaults().theEngine->flatArray(size,vect);
205 double HepRandom::operator()() {
211 std::cerr <<
"HepRandom::engine() called -- there is no assigned engine!\n";
212 return *theDefaults().theEngine.
get();
223 std::ostream & HepRandom::put(std::ostream & os)
const {
return os;}
230 void HepRandom::setTheSeed(
long seed,
int lux)
232 theDefaults().theEngine->setSeed(seed,lux);
235 long HepRandom::getTheSeed()
237 return theDefaults().theEngine->getSeed();
240 void HepRandom::setTheSeeds(
const long* seeds,
int aux)
242 theDefaults().theEngine->setSeeds(seeds,aux);
245 const long* HepRandom::getTheSeeds ()
247 return theDefaults().theEngine->getSeeds();
250 void HepRandom::getTheTableSeeds(
long* seeds,
int index)
252 if ((index >= 0) && (index < 215)) {
253 seeds[0] = seedTable[index][0];
254 seeds[1] = seedTable[index][1];
261 return theDefaults().theGenerator.
get();
266 return theDefaults().theEngine.
get();
274 void HepRandom::saveEngineStatus(
const char filename[] )
276 theDefaults().theEngine->saveStatus( filename );
279 void HepRandom::restoreEngineStatus(
const char filename[] )
281 theDefaults().theEngine->restoreStatus( filename );
284 std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
285 os << *getTheEngine();
289 std::istream& HepRandom::restoreFullState ( std::istream & is ) {
290 is >> *getTheEngine();
294 std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
295 return StaticRandomStates::save(os);
298 std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
299 return StaticRandomStates::restore(is);
302 void HepRandom::showEngineStatus()
304 theDefaults().theEngine->showStatus();
307 int HepRandom::createInstance()
309 return static_cast<int>( theDefaults().ensureInitialized() );