Skip to content

Instantly share code, notes, and snippets.

@rueian
Last active February 20, 2022 13:28
Show Gist options
  • Save rueian/4ccd4368a3923ea9ccc1cc3e078b476c to your computer and use it in GitHub Desktop.
Save rueian/4ccd4368a3923ea9ccc1cc3e078b476c to your computer and use it in GitHub Desktop.
func (r *ring) EnqueueRequest(req request) chan response {
s := &r.slots[atomic.AddUint64(&r.write, 1)&mask]
s.cond.L.Lock()
for s.mark != 0 {
s.cond.Wait()
}
s.req = req
s.mark = 1
s.cond.L.Unlock()
return s.ch
}
func (r *ring) NextRequestToSend() (req request, ok bool) {
r.read1++
s := &r.slots[r.read1&mask]
s.cond.L.Lock()
if ok = s.mark == 1; ok {
s.mark = 2
req = s.req
} else {
r.read1--
}
s.cond.L.Unlock()
return
}
func (r *ring) ReplyToNextRequest(resp response) {
r.read2++
s := &r.slots[r.read2&mask]
s.cond.L.Lock()
if s.mark == 2 {
s.mark = 0
s.ch <- resp
} else {
panic("out-of-band response should not be passed in")
}
s.cond.L.Unlock()
s.cond.Signal()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment