tls: add gc-aware pthread slots

This commit is contained in:
Li Jie
2025-10-15 10:23:09 +08:00
parent dba7bd498f
commit 2110db7263
7 changed files with 501 additions and 0 deletions

View File

@@ -40,6 +40,12 @@ func Realloc(ptr c.Pointer, size uintptr) c.Pointer
//go:linkname Free C.GC_free
func Free(ptr c.Pointer)
//go:linkname AddRoots C.GC_add_roots
func AddRoots(start, end c.Pointer)
//go:linkname RemoveRoots C.GC_remove_roots
func RemoveRoots(start, end c.Pointer)
// -----------------------------------------------------------------------------
//go:linkname RegisterFinalizer C.GC_register_finalizer

View File

@@ -0,0 +1,88 @@
//go:build llgo
package tls
import (
"unsafe"
c "github.com/goplus/llgo/runtime/internal/clite"
"github.com/goplus/llgo/runtime/internal/clite/pthread"
)
type Handle[T any] struct {
key pthread.Key
destructor func(*T)
}
// Alloc creates a TLS handle backed by pthread TLS.
func Alloc[T any](destructor func(*T)) Handle[T] {
var key pthread.Key
if ret := key.Create(slotDestructor[T]); ret != 0 {
c.Fprintf(c.Stderr, c.Str("tls: pthread_key_create failed (errno=%d)\n"), ret)
panic("tls: failed to create thread local storage key")
}
return Handle[T]{key: key, destructor: destructor}
}
// Get returns the value stored in the current thread's slot.
func (h Handle[T]) Get() T {
if ptr := h.key.Get(); ptr != nil {
return (*slot[T])(ptr).value
}
var zero T
return zero
}
// Set stores v in the current thread's slot, creating it if necessary.
func (h Handle[T]) Set(v T) {
s := h.ensureSlot()
s.value = v
}
// Clear zeroes the current thread's slot value without freeing the slot.
func (h Handle[T]) Clear() {
if ptr := h.key.Get(); ptr != nil {
s := (*slot[T])(ptr)
var zero T
s.value = zero
}
}
func (h Handle[T]) ensureSlot() *slot[T] {
if ptr := h.key.Get(); ptr != nil {
return (*slot[T])(ptr)
}
size := unsafe.Sizeof(slot[T]{})
mem := c.Calloc(1, size)
if mem == nil {
panic("tls: failed to allocate thread slot")
}
s := (*slot[T])(mem)
s.destructor = h.destructor
if existing := h.key.Get(); existing != nil {
c.Free(mem)
return (*slot[T])(existing)
}
if ret := h.key.Set(mem); ret != 0 {
c.Free(mem)
c.Fprintf(c.Stderr, c.Str("tls: pthread_setspecific failed (errno=%d)\n"), ret)
panic("tls: failed to set thread local storage value")
}
registerSlot(s)
return s
}
func slotDestructor[T any](ptr c.Pointer) {
s := (*slot[T])(ptr)
if s == nil {
return
}
if s.destructor != nil {
s.destructor(&s.value)
}
deregisterSlot(s)
var zero T
s.value = zero
s.destructor = nil
c.Free(ptr)
}

View File

@@ -0,0 +1,53 @@
//go:build llgo && !nogc
package tls
import (
"unsafe"
c "github.com/goplus/llgo/runtime/internal/clite"
"github.com/goplus/llgo/runtime/internal/clite/bdwgc"
)
const slotRegistered = 1 << iota
const maxSlotSize = 1 << 20 // 1 MiB sanity cap
type slot[T any] struct {
value T
state uintptr
destructor func(*T)
}
func registerSlot[T any](s *slot[T]) {
if s.state&slotRegistered != 0 {
return
}
start, end := s.rootRange()
size := uintptr(end) - uintptr(start)
if size == 0 {
return
}
if size > maxSlotSize {
panic("tls: slot size exceeds maximum")
}
bdwgc.AddRoots(start, end)
s.state |= slotRegistered
}
func deregisterSlot[T any](s *slot[T]) {
if s == nil || s.state&slotRegistered == 0 {
return
}
s.state &^= slotRegistered
start, end := s.rootRange()
if uintptr(end) > uintptr(start) {
bdwgc.RemoveRoots(start, end)
}
}
func (s *slot[T]) rootRange() (start, end c.Pointer) {
begin := unsafe.Pointer(s)
endPtr := unsafe.Pointer(uintptr(begin) + unsafe.Sizeof(*s))
return c.Pointer(begin), c.Pointer(endPtr)
}

View File

@@ -0,0 +1,12 @@
//go:build llgo && nogc
package tls
type slot[T any] struct {
value T
destructor func(*T)
}
func registerSlot[T any](s *slot[T]) {}
func deregisterSlot[T any](s *slot[T]) {}

View File

