代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。