Blame vendor/github.com/Azure/azure-storage-blob-go/azblob/atomicmorph.go

Packit 63bb0d
package azblob
Packit 63bb0d
Packit 63bb0d
import "sync/atomic"
Packit 63bb0d
Packit 63bb0d
// AtomicMorpherInt32 identifies a method passed to and invoked by the AtomicMorphInt32 function.
Packit 63bb0d
// The AtomicMorpher callback is passed a startValue and based on this value it returns
Packit 63bb0d
// what the new value should be and the result that AtomicMorph should return to its caller.
Packit 63bb0d
type atomicMorpherInt32 func(startVal int32) (val int32, morphResult interface{})
Packit 63bb0d
Packit 63bb0d
const targetAndMorpherMustNotBeNil = "target and morpher must not be nil"
Packit 63bb0d
Packit 63bb0d
// AtomicMorph atomically morphs target in to new value (and result) as indicated bythe AtomicMorpher callback function.
Packit 63bb0d
func atomicMorphInt32(target *int32, morpher atomicMorpherInt32) interface{} {
Packit 63bb0d
	for {
Packit 63bb0d
		currentVal := atomic.LoadInt32(target)
Packit 63bb0d
		desiredVal, morphResult := morpher(currentVal)
Packit 63bb0d
		if atomic.CompareAndSwapInt32(target, currentVal, desiredVal) {
Packit 63bb0d
			return morphResult
Packit 63bb0d
		}
Packit 63bb0d
	}
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// AtomicMorpherUint32 identifies a method passed to and invoked by the AtomicMorph function.
Packit 63bb0d
// The AtomicMorpher callback is passed a startValue and based on this value it returns
Packit 63bb0d
// what the new value should be and the result that AtomicMorph should return to its caller.
Packit 63bb0d
type atomicMorpherUint32 func(startVal uint32) (val uint32, morphResult interface{})
Packit 63bb0d
Packit 63bb0d
// AtomicMorph atomically morphs target in to new value (and result) as indicated bythe AtomicMorpher callback function.
Packit 63bb0d
func atomicMorphUint32(target *uint32, morpher atomicMorpherUint32) interface{} {
Packit 63bb0d
	for {
Packit 63bb0d
		currentVal := atomic.LoadUint32(target)
Packit 63bb0d
		desiredVal, morphResult := morpher(currentVal)
Packit 63bb0d
		if atomic.CompareAndSwapUint32(target, currentVal, desiredVal) {
Packit 63bb0d
			return morphResult
Packit 63bb0d
		}
Packit 63bb0d
	}
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// AtomicMorpherUint64 identifies a method passed to and invoked by the AtomicMorphUint64 function.
Packit 63bb0d
// The AtomicMorpher callback is passed a startValue and based on this value it returns
Packit 63bb0d
// what the new value should be and the result that AtomicMorph should return to its caller.
Packit 63bb0d
type atomicMorpherInt64 func(startVal int64) (val int64, morphResult interface{})
Packit 63bb0d
Packit 63bb0d
// AtomicMorph atomically morphs target in to new value (and result) as indicated bythe AtomicMorpher callback function.
Packit 63bb0d
func atomicMorphInt64(target *int64, morpher atomicMorpherInt64) interface{} {
Packit 63bb0d
	for {
Packit 63bb0d
		currentVal := atomic.LoadInt64(target)
Packit 63bb0d
		desiredVal, morphResult := morpher(currentVal)
Packit 63bb0d
		if atomic.CompareAndSwapInt64(target, currentVal, desiredVal) {
Packit 63bb0d
			return morphResult
Packit 63bb0d
		}
Packit 63bb0d
	}
Packit 63bb0d
}
Packit 63bb0d
Packit 63bb0d
// AtomicMorpherUint64 identifies a method passed to and invoked by the AtomicMorphUint64 function.
Packit 63bb0d
// The AtomicMorpher callback is passed a startValue and based on this value it returns
Packit 63bb0d
// what the new value should be and the result that AtomicMorph should return to its caller.
Packit 63bb0d
type atomicMorpherUint64 func(startVal uint64) (val uint64, morphResult interface{})
Packit 63bb0d
Packit 63bb0d
// AtomicMorph atomically morphs target in to new value (and result) as indicated bythe AtomicMorpher callback function.
Packit 63bb0d
func atomicMorphUint64(target *uint64, morpher atomicMorpherUint64) interface{} {
Packit 63bb0d
	for {
Packit 63bb0d
		currentVal := atomic.LoadUint64(target)
Packit 63bb0d
		desiredVal, morphResult := morpher(currentVal)
Packit 63bb0d
		if atomic.CompareAndSwapUint64(target, currentVal, desiredVal) {
Packit 63bb0d
			return morphResult
Packit 63bb0d
		}
Packit 63bb0d
	}
Packit 63bb0d
}