Fix race condition in DefaultOnWidthChanged

This commit is contained in:
Kyle Carberry 2022-02-10 23:42:33 -06:00
parent 2972be24d4
commit 0d62993714
1 changed files with 4 additions and 0 deletions

View File

@ -1,3 +1,4 @@
//go:build darwin || dragonfly || freebsd || (linux && !appengine) || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux,!appengine netbsd openbsd solaris // +build darwin dragonfly freebsd linux,!appengine netbsd openbsd solaris
package readline package readline
@ -60,11 +61,14 @@ func GetStdin() int {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
var ( var (
widthChangeMutex sync.Mutex
widthChange sync.Once widthChange sync.Once
widthChangeCallback func() widthChangeCallback func()
) )
func DefaultOnWidthChanged(f func()) { func DefaultOnWidthChanged(f func()) {
widthChangeMutex.Lock()
defer widthChangeMutex.Unlock()
widthChangeCallback = f widthChangeCallback = f
widthChange.Do(func() { widthChange.Do(func() {
ch := make(chan os.Signal, 1) ch := make(chan os.Signal, 1)