ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4MTBarrier.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file 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 //
26 // ---------------------------------------------------------------
27 /*
28  * G4MTBarrier.cc
29  *
30  * Created on: Feb 10, 2016
31  * Author: adotti
32  * Updated on: Feb 9, 2018
33  * Author: jmadsen
34  */
35 
36 #include "G4MTBarrier.hh"
37 #include "G4AutoLock.hh"
38 
39 G4MTBarrier::G4MTBarrier(unsigned int numThreads ) :
40  m_numActiveThreads(numThreads),
41  m_counter(0)
42 {}
43 
45  //Step-1: Worker acquires lock on shared resource (the counter)
46  G4AutoLock lock(&m_mutex);
47  //Step-2: Worker increases counter
48  ++m_counter;
49  //Step-3: Worker broadcasts that the counter has changed
51  //Step-4: Worker waits on condition to continue
53 }
54 
56  while (true)
57  {
58  //Step-2: Acquires lock on shared resource (the counter)
59  G4AutoLock lock(&m_mutex);
60  //If the counter equals active threads, all threads are ready, exit the loop
61  if ( m_counter == m_numActiveThreads ) { break; }
62  //Step-3: Not all workers are ready, wait for the number to change
63  //before repeating the check
65  }
66 }
67 
69  //Step-4: re-aquire lock and re-set shared resource for future re-use
70  G4AutoLock lock(&m_mutex);
71  m_counter = 0;
73 }
74 
76  //Step-1: Master enters a loop to wait all workers to be ready
77  Wait();
78  //Done, all workers are ready, broadcast a continue signal
80 }
81 
83  G4AutoLock l(&m_mutex);
84  m_counter = 0;
85 }
86 
87 unsigned int G4MTBarrier::GetCounter() {
88  G4AutoLock l(&m_mutex);
89  const unsigned int result = m_counter;
90  return result;
91 }