-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
177 lines (142 loc) · 2.43 KB
/
pool.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package native
import (
"os"
"sync"
"sync/atomic"
)
const (
POOL_STATUS_NORMAL int32 = iota
POOL_STATUS_FINISH
)
/*
*/
type PoolData interface {
ItemSize() uint
Dim() []uint
ClearData() PoolData
FreeData()
}
/*
*/
type IPool interface {
Get() PoolData
Put(PoolData)
FreeData()
}
/*
*/
type Status struct {
status int32
}
func (o *Status) IsFinish() bool {
return atomic.LoadInt32(&o.status) == POOL_STATUS_FINISH
}
func (o *Status) setFinish() bool {
return atomic.CompareAndSwapInt32(&o.status, POOL_STATUS_NORMAL, POOL_STATUS_FINISH)
}
/*
*/
func PoolManagerDefaultKey(dim ...uint) (key uint) {
key = 1
for _, d := range dim {
key *= d
}
return
}
/*
*/
type PoolManager struct {
sync.Map
Status
itemSize uint
key func(...uint) uint
new func(IPool, ...uint) interface{}
}
func NewPoolManager(itemSize uint, new func(IPool, ...uint) interface{}) *PoolManager {
return &PoolManager{
itemSize: itemSize,
key: PoolManagerDefaultKey,
new: new,
}
}
func (o *PoolManager) SetKey(key func(...uint) uint) *PoolManager {
o.key = key
return o
}
func (o *PoolManager) getPool(dim ...uint) IPool {
key := o.key(dim...)
if pool, ok := o.Load(key); ok {
return pool.(IPool)
}
pool, _ := o.LoadOrStore(key, NewPool(func(pool IPool) interface{} {
return o.new(pool, dim...)
}))
return pool.(IPool)
}
func (o *PoolManager) Get(dim ...uint) interface{} {
return o.getPool(dim...).Get()
}
func (o *PoolManager) Put(data PoolData) (err error) {
if o.IsFinish() {
return
}
if data.ItemSize() != o.itemSize {
err = os.ErrInvalid
return
}
o.getPool(data.Dim()...).Put(data)
return
}
func (o *PoolManager) FreeData() {
if !o.setFinish() {
return
}
o.Range(func(_, value interface{}) bool {
value.(IPool).FreeData()
return true
})
}
/*
*/
type Pool struct {
sync.Pool
Status
status int32
}
func NewPool(new func(pool IPool) interface{}) (pool *Pool) {
pool = &Pool{
status: POOL_STATUS_NORMAL,
}
pool.New = func() interface{} {
if pool.IsFinish() {
return nil
}
return new(pool)
}
return
}
func (o *Pool) Get() PoolData {
if o.IsFinish() {
return nil
}
return o.Pool.Get().(PoolData)
}
func (o *Pool) Put(data PoolData) {
if o.IsFinish() {
return
}
data.ClearData()
o.Pool.Put(data)
}
func (o *Pool) FreeData() {
if !o.setFinish() {
return
}
for {
if data := o.Pool.Get(); data == nil {
break
} else {
data.(PoolData).FreeData()
}
}
}