ebiten: bug fix: use time.Now/Since to check the cache

time.Now().Unix() is a wall clock and not reliable to use as a
cache expiration.
This commit is contained in:
Hajime Hoshi
2026-02-14 02:30:25 +09:00
parent 0c440698e0
commit 163973849f
+5 -4
View File
@@ -49,24 +49,25 @@ var theSystemColorCache systemColorCache
type systemColorCache struct {
mode atomic.Int32
updated atomic.Int64
lastUpdated atomic.Pointer[time.Time]
m sync.Mutex
}
func (s *systemColorCache) get() ColorMode {
if time.Now().Unix()-s.updated.Load() < 1 {
if t := s.lastUpdated.Load(); t != nil && time.Since(*t) < time.Second {
return ColorMode(s.mode.Load())
}
s.m.Lock()
defer s.m.Unlock()
if time.Now().Unix()-s.updated.Load() < 1 {
now := time.Now()
if t := s.lastUpdated.Load(); t != nil && now.Sub(*t) < time.Second {
return ColorMode(s.mode.Load())
}
clr := colormode.SystemColorMode()
s.mode.Store(int32(clr))
s.updated.Store(time.Now().Unix())
s.lastUpdated.Store(&now)
return ColorMode(clr)
}