Skip to content

Commit

Permalink
More atomic.Value cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 26, 2023
1 parent 554a7b9 commit 02b1455
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
15 changes: 3 additions & 12 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,11 @@ type App interface {
SetCloudProvider(CloudProvider) // configure cloud for this app
}

// app contains an App variable, but due to atomic.Value restrictions on
// interfaces we need to use an indirect type, i.e. appContainer.
var app atomic.Pointer[appContainer] // appContainer

// appContainer is a dummy container that holds an App instance. This
// struct exists to guarantee that atomic.Value can store objects with
// same type.
type appContainer struct {
current App
}
var app atomic.Pointer[App]

// SetCurrentApp is an internal function to set the app instance currently running.
func SetCurrentApp(current App) {
app.Store(&appContainer{current})
app.Store(&current)
}

// CurrentApp returns the current application, for which there is only 1 per process.
Expand All @@ -104,7 +95,7 @@ func CurrentApp() App {
LogError("Attempt to access current Fyne app when none is started", nil)
return nil
}
return val.current
return *val
}

// AppMetadata captures the build metadata for an application.
Expand Down
9 changes: 5 additions & 4 deletions internal/cache/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func ResetThemeCaches() {
})

fontSizeLock.Lock()
fontSizeCache = map[fontSizeEntry]fontMetric{}
fontSizeCache = map[fontSizeEntry]*fontMetric{}
fontSizeLock.Unlock()
}

Expand Down Expand Up @@ -215,7 +215,7 @@ func matchesACanvas(cinfo *canvasInfo, canvases []fyne.Canvas) bool {
}

type expiringCache struct {
expires atomic.Value // time.time
expires atomic.Pointer[time.Time]
}

// isExpired check if the cache data is expired.
Expand All @@ -224,12 +224,13 @@ func (c *expiringCache) isExpired(now time.Time) bool {
if t == nil {
return (time.Time{}).Before(now)
}
return t.(time.Time).Before(now)
return (*t).Before(now)
}

// setAlive updates expiration time.
func (c *expiringCache) setAlive() {
c.expires.Store(timeNow().Add(cacheDuration))
time := timeNow().Add(cacheDuration)
c.expires.Store(&time)
}

type expiringCacheNoLock struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/cache/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

var (
fontSizeCache = map[fontSizeEntry]fontMetric{}
fontSizeCache = map[fontSizeEntry]*fontMetric{}
fontSizeLock = sync.RWMutex{}
)

Expand Down Expand Up @@ -40,7 +40,7 @@ func GetFontMetrics(text string, fontSize float32, style fyne.TextStyle) (size f
// SetFontMetrics stores a calculated font size and baseline for parameters that were missing from the cache.
func SetFontMetrics(text string, fontSize float32, style fyne.TextStyle, size fyne.Size, base float32) {
ent := fontSizeEntry{text, fontSize, style}
metric := fontMetric{size: size, baseLine: base}
metric := &fontMetric{size: size, baseLine: base}
metric.setAlive()
fontSizeLock.Lock()
fontSizeCache[ent] = metric
Expand Down

0 comments on commit 02b1455

Please sign in to comment.