Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 83ac0bf9 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I381ee79e,Ie30edbb2

* changes:
  Replace nil-able *sync.Waitgroup with sync.Once
  Fix top-down resolve re-walking graph too much.
parents 0cd509a0 5c12c667
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -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)
+108 −123
Original line number Diff line number Diff line
@@ -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)
@@ -171,42 +149,48 @@ func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) {

		walk = func(fnode *TargetNode, cs LicenseConditionSet, treatAsAggregate bool) {
			defer wg.Done()
		mu.Lock()
		fnode.resolution |= conditionsFn(fnode)
		fnode.resolution |= cs
		fnode.pure = treatAsAggregate
		amap[fnode] = struct{}{}
		cs = fnode.resolution
		mu.Unlock()
		// for each dependency
		for _, edge := range fnode.edges {
			func(edge *TargetEdge) {
				// dcs holds the dpendency conditions inherited from the target
				dcs := targetConditionsPropagatingToDep(lg, edge, cs, treatAsAggregate, conditionsFn)
				dnode := edge.dependency
			continueWalk := func() bool {
				mu.Lock()
				defer mu.Unlock()
				depcs := dnode.resolution
				_, alreadyWalked := amap[dnode]
				if !dcs.IsEmpty() && alreadyWalked {
					if dcs.Difference(depcs).IsEmpty() {

				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
							return false
						}
						// non-aggregates don't need walking as non-aggregate a 2nd time
						if !dnode.pure {
							return
						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())
			}(edge)
			}
		}

@@ -219,4 +203,5 @@ func TraceTopDownConditions(lg *LicenseGraph, conditionsFn TraceConditions) {
		}
		wg.Done()
		wg.Wait()
	})
}