Geant4
10.03.p01
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
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
// $Id$
26
//
27
// ---------------------------------------------------------------
28
/*
29
* G4MTBarrier.cc
30
*
31
* Created on: Feb 10, 2016
32
* Author: adotti
33
*/
34
35
#include "
G4MTBarrier.hh
"
36
#include "
G4AutoLock.hh
"
37
38
G4MTBarrier::G4MTBarrier
(
unsigned
int
numThreads ) :
39
m_numActiveThreads(numThreads),
40
m_counter(0),
41
m_mutex(
G4MUTEX_INITIALIZER
),
42
m_counterChanged(
G4CONDITION_INITIALIZER
),
43
m_continue(
G4CONDITION_INITIALIZER
)
44
{
45
#if defined(WIN32)
46
InitializeCriticalSection( &cs1 );
47
InitializeCriticalSection( &cs2 );
48
#endif
49
}
50
51
void
G4MTBarrier::ThisWorkerReady
() {
52
//Step-1: Worker acquires lock on shared resource (the counter)
53
#ifndef WIN32
54
G4AutoLock
lock(&m_mutex);
55
#else
56
EnterCriticalSection( &cs1 );
57
#endif
58
//Step-2: Worker increases counter
59
++m_counter;
60
//Step-3: Worker broadcasts that the counter has changed
61
G4CONDITIONBROADCAST
(&m_counterChanged);
62
//Step-4: Worker waits on condition to continue
63
#ifndef WIN32
64
G4CONDITIONWAIT
(&m_continue,&m_mutex);
65
#else
66
# ifdef G4MULTITHREADED
67
G4CONDITIONWAIT
(&m_continue,&cs1);
68
# endif
69
LeaveCriticalSection(&cs1);
70
#endif
71
}
72
73
void
G4MTBarrier::Wait
() {
74
while
(
true
)
75
{
76
//Step-2: Acquires lock on shared resource (the counter)
77
#ifndef WIN32
78
G4AutoLock
lock(&m_mutex);
79
#else
80
EnterCriticalSection(&cs2);
81
#endif
82
//If the counter equals active threads, all threads are ready, exit the loop
83
if
( m_counter == m_numActiveThreads ) {
break
; }
84
//Step-3: Not all workers are ready, wait for the number to change
85
//before repeating the check
86
#ifdef WIN32
87
# ifdef G4MULTITHREADED
88
G4CONDITIONWAIT
(&m_counterChanged,&cs2);
89
# endif
90
LeaveCriticalSection(&cs2);
91
#else
92
G4CONDITIONWAIT
(&m_counterChanged,&m_mutex);
93
#endif
94
}
95
}
96
97
void
G4MTBarrier::ReleaseBarrier
() {
98
//Step-4: re-aquire lock and re-set shared resource for future re-use
99
G4AutoLock
lock(&m_mutex);
100
m_counter = 0;
101
G4CONDITIONBROADCAST
(&m_continue);
102
}
103
104
void
G4MTBarrier::WaitForReadyWorkers
() {
105
//Step-1: Master enters a loop to wait all workers to be ready
106
Wait
();
107
//Done, all workers are ready, broadcast a continue signal
108
ReleaseBarrier
();
109
}
110
111
void
G4MTBarrier::ResetCounter
() {
112
G4AutoLock
l(&m_mutex);
113
m_counter = 0;
114
}
115
116
unsigned
int
G4MTBarrier::GetCounter
() {
117
G4AutoLock
l(&m_mutex);
118
const
unsigned
int
result
= m_counter;
119
return
result
;
120
}
result
G4double G4ParticleHPJENDLHEData::G4double result
Definition:
G4ParticleHPJENDLHEData.cc:257
G4MTBarrier.hh
G4MTBarrier::G4MTBarrier
G4MTBarrier()
Definition:
G4MTBarrier.hh:131
G4MTBarrier::WaitForReadyWorkers
virtual void WaitForReadyWorkers()
Definition:
G4MTBarrier.cc:104
G4CONDITIONWAIT
#define G4CONDITIONWAIT(cond, mutex)
Definition:
G4Threading.hh:191
G4MUTEX_INITIALIZER
#define G4MUTEX_INITIALIZER
Definition:
G4Threading.hh:175
G4MTBarrier::ThisWorkerReady
void ThisWorkerReady()
Definition:
G4MTBarrier.cc:51
G4ImpMutexAutoLock
Definition:
G4AutoLock.hh:108
G4MTBarrier::ResetCounter
void ResetCounter()
Definition:
G4MTBarrier.cc:111
G4MTBarrier::GetCounter
unsigned int GetCounter()
Definition:
G4MTBarrier.cc:116
G4MTBarrier::ReleaseBarrier
void ReleaseBarrier()
Definition:
G4MTBarrier.cc:97
G4MTBarrier::Wait
void Wait()
Definition:
G4MTBarrier.cc:73
G4AutoLock.hh
G4CONDITIONBROADCAST
#define G4CONDITIONBROADCAST(cond)
Definition:
G4Threading.hh:192
G4CONDITION_INITIALIZER
#define G4CONDITION_INITIALIZER
Definition:
G4Threading.hh:190
geant4.10.03.p01
source
global
management
src
G4MTBarrier.cc
Generated on Thu Mar 16 2017 22:37:56 for Geant4 by
1.8.5