Skip to content

Commit

Permalink
refactor: inline ring acquire loop manually
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Dec 26, 2021
1 parent 742d058 commit 37c189b
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions internal/queue/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,27 @@ type node struct {
}

func (r *Ring) PutOne(m cmds.Completed) chan proto.Result {
n := r.acquire(atomic.AddUint64(&r.write, 1) & r.mask)
n := &r.store[atomic.AddUint64(&r.write, 1)&r.mask]
for !atomic.CompareAndSwapUint32(&n.mark, 0, 1) {
runtime.Gosched()
}
n.one = m
n.multi = nil
atomic.StoreUint32(&n.mark, 2)
return n.ch
}

func (r *Ring) PutMulti(m []cmds.Completed) chan proto.Result {
n := r.acquire(atomic.AddUint64(&r.write, 1) & r.mask)
n := &r.store[atomic.AddUint64(&r.write, 1)&r.mask]
for !atomic.CompareAndSwapUint32(&n.mark, 0, 1) {
runtime.Gosched()
}
n.one = cmds.Completed{}
n.multi = m
atomic.StoreUint32(&n.mark, 2)
return n.ch
}

func (r *Ring) acquire(position uint64) *node {
n := &r.store[position]
for !atomic.CompareAndSwapUint32(&n.mark, 0, 1) {
runtime.Gosched()
}
return n
}

// NextWriteCmd should be only called by one dedicated thread
func (r *Ring) NextWriteCmd() (cmds.Completed, []cmds.Completed, chan proto.Result) {
r.read1++
Expand Down

0 comments on commit 37c189b

Please sign in to comment.