1 Star 0 Fork 1

Thoughtworks/go-lock-free-ring-buffer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ring_buffer_api.go 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
package lfring
// RingBuffer defines the behavior of ring buffer
type RingBuffer interface {
Offer(interface{}) (success bool)
Poll() (value interface{}, success bool)
SingleProducerOffer(valueSupplier func() (v interface{}, finish bool))
SingleConsumerPoll(valueConsumer func(interface{}))
}
// BufferType contains different type names of ring buffer
type BufferType int
const (
// Classical ring buffer is a classical implementation of ring buffer
Classical BufferType = iota
// NodeBased is a type of ring buffer that implemented as node based,
// see https://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue
NodeBased
)
// New build a RingBuffer with BufferType and capacity.
// Expand capacity as power-of-two, to make head/tail calculate faster and simpler
func New(t BufferType, capacity uint64) RingBuffer {
realCapacity := findPowerOfTwo(capacity)
switch t {
case NodeBased:
return newNodeBased(realCapacity)
case Classical:
return newClassical(realCapacity)
default:
panic("shouldn't goes here.")
}
}
// findPowerOfTwo return the input number as round up to it's power of two
// The algorithm only care about the MSB of (givenNum -1), through the below procedure,
// the MSB will be spread to all lower bit than MSB. At last do (givenNum + 1) we
// can get power of two form of givenNum.
func findPowerOfTwo(givenMum uint64) uint64 {
givenMum--
givenMum |= givenMum >> 1
givenMum |= givenMum >> 2
givenMum |= givenMum >> 4
givenMum |= givenMum >> 8
givenMum |= givenMum >> 16
givenMum |= givenMum >> 32
givenMum++
return givenMum
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/thoughtworks/go-lock-free-ring-buffer.git
git@gitee.com:thoughtworks/go-lock-free-ring-buffer.git
thoughtworks
go-lock-free-ring-buffer
go-lock-free-ring-buffer
master

搜索帮助