Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4MTBarrier.cc
이 파일의 문서화 페이지로 가기
1 // ********************************************************************
2 // * License and Disclaimer *
3 // * *
4 // * The Geant4 software is copyright of the Copyright Holders of *
5 // * the Geant4 Collaboration. It is provided under the terms and *
6 // * conditions of the Geant4 Software License, included in the file *
7 // * LICENSE and available at http://cern.ch/geant4/license . These *
8 // * include a list of copyright holders. *
9 // * *
10 // * Neither the authors of this software system, nor their employing *
11 // * institutes,nor the agencies providing financial support for this *
12 // * work make any representation or warranty, express or implied, *
13 // * regarding this software system or assume any liability for its *
14 // * use. Please see the license in the file LICENSE and URL above *
15 // * for the full disclaimer and the limitation of liability. *
16 // * *
17 // * This code implementation is the result of the scientific and *
18 // * technical work of the GEANT4 collaboration. *
19 // * By using, copying, modifying or distributing the software (or *
20 // * any work based on the software) you agree to acknowledge its *
21 // * use in resulting scientific publications, and indicate your *
22 // * acceptance of all terms of the Geant4 Software license. *
23 // ********************************************************************
24 //
25 // $Id$
26 //
27 // ---------------------------------------------------------------
28 /*
29  * G4MTBarrier.cc
30  *
31  * Created on: Feb 10, 2016
32  * Author: adotti
33  * Updated on: Feb 9, 2018
34  * Author: jmadsen
35  */
36 
37 #include "G4MTBarrier.hh"
38 #include "G4AutoLock.hh"
39 
40 G4MTBarrier::G4MTBarrier(unsigned int numThreads ) :
41  m_numActiveThreads(numThreads),
42  m_counter(0)
43 {}
44 
46  //Step-1: Worker acquires lock on shared resource (the counter)
47  G4AutoLock lock(&m_mutex);
48  //Step-2: Worker increases counter
49  ++m_counter;
50  //Step-3: Worker broadcasts that the counter has changed
52  //Step-4: Worker waits on condition to continue
54 }
55 
57  while (true)
58  {
59  //Step-2: Acquires lock on shared resource (the counter)
60  G4AutoLock lock(&m_mutex);
61  //If the counter equals active threads, all threads are ready, exit the loop
62  if ( m_counter == m_numActiveThreads ) { break; }
63  //Step-3: Not all workers are ready, wait for the number to change
64  //before repeating the check
66  }
67 }
68 
70  //Step-4: re-aquire lock and re-set shared resource for future re-use
71  G4AutoLock lock(&m_mutex);
72  m_counter = 0;
74 }
75 
77  //Step-1: Master enters a loop to wait all workers to be ready
78  Wait();
79  //Done, all workers are ready, broadcast a continue signal
81 }
82 
84  G4AutoLock l(&m_mutex);
85  m_counter = 0;
86 }
87 
88 unsigned int G4MTBarrier::GetCounter() {
89  G4AutoLock l(&m_mutex);
90  const unsigned int result = m_counter;
91  return result;
92 }
G4Condition m_continue
Definition: G4MTBarrier.hh:153
#define G4CONDITIONBROADCAST(cond)
Definition: G4Threading.hh:253
void ResetCounter()
Definition: G4MTBarrier.cc:83
void Wait()
Definition: G4MTBarrier.cc:56
virtual void WaitForReadyWorkers()
Definition: G4MTBarrier.cc:76
#define G4CONDITIONWAIT(cond, mutex)
Definition: G4Threading.hh:251
unsigned int GetCounter()
Definition: G4MTBarrier.cc:88
G4Condition m_counterChanged
Definition: G4MTBarrier.hh:152
G4double G4ParticleHPJENDLHEData::G4double result
void ReleaseBarrier()
Definition: G4MTBarrier.cc:69
unsigned int m_counter
Definition: G4MTBarrier.hh:150
unsigned int m_numActiveThreads
Definition: G4MTBarrier.hh:149
G4Mutex m_mutex
Definition: G4MTBarrier.hh:151
void ThisWorkerReady()
Definition: G4MTBarrier.cc:45