@@ -0,0 +1,125 @@
//go:build llgo
package tls_test
import (
"fmt"
"sync"
"testing"
"github.com/goplus/llgo/runtime/internal/clite/tls"
)
func TestAllocReadWrite(t *testing.T) {
h := tls.Alloc[int](nil)
if got := h.Get(); got != 0 {
t.Fatalf("zero slot = %d, want 0", got)
}
h.Set(42)
if got := h.Get(); got != 42 {
t.Fatalf("Set/Get mismatch: got %d", got)
}
h.Clear()
if got := h.Get(); got != 0 {
t.Fatalf("Clear() did not reset slot, got %d", got)
}
}
func TestAllocThreadLocalIsolation(t *testing.T) {
h := tls.Alloc[int](nil)
h.Set(7)
const want = 99
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if got := h.Get(); got != 0 {
t.Errorf("new goroutine initial value = %d, want 0", got)
}
h.Set(want)
if got := h.Get(); got != want {
t.Errorf("goroutine value = %d, want %d", got, want)
}
}()
wg.Wait()
if got := h.Get(); got != 7 {
t.Fatalf("main goroutine value changed to %d", got)
}
}
func TestDestructorRuns(t *testing.T) {
var mu sync.Mutex
var calls int
values := make([]int, 0, 1)
h := tls.Alloc[*int](func(p **int) {
mu.Lock()
defer mu.Unlock()
if p != nil && *p != nil {
calls++
values = append(values, **p)
}
})
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
val := new(int)
*val = 123
h.Set(val)
}()
wg.Wait()
mu.Lock()
defer mu.Unlock()
if calls == 0 {
t.Fatalf("expected destructor to be invoked")
}
if len(values) != 1 || values[0] != 123 {
t.Fatalf("destructor saw unexpected values: %v", values)
}
}
func TestAllocStress(t *testing.T) {
const sequentialIterations = 200_000
h := tls.Alloc[int](nil)
for i := 0; i < sequentialIterations; i++ {
h.Set(i)
if got := h.Get(); got != i {
t.Fatalf("stress iteration %d: got %d want %d", i, got, i)
}
}
var wg sync.WaitGroup
const (
goroutines = 32
iterationsPerGoroutine = 1_000
)
errs := make(chan error, goroutines)
wg.Add(goroutines)
for g := 0; g < goroutines; g++ {
go func(offset int) {
defer wg.Done()
local := tls.Alloc[int](nil)
for i := 0; i < iterationsPerGoroutine; i++ {
v := offset*iterationsPerGoroutine + i
local.Set(v)
if got := local.Get(); got != v {
errs <- fmt.Errorf("goroutine %d iteration %d: got %d want %d", offset, i, got, v)
return
}
}
}(g)
}
wg.Wait()
close(errs)
for err := range errs {
if err != nil {
t.Fatal(err)
}
}
}

View File

