diff options
| author | 2024-01-17 14:54:30 +0000 | |
|---|---|---|
| committer | 2024-01-17 14:54:30 +0000 | |
| commit | 906639ad7eb92e9d631599f78979908930e59c84 (patch) | |
| tree | dfef6d0a5ba47c49c214e4537a89dee6ad528df8 /vendor/github.com/sourcegraph/conc/waitgroup.go | |
| parent | [bugfix] Better Postgres search case insensitivity (#2526) (diff) | |
| download | gotosocial-906639ad7eb92e9d631599f78979908930e59c84.tar.xz | |
[chore] update viper version (#2539)
* update viper version
* removes our last uses of the slice package
* fix tests
Diffstat (limited to 'vendor/github.com/sourcegraph/conc/waitgroup.go')
| -rw-r--r-- | vendor/github.com/sourcegraph/conc/waitgroup.go | 52 | 
1 files changed, 52 insertions, 0 deletions
diff --git a/vendor/github.com/sourcegraph/conc/waitgroup.go b/vendor/github.com/sourcegraph/conc/waitgroup.go new file mode 100644 index 000000000..47b1bc1a5 --- /dev/null +++ b/vendor/github.com/sourcegraph/conc/waitgroup.go @@ -0,0 +1,52 @@ +package conc + +import ( +	"sync" + +	"github.com/sourcegraph/conc/panics" +) + +// 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 +// 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. +type WaitGroup struct { +	wg sync.WaitGroup +	pc panics.Catcher +} + +// Go spawns a new goroutine in the WaitGroup. +func (h *WaitGroup) Go(f func()) { +	h.wg.Add(1) +	go func() { +		defer h.wg.Done() +		h.pc.Try(f) +	}() +} + +// Wait will block until all goroutines spawned with Go exit and will +// propagate any panics spawned in a child goroutine. +func (h *WaitGroup) Wait() { +	h.wg.Wait() + +	// Propagate a panic if we caught one from a child goroutine. +	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. +func (h *WaitGroup) WaitAndRecover() *panics.Recovered { +	h.wg.Wait() + +	// Return a recovered panic if we caught one from a child goroutine. +	return h.pc.Recovered() +}  | 
