-
Notifications
You must be signed in to change notification settings - Fork 4
/
unsafeptr.go
40 lines (32 loc) · 970 Bytes
/
unsafeptr.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
//go:build !purego || !appengine || !js
// +build !purego !appengine !js
package atomix
import (
"sync/atomic"
"unsafe"
)
// UnsafePointer is an atomic unsafe.Pointer.
type UnsafePointer struct {
atomicType
value unsafe.Pointer
}
// NewUnsafePointer creates an UnsafePointer.
func NewUnsafePointer(value unsafe.Pointer) *UnsafePointer {
return &UnsafePointer{value: value}
}
// Load atomically the value.
func (p *UnsafePointer) Load() unsafe.Pointer {
return atomic.LoadPointer(&p.value)
}
// Store atomically the given value.
func (p *UnsafePointer) Store(new unsafe.Pointer) {
atomic.StorePointer(&p.value, new)
}
// Swap sets the given value and returns the previous value.
func (p *UnsafePointer) Swap(new unsafe.Pointer) unsafe.Pointer {
return atomic.SwapPointer(&p.value, new)
}
// CAS is an atomic Compare-And-Swap operation.
func (p *UnsafePointer) CAS(old, new unsafe.Pointer) bool {
return atomic.CompareAndSwapPointer(&p.value, old, new)
}