Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4UserWorkerThreadInitialization.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
27 #include "G4WorkerThread.hh"
28 #include "G4WorkerRunManager.hh"
29 #include "G4MTRunManagerKernel.hh"
31 #include "G4UImanager.hh"
32 #include "G4VUserPhysicsList.hh"
33 #include "G4AutoLock.hh"
34 #include <sstream>
35 
36 //Will need this for TPMalloc
37 //#ifdef G4MULTITHREADED
38 //#define TPMALLOCDEFINESTUB
39 //#include "tpmalloc/tpmallocstub.h"
40 //#endif
41 
42 #ifdef G4MULTITHREADED
44 {
45  //Note: this method is called by G4MTRunManager, here we are still sequential
46  //Create a new thread/worker structure
47  G4Thread* worker = new G4Thread;
48 #ifdef WIN32
49  G4THREADCREATE(worker, (LPTHREAD_START_ROUTINE)&G4MTRunManagerKernel::StartThread , wTC );
50 #else
52 #endif
53  return worker;
54 }
55 #else
57 {
58  return new G4Thread;
59 }
60 #endif
61 
62 //Avoid compilation warning in sequential
63 #ifdef G4MULTITHREADED
65 {
66  G4THREADJOIN(*aThread);
67 }
68 #else
70 {
71 }
72 #endif
73 
75 {;}
76 
78 {;}
79 
80 namespace {
81  G4Mutex rngCreateMutex = G4MUTEX_INITIALIZER;
82 }
83 
84 #include "globals.hh"
86 {
87  G4AutoLock l(&rngCreateMutex);
88  //No default available, let's create the instance of random stuff
89  //A Call to this just forces the creation to defaults
90  G4Random::getTheEngine();
91  //Poor man's solution to check which RNG Engine is used in master thread
92  CLHEP::HepRandomEngine* retRNG= 0;
93 
94  // Need to make these calls thread safe
95  if ( dynamic_cast<const CLHEP::HepJamesRandom*>(aNewRNG) ) {
96  retRNG= new CLHEP::HepJamesRandom;
97  }
98  if ( dynamic_cast<const CLHEP::MixMaxRng*>(aNewRNG) ) {
99  retRNG= new CLHEP::MixMaxRng;
100  }
101  if ( dynamic_cast<const CLHEP::RanecuEngine*>(aNewRNG) ) {
102  retRNG= new CLHEP::RanecuEngine;
103  }
104  if ( dynamic_cast<const CLHEP::Ranlux64Engine*>(aNewRNG) ) {
105  retRNG= new CLHEP::Ranlux64Engine;
106  }
107  if ( dynamic_cast<const CLHEP::MTwistEngine*>(aNewRNG) ) {
108  retRNG= new CLHEP::MTwistEngine;
109  }
110  if ( dynamic_cast<const CLHEP::DualRand*>(aNewRNG) ) {
111  retRNG= new CLHEP::DualRand;
112  }
113  if ( dynamic_cast<const CLHEP::RanluxEngine*>(aNewRNG) ) {
114  retRNG= new CLHEP::RanluxEngine;
115  }
116  if ( dynamic_cast<const CLHEP::RanshiEngine*>(aNewRNG) ) {
117  retRNG= new CLHEP::RanshiEngine;
118  }
119 
120  if( retRNG != 0 ) {
121  G4Random::setTheEngine( retRNG );
122  }else{
123  // Does a new method, such as aNewRng->newEngine() exist to clone it ?
125  msg<< " Unknown type of RNG Engine - " << G4endl
126  << " Can cope only with HepJamesRandom, MixMaxRng, Ranecu, Ranlux64, MTwistEngine, DualRand, Ranlux or Ranshi."
127  << G4endl
128  << " Cannot clone this type of RNG engine, as required for this thread" << G4endl
129  << " Aborting " << G4endl;
130  G4Exception("G4UserWorkerInitializition::SetupRNGEngine()",
131  "Run0122",FatalException,msg);
132  }
133 }
134 
136 {
137  return new G4WorkerRunManager();
138 }
139 
static void * StartThread(void *context)
#define G4THREADJOIN(worker)
Definition: G4Threading.hh:182
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
virtual void SetupRNGEngine(const CLHEP::HepRandomEngine *aRNGEngine) const
virtual G4WorkerRunManager * CreateWorkerRunManager() const
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
G4int G4Thread
Definition: G4Threading.hh:174
#define G4THREADCREATE(worker, func, arg)
Definition: G4Threading.hh:181
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4int G4Mutex
Definition: G4Threading.hh:173
virtual G4Thread * CreateAndStartWorker(G4WorkerThread *workerThreadContext)
#define G4endl
Definition: G4ios.hh:61