1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2022-07-26 14:19:40 +08:00
parent 23865214c8
commit 792458a185
5 changed files with 144 additions and 14 deletions

View File

@ -0,0 +1,41 @@
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()
}