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) /// (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)

View File

@@ -49,19 +49,7 @@ 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
if wg != nil {
lg.mu.Unlock()
wg.Wait()
return
}
wg = &sync.WaitGroup{}
wg.Add(1)
lg.wgBU = wg
lg.mu.Unlock()
// amap identifes targets previously walked. (guarded by mu) // amap identifes targets previously walked. (guarded by mu)
amap := make(map[*TargetNode]struct{}) amap := make(map[*TargetNode]struct{})
@@ -125,8 +113,7 @@ func TraceBottomUpConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
rnode := lg.targets[rname] rnode := lg.targets[rname]
_ = walk(rnode, rnode.IsContainer()) _ = 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,18 +132,9 @@ 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) wg.Add(1)
lg.wgTD = wg
lg.mu.Unlock()
// start with the conditions propagated up the graph // start with the conditions propagated up the graph
TraceBottomUpConditions(lg, conditionsFn) TraceBottomUpConditions(lg, conditionsFn)
@@ -174,6 +152,7 @@ func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
continueWalk := func() bool { continueWalk := func() bool {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
depcs := fnode.resolution depcs := fnode.resolution
_, alreadyWalked := amap[fnode] _, alreadyWalked := amap[fnode]
if alreadyWalked { if alreadyWalked {
@@ -224,4 +203,5 @@ func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
} }
wg.Done() wg.Done()
wg.Wait() wg.Wait()
})
} }