github.com/songzhibin97/gkit@v1.2.13/sys/syncx/pool_race.go (about)

     1  //go:build race
     2  // +build race
     3  
     4  package syncx
     5  
     6  import (
     7  	"sync"
     8  )
     9  
    10  type Pool struct {
    11  	p    sync.Pool
    12  	once sync.Once
    13  	// New optionally specifies a function to generate
    14  	// a value when Get would otherwise return nil.
    15  	// It may not be changed concurrently with calls to Get.
    16  	New func() interface{}
    17  	// NoGC any objects in this Pool.
    18  	NoGC bool
    19  }
    20  
    21  func (p *Pool) init() {
    22  	p.once.Do(func() {
    23  		p.p = sync.Pool{
    24  			New: p.New,
    25  		}
    26  	})
    27  }
    28  
    29  // Put adds x to the pool.
    30  func (p *Pool) Put(x interface{}) {
    31  	p.init()
    32  	p.p.Put(x)
    33  }
    34  
    35  // Get selects an arbitrary item from the Pool, removes it from the
    36  // Pool, and returns it to the caller.
    37  // Get may choose to ignore the pool and treat it as empty.
    38  // Callers should not assume any relation between values passed to Put and
    39  // the values returned by Get.
    40  //
    41  // If Get would otherwise return nil and p.New is non-nil, Get returns
    42  // the result of calling p.New.
    43  func (p *Pool) Get() (x interface{}) {
    44  	p.init()
    45  	return p.p.Get()
    46  }