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:
Bob Badour 2022-10-29 22:45:12 -07:00
parent d2c28ba897
commit 5c12c66769
2 changed files with 130 additions and 152 deletions

View file

@ -58,13 +58,11 @@ type LicenseGraph struct {
/// (guarded by mu)
targets map[string]*TargetNode
// wgBU becomes non-nil when the bottom-up resolve begins and reaches 0
// (i.e. Wait() proceeds) when the bottom-up resolve completes. (guarded by mu)
wgBU *sync.WaitGroup
// onceBottomUp makes sure the bottom-up resolve walk only happens one time.
onceBottomUp sync.Once
// wgTD becomes non-nil when the top-down resolve begins and reaches 0 (i.e. Wait()
// proceeds) when the top-down resolve completes. (guarded by mu)
wgTD *sync.WaitGroup
// onceTopDown makes sure the top-down resolve walk only happens one time.
onceTopDown sync.Once
// shippedNodes caches the results of a full walk of nodes identifying targets
// distributed either directly or as derivative works. (creation guarded by mu)

View file

@ -49,19 +49,7 @@ func ResolveBottomUpConditions(lg *LicenseGraph) {
func TraceBottomUpConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
// short-cut if already walked and cached
lg.mu.Lock()
wg := lg.wgBU
if wg != nil {
lg.mu.Unlock()
wg.Wait()
return
}
wg = &sync.WaitGroup{}
wg.Add(1)
lg.wgBU = wg
lg.mu.Unlock()
lg.onceBottomUp.Do(func() {
// amap identifes targets previously walked. (guarded by mu)
amap := make(map[*TargetNode]struct{})
@ -125,8 +113,7 @@ func TraceBottomUpConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
rnode := lg.targets[rname]
_ = walk(rnode, rnode.IsContainer())
}
wg.Done()
})
}
// ResolveTopDownCondtions performs a top-down walk of the LicenseGraph
@ -145,18 +132,9 @@ func ResolveTopDownConditions(lg *LicenseGraph) {
func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
// short-cut if already walked and cached
lg.mu.Lock()
wg := lg.wgTD
if wg != nil {
lg.mu.Unlock()
wg.Wait()
return
}
wg = &sync.WaitGroup{}
lg.onceTopDown.Do(func() {
wg := &sync.WaitGroup{}
wg.Add(1)
lg.wgTD = wg
lg.mu.Unlock()
// start with the conditions propagated up the graph
TraceBottomUpConditions(lg, conditionsFn)
@ -174,6 +152,7 @@ func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
continueWalk := func() bool {
mu.Lock()
defer mu.Unlock()
depcs := fnode.resolution
_, alreadyWalked := amap[fnode]
if alreadyWalked {
@ -224,4 +203,5 @@ func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
}
wg.Done()
wg.Wait()
})
}