Ai
1 Star 3 Fork 0

TimAndy/routine

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
thread_local_map.go 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
TimAndy 提交于 2025-03-12 10:33 +08:00 . Fix race detection errors caused by g struct reuse
package routine
var unset entry = &object{}
type object struct {
none bool //nolint:unused
}
type threadLocalMap struct {
table []entry
}
func (mp *threadLocalMap) get(index int) entry {
lookup := mp.table
if index < len(lookup) {
return lookup[index]
}
return unset
}
func (mp *threadLocalMap) set(index int, value entry) {
lookup := mp.table
if index < len(lookup) {
lookup[index] = value
return
}
mp.expandAndSet(index, value)
}
func (mp *threadLocalMap) remove(index int) {
lookup := mp.table
if index < len(lookup) {
lookup[index] = unset
}
}
func (mp *threadLocalMap) expandAndSet(index int, value entry) {
oldArray := mp.table
oldCapacity := len(oldArray)
newCapacity := index
newCapacity |= newCapacity >> 1
newCapacity |= newCapacity >> 2
newCapacity |= newCapacity >> 4
newCapacity |= newCapacity >> 8
newCapacity |= newCapacity >> 16
newCapacity++
newArray := make([]entry, newCapacity)
copy(newArray, oldArray)
fill(newArray, oldCapacity, newCapacity, unset)
newArray[index] = value
mp.table = newArray
}
//go:norace
func createInheritedMap() *threadLocalMap {
parent := currentThread(false)
if parent == nil {
return nil
}
parentMap := parent.inheritableThreadLocals
if parentMap == nil {
return nil
}
lookup := parentMap.table
if lookup == nil {
return nil
}
table := make([]entry, len(lookup))
copy(table, lookup)
for i := 0; i < len(table); i++ {
if c, ok := entryAssert[Cloneable](table[i]); ok && !isNil(c) {
table[i] = entry(c.Clone())
}
}
return &threadLocalMap{table: table}
}
func fill[T any](a []T, fromIndex int, toIndex int, val T) {
for i := fromIndex; i < toIndex; i++ {
a[i] = val
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/timandy/routine.git
git@gitee.com:timandy/routine.git
timandy
routine
routine
main

搜索帮助