github.com/aaabigfish/gopkg@v1.1.0/cache/asynccache/atomic_error.go (about) 1 // Copyright 2021 ByteDance Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Copyright (c) 2016 Uber Technologies, Inc. 16 // 17 // Permission is hereby granted, free of charge, to any person obtaining a copy 18 // of this software and associated documentation files (the "Software"), to deal 19 // in the Software without restriction, including without limitation the rights 20 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 21 // copies of the Software, and to permit persons to whom the Software is 22 // furnished to do so, subject to the following conditions: 23 // 24 // The above copyright notice and this permission notice shall be included in 25 // all copies or substantial portions of the Software. 26 // 27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 30 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 31 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 32 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 33 // THE SOFTWARE. 34 35 package asynccache 36 37 import "sync/atomic" 38 39 // Error is an atomic type-safe wrapper around Value for errors 40 type Error struct{ v atomic.Value } 41 42 // errorHolder is non-nil holder for error object. 43 // atomic.Value panics on saving nil object, so err object needs to be 44 // wrapped with valid object first. 45 type errorHolder struct{ err error } 46 47 // Load atomically loads the wrapped error 48 func (e *Error) Load() error { 49 v := e.v.Load() 50 if v == nil { 51 return nil 52 } 53 54 eh := v.(errorHolder) 55 return eh.err 56 } 57 58 // Store atomically stores error. 59 // NOTE: a holder object is allocated on each Store call. 60 func (e *Error) Store(err error) { 61 e.v.Store(errorHolder{err: err}) 62 }