12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package sync
- import (
- "sync"
- )
- type ExclusivePool struct {
- lock sync.Mutex
-
- pool map[string]*sync.Mutex
-
-
-
-
-
- count map[string]int
- }
- func NewExclusivePool() *ExclusivePool {
- return &ExclusivePool{
- pool: make(map[string]*sync.Mutex),
- count: make(map[string]int),
- }
- }
- func (p *ExclusivePool) CheckIn(identity string) {
- p.lock.Lock()
- lock, has := p.pool[identity]
- if !has {
- lock = &sync.Mutex{}
- p.pool[identity] = lock
- }
- p.count[identity]++
- p.lock.Unlock()
- lock.Lock()
- }
- func (p *ExclusivePool) CheckOut(identity string) {
- p.lock.Lock()
- defer p.lock.Unlock()
- p.pool[identity].Unlock()
- if p.count[identity] == 1 {
- delete(p.pool, identity)
- delete(p.count, identity)
- } else {
- p.count[identity]--
- }
- }
|