2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-10 03:31:52 +08:00

Remove use of mutex around done chan

This commit is contained in:
Abhilash Gnan 2020-02-29 16:32:29 +01:00 committed by Soheil Hassas Yeganeh
parent e13d1cbf02
commit a192073df5

38
cmux.go
View File

@ -113,7 +113,6 @@ type cMux struct {
donec chan struct{}
sls []matchersListener
readTimeout time.Duration
mu sync.Mutex
}
func matchersToMatchWriters(matchers []Matcher) []MatchWriter {
@ -149,7 +148,7 @@ func (m *cMux) Serve() error {
var wg sync.WaitGroup
defer func() {
m.closeDoneChanLocked()
m.closeDoneChan()
wg.Wait()
for _, sl := range m.sls {
@ -162,13 +161,16 @@ func (m *cMux) Serve() error {
}()
for {
c, err := m.root.Accept()
if err != nil {
select {
case <-m.getDoneChan():
case <-m.donec:
// cmux was closed with cmux.Close()
return nil
default:
// do nothing
}
c, err := m.root.Accept()
if err != nil {
if !m.handleErr(err) {
return err
}
@ -197,7 +199,7 @@ func (m *cMux) serve(c net.Conn, donec <-chan struct{}, wg *sync.WaitGroup) {
}
select {
case sl.l.connc <- muc:
case <-m.getDoneChan():
case <-m.donec:
_ = c.Close()
}
return
@ -213,31 +215,15 @@ func (m *cMux) serve(c net.Conn, donec <-chan struct{}, wg *sync.WaitGroup) {
}
func (m *cMux) Close() {
m.mu.Lock()
defer m.mu.Unlock()
m.closeDoneChanLocked()
m.closeDoneChan()
}
func (m *cMux) getDoneChan() chan struct{} {
m.mu.Lock()
defer m.mu.Unlock()
return m.getDoneChanLocked()
}
func (m *cMux) getDoneChanLocked() chan struct{} {
if m.donec == nil {
m.donec = make(chan struct{})
}
return m.donec
}
func (m *cMux) closeDoneChanLocked() {
ch := m.getDoneChanLocked()
func (m *cMux) closeDoneChan() {
select {
case <-ch:
case <-m.donec:
// Already closed. Don't close again
default:
close(ch)
close(m.donec)
}
}