2 Star 2 Fork 9

王布衣/gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
default_writer.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
王布衣 提交于 2023-06-26 15:30 . 新增项目内的消息队列
package fastqueue
import "runtime"
const (
SpinMask = 1024*16 - 1 // arbitrary; we'll want to experiment with different values
)
type DefaultWriter struct {
written *Cursor // the ring buffer has been written up to this sequence
upstream Barrier // all of the readers have advanced up to this sequence
capacity int64
previous int64
gate int64
}
func NewWriter(written *Cursor, upstream Barrier, capacity int64) *DefaultWriter {
return &DefaultWriter{
upstream: upstream,
written: written,
capacity: capacity,
previous: defaultCursorValue,
gate: defaultCursorValue,
}
}
func (this *DefaultWriter) Reserve(count int64) int64 {
if count <= 0 {
panic(ErrMinimumReservationSize)
}
this.previous += count
for spin := int64(0); this.previous-this.capacity > this.gate; spin++ {
if spin&SpinMask == 0 {
runtime.Gosched() // LockSupport.parkNanos(1L); http://bit.ly/1xiDINZ
}
this.gate = this.upstream.Load()
}
return this.previous
}
func (this *DefaultWriter) Commit(_, upper int64) {
this.written.Store(upper)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.15.8

搜索帮助