Skip to content

Commit

Permalink
recycle pcb in next waitio
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Jul 15, 2021
1 parent 2fcc590 commit d1789c9
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type watcher struct {
pendingCreate []*aiocb
pendingProcessing []*aiocb // swaped pending
pendingMutex sync.Mutex
recycles []*aiocb

// IO-completion events to user
chResults chan *aiocb
Expand Down Expand Up @@ -179,15 +180,21 @@ func (w *watcher) notifyPending() {
// WaitIO blocks until any read/write completion, or error.
// An internal 'buf' returned or 'r []OpResult' are safe to use BEFORE next call to WaitIO().
func (w *watcher) WaitIO() (r []OpResult, err error) {
// recycle previous aiocb
for k := range w.recycles {
aiocbPool.Put(w.recycles[k])
}
w.recycles = w.recycles[:0]

for {
select {
case pcb := <-w.chResults:
r = append(r, OpResult{Operation: pcb.op, Conn: pcb.conn, IsSwapBuffer: pcb.useSwap, Buffer: pcb.buffer, Size: pcb.size, Error: pcb.err, Context: pcb.ctx})
aiocbPool.Put(pcb)
w.recycles = append(w.recycles, pcb)
for len(w.chResults) > 0 {
pcb := <-w.chResults
r = append(r, OpResult{Operation: pcb.op, Conn: pcb.conn, IsSwapBuffer: pcb.useSwap, Buffer: pcb.buffer, Size: pcb.size, Error: pcb.err, Context: pcb.ctx})
aiocbPool.Put(pcb)
w.recycles = append(w.recycles, pcb)
}

atomic.CompareAndSwapInt32(&w.shouldSwap, 0, 1)
Expand Down

0 comments on commit d1789c9

Please sign in to comment.