From 0759b70c5065835d5e341135cdc42242683e0e15 Mon Sep 17 00:00:00 2001 From: Jaden Weiss Date: Fri, 7 Feb 2020 19:20:36 -0500 Subject: [PATCH] run init in a goroutine --- src/runtime/gc_conservative.go | 2 +- src/runtime/gc_leaking.go | 4 ++++ src/runtime/gc_none.go | 4 ++++ src/runtime/runtime_unix.go | 8 ++++---- src/runtime/runtime_wasm.go | 4 ++++ src/runtime/scheduler_any.go | 5 +++-- src/runtime/scheduler_none.go | 1 + 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/runtime/gc_conservative.go b/src/runtime/gc_conservative.go index 41a27523..319e68b5 100644 --- a/src/runtime/gc_conservative.go +++ b/src/runtime/gc_conservative.go @@ -180,7 +180,7 @@ func (b gcBlock) unmark() { // No memory may be allocated before this is called. That means the runtime and // any packages the runtime depends upon may not allocate memory during package // initialization. -func init() { +func initHeap() { totalSize := heapEnd - heapStart // Allocate some memory to keep 2 bits of information about every block. diff --git a/src/runtime/gc_leaking.go b/src/runtime/gc_leaking.go index fb0f9086..f5e35951 100644 --- a/src/runtime/gc_leaking.go +++ b/src/runtime/gc_leaking.go @@ -45,3 +45,7 @@ func KeepAlive(x interface{}) { func SetFinalizer(obj interface{}, finalizer interface{}) { // Unimplemented. } + +func initHeap() { + // Nothing to initialize. +} diff --git a/src/runtime/gc_none.go b/src/runtime/gc_none.go index b7fc628d..c69d2a24 100644 --- a/src/runtime/gc_none.go +++ b/src/runtime/gc_none.go @@ -27,3 +27,7 @@ func KeepAlive(x interface{}) { func SetFinalizer(obj interface{}, finalizer interface{}) { // Unimplemented. } + +func initHeap() { + // Nothing to initialize. +} diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index bb34a0d5..daf934ea 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -26,10 +26,7 @@ func clock_gettime(clk_id int32, ts *timespec) const heapSize = 1 * 1024 * 1024 // 1MB to start -var ( - heapStart = uintptr(malloc(heapSize)) - heapEnd = heapStart + heapSize -) +var heapStart, heapEnd uintptr type timeUnit int64 @@ -50,6 +47,9 @@ func postinit() {} // Entry point for Go. Initialize all packages and call main.main(). //go:export main func main() int { + heapStart = uintptr(malloc(heapSize)) + heapEnd = heapStart + heapSize + run() // For libc compatibility. diff --git a/src/runtime/runtime_wasm.go b/src/runtime/runtime_wasm.go index 90a25a82..8042161b 100644 --- a/src/runtime/runtime_wasm.go +++ b/src/runtime/runtime_wasm.go @@ -22,6 +22,10 @@ func postinit() {} //export _start func _start() { + // These need to be initialized early so that the heap can be initialized. + heapStart = uintptr(unsafe.Pointer(&heapStartSymbol)) + heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize) + run() } diff --git a/src/runtime/scheduler_any.go b/src/runtime/scheduler_any.go index b96a4eba..41a90453 100644 --- a/src/runtime/scheduler_any.go +++ b/src/runtime/scheduler_any.go @@ -14,9 +14,10 @@ func sleep(duration int64) { // run is called by the program entry point to execute the go program. // With a scheduler, init and the main function are invoked in a goroutine before starting the scheduler. func run() { - initAll() - postinit() + initHeap() go func() { + initAll() + postinit() callMain() }() scheduler() diff --git a/src/runtime/scheduler_none.go b/src/runtime/scheduler_none.go index a86110f2..d462ca15 100644 --- a/src/runtime/scheduler_none.go +++ b/src/runtime/scheduler_none.go @@ -16,6 +16,7 @@ func getSystemStackPointer() uintptr { // run is called by the program entry point to execute the go program. // With the "none" scheduler, init and the main function are invoked directly. func run() { + initHeap() initAll() postinit() callMain()