You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
atomic.Value seems to be the preferred replacement for the atomic.*Pointer methods in code that avoids package unsafe. Unfortunately, atomic.Value doesn't support swaps, making it much less powerful than the equivalent Pointer methods.
When investigating sync.RWMutex usage in the standard library (#17973), I discovered an RWMutexin crypto/tls guarding two fields, sessionTicketKeys and originalConfig, that are always updated independently. It's trivial to replace sessionTicketKeys with an atomic.Value, but originalConfig needs an atomic swap.
More generally, it would be nice if atomic.Value were as complete a replacement as possible for unsafe.Pointer with atomic operations.
Adding CompareAndSwap was discussed previously (#11260).
The major arguments against at the time seem to have been:
Swap, unlike CompareAndSwap, does not require comparability. (Personally I think it would be good to add CompareAndSwap too and simply panic for uncomparable types, but as I don't have a use-case for that I would prefer to keep it out-of-scope for this proposal.)
I have identified a concrete use-case in the crypto/tls package.
According to @bradfitz in CL 41930, unsafe.Pointer is strongly discouraged outside of the sync, runtime, and reflect packages.
atomic.Valueseems to be the preferred replacement for theatomic.*Pointermethods in code that avoids packageunsafe. Unfortunately,atomic.Valuedoesn't support swaps, making it much less powerful than the equivalentPointermethods.When investigating
sync.RWMutexusage in the standard library (#17973), I discovered anRWMutexincrypto/tlsguarding two fields,sessionTicketKeysandoriginalConfig, that are always updated independently. It's trivial to replacesessionTicketKeyswith anatomic.Value, butoriginalConfigneeds an atomic swap.More generally, it would be nice if
atomic.Valuewere as complete a replacement as possible forunsafe.Pointerwith atomic operations.Adding
CompareAndSwapwas discussed previously (#11260).The major arguments against at the time seem to have been:
unsafe.Pointeras an alternative. (sync/atomic: atomic.Value doesn't support CompareAndSwap #11260 (comment))To address those points directly:
Swap, unlikeCompareAndSwap, does not require comparability. (Personally I think it would be good to addCompareAndSwaptoo and simply panic for uncomparable types, but as I don't have a use-case for that I would prefer to keep it out-of-scope for this proposal.)crypto/tlspackage.unsafe.Pointeris strongly discouraged outside of thesync,runtime, andreflectpackages.(@dvyukov, @josharian, @OneOfOne, @cespare, @adg, @rsc)