Debian Codegraph @

go/ssa/task.go

raw symbols SCIP
1 // Copyright 2024 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package ssascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/
6
7 import (
8 "sync/atomicscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/"
9 )
10
11 // Each task has two states: it is initially "active",
12 // and transitions to "done".
13 //
14 // tasks form a directed graph. An edge from x to y (with y in x.edges)
15 // indicates that the task x waits on the task y to be done.
16 // Cycles are permitted.
17 //
18 // Calling x.wait() blocks the calling goroutine until task x,
19 // and all the tasks transitively reachable from x are done.
20 //
21 // The nil *task is always considered done.
22 type taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task# struct {
23 donescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#done. chan unitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/unit# // close when the task is done.
24 edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#edges. map[*taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#]unitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/unit# // set of predecessors of this task.
25 transitivescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#transitive. atomicscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/.Boolscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Bool# // true once it is known all predecessors are done.
26 }
27
28 func (x *taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#) isTransitivelyDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#isTransitivelyDone().() bool { return x == nil || x.transitivescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#transitive..Loadscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Bool#Load().() }
29
30 // addEdge creates an edge from x to y, indicating that
31 // x.wait() will not return before y is done.
32 // All calls to x.addEdge(...) should happen before x.markDone().
33 func (x *taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#) addEdgescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#addEdge().(y *taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#) {
34 if x == y || y.isTransitivelyDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#isTransitivelyDone().() {
35 return // no work remaining
36 }
37
38 // heuristic done check
39 select {
40 case <-x.donescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#done.:
41 panic("cannot add an edge to a done task")
42 default:
43 }
44
45 if x.edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#edges. == nil {
46 x.edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#edges. = make(map[*taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#]unitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/unit#)
47 }
48 x.edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#edges.[y] = unitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/unit#{}
49 }
50
51 // markDone changes the task's state to markDone.
52 func (x *taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#) markDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#markDone().() {
53 if x != nil {
54 close(x.donescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#done.)
55 }
56 }
57
58 // wait blocks until x and all the tasks it can reach through edges are done.
59 func (x *taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#) waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#wait().() {
60 if x.isTransitivelyDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#isTransitivelyDone().() {
61 return // already known to be done. Skip allocations.
62 }
63
64 // Use BFS to wait on u.done to be closed, for all u transitively
65 // reachable from x via edges.
66 //
67 // This work can be repeated by multiple workers doing wait().
68 //
69 // Note: Tarjan's SCC algorithm is able to mark SCCs as transitively done
70 // as soon as the SCC has been visited. This is theoretically faster, but is
71 // a more complex algorithm. Until we have evidence, we need the more complex
72 // algorithm, the simpler algorithm BFS is implemented.
73 //
74 // In Go 1.23, ssa/TestStdlib reaches <=3 *tasks per wait() in most schedules
75 // On some schedules, there is a cycle building net/http and internal/trace/testtrace
76 // due to slices functions.
77 work := []*taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#{x}
78 enqueued := map[*taskscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#]unitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/unit#{x: {}}
79 for i := 0; i < len(work); i++ {
80 u := work[i]
81 if u.isTransitivelyDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#isTransitivelyDone().() { // already transitively done
82 work[i] = nil
83 continue
84 }
85 <-u.donescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#done. // wait for u to be marked done.
86
87 for v := range u.edgesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#edges. {
88 if _, ok := enqueued[v]; !ok {
89 enqueued[v] = unitscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/unit#{}
90 work = append(work, v)
91 }
92 }
93 }
94
95 // work is transitively closed over dependencies.
96 // u in work is done (or transitively done and skipped).
97 // u is transitively done.
98 for _, u := range work {
99 if u != nil {
100 x.transitivescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ssa`/task#transitive..Storescip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Bool#Store().(true)
101 }
102 }
103 }