forked from xilp/systray
-
Notifications
You must be signed in to change notification settings - Fork 1
/
refs.go
131 lines (109 loc) · 2.64 KB
/
refs.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
130
131
package systray
import (
"math/rand"
"sync"
"sync/atomic"
)
/*
#include <inttypes.h>
*/
import "C"
/*
Storage implementation borrowed from:
https://github.com/justinfx/gofileseq/blob/master/cpp/export/storage.go
*/
func init() {
gSystrays = &systrayMap{
lock: new(sync.RWMutex),
m: make(map[refId]*systrayRef),
}
}
// global map to track references to _Systray instances
var gSystrays *systrayMap
type refId uint64
// uuid generates a new unique id using the shared generator
func uuid() refId {
u := uint64(rand.Uint32())<<32 + uint64(rand.Uint32())
return refId(u)
}
// systrayRef is a reference entry for a tracked _Systray instance
// in the map
type systrayRef struct {
*_Systray
refs uint32
}
// systrayMap tracks references to _Systray instances,
// and provides reference counting and lookup via an id.
type systrayMap struct {
lock *sync.RWMutex
m map[refId]*systrayRef
}
// Len returns the number of _Systray references that are being tracked
func (m *systrayMap) Len() int {
m.lock.RLock()
l := len(m.m)
m.lock.RUnlock()
return l
}
// Add adds a new references to a _Systray instance, and increments
// the reference count to 1
func (m *systrayMap) Add(tray *_Systray) refId {
m.lock.Lock()
id := uuid()
m.m[id] = &systrayRef{tray, 1}
m.lock.Unlock()
// fmt.Printf("storage: Added ref to %v with id %d\n", tray, id)
return id
}
// Incref increments the reference count for a tracked reference
func (m *systrayMap) Incref(id refId) {
m.lock.RLock()
ref, ok := m.m[id]
m.lock.RUnlock()
if !ok {
return
}
atomic.AddUint32(&ref.refs, 1)
// fmt.Printf("storage: Incref %v (id: %v) to %d\n", ref, id, ref.refs)
}
// Decref decrements a reference count for a tracked reference.
// If the count reaches zero, it will be removed from the map.
func (m *systrayMap) Decref(id refId) {
m.lock.RLock()
ref, ok := m.m[id]
m.lock.RUnlock()
if !ok {
return
}
refs := atomic.AddUint32(&ref.refs, ^uint32(0))
// fmt.Printf("storage: Decref %v to %d\n", ref, refs)
if refs != 0 {
return
}
m.lock.Lock()
if atomic.LoadUint32(&ref.refs) == 0 {
// fmt.Printf("storage: Deleting %v\n", ref)
delete(m.m, id)
}
m.lock.Unlock()
}
// Get returns an tracked _Systray reference by its id,
// and a bool indicating whether the reference exists.
// If the reference does not exist, then the pointer will
// be invalid
func (m *systrayMap) Get(id refId) (*systrayRef, bool) {
m.lock.RLock()
ref, ok := m.m[id]
m.lock.RUnlock()
return ref, ok
}
// Exported functions for C
//
//export systray_Incref
func systray_Incref(id refId) {
gSystrays.Incref(id)
}
//export systray_Decref
func systray_Decref(id refId) {
gSystrays.Decref(id)
}