Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4PDefSplitter.hh
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 //
26 //
27 // $Id: $
28 //
29 //
30 // ------------------------------------------------------------
31 //
32 // GEANT 4 class header file
33 //
34 // ---------------- G4PDefSplitter ----------------
35 //
36 // Utility template class for splitting RW data for thread-safety from
37 // classes: G4ParticleDefinition, G4VDecayChannel.
38 //
39 // ------------------------------------------------------------
40 // History:
41 // 01.25.2009 Xin Dong: First implementation from automatic MT conversion.
42 // ------------------------------------------------------------
43 #ifndef G4PDEFSPLITTER_HH
44 #define G4PDEFSPLITTER_HH
45 
46 #include <stdlib.h>
47 
48 #include "globals.hh"
49 #include "pwdefs.hh"
50 
51 template <class T> // T is the private data from the object to be split
53 {
54  public:
55 
56  G4PDefSplitter() : totalobj(0) {}
57 
59  // Invoked by the master or work thread to create a new subinstance
60  // whenever a new split class instance is created. For each worker
61  // thread, ions are created dynamically.
62  {
63  totalobj++;
64  if (totalobj > slavetotalspace) { NewSubInstances(); }
65  return (totalobj - 1);
66  }
67 
69  // Invoked by each worker thread to grow the subinstance array and
70  // initialize each new subinstance using a particular method defined
71  // by the subclass.
72  {
73  if (slavetotalspace >= totalobj) { return; }
74  G4int originaltotalspace = slavetotalspace;
75  slavetotalspace = totalobj + 512;
76  offset = (T *) realloc(offset, slavetotalspace * sizeof(T));
77 
78  if (offset == 0)
79  {
80  G4Exception("G4PDefSplitter::NewSubInstances()",
81  "OutOfMemory", FatalException, "Cannot malloc space!");
82  }
83 
84  for (G4int i = originaltotalspace; i < slavetotalspace; i++)
85  {
86  offset[i].initialize();
87  }
88  }
89 
90  void FreeSlave()
91  // Invoked by all threads to free the subinstance array.
92  {
93  if (!offset) { return; }
94  free(offset);
95  offset = 0;
96  }
97 
98  public:
99 
102 
103  private:
104 
105  G4int totalobj;
106 };
107 
108 #endif
void free(void *__ptr)
Definition: hjmalloc.cc:140
#define G4ThreadLocal
Definition: tls.hh:52
int G4int
Definition: G4Types.hh:78
static G4PART_DLL G4ThreadLocal T * offset
static G4PART_DLL G4ThreadLocal G4int slavetotalspace
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void NewSubInstances()
#define G4PART_DLL
Definition: pwdefs.hh:48
void * realloc(void *__ptr, size_t __size)
Definition: hjmalloc.cc:103
G4int CreateSubInstance()