Skip to content

Commit 1ed418e

Browse files
committed
tls: document package and guard helpers
1 parent 2110db7 commit 1ed418e

6 files changed

Lines changed: 118 additions & 1 deletion

File tree

runtime/internal/clite/bdwgc/bdwgc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,17 @@ func Realloc(ptr c.Pointer, size uintptr) c.Pointer
4040
//go:linkname Free C.GC_free
4141
func Free(ptr c.Pointer)
4242

43+
// AddRoots registers a memory region [start, end) as a GC root. The caller
44+
// must ensure that the range remains valid until RemoveRoots is invoked with
45+
// the same boundaries. This is typically used for TLS slots that store Go
46+
// pointers.
47+
//
4348
//go:linkname AddRoots C.GC_add_roots
4449
func AddRoots(start, end c.Pointer)
4550

51+
// RemoveRoots unregisters a region previously registered with AddRoots. The
52+
// start and end pointers must exactly match the earlier AddRoots call.
53+
//
4654
//go:linkname RemoveRoots C.GC_remove_roots
4755
func RemoveRoots(start, end c.Pointer)
4856

runtime/internal/clite/tls/tls_common.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
//go:build llgo
22

3+
/*
4+
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// Package tls provides generic thread-local storage backed by POSIX pthread
20+
// TLS. When built with the GC-enabled configuration (llgo && !nogc), TLS slots
21+
// are automatically registered with the BDWGC garbage collector so pointers
22+
// stored in thread-local state remain visible to the collector. Builds without
23+
// GC integration (llgo && nogc) simply fall back to pthread TLS without root
24+
// registration.
25+
//
26+
// Basic usage:
27+
//
28+
// h := tls.Alloc[int](nil)
29+
// h.Set(42)
30+
// val := h.Get() // returns 42
31+
//
32+
// With destructor:
33+
//
34+
// h := tls.Alloc[*Resource](func(r **Resource) {
35+
// if r != nil && *r != nil {
36+
// (*r).Close()
37+
// }
38+
// })
39+
//
40+
// Build tags:
41+
// - llgo && !nogc: Enables GC-aware slot registration via BDWGC
42+
// - llgo && nogc: Disables GC integration; TLS acts as plain pthread TLS
343
package tls
444

545
import (

runtime/internal/clite/tls/tls_gc.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
//go:build llgo && !nogc
22

3+
/*
4+
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
319
package tls
420

521
import (
@@ -48,6 +64,11 @@ func deregisterSlot[T any](s *slot[T]) {
4864

4965
func (s *slot[T]) rootRange() (start, end c.Pointer) {
5066
begin := unsafe.Pointer(s)
51-
endPtr := unsafe.Pointer(uintptr(begin) + unsafe.Sizeof(*s))
67+
size := unsafe.Sizeof(*s)
68+
beginAddr := uintptr(begin)
69+
if beginAddr > ^uintptr(0)-size {
70+
panic("tls: pointer arithmetic overflow in rootRange")
71+
}
72+
endPtr := unsafe.Pointer(beginAddr + size)
5273
return c.Pointer(begin), c.Pointer(endPtr)
5374
}

runtime/internal/clite/tls/tls_nogc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
//go:build llgo && nogc
22

3+
/*
4+
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
319
package tls
420

521
type slot[T any] struct {

runtime/internal/clite/tls/tls_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
//go:build llgo
22

3+
/*
4+
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
319
package tls_test
420

521
import (

ssa/di_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
//go:build !llgo
22

3+
/*
4+
* Copyright (c) 2025 The GoPlus Authors (goplus.org). All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
319
package ssa
420

521
import (

0 commit comments

Comments
 (0)