blob: 5a29afebda32a22143e5dd41ed2e71b5e76a088c (
plain)
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
|
package sync
import "internal/task"
// Pool is a very simple implementation of sync.Pool.
type Pool struct {
lock task.PMutex
New func() interface{}
items []interface{}
}
// Get returns an item in the pool, or the value of calling Pool.New() if there are no items.
func (p *Pool) Get() interface{} {
p.lock.Lock()
if len(p.items) > 0 {
x := p.items[len(p.items)-1]
p.items = p.items[:len(p.items)-1]
p.lock.Unlock()
return x
}
p.lock.Unlock()
if p.New == nil {
return nil
}
return p.New()
}
// Put adds a value back into the pool.
func (p *Pool) Put(x interface{}) {
p.lock.Lock()
p.items = append(p.items, x)
p.lock.Unlock()
}
|