Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4MTBarrier.cc
Go to the documentation of this file.
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// G4MTBarrier class implementation
26//
27// Author: A.Dotti (SLAC), 10 February 2016
28// Revision: J.Madsen (NERSC), 09 February 2018
29// --------------------------------------------------------------------
30
31#include "G4MTBarrier.hh"
32#include "G4AutoLock.hh"
33
34// --------------------------------------------------------------------
35G4MTBarrier::G4MTBarrier(unsigned int numThreads)
36 : m_numActiveThreads(numThreads)
37{}
38
39// --------------------------------------------------------------------
41{
42 // Step-1: Worker acquires lock on shared resource (the counter)
43 G4AutoLock lock(&m_mutex);
44 // Step-2: Worker increases counter
45 ++m_counter;
46 // Step-3: Worker broadcasts that the counter has changed
47 G4CONDITIONBROADCAST(&m_counterChanged);
48 // Step-4: Worker waits on condition to continue
49 G4CONDITIONWAIT(&m_continue, &lock);
50}
51
52// --------------------------------------------------------------------
54{
55 while(true)
56 {
57 // Step-2: Acquires lock on shared resource (the counter)
58 G4AutoLock lock(&m_mutex);
59 // If the counter equals active threads, all threads are ready, exit the
60 // loop
61 if(m_counter == m_numActiveThreads)
62 {
63 break;
64 }
65 // Step-3: Not all workers are ready, wait for the number to change
66 // before repeating the check
67 G4CONDITIONWAIT(&m_counterChanged, &lock);
68 }
69}
70
71// --------------------------------------------------------------------
73{
74 // Step-4: re-aquire lock and re-set shared resource for future re-use
75 G4AutoLock lock(&m_mutex);
76 m_counter = 0;
77 G4CONDITIONBROADCAST(&m_continue);
78}
79
80// --------------------------------------------------------------------
82{
83 // Step-1: Master enters a loop to wait all workers to be ready
84 Wait();
85 // Done, all workers are ready, broadcast a continue signal
87}
88
89// --------------------------------------------------------------------
91{
92 G4AutoLock l(&m_mutex);
93 m_counter = 0;
94}
95
96// --------------------------------------------------------------------
98{
99 G4AutoLock l(&m_mutex);
100 const unsigned int result = m_counter;
101 return result;
102}
#define G4CONDITIONWAIT(cond, mutex)
Definition: G4Threading.hh:275
#define G4CONDITIONBROADCAST(cond)
Definition: G4Threading.hh:279
void ThisWorkerReady()
Definition: G4MTBarrier.cc:40
void ResetCounter()
Definition: G4MTBarrier.cc:90
void Wait()
Definition: G4MTBarrier.cc:53
virtual void WaitForReadyWorkers()
Definition: G4MTBarrier.cc:81
void ReleaseBarrier()
Definition: G4MTBarrier.cc:72
unsigned int GetCounter()
Definition: G4MTBarrier.cc:97