This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
atomic.go
129 lines (102 loc) · 2.75 KB
/
atomic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package atomic
import "sync/atomic"
// This file contains some simplified atomics primitives that Golang default library does not offer
// like, Boolean
// Takes in a uint32 and converts to bool by checking whether the last bit is set to 1
func toBool(n uint32) bool {
return n&1 == 1
}
// Takes in a bool and returns a uint32 representation
func toInt(b bool) uint32 {
if b {
return 1
}
return 0
}
// Bool is an atomic Boolean.
// It stores the bool as a uint32 internally. This is to use the uint32 atomic functions available in golang
type Bool struct{ v uint32 }
// NewBool creates a Bool.
func NewBool(initial bool) Bool {
return Bool{v: toInt(initial)}
}
// Load atomically loads the Boolean.
func (b *Bool) Load() bool {
return toBool(atomic.LoadUint32(&b.v))
}
// CAS is an atomic compare-and-swap.
func (b *Bool) CompareAndSwap(old, new bool) bool {
return atomic.CompareAndSwapUint32(&b.v, toInt(old), toInt(new))
}
// Store atomically stores the passed value.
func (b *Bool) Store(new bool) {
atomic.StoreUint32(&b.v, toInt(new))
}
// Swap sets the given value and returns the previous value.
func (b *Bool) Swap(new bool) bool {
return toBool(atomic.SwapUint32(&b.v, toInt(new)))
}
// Toggle atomically negates the Boolean and returns the previous value.
func (b *Bool) Toggle() bool {
return toBool(atomic.AddUint32(&b.v, 1) - 1)
}
type Uint32 struct {
v uint32
}
// Returns a loaded uint32 value
func (u *Uint32) Load() uint32 {
return atomic.LoadUint32(&u.v)
}
// CAS is an atomic compare-and-swap.
func (u *Uint32) CompareAndSwap(old, new uint32) bool {
return atomic.CompareAndSwapUint32(&u.v, old, new)
}
// Add a delta to the number
func (u *Uint32) Add(delta uint32) uint32 {
return atomic.AddUint32(&u.v, delta)
}
// Increment the value
func (u *Uint32) Inc() uint32 {
return atomic.AddUint32(&u.v, 1)
}
// Set the value
func (u *Uint32) Store(v uint32) {
atomic.StoreUint32(&u.v, v)
}
func NewUint32(v uint32) Uint32 {
return Uint32{v: v}
}
type Int32 struct {
v int32
}
// Returns a loaded uint32 value
func (i *Int32) Load() int32 {
return atomic.LoadInt32(&i.v)
}
// CAS is an atomic compare-and-swap.
func (i *Int32) CompareAndSwap(old, new int32) bool {
return atomic.CompareAndSwapInt32(&i.v, old, new)
}
// Add a delta to the number
func (i *Int32) Add(delta int32) int32 {
return atomic.AddInt32(&i.v, delta)
}
// Subtract a delta from the number
func (i *Int32) Sub(delta int32) int32 {
return atomic.AddInt32(&i.v, -delta)
}
// Increment the value
func (i *Int32) Inc() int32 {
return atomic.AddInt32(&i.v, 1)
}
// Decrement the value
func (i *Int32) Dec() int32 {
return atomic.AddInt32(&i.v, -1)
}
// Set the value
func (i *Int32) Store(v int32) {
atomic.StoreInt32(&i.v, v)
}
func NewInt32(v int32) Int32 {
return Int32{v: v}
}