Files
bl/common/utils/lockfree-1.1.3/cursor.go
昔念 269256a861 feat(common): 添加无锁并发工具包依赖
新增 lockfree-1.1.3 工具包到 go.work 文件中,为项目提供无锁并发数据结构支持,
提升高并发场景下的性能表现。
2025-12-05 00:36:28 +08:00

41 lines
1008 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package lockfree
import "sync/atomic"
// cursor 游标一直持续增长的一个uint64序列
// 该序列用于wgWrite Goroutine获取对应写入到buffer中元素的位置操作
// 通过使用atomic操作避免锁提高性能
// 通过使用padding填充的方式填充前面和后面各使用7个uint64缓存行填充避免伪共享问题
type cursor struct {
p1, p2, p3, p4, p5, p6, p7 uint64
v uint64
p9, p10, p11, p12, p13, p14, p15 uint64
}
func newCursor() *cursor {
return &cursor{}
}
func (c *cursor) increment() uint64 {
return atomic.AddUint64(&c.v, 1)
}
func (c *cursor) atomicLoad() uint64 {
return atomic.LoadUint64(&c.v)
}
func (c *cursor) load() uint64 {
return c.v
}
func (c *cursor) store(expectVal, newVal uint64) bool {
return atomic.CompareAndSwapUint64(&c.v, expectVal, newVal)
}