File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -40,9 +40,17 @@ func Realloc(ptr c.Pointer, size uintptr) c.Pointer
4040//go:linkname Free C.GC_free
4141func 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
4449func 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
4755func RemoveRoots (start , end c.Pointer )
4856
Original file line number Diff line number Diff line change 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
343package tls
444
545import (
Original file line number Diff line number Diff line change 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+
319package tls
420
521import (
@@ -48,6 +64,11 @@ func deregisterSlot[T any](s *slot[T]) {
4864
4965func (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}
Original file line number Diff line number Diff line change 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+
319package tls
420
521type slot [T any ] struct {
Original file line number Diff line number Diff line change 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+
319package tls_test
420
521import (
Original file line number Diff line number Diff line change 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+
319package ssa
420
521import (
You can’t perform that action at this time.
0 commit comments