@@ -43,3 +43,49 @@ func (v *Value[T]) Swap(new T) (old T) {
4343func (v * Value [T ]) CompareAndSwap (old , new T ) (swapped bool ) {
4444 return v .v .CompareAndSwap (old , new )
4545}
46+
47+ // An Int provides atomic operations on an integer value of type T.
48+ // It is a generic wrapper around [atomic.Int64], allowing usage with
49+ // signed integer types such as int, int8, int16, int32, and int64.
50+ type Int [T ~ int64 | ~ int32 | ~ int16 | ~ int8 | ~ int ] struct {
51+ v atomic.Int64
52+ }
53+
54+ // Add atomically adds delta to the value and returns the new value.
55+ func (v * Int [T ]) Add (delta T ) (new T ) {
56+ return T (v .v .Add (int64 (delta )))
57+ }
58+
59+ // And atomically performs a bitwise AND operation with mask and returns
60+ // the previous value.
61+ func (v * Int [T ]) And (mask T ) (old T ) {
62+ return T (v .v .And (int64 (mask )))
63+ }
64+
65+ // CompareAndSwap executes the compare-and-swap operation for the [Int].
66+ // It compares the current value with old, and if they are equal,
67+ // sets it to new and returns true. Otherwise, it returns false.
68+ func (v * Int [T ]) CompareAndSwap (old T , new T ) (swapped bool ) {
69+ return v .v .CompareAndSwap (int64 (old ), int64 (new ))
70+ }
71+
72+ // Load atomically loads and returns the current value.
73+ func (v * Int [T ]) Load () T {
74+ return T (v .v .Load ())
75+ }
76+
77+ // Or atomically performs a bitwise OR operation with mask and returns
78+ // the previous value.
79+ func (v * Int [T ]) Or (mask T ) (old T ) {
80+ return T (v .v .Or (int64 (mask )))
81+ }
82+
83+ // Store atomically stores val into the [Int].
84+ func (v * Int [T ]) Store (val T ) {
85+ v .v .Store (int64 (val ))
86+ }
87+
88+ // Swap atomically stores new into the [Int] and returns the previous value.
89+ func (v * Int [T ]) Swap (new T ) (old T ) {
90+ return T (v .v .Swap (int64 (new )))
91+ }
0 commit comments