fix terminal data race (#99)

This commit is contained in:
chzyer 2017-01-25 11:55:32 +08:00 committed by GitHub
parent c914be64f0
commit eef24db1d5
1 changed files with 18 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import (
) )
type Terminal struct { type Terminal struct {
m sync.Mutex
cfg *Config cfg *Config
outchan chan rune outchan chan rune
closed int32 closed int32
@ -121,7 +122,7 @@ func (t *Terminal) ioloop() {
expectNextChar bool expectNextChar bool
) )
buf := bufio.NewReader(t.cfg.Stdin) buf := bufio.NewReader(t.getStdin())
for { for {
if !expectNextChar { if !expectNextChar {
atomic.StoreInt32(&t.isReading, 0) atomic.StoreInt32(&t.isReading, 0)
@ -206,10 +207,26 @@ func (t *Terminal) Close() error {
return t.ExitRawMode() return t.ExitRawMode()
} }
func (t *Terminal) GetConfig() *Config {
t.m.Lock()
cfg := *t.cfg
t.m.Unlock()
return &cfg
}
func (t *Terminal) getStdin() io.Reader {
t.m.Lock()
r := t.cfg.Stdin
t.m.Unlock()
return r
}
func (t *Terminal) SetConfig(c *Config) error { func (t *Terminal) SetConfig(c *Config) error {
if err := c.Init(); err != nil { if err := c.Init(); err != nil {
return err return err
} }
t.m.Lock()
t.cfg = c t.cfg = c
t.m.Unlock()
return nil return nil
} }