Created
June 8, 2016 16:03
-
-
Save petermattis/f9b02c59e00db287ae6d2b47bdb6e33b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go | |
index f2a2a60..53c962c 100644 | |
--- a/src/cmd/go/build.go | |
+++ b/src/cmd/go/build.go | |
@@ -694,6 +694,8 @@ type builder struct { | |
exec sync.Mutex | |
readySema chan bool | |
ready actionQueue | |
+ | |
+ tasks chan func() | |
} | |
// An action represents a single action in the action graph. | |
@@ -1236,6 +1238,7 @@ func (b *builder) do(root *action) { | |
} | |
b.readySema = make(chan bool, len(all)) | |
+ b.tasks = make(chan func(), buildP) | |
// Initialize per-action execution state. | |
for _, a := range all { | |
@@ -1312,6 +1315,8 @@ func (b *builder) do(root *action) { | |
a := b.ready.pop() | |
b.exec.Unlock() | |
handle(a) | |
+ case task := <-b.tasks: | |
+ task() | |
case <-interrupted: | |
setExitStatus(1) | |
return | |
@@ -3141,12 +3146,16 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi | |
staticLibs = []string{"-Wl,--start-group", "-lmingwex", "-lmingw32", "-Wl,--end-group"} | |
} | |
+ var tasks []func() | |
+ var results chan error | |
+ | |
cflags := stringList(cgoCPPFLAGS, cgoCFLAGS) | |
for _, cfile := range cfiles { | |
+ cfile := cfile | |
ofile := obj + cfile[:len(cfile)-1] + "o" | |
- if err := b.gcc(p, ofile, cflags, obj+cfile); err != nil { | |
- return nil, nil, err | |
- } | |
+ tasks = append(tasks, func() { | |
+ results <- b.gcc(p, ofile, cflags, obj+cfile) | |
+ }) | |
linkobj = append(linkobj, ofile) | |
if !strings.HasSuffix(ofile, "_cgo_main.o") { | |
outObj = append(outObj, ofile) | |
@@ -3154,35 +3163,65 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi | |
} | |
for _, file := range gccfiles { | |
+ file := file | |
ofile := obj + cgoRe.ReplaceAllString(file[:len(file)-1], "_") + "o" | |
- if err := b.gcc(p, ofile, cflags, file); err != nil { | |
- return nil, nil, err | |
- } | |
+ tasks = append(tasks, func() { | |
+ results <- b.gcc(p, ofile, cflags, file) | |
+ }) | |
linkobj = append(linkobj, ofile) | |
outObj = append(outObj, ofile) | |
} | |
cxxflags := stringList(cgoCPPFLAGS, cgoCXXFLAGS) | |
for _, file := range gxxfiles { | |
+ file := file | |
// Append .o to the file, just in case the pkg has file.c and file.cpp | |
ofile := obj + cgoRe.ReplaceAllString(file, "_") + ".o" | |
- if err := b.gxx(p, ofile, cxxflags, file); err != nil { | |
- return nil, nil, err | |
- } | |
+ tasks = append(tasks, func() { | |
+ results <- b.gxx(p, ofile, cxxflags, file) | |
+ }) | |
linkobj = append(linkobj, ofile) | |
outObj = append(outObj, ofile) | |
} | |
for _, file := range mfiles { | |
+ file := file | |
// Append .o to the file, just in case the pkg has file.c and file.m | |
ofile := obj + cgoRe.ReplaceAllString(file, "_") + ".o" | |
- if err := b.gcc(p, ofile, cflags, file); err != nil { | |
- return nil, nil, err | |
- } | |
+ tasks = append(tasks, func() { | |
+ results <- b.gcc(p, ofile, cflags, file) | |
+ }) | |
linkobj = append(linkobj, ofile) | |
outObj = append(outObj, ofile) | |
} | |
+ // Give the results channel enough capacity so that sending the | |
+ // result is guaranteed not to block. | |
+ results = make(chan error, len(tasks)) | |
+ | |
+ // Feed the tasks into the b.tasks channel on a separate goroutine | |
+ // because the b.tasks channel's limited capacity might cause | |
+ // sending the task to block. | |
+ go func() { | |
+ for _, task := range tasks { | |
+ b.tasks <- task | |
+ } | |
+ }() | |
+ | |
+ // Loop until we've received results from all of our tasks or an | |
+ // error occurs. | |
+ for count := 0; count < len(tasks); { | |
+ select { | |
+ case err := <-results: | |
+ if err != nil { | |
+ return nil, nil, err | |
+ } | |
+ count++ | |
+ case task := <-b.tasks: | |
+ task() | |
+ } | |
+ } | |
+ | |
linkobj = append(linkobj, p.SysoFiles...) | |
dynobj := obj + "_cgo_.o" | |
pie := (goarch == "arm" && goos == "linux") || goos == "android" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment