-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatomic_ref.go
94 lines (77 loc) · 2.07 KB
/
atomic_ref.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
package weak_ref
import (
"reflect"
"sync/atomic"
"unsafe"
)
func create_ref(val interface{}) interface{} {
val_v := reflect.ValueOf(val)
if val_v.Kind() != reflect.Ptr {
panic("unaddreable value")
}
addr_v := reflect.New(val_v.Type())
addr_v.Elem().Set(val_v)
return addr_v.Interface()
}
func to_val_ptr(typ reflect.Type, val interface{}) unsafe.Pointer {
if val == nil {
return nil
}
val_v := reflect.ValueOf(val)
if !val_v.IsValid() {
panic("invalid value")
}
if val_v.Kind() != reflect.Ptr {
panic("unaddreable value")
}
if val_v.Type() != typ {
panic("addr and val type mismatch")
}
return unsafe.Pointer(val_v.Pointer())
}
func to_val(typ reflect.Type, ptr unsafe.Pointer) interface{} {
if ptr == nil {
return nil
}
val_v := reflect.NewAt(typ.Elem(), ptr)
if !val_v.IsValid() {
return nil
}
return val_v.Interface()
}
func to_addr_ptr(addr interface{}) (reflect.Type, unsafe.Pointer) {
addr_v := reflect.ValueOf(addr)
if !addr_v.IsValid() {
panic("invalid value")
}
if addr_v.Kind() != reflect.Ptr {
panic("unaddreable value")
}
if addr_v.Type().Elem().Kind() != reflect.Ptr {
panic("unaddreable value")
}
return addr_v.Type(), unsafe.Pointer(addr_v.Pointer())
}
func cas_ref(addr interface{}, old_val, new_val interface{}) bool {
addr_typ, addr_ptr := to_addr_ptr(addr)
swap := atomic.CompareAndSwapPointer((*unsafe.Pointer)(addr_ptr),
to_val_ptr(addr_typ.Elem(), old_val),
to_val_ptr(addr_typ.Elem(), new_val))
return swap
}
func swap_ref(addr interface{}, val interface{}) interface{} {
addr_typ, addr_ptr := to_addr_ptr(addr)
old_ptr := atomic.SwapPointer((*unsafe.Pointer)(addr_ptr),
to_val_ptr(addr_typ.Elem(), val))
return to_val(addr_typ.Elem(), old_ptr)
}
func store_ref(addr interface{}, val interface{}) {
addr_typ, addr_ptr := to_addr_ptr(addr)
atomic.StorePointer((*unsafe.Pointer)(addr_ptr),
to_val_ptr(addr_typ.Elem(), val))
}
func load_ref(addr interface{}) interface{} {
addr_typ, addr_ptr := to_addr_ptr(addr)
ptr := atomic.LoadPointer((*unsafe.Pointer)(addr_ptr))
return to_val(addr_typ.Elem(), ptr)
}