github.com/tursom/GoCollections@v0.3.10/concurrent/collections/Park.go (about) 1 package collections 2 3 import ( 4 "sync" 5 6 "github.com/tursom/GoCollections/util/time" 7 ) 8 9 type Park struct { 10 lock sync.Mutex 11 ch chan struct{} 12 } 13 14 func (p *Park) getCh() chan struct{} { 15 p.lock.Lock() 16 defer p.lock.Unlock() 17 18 if p.ch == nil { 19 p.ch = make(chan struct{}) 20 } 21 22 return p.ch 23 } 24 25 func (p *Park) Park() { 26 <-p.getCh() 27 } 28 29 func (p *Park) ParkT(timeout time.Duration) { 30 select { 31 case <-p.getCh(): 32 case <-time.After(timeout): 33 } 34 } 35 36 func (p *Park) Unpark() { 37 p.lock.Lock() 38 defer p.lock.Unlock() 39 40 if p.ch == nil { 41 return 42 } 43 44 close(p.ch) 45 p.ch = nil 46 }