interp: run goroutine starts and checks at runtime
This change prevents interp from trying to execute goroutine starts or checks. This fixes a bug where a goroutine started by an init function would run before the init function.
Этот коммит содержится в:
родитель
0aed62efe4
коммит
9db8826b3b
3 изменённых файлов: 12 добавлений и 1 удалений
|
@ -197,7 +197,8 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
|
||||||
// which case this call won't even get to this point but will
|
// which case this call won't even get to this point but will
|
||||||
// already be emitted in initAll.
|
// already be emitted in initAll.
|
||||||
continue
|
continue
|
||||||
case strings.HasPrefix(callFn.name, "runtime.print") || callFn.name == "runtime._panic" || callFn.name == "runtime.hashmapGet" || callFn.name == "os.runtime_args":
|
case strings.HasPrefix(callFn.name, "runtime.print") || callFn.name == "runtime._panic" || callFn.name == "runtime.hashmapGet" ||
|
||||||
|
callFn.name == "os.runtime_args" || callFn.name == "internal/task.start" || callFn.name == "internal/task.Current":
|
||||||
// These functions should be run at runtime. Specifically:
|
// These functions should be run at runtime. Specifically:
|
||||||
// * Print and panic functions are best emitted directly without
|
// * Print and panic functions are best emitted directly without
|
||||||
// interpreting them, otherwise we get a ton of putchar (etc.)
|
// interpreting them, otherwise we get a ton of putchar (etc.)
|
||||||
|
@ -208,6 +209,8 @@ func (r *runner) run(fn *function, params []value, parentMem *memoryView, indent
|
||||||
// * os.runtime_args reads globals that are initialized outside
|
// * os.runtime_args reads globals that are initialized outside
|
||||||
// the view of the interp package so it always needs to be run
|
// the view of the interp package so it always needs to be run
|
||||||
// at runtime.
|
// at runtime.
|
||||||
|
// * internal/task.start, internal/task.Current: start and read shcheduler state,
|
||||||
|
// which is modified elsewhere.
|
||||||
err := r.runAtRuntime(fn, inst, locals, &mem, indent)
|
err := r.runAtRuntime(fn, inst, locals, &mem, indent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, mem, err
|
return nil, mem, err
|
||||||
|
|
6
testdata/goroutines.go
предоставленный
6
testdata/goroutines.go
предоставленный
|
@ -6,6 +6,12 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
println("init")
|
||||||
|
go println("goroutine in init")
|
||||||
|
time.Sleep(1 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
println("main 1")
|
println("main 1")
|
||||||
go sub()
|
go sub()
|
||||||
|
|
2
testdata/goroutines.txt
предоставленный
2
testdata/goroutines.txt
предоставленный
|
@ -1,3 +1,5 @@
|
||||||
|
init
|
||||||
|
goroutine in init
|
||||||
main 1
|
main 1
|
||||||
sub 1
|
sub 1
|
||||||
main 2
|
main 2
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче