1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00
Files
go-mixed/locker/chan_source_locker.go
2022-07-26 14:19:40 +08:00

42 lines
644 B
Go

package locker
type ChanLocker interface {
Get(key string) (ch <-chan int, ok bool)
Release(key string)
}
type chanSourceLock struct {
m RWLocker
content map[string]chan int
}
func (s *chanSourceLock) Get(key string) (ch <-chan int, ok bool) {
s.m.RLock()
ch, ok = s.content[key]
s.m.RUnlock()
if ok {
return
}
s.m.Lock()
ch, ok = s.content[key]
if ok {
s.m.Unlock()
return
}
s.content[key] = make(chan int)
ch = s.content[key]
ok = true
s.m.Unlock()
return
}
func (s *chanSourceLock) Release(key string) {
s.m.Lock()
ch, ok := s.content[key]
if ok {
close(ch)
delete(s.content, key)
}
s.m.Unlock()
}