Add ResultPipes of different implementations

This commit is contained in:
Tevin Zhang 2019-02-12 18:39:04 +08:00
parent 32a4a549c2
commit e298e4beb3
3 changed files with 73 additions and 0 deletions

7
result_pipes.go Normal file
View File

@ -0,0 +1,7 @@
package tcp
type resultPipes interface {
popResultPipe(int) (chan error, bool)
deregisterResultPipe(int)
registerResultPipe(int, chan error)
}

35
result_pipes_mu.go Normal file
View File

@ -0,0 +1,35 @@
package tcp
import "sync"
type resultPipesMU struct {
l sync.Mutex
fdResultPipes map[int]chan error
}
func newResultPipesMU() *resultPipesMU {
return &resultPipesMU{fdResultPipes: make(map[int]chan error)}
}
func (r *resultPipesMU) popResultPipe(fd int) (chan error, bool) {
r.l.Lock()
p, exists := r.fdResultPipes[fd]
if exists {
delete(r.fdResultPipes, fd)
}
r.l.Unlock()
return p, exists
}
func (r *resultPipesMU) deregisterResultPipe(fd int) {
r.l.Lock()
delete(r.fdResultPipes, fd)
r.l.Unlock()
}
func (r *resultPipesMU) registerResultPipe(fd int, pipe chan error) {
// NOTE: the pipe should have been put back if c.fdResultPipes[fd] exists.
r.l.Lock()
r.fdResultPipes[fd] = pipe
r.l.Unlock()
}

31
result_pipes_sync_map.go Normal file
View File

@ -0,0 +1,31 @@
package tcp
import "sync"
type resultPipesSyncMap struct {
sync.Map
}
func newResultPipesSyncMap() *resultPipesSyncMap {
return &resultPipesSyncMap{}
}
func (r *resultPipesSyncMap) popResultPipe(fd int) (chan error, bool) {
p, exist := r.Load(fd)
if exist {
r.Delete(fd)
}
if p != nil {
return p.(chan error), exist
}
return nil, exist
}
func (r *resultPipesSyncMap) deregisterResultPipe(fd int) {
r.Delete(fd)
}
func (r *resultPipesSyncMap) registerResultPipe(fd int, pipe chan error) {
// NOTE: the pipe should have been put back if c.fdResultPipes[fd] exists.
r.Store(fd, pipe)
}