2013-12-18 12:08:34 -07:00
|
|
|
// Copyright 2013 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package sync
|
|
|
|
|
2014-01-24 11:29:53 -07:00
|
|
|
import (
|
2015-09-23 02:03:54 -06:00
|
|
|
"internal/race"
|
2014-01-24 11:29:53 -07:00
|
|
|
"runtime"
|
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2014-04-09 13:45:18 -06:00
|
|
|
// A Pool is a set of temporary objects that may be individually saved and
|
|
|
|
// retrieved.
|
2013-12-18 12:08:34 -07:00
|
|
|
//
|
2014-04-09 13:45:18 -06:00
|
|
|
// Any item stored in the Pool may be removed automatically at any time without
|
|
|
|
// notification. If the Pool holds the only reference when this happens, the
|
|
|
|
// item might be deallocated.
|
2013-12-18 12:08:34 -07:00
|
|
|
//
|
|
|
|
// A Pool is safe for use by multiple goroutines simultaneously.
|
|
|
|
//
|
2014-04-09 13:45:18 -06:00
|
|
|
// Pool's purpose is to cache allocated but unused items for later reuse,
|
|
|
|
// relieving pressure on the garbage collector. That is, it makes it easy to
|
|
|
|
// build efficient, thread-safe free lists. However, it is not suitable for all
|
|
|
|
// free lists.
|
|
|
|
//
|
|
|
|
// An appropriate use of a Pool is to manage a group of temporary items
|
|
|
|
// silently shared among and potentially reused by concurrent independent
|
|
|
|
// clients of a package. Pool provides a way to amortize allocation overhead
|
|
|
|
// across many clients.
|
|
|
|
//
|
|
|
|
// An example of good use of a Pool is in the fmt package, which maintains a
|
|
|
|
// dynamically-sized store of temporary output buffers. The store scales under
|
|
|
|
// load (when many goroutines are actively printing) and shrinks when
|
|
|
|
// quiescent.
|
|
|
|
//
|
|
|
|
// On the other hand, a free list maintained as part of a short-lived object is
|
|
|
|
// not a suitable use for a Pool, since the overhead does not amortize well in
|
|
|
|
// that scenario. It is more efficient to have such objects implement their own
|
|
|
|
// free list.
|
2013-12-20 12:15:50 -07:00
|
|
|
//
|
2016-04-14 15:33:28 -06:00
|
|
|
// A Pool must not be copied after first use.
|
2013-12-18 12:08:34 -07:00
|
|
|
type Pool struct {
|
2016-04-14 15:33:28 -06:00
|
|
|
noCopy noCopy
|
|
|
|
|
2014-04-14 11:13:32 -06:00
|
|
|
local unsafe.Pointer // local fixed-size per-P pool, actual type is [P]poolLocal
|
|
|
|
localSize uintptr // size of the local array
|
2013-12-18 12:08:34 -07:00
|
|
|
|
|
|
|
// New optionally specifies a function to generate
|
|
|
|
// a value when Get would otherwise return nil.
|
|
|
|
// It may not be changed concurrently with calls to Get.
|
|
|
|
New func() interface{}
|
|
|
|
}
|
|
|
|
|
2014-01-24 11:29:53 -07:00
|
|
|
// Local per-P Pool appendix.
|
|
|
|
type poolLocal struct {
|
2014-04-14 11:13:32 -06:00
|
|
|
private interface{} // Can be used only by the respective P.
|
|
|
|
shared []interface{} // Can be used by any P.
|
|
|
|
Mutex // Protects shared.
|
|
|
|
pad [128]byte // Prevents false sharing.
|
2014-01-24 11:29:53 -07:00
|
|
|
}
|
2013-12-18 12:08:34 -07:00
|
|
|
|
|
|
|
// Put adds x to the pool.
|
|
|
|
func (p *Pool) Put(x interface{}) {
|
2015-09-23 02:03:54 -06:00
|
|
|
if race.Enabled {
|
2014-01-25 09:11:16 -07:00
|
|
|
// Under race detector the Pool degenerates into no-op.
|
|
|
|
// It's conforming, simple and does not introduce excessive
|
|
|
|
// happens-before edges between unrelated goroutines.
|
|
|
|
return
|
|
|
|
}
|
2013-12-18 12:08:34 -07:00
|
|
|
if x == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-01-24 11:29:53 -07:00
|
|
|
l := p.pin()
|
2014-04-14 11:13:32 -06:00
|
|
|
if l.private == nil {
|
|
|
|
l.private = x
|
|
|
|
x = nil
|
|
|
|
}
|
|
|
|
runtime_procUnpin()
|
|
|
|
if x == nil {
|
2014-01-24 11:29:53 -07:00
|
|
|
return
|
2013-12-18 12:08:34 -07:00
|
|
|
}
|
2014-04-14 11:13:32 -06:00
|
|
|
l.Lock()
|
|
|
|
l.shared = append(l.shared, x)
|
|
|
|
l.Unlock()
|
2013-12-18 12:08:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get selects an arbitrary item from the Pool, removes it from the
|
|
|
|
// Pool, and returns it to the caller.
|
|
|
|
// Get may choose to ignore the pool and treat it as empty.
|
|
|
|
// Callers should not assume any relation between values passed to Put and
|
|
|
|
// the values returned by Get.
|
|
|
|
//
|
|
|
|
// If Get would otherwise return nil and p.New is non-nil, Get returns
|
|
|
|
// the result of calling p.New.
|
|
|
|
func (p *Pool) Get() interface{} {
|
2015-09-23 02:03:54 -06:00
|
|
|
if race.Enabled {
|
2014-01-25 09:11:16 -07:00
|
|
|
if p.New != nil {
|
|
|
|
return p.New()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-01-24 11:29:53 -07:00
|
|
|
l := p.pin()
|
2014-04-14 11:13:32 -06:00
|
|
|
x := l.private
|
|
|
|
l.private = nil
|
|
|
|
runtime_procUnpin()
|
|
|
|
if x != nil {
|
2014-01-24 11:29:53 -07:00
|
|
|
return x
|
|
|
|
}
|
2014-04-14 11:13:32 -06:00
|
|
|
l.Lock()
|
|
|
|
last := len(l.shared) - 1
|
|
|
|
if last >= 0 {
|
|
|
|
x = l.shared[last]
|
|
|
|
l.shared = l.shared[:last]
|
2014-01-24 11:29:53 -07:00
|
|
|
}
|
2014-04-14 11:13:32 -06:00
|
|
|
l.Unlock()
|
|
|
|
if x != nil {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return p.getSlow()
|
2014-01-24 11:29:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pool) getSlow() (x interface{}) {
|
2014-04-14 11:13:32 -06:00
|
|
|
// See the comment in pin regarding ordering of the loads.
|
|
|
|
size := atomic.LoadUintptr(&p.localSize) // load-acquire
|
|
|
|
local := p.local // load-consume
|
|
|
|
// Try to steal one element from other procs.
|
2014-01-24 11:29:53 -07:00
|
|
|
pid := runtime_procPin()
|
2014-04-14 11:13:32 -06:00
|
|
|
runtime_procUnpin()
|
|
|
|
for i := 0; i < int(size); i++ {
|
|
|
|
l := indexLocal(local, (pid+i+1)%int(size))
|
|
|
|
l.Lock()
|
|
|
|
last := len(l.shared) - 1
|
2014-01-24 11:29:53 -07:00
|
|
|
if last >= 0 {
|
2014-04-14 11:13:32 -06:00
|
|
|
x = l.shared[last]
|
|
|
|
l.shared = l.shared[:last]
|
|
|
|
l.Unlock()
|
|
|
|
break
|
2014-01-24 11:29:53 -07:00
|
|
|
}
|
2014-04-14 11:13:32 -06:00
|
|
|
l.Unlock()
|
2013-12-18 12:08:34 -07:00
|
|
|
}
|
2014-01-24 11:29:53 -07:00
|
|
|
|
2013-12-18 12:08:34 -07:00
|
|
|
if x == nil && p.New != nil {
|
|
|
|
x = p.New()
|
|
|
|
}
|
2014-04-14 11:13:32 -06:00
|
|
|
return x
|
2013-12-18 12:08:34 -07:00
|
|
|
}
|
2014-01-24 11:29:53 -07:00
|
|
|
|
2014-04-09 13:45:18 -06:00
|
|
|
// pin pins the current goroutine to P, disables preemption and returns poolLocal pool for the P.
|
2014-01-24 11:29:53 -07:00
|
|
|
// Caller must call runtime_procUnpin() when done with the pool.
|
|
|
|
func (p *Pool) pin() *poolLocal {
|
|
|
|
pid := runtime_procPin()
|
|
|
|
// In pinSlow we store to localSize and then to local, here we load in opposite order.
|
2016-01-27 13:49:13 -07:00
|
|
|
// Since we've disabled preemption, GC cannot happen in between.
|
2014-01-24 11:29:53 -07:00
|
|
|
// Thus here we must observe local at least as large localSize.
|
|
|
|
// We can observe a newer/larger local, it is fine (we must observe its zero-initialized-ness).
|
|
|
|
s := atomic.LoadUintptr(&p.localSize) // load-acquire
|
|
|
|
l := p.local // load-consume
|
|
|
|
if uintptr(pid) < s {
|
|
|
|
return indexLocal(l, pid)
|
|
|
|
}
|
|
|
|
return p.pinSlow()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pool) pinSlow() *poolLocal {
|
|
|
|
// Retry under the mutex.
|
2014-04-14 11:13:32 -06:00
|
|
|
// Can not lock the mutex while pinned.
|
2014-01-24 11:29:53 -07:00
|
|
|
runtime_procUnpin()
|
2014-04-14 11:13:32 -06:00
|
|
|
allPoolsMu.Lock()
|
|
|
|
defer allPoolsMu.Unlock()
|
2014-01-24 11:29:53 -07:00
|
|
|
pid := runtime_procPin()
|
2014-04-14 11:13:32 -06:00
|
|
|
// poolCleanup won't be called while we are pinned.
|
2014-01-24 11:29:53 -07:00
|
|
|
s := p.localSize
|
|
|
|
l := p.local
|
|
|
|
if uintptr(pid) < s {
|
|
|
|
return indexLocal(l, pid)
|
|
|
|
}
|
|
|
|
if p.local == nil {
|
2014-04-14 11:13:32 -06:00
|
|
|
allPools = append(allPools, p)
|
2014-01-24 11:29:53 -07:00
|
|
|
}
|
|
|
|
// If GOMAXPROCS changes between GCs, we re-allocate the array and lose the old one.
|
|
|
|
size := runtime.GOMAXPROCS(0)
|
|
|
|
local := make([]poolLocal, size)
|
2016-04-14 20:09:36 -06:00
|
|
|
atomic.StorePointer(&p.local, unsafe.Pointer(&local[0])) // store-release
|
|
|
|
atomic.StoreUintptr(&p.localSize, uintptr(size)) // store-release
|
2014-01-24 11:29:53 -07:00
|
|
|
return &local[pid]
|
|
|
|
}
|
|
|
|
|
2014-04-14 11:13:32 -06:00
|
|
|
func poolCleanup() {
|
|
|
|
// This function is called with the world stopped, at the beginning of a garbage collection.
|
|
|
|
// It must not allocate and probably should not call any runtime functions.
|
|
|
|
// Defensively zero out everything, 2 reasons:
|
|
|
|
// 1. To prevent false retention of whole Pools.
|
|
|
|
// 2. If GC happens while a goroutine works with l.shared in Put/Get,
|
|
|
|
// it will retain whole Pool. So next cycle memory consumption would be doubled.
|
|
|
|
for i, p := range allPools {
|
|
|
|
allPools[i] = nil
|
|
|
|
for i := 0; i < int(p.localSize); i++ {
|
|
|
|
l := indexLocal(p.local, i)
|
|
|
|
l.private = nil
|
|
|
|
for j := range l.shared {
|
|
|
|
l.shared[j] = nil
|
|
|
|
}
|
|
|
|
l.shared = nil
|
|
|
|
}
|
2014-10-22 10:23:49 -06:00
|
|
|
p.local = nil
|
|
|
|
p.localSize = 0
|
2014-04-14 11:13:32 -06:00
|
|
|
}
|
|
|
|
allPools = []*Pool{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
allPoolsMu Mutex
|
|
|
|
allPools []*Pool
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
runtime_registerPoolCleanup(poolCleanup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexLocal(l unsafe.Pointer, i int) *poolLocal {
|
|
|
|
return &(*[1000000]poolLocal)(l)[i]
|
2014-01-24 11:29:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implemented in runtime.
|
2014-04-14 11:13:32 -06:00
|
|
|
func runtime_registerPoolCleanup(cleanup func())
|
2014-01-24 11:29:53 -07:00
|
|
|
func runtime_procPin() int
|
|
|
|
func runtime_procUnpin()
|