diff options
author | Ayke van Laethem <[email protected]> | 2024-10-28 16:07:43 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-11-22 11:55:52 +0100 |
commit | 51504bfd2ec3c295b0bc370befe6bc37ad782c7d (patch) | |
tree | 6fe2f66f4275c4585ce5c6261fa79af7477b934b /src | |
parent | 79164dae71d5af32d316a5fc1dc5c996f04cc0f1 (diff) | |
download | tinygo-51504bfd2ec3c295b0bc370befe6bc37ad782c7d.tar.gz tinygo-51504bfd2ec3c295b0bc370befe6bc37ad782c7d.zip |
sync: make Pool thread-safe
Make sure the object is locked when trying to modify it.
Binary size seems unaffected when not using threading.
Diffstat (limited to 'src')
-rw-r--r-- | src/sync/pool.go | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/sync/pool.go b/src/sync/pool.go index 41f06a65c..5a29afebd 100644 --- a/src/sync/pool.go +++ b/src/sync/pool.go @@ -1,18 +1,24 @@ 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 } @@ -21,5 +27,7 @@ func (p *Pool) Get() interface{} { // 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() } |