uintptr.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // @generated Code generated by gen-atomicint.
  2. // Copyright (c) 2020-2021 Uber Technologies, Inc.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. package atomic
  22. import (
  23. "encoding/json"
  24. "strconv"
  25. "sync/atomic"
  26. )
  27. // Uintptr is an atomic wrapper around uintptr.
  28. type Uintptr struct {
  29. _ nocmp // disallow non-atomic comparison
  30. v uintptr
  31. }
  32. // NewUintptr creates a new Uintptr.
  33. func NewUintptr(val uintptr) *Uintptr {
  34. return &Uintptr{v: val}
  35. }
  36. // Load atomically loads the wrapped value.
  37. func (i *Uintptr) Load() uintptr {
  38. return atomic.LoadUintptr(&i.v)
  39. }
  40. // Add atomically adds to the wrapped uintptr and returns the new value.
  41. func (i *Uintptr) Add(delta uintptr) uintptr {
  42. return atomic.AddUintptr(&i.v, delta)
  43. }
  44. // Sub atomically subtracts from the wrapped uintptr and returns the new value.
  45. func (i *Uintptr) Sub(delta uintptr) uintptr {
  46. return atomic.AddUintptr(&i.v, ^(delta - 1))
  47. }
  48. // Inc atomically increments the wrapped uintptr and returns the new value.
  49. func (i *Uintptr) Inc() uintptr {
  50. return i.Add(1)
  51. }
  52. // Dec atomically decrements the wrapped uintptr and returns the new value.
  53. func (i *Uintptr) Dec() uintptr {
  54. return i.Sub(1)
  55. }
  56. // CAS is an atomic compare-and-swap.
  57. func (i *Uintptr) CAS(old, new uintptr) (swapped bool) {
  58. return atomic.CompareAndSwapUintptr(&i.v, old, new)
  59. }
  60. // Store atomically stores the passed value.
  61. func (i *Uintptr) Store(val uintptr) {
  62. atomic.StoreUintptr(&i.v, val)
  63. }
  64. // Swap atomically swaps the wrapped uintptr and returns the old value.
  65. func (i *Uintptr) Swap(val uintptr) (old uintptr) {
  66. return atomic.SwapUintptr(&i.v, val)
  67. }
  68. // MarshalJSON encodes the wrapped uintptr into JSON.
  69. func (i *Uintptr) MarshalJSON() ([]byte, error) {
  70. return json.Marshal(i.Load())
  71. }
  72. // UnmarshalJSON decodes JSON into the wrapped uintptr.
  73. func (i *Uintptr) UnmarshalJSON(b []byte) error {
  74. var v uintptr
  75. if err := json.Unmarshal(b, &v); err != nil {
  76. return err
  77. }
  78. i.Store(v)
  79. return nil
  80. }
  81. // String encodes the wrapped value as a string.
  82. func (i *Uintptr) String() string {
  83. v := i.Load()
  84. return strconv.FormatUint(uint64(v), 10)
  85. }