Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions limited_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (p *limitedPool) newWorker(work chan *workUnit, cancel chan struct{}) {
if wu.cancelled.Load() == nil {
value, err = wu.fn(wu)

wu.Lock()
wu.writing.Store(struct{}{})

// need to check again in case the WorkFunc cancelled this unit of work
Expand All @@ -100,6 +101,7 @@ func (p *limitedPool) newWorker(work chan *workUnit, cancel chan struct{}) {
// of work to be done first so we use close
close(wu.done)
}
wu.Unlock()
}

case <-cancel:
Expand Down
2 changes: 2 additions & 0 deletions unlimited_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (p *unlimitedPool) Queue(fn WorkFunc) WorkUnit {
if w.cancelled.Load() == nil {
val, err := w.fn(w)

w.Lock()
w.writing.Store(struct{}{})

// need to check again in case the WorkFunc cancelled this unit of work
Expand All @@ -88,6 +89,7 @@ func (p *unlimitedPool) Queue(fn WorkFunc) WorkUnit {
// of work to be done first so we use close
close(w.done)
}
w.Unlock()
}
}(w)

Expand Down
8 changes: 7 additions & 1 deletion work_unit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pool

import "sync/atomic"
import (
"sync"
"sync/atomic"
)

// WorkUnit contains a single uint of works values
type WorkUnit interface {
Expand Down Expand Up @@ -35,6 +38,7 @@ type workUnit struct {
cancelled atomic.Value
cancelling atomic.Value
writing atomic.Value
sync.Mutex
}

// Cancel cancels this specific unit of work, if not already committed to processing.
Expand All @@ -44,6 +48,8 @@ func (wu *workUnit) Cancel() {

func (wu *workUnit) cancelWithError(err error) {

wu.Lock()
defer wu.Unlock()
wu.cancelling.Store(struct{}{})

if wu.writing.Load() == nil && wu.cancelled.Load() == nil {
Expand Down