compiler: add support for 'go' on func values

This commit allows starting a new goroutine directly from a func value,
not just when the static callee is known.

This is necessary to support the whole time package, not just the
commonly used subset that was compiled with the SimpleDCE pass enabled.
This commit is contained in:
Ayke van Laethem
2019-08-15 15:52:26 +02:00
committed by Ron Evans
parent e4fc3bb66a
commit bbc3046687
6 changed files with 238 additions and 131 deletions
+18 -1
View File
@@ -28,6 +28,19 @@ func main() {
var printer Printer
printer = &myPrinter{}
printer.Print()
sleepFuncValue(func(x int) {
time.Sleep(1 * time.Millisecond)
println("slept inside func pointer", x)
})
time.Sleep(1 * time.Millisecond)
n := 20
sleepFuncValue(func(x int) {
time.Sleep(1 * time.Millisecond)
println("slept inside closure, with value:", n, x)
})
time.Sleep(2 * time.Millisecond)
}
func sub() {
@@ -47,6 +60,10 @@ func delayedValue() int {
return 42
}
func sleepFuncValue(fn func(int)) {
go fn(8)
}
func nowait() {
println("non-blocking goroutine")
}
@@ -55,7 +72,7 @@ type Printer interface {
Print()
}
type myPrinter struct{
type myPrinter struct {
}
func (i *myPrinter) Print() {
+2
View File
@@ -11,3 +11,5 @@ value produced after some time: 42
non-blocking goroutine
done with non-blocking goroutine
async interface method call
slept inside func pointer 8
slept inside closure, with value: 20 8