@@ -53,6 +53,96 @@ func newDIBuilder(prog Program, pkg Package, positioner Positioner) diBuilder {
return b
}
func hasTypeParam(typ types.Type) bool {
visited := make(map[types.Type]bool)
var visit func(types.Type) bool
visit = func(tt types.Type) bool {
if tt == nil {
return false
}
if visited[tt] {
return false
}
visited[tt] = true
switch t := tt.(type) {
case *types.TypeParam:
return true
case *types.Named:
if tp := t.TypeParams(); tp != nil && tp.Len() > 0 {
if ta := t.TypeArgs(); ta == nil || ta.Len() == 0 {
return true
}
}
if ta := t.TypeArgs(); ta != nil {
for i := 0; i < ta.Len(); i++ {
if visit(ta.At(i)) {
return true
}
}
}
return visit(t.Underlying())
case *types.Pointer:
return visit(t.Elem())
case *types.Slice:
return visit(t.Elem())
case *types.Array:
return visit(t.Elem())
case *types.Map:
return visit(t.Key()) || visit(t.Elem())
case *types.Chan:
return visit(t.Elem())
case *types.Signature:
if tp := t.TypeParams(); tp != nil && tp.Len() > 0 {
return true
}
if params := t.Params(); params != nil {
for i := 0; i < params.Len(); i++ {
if visit(params.At(i).Type()) {
return true
}
}
}
if results := t.Results(); results != nil {
for i := 0; i < results.Len(); i++ {
if visit(results.At(i).Type()) {
return true
}
}
}
return false
case *types.Tuple:
for i := 0; i < t.Len(); i++ {
if visit(t.At(i).Type()) {
return true
}
}
return false
case *types.Struct:
for i := 0; i < t.NumFields(); i++ {
if visit(t.Field(i).Type()) {
return true
}
}
return false
case *types.Interface:
for i := 0; i < t.NumMethods(); i++ {
if visit(t.Method(i).Type()) {
return true
}
}
for i := 0; i < t.NumEmbeddeds(); i++ {
if visit(t.EmbeddedType(i)) {
return true
}
}
return false
default:
return false
}
}
return visit(typ)
}
// New method to add named metadata operand
func (b diBuilder) addNamedMetadataOperand(name string, intValue int, stringValue string, intValue2 int) {
ctx := b.m.Context()
@@ -522,6 +612,9 @@ func (b diBuilder) dbgValue(v Expr, dv DIVar, scope DIScope, pos token.Position,
}
func (b diBuilder) diType(t Type, pos token.Position) DIType {
if hasTypeParam(t.RawType()) {
return &aDIType{}
}
name := t.RawType().String()
return b.diTypeEx(name, t, pos)
}

124
ssa/di_test.go Normal file
View File

@@ -0,0 +1,124 @@
//go:build !llgo
package ssa
import (
"go/token"
"go/types"
"testing"
)
func TestHasTypeParam(t *testing.T) {
generic := newGenericNamedType("Box")
instantiated, err := types.Instantiate(types.NewContext(), generic, []types.Type{types.Typ[types.String]}, true)
if err != nil {
t.Fatalf("Instantiate: %v", err)
}
arrayType := func() types.Type {
tp := newTypeParam("ArrayElem")
return types.NewArray(tp, 3)
}()
chanType := types.NewChan(types.SendRecv, newTypeParam("ChanElem"))
tupleType := func() types.Type {
tp := newTypeParam("TupleElem")
elem := types.NewVar(token.NoPos, nil, "v", tp)
return types.NewTuple(elem)
}()
structWithParam := func() types.Type {
tp := newTypeParam("StructElem")
field := types.NewVar(token.NoPos, nil, "value", tp)
return types.NewStruct([]*types.Var{field}, nil)
}()
signatureWithParam := func() types.Type {
tp := newTypeParam("SigParam")
params := types.NewTuple(types.NewVar(token.NoPos, nil, "x", tp))
return types.NewSignatureType(nil, nil, []*types.TypeParam{tp}, params, types.NewTuple(), false)
}()
signatureWithResult := func() types.Type {
tp := newTypeParam("SigResult")
results := types.NewTuple(types.NewVar(token.NoPos, nil, "res", tp))
return types.NewSignatureType(nil, nil, []*types.TypeParam{tp}, types.NewTuple(), results, false)
}()
interfaceWithParam := func() types.Type {
tp := newTypeParam("IfaceParam")
params := types.NewTuple(types.NewVar(token.NoPos, nil, "v", tp))
method := types.NewFunc(token.NoPos, nil, "Do", types.NewSignatureType(nil, nil, []*types.TypeParam{tp}, params, types.NewTuple(), false))
iface := types.NewInterfaceType([]*types.Func{method}, nil)
iface.Complete()
return iface
}()
interfaceWithEmbed := func() types.Type {
base := interfaceWithParam
tp := newTypeParam("EmbedParam")
embedMethod := types.NewFunc(token.NoPos, nil, "Run", types.NewSignatureType(nil, nil, []*types.TypeParam{tp}, types.NewTuple(), types.NewTuple(), false))
iface := types.NewInterfaceType([]*types.Func{embedMethod}, []types.Type{base})
iface.Complete()
return iface
}()
selfRecursive := func() types.Type {
typeName := types.NewTypeName(token.NoPos, nil, "Node", nil)
placeholder := types.NewStruct(nil, nil)
named := types.NewNamed(typeName, placeholder, nil)
field := types.NewVar(token.NoPos, nil, "next", types.NewPointer(named))
structType := types.NewStruct([]*types.Var{field}, nil)
named.SetUnderlying(structType)
return named
}()
tests := []struct {
name string
typ types.Type
want bool
}{
{"basic", types.Typ[types.Int], false},
{"typeParam", newTypeParam("T"), true},
{"pointerToTypeParam", types.NewPointer(newTypeParam("PtrT")), true},
{"sliceOfTypeParam", types.NewSlice(newTypeParam("SliceT")), true},
{"arrayOfTypeParam", arrayType, true},
{"mapWithTypeParam", types.NewMap(newTypeParam("MapKey"), types.Typ[types.String]), true},
{"chanOfTypeParam", chanType, true},
{"tupleWithTypeParam", tupleType, true},
{"structWithTypeParam", structWithParam, true},
{"signatureWithTypeParam", signatureWithParam, true},
{"signatureWithResultTypeParam", signatureWithResult, true},
{"interfaceWithTypeParam", interfaceWithParam, true},
{"interfaceWithEmbeddedTypeParam", interfaceWithEmbed, true},
{"namedGeneric", generic, true},
{"pointerToNamedGeneric", types.NewPointer(generic), true},
{"namedInstanceNoParam", instantiated, false},
{"selfRecursiveStruct", selfRecursive, false},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if got := hasTypeParam(tc.typ); got != tc.want {
t.Fatalf("hasTypeParam(%s) = %v, want %v", tc.name, got, tc.want)
}
})
}
}
func newTypeParam(name string) *types.TypeParam {
iface := types.NewInterfaceType(nil, nil)
iface.Complete()
return types.NewTypeParam(types.NewTypeName(token.NoPos, nil, name, nil), iface)
}
func newGenericNamedType(name string) *types.Named {
tp := newTypeParam("T")
field := types.NewVar(token.NoPos, nil, "value", tp)
structType := types.NewStruct([]*types.Var{field}, nil)
named := types.NewNamed(types.NewTypeName(token.NoPos, nil, name, nil), structType, nil)
named.SetTypeParams([]*types.TypeParam{tp})
return named
}