mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2026-04-22 15:57:15 +08:00
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:
+7
-6
@@ -48,25 +48,26 @@ func SystemColorMode() ColorMode {
|
||||
var theSystemColorCache systemColorCache
|
||||
|
||||
type systemColorCache struct {
|
||||
mode atomic.Int32
|
||||
updated atomic.Int64
|
||||
m sync.Mutex
|
||||
mode atomic.Int32
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user