summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-runners
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/codeberg.org/gruf/go-runners')
-rw-r--r--vendor/codeberg.org/gruf/go-runners/LICENSE9
-rw-r--r--vendor/codeberg.org/gruf/go-runners/README.md3
-rw-r--r--vendor/codeberg.org/gruf/go-runners/context.go69
-rw-r--r--vendor/codeberg.org/gruf/go-runners/pointer.go22
-rw-r--r--vendor/codeberg.org/gruf/go-runners/process.go73
-rw-r--r--vendor/codeberg.org/gruf/go-runners/service.go222
6 files changed, 0 insertions, 398 deletions
diff --git a/vendor/codeberg.org/gruf/go-runners/LICENSE b/vendor/codeberg.org/gruf/go-runners/LICENSE
deleted file mode 100644
index b7c4417ac..000000000
--- a/vendor/codeberg.org/gruf/go-runners/LICENSE
+++ /dev/null
@@ -1,9 +0,0 @@
-MIT License
-
-Copyright (c) 2021 gruf
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/vendor/codeberg.org/gruf/go-runners/README.md b/vendor/codeberg.org/gruf/go-runners/README.md
deleted file mode 100644
index 91cc1528d..000000000
--- a/vendor/codeberg.org/gruf/go-runners/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# go-runners
-
-Provides a means a simple means of managing long-running functions and services \ No newline at end of file
diff --git a/vendor/codeberg.org/gruf/go-runners/context.go b/vendor/codeberg.org/gruf/go-runners/context.go
deleted file mode 100644
index e02dcab22..000000000
--- a/vendor/codeberg.org/gruf/go-runners/context.go
+++ /dev/null
@@ -1,69 +0,0 @@
-package runners
-
-import (
- "context"
- "sync/atomic"
- "time"
-)
-
-// closedctx is an always closed context.
-var closedctx = func() context.Context {
- ctx := make(chan struct{})
- close(ctx)
- return CancelCtx(ctx)
-}()
-
-// Closed returns an always closed context.
-func Closed() context.Context {
- return closedctx
-}
-
-// CtxWithCancel returns a new context.Context impl with cancel.
-func CtxWithCancel() (context.Context, context.CancelFunc) {
- var once atomic.Uint32
- ctx := make(chan struct{})
- return CancelCtx(ctx), func() {
- if once.CompareAndSwap(0, 1) {
- close(ctx)
- }
- }
-}
-
-// CancelCtx is the simplest possible cancellable context.
-type CancelCtx (<-chan struct{})
-
-func (CancelCtx) Deadline() (time.Time, bool) {
- return time.Time{}, false
-}
-
-func (ctx CancelCtx) Done() <-chan struct{} {
- return ctx
-}
-
-func (ctx CancelCtx) Err() error {
- select {
- case <-ctx:
- return context.Canceled
- default:
- return nil
- }
-}
-
-func (CancelCtx) Value(key interface{}) interface{} {
- return nil
-}
-
-func (ctx CancelCtx) String() string {
- var state string
- select {
- case <-ctx:
- state = "closed"
- default:
- state = "open"
- }
- return "CancelCtx{state:" + state + "}"
-}
-
-func (ctx CancelCtx) GoString() string {
- return "runners." + ctx.String()
-}
diff --git a/vendor/codeberg.org/gruf/go-runners/pointer.go b/vendor/codeberg.org/gruf/go-runners/pointer.go
deleted file mode 100644
index cc139309f..000000000
--- a/vendor/codeberg.org/gruf/go-runners/pointer.go
+++ /dev/null
@@ -1,22 +0,0 @@
-package runners
-
-import (
- "sync/atomic"
- "unsafe"
-)
-
-// atomic_pointer wraps an unsafe.Pointer with
-// receiver methods for their atomic counterparts.
-type atomic_pointer struct{ p unsafe.Pointer }
-
-func (p *atomic_pointer) Load() unsafe.Pointer {
- return atomic.LoadPointer(&p.p)
-}
-
-func (p *atomic_pointer) Store(ptr unsafe.Pointer) {
- atomic.StorePointer(&p.p, ptr)
-}
-
-func (p *atomic_pointer) CAS(old, new unsafe.Pointer) bool {
- return atomic.CompareAndSwapPointer(&p.p, old, new)
-}
diff --git a/vendor/codeberg.org/gruf/go-runners/process.go b/vendor/codeberg.org/gruf/go-runners/process.go
deleted file mode 100644
index 3feeff258..000000000
--- a/vendor/codeberg.org/gruf/go-runners/process.go
+++ /dev/null
@@ -1,73 +0,0 @@
-package runners
-
-import (
- "fmt"
- "unsafe"
-
- "sync"
-)
-
-// Processor acts similarly to a sync.Once object, except that it is reusable. After
-// the first call to Process(), any further calls before this first has returned will
-// block until the first call has returned, and return the same error. This ensures
-// that only a single instance of it is ever running at any one time.
-type Processor struct{ p atomic_pointer }
-
-// Process will process the given function if first-call, else blocking until
-// the first function has returned, returning the same error result.
-func (p *Processor) Process(proc func() error) (err error) {
- var i *proc_instance
-
- for {
- // Attempt to load existing instance.
- ptr := (*proc_instance)(p.p.Load())
- if ptr != nil {
-
- // Wait on existing.
- ptr.wait.Wait()
- err = ptr.err
- return
- }
-
- if i == nil {
- // Allocate instance.
- i = new(proc_instance)
- i.wait.Add(1)
- }
-
- // Try to acquire start slot by
- // setting ptr to *our* instance.
- if p.p.CAS(nil, unsafe.Pointer(i)) {
- defer func() {
- if r := recover(); r != nil {
- if i.err != nil {
- rOld := r // wrap the panic so we don't lose existing returned error
- r = fmt.Errorf("panic occured after error %q: %v", i.err.Error(), rOld)
- }
-
- // Catch panics and wrap as error return.
- i.err = fmt.Errorf("caught panic: %v", r)
- }
-
- // Set return.
- err = i.err
-
- // Release the
- // goroutines.
- i.wait.Done()
-
- // Free processor.
- p.p.Store(nil)
- }()
-
- // Run func.
- i.err = proc()
- return
- }
- }
-}
-
-type proc_instance struct {
- wait sync.WaitGroup
- err error
-}
diff --git a/vendor/codeberg.org/gruf/go-runners/service.go b/vendor/codeberg.org/gruf/go-runners/service.go
deleted file mode 100644
index fe41807f9..000000000
--- a/vendor/codeberg.org/gruf/go-runners/service.go
+++ /dev/null
@@ -1,222 +0,0 @@
-package runners
-
-import (
- "context"
- "sync"
- "sync/atomic"
- "unsafe"
-)
-
-// Service provides a means of tracking a single long-running Service, provided protected state
-// changes and preventing multiple instances running. Also providing Service state information.
-type Service struct{ p atomic_pointer }
-
-// Run will run the supplied function until completion, using given context to propagate cancel.
-// Immediately returns false if the Service is already running, and true after completed run.
-func (svc *Service) Run(fn func(context.Context)) (ok bool) {
- var ptr *svc_instance
-
- // Attempt to start.
- ptr, ok = svc.start()
- if !ok {
- return
- }
-
- // Run given function.
- defer svc.on_done(ptr)
- fn(CancelCtx(ptr.done))
- return
-}
-
-// GoRun will run the supplied function until completion in a goroutine, using given context to
-// propagate cancel. Immediately returns boolean indicating success, or that service is already running.
-func (svc *Service) GoRun(fn func(context.Context)) (ok bool) {
- var ptr *svc_instance
-
- // Attempt to start.
- ptr, ok = svc.start()
- if !ok {
- return
- }
-
- go func() {
- // Run given function.
- defer svc.on_done(ptr)
- fn(CancelCtx(ptr.done))
- }()
-
- return
-}
-
-// RunWait is functionally the same as .Run(), but blocks until the first instance of .Run() returns.
-func (svc *Service) RunWait(fn func(context.Context)) (ok bool) {
- var ptr *svc_instance
-
- // Attempt to start.
- ptr, ok = svc.start()
- if !ok {
- <-ptr.done
- return
- }
-
- // Run given function.
- defer svc.on_done(ptr)
- fn(CancelCtx(ptr.done))
- return
-}
-
-// GoRunWait is functionally the same as .RunWait(), but blocks until the first instance of RunWait() returns.
-func (svc *Service) GoRunWait(fn func(context.Context)) (ok bool) {
- var ptr *svc_instance
-
- // Attempt to start.
- ptr, ok = svc.start()
- if !ok {
- <-ptr.done
- return
- }
-
- go func() {
- // Run given function.
- defer svc.on_done(ptr)
- fn(CancelCtx(ptr.done))
- }()
-
- return
-}
-
-// Stop will attempt to stop the service, cancelling the running function's context. Immediately
-// returns false if not running, and true only after Service is fully stopped.
-func (svc *Service) Stop() bool {
- return svc.must_get().stop()
-}
-
-// Running returns if Service is running (i.e. NOT fully stopped, but may be *stopping*).
-func (svc *Service) Running() bool {
- return svc.must_get().running()
-}
-
-// Done returns a channel that's closed when Service.Stop() is called. It is
-// the same channel provided to the currently running service function.
-func (svc *Service) Done() <-chan struct{} {
- return svc.must_get().done
-}
-
-func (svc *Service) start() (*svc_instance, bool) {
- ptr := svc.must_get()
- return ptr, ptr.start()
-}
-
-func (svc *Service) on_done(ptr *svc_instance) {
- // Ensure stopped.
- ptr.stop_private()
-
- // Free service.
- svc.p.Store(nil)
-}
-
-func (svc *Service) must_get() *svc_instance {
- var newptr *svc_instance
-
- for {
- // Try to load existing instance.
- ptr := (*svc_instance)(svc.p.Load())
- if ptr != nil {
- return ptr
- }
-
- if newptr == nil {
- // Allocate new instance.
- newptr = new(svc_instance)
- newptr.done = make(chan struct{})
- }
-
- // Attempt to acquire slot by setting our ptr.
- if !svc.p.CAS(nil, unsafe.Pointer(newptr)) {
- continue
- }
-
- return newptr
- }
-}
-
-type svc_instance struct {
- wait sync.WaitGroup
- done chan struct{}
- state atomic.Uint32
-}
-
-const (
- started_bit = uint32(1) << 0
- stopping_bit = uint32(1) << 1
- finished_bit = uint32(1) << 2
-)
-
-func (i *svc_instance) start() (ok bool) {
- // Acquire start by setting 'started' bit.
- switch old := i.state.Or(started_bit); {
-
- case old&finished_bit != 0:
- // Already finished.
-
- case old&started_bit == 0:
- // Successfully started!
- i.wait.Add(1)
- ok = true
- }
-
- return
-}
-
-// NOTE: MAY ONLY BE CALLED BY STARTING GOROUTINE.
-func (i *svc_instance) stop_private() {
- // Attempt set both stopping and finished bits.
- old := i.state.Or(stopping_bit | finished_bit)
-
- // Only if we weren't already
- // stopping do we close channel.
- if old&stopping_bit == 0 {
- close(i.done)
- }
-
- // Release
- // waiters.
- i.wait.Done()
-}
-
-func (i *svc_instance) stop() (ok bool) {
- // Attempt to set the 'stopping' bit.
- switch old := i.state.Or(stopping_bit); {
-
- case old&finished_bit != 0:
- // Already finished.
- return
-
- case old&started_bit == 0:
- // This was never started
- // to begin with, just mark
- // as fully finished here.
- _ = i.state.Or(finished_bit)
- return
-
- case old&stopping_bit == 0:
- // We succesfully stopped
- // instance, close channel.
- close(i.done)
- ok = true
- }
-
- // Wait on stop.
- i.wait.Wait()
- return
-}
-
-// running returns whether service was started and
-// is not yet finished. that indicates that it may
-// have been started and not yet stopped, or that
-// it was started, stopped and not yet returned.
-func (i *svc_instance) running() bool {
- val := i.state.Load()
- return val&started_bit != 0 &&
- val&finished_bit == 0
-}