Replace nil-able *sync.Waitgroup with sync.Once
Simplifies synchronization and eliminates lock for nil waitroup. Test: m droid Test: m out/soong/.intermediates/packages/modules/StatsD/apex/com.android.os.statsd/android_common_com.android.os.statsd_image/NOTICE.html.gz Change-Id: I381ee79e142214e7331241071f076db2f7960ba6
This commit is contained in:
@@ -58,13 +58,11 @@ type LicenseGraph struct {
|
|||||||
/// (guarded by mu)
|
/// (guarded by mu)
|
||||||
targets map[string]*TargetNode
|
targets map[string]*TargetNode
|
||||||
|
|
||||||
// wgBU becomes non-nil when the bottom-up resolve begins and reaches 0
|
// onceBottomUp makes sure the bottom-up resolve walk only happens one time.
|
||||||
// (i.e. Wait() proceeds) when the bottom-up resolve completes. (guarded by mu)
|
onceBottomUp sync.Once
|
||||||
wgBU *sync.WaitGroup
|
|
||||||
|
|
||||||
// wgTD becomes non-nil when the top-down resolve begins and reaches 0 (i.e. Wait()
|
// onceTopDown makes sure the top-down resolve walk only happens one time.
|
||||||
// proceeds) when the top-down resolve completes. (guarded by mu)
|
onceTopDown sync.Once
|
||||||
wgTD *sync.WaitGroup
|
|
||||||
|
|
||||||
// shippedNodes caches the results of a full walk of nodes identifying targets
|
// shippedNodes caches the results of a full walk of nodes identifying targets
|
||||||
// distributed either directly or as derivative works. (creation guarded by mu)
|
// distributed either directly or as derivative works. (creation guarded by mu)
|
||||||
|
@@ -49,84 +49,71 @@ func ResolveBottomUpConditions(lg *LicenseGraph) {
|
|||||||
func TraceBottomUpConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
|
func TraceBottomUpConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
|
||||||
|
|
||||||
// short-cut if already walked and cached
|
// short-cut if already walked and cached
|
||||||
lg.mu.Lock()
|
lg.onceBottomUp.Do(func() {
|
||||||
wg := lg.wgBU
|
// amap identifes targets previously walked. (guarded by mu)
|
||||||
|
amap := make(map[*TargetNode]struct{})
|
||||||
|
|
||||||
if wg != nil {
|
// mu guards concurrent access to amap
|
||||||
lg.mu.Unlock()
|
var mu sync.Mutex
|
||||||
wg.Wait()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
wg = &sync.WaitGroup{}
|
|
||||||
wg.Add(1)
|
|
||||||
lg.wgBU = wg
|
|
||||||
lg.mu.Unlock()
|
|
||||||
|
|
||||||
// amap identifes targets previously walked. (guarded by mu)
|
var walk func(target *TargetNode, treatAsAggregate bool) LicenseConditionSet
|
||||||
amap := make(map[*TargetNode]struct{})
|
|
||||||
|
|
||||||
// mu guards concurrent access to amap
|
walk = func(target *TargetNode, treatAsAggregate bool) LicenseConditionSet {
|
||||||
var mu sync.Mutex
|
priorWalkResults := func() (LicenseConditionSet, bool) {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
var walk func(target *TargetNode, treatAsAggregate bool) LicenseConditionSet
|
if _, alreadyWalked := amap[target]; alreadyWalked {
|
||||||
|
if treatAsAggregate {
|
||||||
walk = func(target *TargetNode, treatAsAggregate bool) LicenseConditionSet {
|
return target.resolution, true
|
||||||
priorWalkResults := func() (LicenseConditionSet, bool) {
|
}
|
||||||
mu.Lock()
|
if !target.pure {
|
||||||
defer mu.Unlock()
|
return target.resolution, true
|
||||||
|
}
|
||||||
if _, alreadyWalked := amap[target]; alreadyWalked {
|
// previously walked in a pure aggregate context,
|
||||||
if treatAsAggregate {
|
// needs to walk again in non-aggregate context
|
||||||
return target.resolution, true
|
} else {
|
||||||
|
target.resolution |= conditionsFn(target)
|
||||||
|
amap[target] = struct{}{}
|
||||||
}
|
}
|
||||||
if !target.pure {
|
target.pure = treatAsAggregate
|
||||||
return target.resolution, true
|
return target.resolution, false
|
||||||
}
|
|
||||||
// previously walked in a pure aggregate context,
|
|
||||||
// needs to walk again in non-aggregate context
|
|
||||||
} else {
|
|
||||||
target.resolution |= conditionsFn(target)
|
|
||||||
amap[target] = struct{}{}
|
|
||||||
}
|
}
|
||||||
target.pure = treatAsAggregate
|
cs, alreadyWalked := priorWalkResults()
|
||||||
return target.resolution, false
|
if alreadyWalked {
|
||||||
}
|
return cs
|
||||||
cs, alreadyWalked := priorWalkResults()
|
}
|
||||||
if alreadyWalked {
|
|
||||||
|
c := make(chan LicenseConditionSet, len(target.edges))
|
||||||
|
// add all the conditions from all the dependencies
|
||||||
|
for _, edge := range target.edges {
|
||||||
|
go func(edge *TargetEdge) {
|
||||||
|
// walk dependency to get its conditions
|
||||||
|
cs := walk(edge.dependency, treatAsAggregate && edge.dependency.IsContainer())
|
||||||
|
|
||||||
|
// turn those into the conditions that apply to the target
|
||||||
|
cs = depConditionsPropagatingToTarget(lg, edge, cs, treatAsAggregate)
|
||||||
|
|
||||||
|
c <- cs
|
||||||
|
}(edge)
|
||||||
|
}
|
||||||
|
for i := 0; i < len(target.edges); i++ {
|
||||||
|
cs |= <-c
|
||||||
|
}
|
||||||
|
mu.Lock()
|
||||||
|
target.resolution |= cs
|
||||||
|
mu.Unlock()
|
||||||
|
|
||||||
|
// return conditions up the tree
|
||||||
return cs
|
return cs
|
||||||
}
|
}
|
||||||
|
|
||||||
c := make(chan LicenseConditionSet, len(target.edges))
|
// walk each of the roots
|
||||||
// add all the conditions from all the dependencies
|
for _, rname := range lg.rootFiles {
|
||||||
for _, edge := range target.edges {
|
rnode := lg.targets[rname]
|
||||||
go func(edge *TargetEdge) {
|
_ = walk(rnode, rnode.IsContainer())
|
||||||
// walk dependency to get its conditions
|
|
||||||
cs := walk(edge.dependency, treatAsAggregate && edge.dependency.IsContainer())
|
|
||||||
|
|
||||||
// turn those into the conditions that apply to the target
|
|
||||||
cs = depConditionsPropagatingToTarget(lg, edge, cs, treatAsAggregate)
|
|
||||||
|
|
||||||
c <- cs
|
|
||||||
}(edge)
|
|
||||||
}
|
}
|
||||||
for i := 0; i < len(target.edges); i++ {
|
})
|
||||||
cs |= <-c
|
|
||||||
}
|
|
||||||
mu.Lock()
|
|
||||||
target.resolution |= cs
|
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
// return conditions up the tree
|
|
||||||
return cs
|
|
||||||
}
|
|
||||||
|
|
||||||
// walk each of the roots
|
|
||||||
for _, rname := range lg.rootFiles {
|
|
||||||
rnode := lg.targets[rname]
|
|
||||||
_ = walk(rnode, rnode.IsContainer())
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Done()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveTopDownCondtions performs a top-down walk of the LicenseGraph
|
// ResolveTopDownCondtions performs a top-down walk of the LicenseGraph
|
||||||
@@ -145,83 +132,76 @@ func ResolveTopDownConditions(lg *LicenseGraph) {
|
|||||||
func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
|
func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
|
||||||
|
|
||||||
// short-cut if already walked and cached
|
// short-cut if already walked and cached
|
||||||
lg.mu.Lock()
|
lg.onceTopDown.Do(func() {
|
||||||
wg := lg.wgTD
|
wg := &sync.WaitGroup{}
|
||||||
|
|
||||||
if wg != nil {
|
|
||||||
lg.mu.Unlock()
|
|
||||||
wg.Wait()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
wg = &sync.WaitGroup{}
|
|
||||||
wg.Add(1)
|
|
||||||
lg.wgTD = wg
|
|
||||||
lg.mu.Unlock()
|
|
||||||
|
|
||||||
// start with the conditions propagated up the graph
|
|
||||||
TraceBottomUpConditions(lg, conditionsFn)
|
|
||||||
|
|
||||||
// amap contains the set of targets already walked. (guarded by mu)
|
|
||||||
amap := make(map[*TargetNode]struct{})
|
|
||||||
|
|
||||||
// mu guards concurrent access to amap
|
|
||||||
var mu sync.Mutex
|
|
||||||
|
|
||||||
var walk func(fnode *TargetNode, cs LicenseConditionSet, treatAsAggregate bool)
|
|
||||||
|
|
||||||
walk = func(fnode *TargetNode, cs LicenseConditionSet, treatAsAggregate bool) {
|
|
||||||
defer wg.Done()
|
|
||||||
continueWalk := func() bool {
|
|
||||||
mu.Lock()
|
|
||||||
defer mu.Unlock()
|
|
||||||
depcs := fnode.resolution
|
|
||||||
_, alreadyWalked := amap[fnode]
|
|
||||||
if alreadyWalked {
|
|
||||||
if cs.IsEmpty() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if cs.Difference(depcs).IsEmpty() {
|
|
||||||
// no new conditions
|
|
||||||
|
|
||||||
// pure aggregates never need walking a 2nd time with same conditions
|
|
||||||
if treatAsAggregate {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// non-aggregates don't need walking as non-aggregate a 2nd time
|
|
||||||
if !fnode.pure {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// previously walked as pure aggregate; need to re-walk as non-aggregate
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fnode.resolution |= conditionsFn(fnode)
|
|
||||||
fnode.resolution |= cs
|
|
||||||
fnode.pure = treatAsAggregate
|
|
||||||
amap[fnode] = struct{}{}
|
|
||||||
cs = fnode.resolution
|
|
||||||
return true
|
|
||||||
}()
|
|
||||||
if !continueWalk {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// for each dependency
|
|
||||||
for _, edge := range fnode.edges {
|
|
||||||
// dcs holds the dpendency conditions inherited from the target
|
|
||||||
dcs := targetConditionsPropagatingToDep(lg, edge, cs, treatAsAggregate, conditionsFn)
|
|
||||||
dnode := edge.dependency
|
|
||||||
// add the conditions to the dependency
|
|
||||||
wg.Add(1)
|
|
||||||
go walk(dnode, dcs, treatAsAggregate && dnode.IsContainer())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// walk each of the roots
|
|
||||||
for _, rname := range lg.rootFiles {
|
|
||||||
rnode := lg.targets[rname]
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
// add the conditions to the root and its transitive closure
|
|
||||||
go walk(rnode, NewLicenseConditionSet(), rnode.IsContainer())
|
// start with the conditions propagated up the graph
|
||||||
}
|
TraceBottomUpConditions(lg, conditionsFn)
|
||||||
wg.Done()
|
|
||||||
wg.Wait()
|
// amap contains the set of targets already walked. (guarded by mu)
|
||||||
|
amap := make(map[*TargetNode]struct{})
|
||||||
|
|
||||||
|
// mu guards concurrent access to amap
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
var walk func(fnode *TargetNode, cs LicenseConditionSet, treatAsAggregate bool)
|
||||||
|
|
||||||
|
walk = func(fnode *TargetNode, cs LicenseConditionSet, treatAsAggregate bool) {
|
||||||
|
defer wg.Done()
|
||||||
|
continueWalk := func() bool {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
depcs := fnode.resolution
|
||||||
|
_, alreadyWalked := amap[fnode]
|
||||||
|
if alreadyWalked {
|
||||||
|
if cs.IsEmpty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if cs.Difference(depcs).IsEmpty() {
|
||||||
|
// no new conditions
|
||||||
|
|
||||||
|
// pure aggregates never need walking a 2nd time with same conditions
|
||||||
|
if treatAsAggregate {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// non-aggregates don't need walking as non-aggregate a 2nd time
|
||||||
|
if !fnode.pure {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// previously walked as pure aggregate; need to re-walk as non-aggregate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fnode.resolution |= conditionsFn(fnode)
|
||||||
|
fnode.resolution |= cs
|
||||||
|
fnode.pure = treatAsAggregate
|
||||||
|
amap[fnode] = struct{}{}
|
||||||
|
cs = fnode.resolution
|
||||||
|
return true
|
||||||
|
}()
|
||||||
|
if !continueWalk {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// for each dependency
|
||||||
|
for _, edge := range fnode.edges {
|
||||||
|
// dcs holds the dpendency conditions inherited from the target
|
||||||
|
dcs := targetConditionsPropagatingToDep(lg, edge, cs, treatAsAggregate, conditionsFn)
|
||||||
|
dnode := edge.dependency
|
||||||
|
// add the conditions to the dependency
|
||||||
|
wg.Add(1)
|
||||||
|
go walk(dnode, dcs, treatAsAggregate && dnode.IsContainer())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// walk each of the roots
|
||||||
|
for _, rname := range lg.rootFiles {
|
||||||
|
rnode := lg.targets[rname]
|
||||||
|
wg.Add(1)
|
||||||
|
// add the conditions to the root and its transitive closure
|
||||||
|
go walk(rnode, NewLicenseConditionSet(), rnode.IsContainer())
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
wg.Wait()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user