Skip to content
Open
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
18 changes: 9 additions & 9 deletions waitgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import (
"github.com/sourcegraph/conc/panics"
)

// NewWaitGroup creates a new WaitGroup.
// NewWaitGroup creates a new [WaitGroup].
func NewWaitGroup() *WaitGroup {
return &WaitGroup{}
}

// WaitGroup is the primary building block for scoped concurrency.
// Goroutines can be spawned in the WaitGroup with the Go method,
// and calling Wait() will ensure that each of those goroutines exits
// Goroutines can be spawned in the WaitGroup with the [WaitGroup.Go] method,
// and calling [WaitGroup.Wait]() will ensure that each of those goroutines exits
// before continuing. Any panics in a child goroutine will be caught
// and propagated to the caller of Wait().
//
// The zero value of WaitGroup is usable, just like sync.WaitGroup.
// Also like sync.WaitGroup, it must not be copied after first use.
// The zero value of WaitGroup is usable, just like [sync.WaitGroup].
// Also like [sync.WaitGroup], it must not be copied after first use.
type WaitGroup struct {
wg sync.WaitGroup
pc panics.Catcher
}

// Go spawns a new goroutine in the WaitGroup.
// Go spawns a new goroutine in the [WaitGroup].
func (h *WaitGroup) Go(f func()) {
h.wg.Add(1)
go func() {
Expand All @@ -33,7 +33,7 @@ func (h *WaitGroup) Go(f func()) {
}()
}

// Wait will block until all goroutines spawned with Go exit and will
// Wait will block until all goroutines spawned with [WaitGroup.Go] exit and will
// propagate any panics spawned in a child goroutine.
func (h *WaitGroup) Wait() {
h.wg.Wait()
Expand All @@ -42,8 +42,8 @@ func (h *WaitGroup) Wait() {
h.pc.Repanic()
}

// WaitAndRecover will block until all goroutines spawned with Go exit and
// will return a *panics.Recovered if one of the child goroutines panics.
// WaitAndRecover will block until all goroutines spawned with [WaitGroup.Go] exit and
// will return a [*panics.Recovered] if one of the child goroutines panics.
func (h *WaitGroup) WaitAndRecover() *panics.Recovered {
h.wg.Wait()

Expand Down