Source File
atomic_min.go
Belonging Package
github.com/redis/go-redis/v9/internal/util
package util/*© 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)ISC LicenseModified by htemelski-redisAdapted from the modified atomic_max, but with inverted logic*/import ()// AtomicMin is a thread-safe Min container// - hasValue indicator true if a value was equal to or greater than threshold// - optional threshold for minimum accepted Min value// - —// - wait-free CompareAndSwap mechanictype AtomicMin struct {// value is current Minvalue atomic.Float64// whether [AtomicMin.Value] has been invoked// with value equal or greater to thresholdhasValue atomic.Bool}// NewAtomicMin returns a thread-safe Min container// - if threshold is not used, AtomicMin is initialization-freefunc () ( *AtomicMin) {:= AtomicMin{}.value.Store(math.MaxFloat64)return &}// Value updates the container with a possible Min value// - isNewMin is true if:// - — value is equal to or greater than any threshold and// - — invocation recorded the first 0 or// - — a new Min// - upon return, Min and Min1 are guaranteed to reflect the invocation// - the return order of concurrent Value invocations is not guaranteed// - Thread-safefunc ( *AtomicMin) ( float64) ( bool) {// math.MaxFloat64 as Min casevar = .hasValue.Load()if == math.MaxFloat64 {if ! {= .hasValue.CompareAndSwap(false, true)}return // math.MaxFloat64 as Min: isNewMin true for first 0 writer}// check against present valuevar = .value.Load()if = < ; ! {return // not a new Min return: isNewMin false}// store the new Minfor {// try to write value to *Minif = .value.CompareAndSwap(, ); {if ! {// may be rarely written multiple times// still faster than CompareAndSwap.hasValue.Store(true)}return // new Min written return: isNewMin true}if = .value.Load(); <= {return // no longer a need to write return: isNewMin false}}}// Min returns current min and value-present flag// - hasValue true indicates that value reflects a Value invocation// - hasValue false: value is zero-value// - Thread-safefunc ( *AtomicMin) () ( float64, bool) {if = .hasValue.Load(); ! {return}= .value.Load()return}// Min1 returns current Minimum whether zero-value or set by Value// - threshold is ignored// - Thread-safefunc ( *AtomicMin) () ( float64) { return .value.Load() }
![]() |
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |