-
Notifications
You must be signed in to change notification settings - Fork 1
/
unsafe.go
35 lines (28 loc) · 954 Bytes
/
unsafe.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
// +build !purego,!appengine,!js
package atom
import (
"sync/atomic"
"unsafe"
)
// Pointer is a wrapper for atomically accessed unsafe.Pointer values.
type Pointer struct {
_ noCopy
value unsafe.Pointer
}
// CompareAndSwap atomically sets the new value only if the current value
// matches the given old value and returns whether the new value was set.
func (p *Pointer) CompareAndSwap(old, new unsafe.Pointer) (swapped bool) {
return atomic.CompareAndSwapPointer(&p.value, old, new)
}
// Set sets the new value regardless of the previous value.
func (p *Pointer) Set(value unsafe.Pointer) {
atomic.StorePointer(&p.value, value)
}
// Swap atomically sets the new value and returns the previous value.
func (p *Pointer) Swap(new unsafe.Pointer) (old unsafe.Pointer) {
return atomic.SwapPointer(&p.value, new)
}
// Value returns the current value.
func (p *Pointer) Value() (value unsafe.Pointer) {
return atomic.LoadPointer(&p.value)
}