Skip to content

Commit

Permalink
Buzz12: Sync primative for stop all
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate North committed Apr 14, 2024
1 parent da40cdd commit 9a4e06a
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions hive.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Hive struct {
notifyComplete chan struct{}
middleware []MiddleFunc
closed chan struct{}
lock sync.Mutex
}

// New initializes a new [*Hive].
Expand All @@ -35,6 +36,8 @@ func (hive *Hive) startCleanupWorker() {
}

func (hive *Hive) removeDoneWorkers() {
hive.lock.Lock()
defer hive.lock.Unlock()
finished := []int{}
for i := range hive.colony {
if hive.colony[i].done.Load() {
Expand Down Expand Up @@ -71,10 +74,16 @@ func (hive *Hive) Submit(workers ...*Worker) {
// StopAll should only be used when you are completely done with the hive. Internal channels are
// closed and all workers are shutdown.
func (hive *Hive) StopAll() {
for i := range hive.colony {
hive.colony[i].Stop()
}
hive.stopAll()
hive.block.Wait()
close(hive.notifyComplete)
<-hive.closed
}

func (hive *Hive) stopAll() {
hive.lock.Lock()
defer hive.lock.Unlock()
for i := range hive.colony {
hive.colony[i].Stop()
}
}

0 comments on commit 9a4e06a

Please sign in to comment.