2021-09-11 19:12:47 +00:00
|
|
|
package errors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// OnceError is an error structure that supports safe multi
|
|
|
|
// threaded usage and setting only once (until reset).
|
|
|
|
type OnceError struct{ err unsafe.Pointer }
|
2021-09-11 19:12:47 +00:00
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// NewOnce returns a new OnceError instance.
|
2021-09-11 19:12:47 +00:00
|
|
|
func NewOnce() OnceError {
|
|
|
|
return OnceError{
|
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// Store will safely set the OnceError to value, no-op if nil.
|
2021-09-11 19:12:47 +00:00
|
|
|
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),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// Load will load the currently stored error.
|
2021-09-11 19:12:47 +00:00
|
|
|
func (e *OnceError) Load() error {
|
|
|
|
return *(*error)(atomic.LoadPointer(&e.err))
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// IsSet returns whether OnceError has been set.
|
2021-09-11 19:12:47 +00:00
|
|
|
func (e *OnceError) IsSet() bool {
|
|
|
|
return (atomic.LoadPointer(&e.err) != nil)
|
|
|
|
}
|
|
|
|
|
2022-05-08 17:49:45 +00:00
|
|
|
// Reset will reset the OnceError value.
|
2021-09-11 19:12:47 +00:00
|
|
|
func (e *OnceError) Reset() {
|
|
|
|
atomic.StorePointer(&e.err, nil)
|
|
|
|
}
|