Skip to content

Commit

Permalink
Remove various old atomic variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 16, 2025
1 parent 921839b commit fcee5d2
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 68 deletions.
12 changes: 7 additions & 5 deletions dialog/color_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dialog

import (
"strconv"
"sync/atomic"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
Expand Down Expand Up @@ -152,7 +151,7 @@ func (e *colorChannelEntry) MinSize() fyne.Size {

type userChangeEntry struct {
widget.Entry
userTyped atomic.Bool
userTyped bool
}

func newUserChangeEntry(text string) *userChangeEntry {
Expand All @@ -164,9 +163,12 @@ func newUserChangeEntry(text string) *userChangeEntry {

func (e *userChangeEntry) setOnChanged(onChanged func(s string)) {
e.Entry.OnChanged = func(text string) {
if !e.userTyped.CompareAndSwap(true, false) {
if !e.userTyped {
return
}

e.userTyped = false

if onChanged != nil {
onChanged(text)
}
Expand All @@ -175,11 +177,11 @@ func (e *userChangeEntry) setOnChanged(onChanged func(s string)) {
}

func (e *userChangeEntry) TypedRune(r rune) {
e.userTyped.Store(true)
e.userTyped = true
e.Entry.TypedRune(r)
}

func (e *userChangeEntry) TypedKey(ev *fyne.KeyEvent) {
e.userTyped.Store(true)
e.userTyped = true
e.Entry.TypedKey(ev)
}
7 changes: 3 additions & 4 deletions internal/animation/animation.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package animation

import (
"sync/atomic"
"time"

"fyne.io/fyne/v2"
Expand All @@ -14,7 +13,7 @@ type anim struct {
reverse bool
start time.Time
total int64
stopped atomic.Bool
stopped bool
}

func newAnim(a *fyne.Animation) *anim {
Expand All @@ -25,9 +24,9 @@ func newAnim(a *fyne.Animation) *anim {
}

func (a *anim) setStopped() {
a.stopped.Store(true)
a.stopped = true
}

func (a *anim) isStopped() bool {
return a.stopped.Load()
return a.stopped
}
27 changes: 3 additions & 24 deletions internal/cache/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cache

import (
"os"
"sync/atomic"
"time"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -181,35 +180,15 @@ func matchesACanvas(cinfo *canvasInfo, canvases []fyne.Canvas) bool {
}

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

// isExpired check if the cache data is expired.
func (c *expiringCache) isExpired(now time.Time) bool {
t := c.expires.Load()
if t == nil {
return (time.Time{}).Before(now)
}
return (*t).Before(now)
}

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

type expiringCacheNoLock struct {
expires time.Time
}

// isExpired check if the cache data is expired.
func (c *expiringCacheNoLock) isExpired(now time.Time) bool {
func (c *expiringCache) isExpired(now time.Time) bool {
return c.expires.Before(now)
}

// setAlive updates expiration time.
func (c *expiringCacheNoLock) setAlive() {
t := timeNow().Add(cacheDuration)
c.expires = t
func (c *expiringCache) setAlive() {
c.expires = timeNow().Add(cacheDuration)
}
17 changes: 0 additions & 17 deletions internal/cache/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,6 @@ func Test_expiringCache(t *testing.T) {
assert.True(t, c.isExpired(tm.now))
}

func Test_expiringCacheNoLock(t *testing.T) {
tm := &timeMock{}
tm.setTime(10, 10)

c := &expiringCacheNoLock{}
assert.True(t, c.isExpired(tm.now))

c.setAlive()

tm.setTime(10, 20)
assert.False(t, c.isExpired(tm.now))

tm.setTime(10, 11)
tm.now = tm.now.Add(cacheDuration)
assert.True(t, c.isExpired(tm.now))
}

type dummyCanvas struct {
fyne.Canvas
}
Expand Down
2 changes: 0 additions & 2 deletions internal/cache/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ func SetSvg(name string, o fyne.CanvasObject, pix *image.NRGBA, w int, h int) {
}

type svgInfo struct {
// An svgInfo can be accessed from different goroutines, e.g., systray.
// Use expiringCache instead of expiringCacheNoLock.
expiringCache
pix *image.NRGBA
w, h int
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/texture_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ func prepareTexture(texture TextureType, canvas fyne.Canvas, free func()) *textu

// textureCacheBase defines base texture cache object.
type textureCacheBase struct {
expiringCacheNoLock
expiringCache
canvas fyne.Canvas
}
9 changes: 5 additions & 4 deletions internal/driver/common/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package common
import (
"image/color"
"reflect"
"sync/atomic"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
Expand Down Expand Up @@ -46,7 +45,7 @@ type Canvas struct {
// the refreshQueue is an unbounded queue which is able to cache
// arbitrary number of fyne.CanvasObject for the rendering.
refreshQueue *async.CanvasObjectQueue
dirty atomic.Bool
dirty bool

mWindowHeadTree, contentTree, menuTree *renderCacheTree
}
Expand Down Expand Up @@ -342,12 +341,14 @@ func (c *Canvas) SetContentTreeAndFocusMgr(content fyne.CanvasObject) {
// CheckDirtyAndClear returns true if the canvas is dirty and
// clears the dirty state atomically.
func (c *Canvas) CheckDirtyAndClear() bool {
return c.dirty.Swap(false)
wasDirty := c.dirty
c.dirty = false
return wasDirty
}

// SetDirty sets canvas dirty flag atomically.
func (c *Canvas) SetDirty() {
c.dirty.Store(true)
c.dirty = true
}

// SetMenuTreeAndFocusMgr sets menu tree and focus manager.
Expand Down
6 changes: 3 additions & 3 deletions internal/driver/mobile/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math"
"runtime"
"strconv"
"sync/atomic"
"time"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -61,7 +60,7 @@ type driver struct {
theme fyne.ThemeVariant
onConfigChanged func(*Configuration)
painting bool
running atomic.Bool
running bool
queuedFuncs *async.UnboundedChan[func()]
}

Expand Down Expand Up @@ -153,9 +152,10 @@ func (d *driver) Quit() {
}

func (d *driver) Run() {
if !d.running.CompareAndSwap(false, true) {
if d.running {
return // Run was called twice.
}
d.running = true

app.Main(func(a app.App) {
d.app = a
Expand Down
18 changes: 10 additions & 8 deletions widget/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package widget

import (
"image/color"
"sync/atomic"
"time"

"fyne.io/fyne/v2"
Expand All @@ -19,7 +18,7 @@ var _ fyne.Widget = (*Activity)(nil)
type Activity struct {
BaseWidget

started atomic.Bool
started bool
}

// NewActivity returns a widget for indicating activity
Expand All @@ -38,19 +37,23 @@ func (a *Activity) MinSize() fyne.Size {

// Start the activity indicator animation
func (a *Activity) Start() {
if !a.started.CompareAndSwap(false, true) {
if a.started {
return // already started
}

a.started = true

a.Refresh()
}

// Stop the activity indicator animation
func (a *Activity) Stop() {
if !a.started.CompareAndSwap(true, false) {
if !a.started {
return // already stopped
}

a.started = false

a.Refresh()
}

Expand All @@ -67,7 +70,7 @@ func (a *Activity) CreateRenderer() fyne.WidgetRenderer {
Tick: r.animate}
r.updateColor()

if a.started.Load() {
if a.started {
r.start()
}

Expand All @@ -88,7 +91,7 @@ type activityRenderer struct {
}

func (a *activityRenderer) Destroy() {
a.parent.started.Store(false)
a.parent.started = false
a.stop()
}

Expand All @@ -106,8 +109,7 @@ func (a *activityRenderer) Objects() []fyne.CanvasObject {
}

func (a *activityRenderer) Refresh() {
started := a.parent.started.Load()
if started {
if a.parent.started {
if !a.wasStarted {
a.start()
}
Expand Down

0 comments on commit fcee5d2

Please sign in to comment.