LoadOrStore loads the value associated with key from the pool if it already exists, or stores it if it does not exist. It returns the value that was either loaded or stored, and true if the value already existed and was loaded, false if the value didn't exist and was stored.
(key, val any)
| 115 | // value that was either loaded or stored, and true if the value already |
| 116 | // existed and was loaded, false if the value didn't exist and was stored. |
| 117 | func (up *UsagePool) LoadOrStore(key, val any) (value any, loaded bool) { |
| 118 | var upv *usagePoolVal |
| 119 | up.Lock() |
| 120 | upv, loaded = up.pool[key] |
| 121 | if loaded { |
| 122 | upv.refs.Add(1) |
| 123 | up.Unlock() |
| 124 | upv.Lock() |
| 125 | if upv.err == nil { |
| 126 | value = upv.value |
| 127 | } else { |
| 128 | upv.value = val |
| 129 | upv.err = nil |
| 130 | } |
| 131 | upv.Unlock() |
| 132 | } else { |
| 133 | upv = &usagePoolVal{value: val} |
| 134 | upv.refs.Store(1) |
| 135 | up.pool[key] = upv |
| 136 | up.Unlock() |
| 137 | value = val |
| 138 | } |
| 139 | return value, loaded |
| 140 | } |
| 141 | |
| 142 | // Range iterates the pool similarly to how sync.Map.Range() does: |
| 143 | // it calls f for every key in the pool, and if f returns false, |
no test coverage detected