From eef24db1d59cac55d773312ab6efe1890187f2de Mon Sep 17 00:00:00 2001 From: chzyer Date: Wed, 25 Jan 2017 11:55:32 +0800 Subject: [PATCH] fix terminal data race (#99) --- terminal.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/terminal.go b/terminal.go index b3d1caf..8014712 100644 --- a/terminal.go +++ b/terminal.go @@ -10,6 +10,7 @@ import ( ) type Terminal struct { + m sync.Mutex cfg *Config outchan chan rune closed int32 @@ -121,7 +122,7 @@ func (t *Terminal) ioloop() { expectNextChar bool ) - buf := bufio.NewReader(t.cfg.Stdin) + buf := bufio.NewReader(t.getStdin()) for { if !expectNextChar { atomic.StoreInt32(&t.isReading, 0) @@ -206,10 +207,26 @@ func (t *Terminal) Close() error { 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 { if err := c.Init(); err != nil { return err } + t.m.Lock() t.cfg = c + t.m.Unlock() return nil }