map_pool.go (784B)
1 package mutexes 2 3 // pool is a very simply memory pool. 4 type pool struct { 5 current []*rwmutex 6 victim []*rwmutex 7 } 8 9 // Acquire will returns a rwmutex from pool (or alloc new). 10 func (p *pool) Acquire() *rwmutex { 11 // First try the current queue 12 if l := len(p.current) - 1; l >= 0 { 13 mu := p.current[l] 14 p.current = p.current[:l] 15 return mu 16 } 17 18 // Next try the victim queue. 19 if l := len(p.victim) - 1; l >= 0 { 20 mu := p.victim[l] 21 p.victim = p.victim[:l] 22 return mu 23 } 24 25 // Lastly, alloc new. 26 return &rwmutex{} 27 } 28 29 // Release places a sync.RWMutex back in the pool. 30 func (p *pool) Release(mu *rwmutex) { 31 p.current = append(p.current, mu) 32 } 33 34 // GC will clear out unused entries from the pool. 35 func (p *pool) GC() { 36 current := p.current 37 p.current = nil 38 p.victim = current 39 }