summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-errors/v2/once.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2022-05-08 19:49:45 +0200
committerLibravatar GitHub <noreply@github.com>2022-05-08 18:49:45 +0100
commit5004e0a9da665ccc0e18cd4075ee636641b71f0a (patch)
treeb7c8269b954ced61afa9fffd7305bd88acca6f8e /vendor/codeberg.org/gruf/go-errors/v2/once.go
parent[bugfix] Fix existing bio text showing as HTML (#531) (diff)
downloadgotosocial-5004e0a9da665ccc0e18cd4075ee636641b71f0a.tar.xz
[bugfix] Fix remote media pruning failing if media already gone (#548)
* fix error check of prune to allow missing files * update go-store library, add test for pruning item with db entry but no file Signed-off-by: kim <grufwub@gmail.com> * remove now-unneccessary error check Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-errors/v2/once.go')
-rw-r--r--vendor/codeberg.org/gruf/go-errors/v2/once.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-errors/v2/once.go b/vendor/codeberg.org/gruf/go-errors/v2/once.go
new file mode 100644
index 000000000..83a45a61f
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-errors/v2/once.go
@@ -0,0 +1,47 @@
+package errors
+
+import (
+ "sync/atomic"
+ "unsafe"
+)
+
+// OnceError is an error structure that supports safe multi
+// threaded usage and setting only once (until reset).
+type OnceError struct{ err unsafe.Pointer }
+
+// NewOnce returns a new OnceError instance.
+func NewOnce() OnceError {
+ return OnceError{
+ err: nil,
+ }
+}
+
+// Store will safely set the OnceError to value, no-op if nil.
+func (e *OnceError) Store(err error) {
+ // Nothing to do
+ if err == nil {
+ return
+ }
+
+ // Only set if not already
+ atomic.CompareAndSwapPointer(
+ &e.err,
+ nil,
+ unsafe.Pointer(&err),
+ )
+}
+
+// Load will load the currently stored error.
+func (e *OnceError) Load() error {
+ return *(*error)(atomic.LoadPointer(&e.err))
+}
+
+// IsSet returns whether OnceError has been set.
+func (e *OnceError) IsSet() bool {
+ return (atomic.LoadPointer(&e.err) != nil)
+}
+
+// Reset will reset the OnceError value.
+func (e *OnceError) Reset() {
+ atomic.StorePointer(&e.err, nil)
+}