From 6bdfdb5f51951584732e6a95a7ee8a5ba5e8cb93 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Thu, 5 Mar 2026 10:07:49 +0100 Subject: runtime: implement part of library initialization in Go All architectures supporting c-shared and c-archive share the same initialization code in assembly, and most of it can be implemented in pure Go. Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64-longtest,gotip-linux-ppc64le_power10,gotip-linux-riscv64,gotip-linux-loong64,gotip-linux-s390x Change-Id: Iaa9fb7d6f9ca8785f1098461646d607ef6b00d47 Reviewed-on: https://go-review.googlesource.com/c/go/+/706417 Auto-Submit: Quim Muntal Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI Reviewed-by: Carlos Amedee --- src/runtime/proc.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/runtime/proc.go') diff --git a/src/runtime/proc.go b/src/runtime/proc.go index db4febace9..71e80e9cb7 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -8156,3 +8156,24 @@ func doInit1(t *initTask) { t.state = 2 // initialization done } } + +// libInit is common startup code for most architectures when +// using -buildmode=c-archive or -buildmode=c-shared. +// +// May run with m.p==nil, so write barriers are not allowed. +// +//go:nowritebarrierrec +//go:nosplit +func libInit() { + // Synchronous initialization. + libpreinit() + + // Asynchronous initialization. + // Prefer creating a thread via cgo if it is available. + if _cgo_sys_thread_create != nil { + asmcgocall(_cgo_sys_thread_create, unsafe.Pointer(abi.FuncPCABIInternal(rt0_lib_go))) + } else { + const stackSize = 0x800000 // 8192KB + newosproc0(stackSize, unsafe.Pointer(abi.FuncPCABIInternal(rt0_lib_go))) + } +} -- cgit v1.3-6-g1900