]> git.lizzy.rs Git - micro.git/commitdiff
Fix minor autosave race condition
authorZachary Yedidia <zyedidia@gmail.com>
Sun, 4 Aug 2019 21:32:42 +0000 (14:32 -0700)
committerZachary Yedidia <zyedidia@gmail.com>
Wed, 25 Dec 2019 22:05:11 +0000 (17:05 -0500)
internal/action/command.go
internal/config/autosave.go

index a062a23d098cc732545887767a1e5b971ca54a94..029f9582f44799efa00512631cdfb9eabee3caa6 100644 (file)
@@ -406,9 +406,10 @@ func SetGlobalOptionNative(option string, nativeValue interface{}) error {
                }
        } else if option == "autosave" {
                if nativeValue.(float64) > 0 {
+                       config.SetAutoTime(int(nativeValue.(float64)))
                        config.StartAutoSave()
                } else {
-                       config.StopAutoSave()
+                       config.SetAutoTime(0)
                }
        } else {
                for _, pl := range config.Plugins {
index 2c5bf815bf316b36fa6c4ffd9adfd64443225d44..db473081aab79dc5910da9c816ac8addd4325eb0 100644 (file)
@@ -1,30 +1,45 @@
 package config
 
 import (
-       "log"
+       "sync"
        "time"
 )
 
 var Autosave chan bool
+var autotime int
+
+// lock for autosave
+var autolock sync.Mutex
 
 func init() {
        Autosave = make(chan bool)
 }
 
+func SetAutoTime(a int) {
+       autolock.Lock()
+       autotime = a
+       autolock.Unlock()
+}
+
+func GetAutoTime() int {
+       autolock.Lock()
+       a := autotime
+       autolock.Unlock()
+       return a
+}
+
 func StartAutoSave() {
        go func() {
                for {
-                       autotime := time.Duration(GlobalSettings["autosave"].(float64))
                        if autotime < 1 {
                                break
                        }
-                       time.Sleep(autotime * time.Second)
-                       log.Println("Autosave")
+                       time.Sleep(time.Duration(autotime) * time.Second)
+                       // it's possible autotime was changed while sleeping
+                       if autotime < 1 {
+                               break
+                       }
                        Autosave <- true
                }
        }()
 }
-
-func StopAutoSave() {
-       GlobalSettings["autosave"] = float64(0)
-}