Skip to content

Commit 51504bf

Browse files
aykevldeadprogram
authored andcommitted
sync: make Pool thread-safe
Make sure the object is locked when trying to modify it. Binary size seems unaffected when not using threading.
1 parent 79164da commit 51504bf

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/sync/pool.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
package sync
22

3+
import "internal/task"
4+
35
// Pool is a very simple implementation of sync.Pool.
46
type Pool struct {
7+
lock task.PMutex
58
New func() interface{}
69
items []interface{}
710
}
811

912
// Get returns an item in the pool, or the value of calling Pool.New() if there are no items.
1013
func (p *Pool) Get() interface{} {
14+
p.lock.Lock()
1115
if len(p.items) > 0 {
1216
x := p.items[len(p.items)-1]
1317
p.items = p.items[:len(p.items)-1]
18+
p.lock.Unlock()
1419
return x
1520
}
21+
p.lock.Unlock()
1622
if p.New == nil {
1723
return nil
1824
}
@@ -21,5 +27,7 @@ func (p *Pool) Get() interface{} {
2127

2228
// Put adds a value back into the pool.
2329
func (p *Pool) Put(x interface{}) {
30+
p.lock.Lock()
2431
p.items = append(p.items, x)
32+
p.lock.Unlock()
2533
}

0 commit comments

Comments
 (0)