Skip to content

Commit

Permalink
More usages of new atomic APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 26, 2023
1 parent 206f276 commit 4ecf18b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
11 changes: 5 additions & 6 deletions internal/async/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func main() {
Name: "CanvasObject",
Imports: `import (
"sync"
"sync/atomic"
"fyne.io/fyne/v2"
)`,
Expand Down Expand Up @@ -215,7 +214,7 @@ package async
type {{.Name}}Queue struct {
head *item{{.Name}}
tail *item{{.Name}}
len uint64
len atomic.Uint64
}
// New{{.Name}}Queue returns a queue for caching values.
Expand Down Expand Up @@ -256,7 +255,7 @@ package async
type {{.Name}}Queue struct {
head unsafe.Pointer
tail unsafe.Pointer
len uint64
len atomic.Uint64
}
// New{{.Name}}Queue returns a queue for caching values.
Expand Down Expand Up @@ -306,7 +305,7 @@ func (q *{{.Name}}Queue) In(v {{.Type}}) {
if lastnext == nil {
if cas{{.Name}}Item(&last.next, lastnext, i) {
cas{{.Name}}Item(&q.tail, last, i)
atomic.AddUint64(&q.len, 1)
q.len.Add(1)
return
}
} else {
Expand All @@ -333,7 +332,7 @@ func (q *{{.Name}}Queue) Out() {{.Type}} {
} else {
v := firstnext.v
if cas{{.Name}}Item(&q.head, first, firstnext) {
atomic.AddUint64(&q.len, ^uint64(0))
q.len.Add(^uint64(0))
item{{.Name}}Pool.Put(first)
return v
}
Expand All @@ -344,6 +343,6 @@ func (q *{{.Name}}Queue) Out() {{.Type}} {
// Len returns the length of the queue.
func (q *{{.Name}}Queue) Len() uint64 {
return atomic.LoadUint64(&q.len)
return q.len.Load()
}
`))
7 changes: 3 additions & 4 deletions internal/async/queue_canvasobject.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/async/queue_pure_canvasobject.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/async/queue_unsafe_canvasobject.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions internal/driver/glfw/window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1220,14 +1220,14 @@ func TestWindow_TappedAndDoubleTapped(t *testing.T) {
w := createWindow("Test").(*window)
waitSingleTapped := make(chan struct{})
waitDoubleTapped := make(chan struct{})
tapped := int32(0) // atomic
tapped := atomic.Int32{}
but := newDoubleTappableButton()
but.OnTapped = func() {
atomic.StoreInt32(&tapped, 1)
tapped.Store(1)
waitSingleTapped <- struct{}{}
}
but.onDoubleTap = func() {
atomic.StoreInt32(&tapped, 2)
tapped.Store(2)
waitDoubleTapped <- struct{}{}
}
w.SetContent(container.NewBorder(nil, nil, nil, nil, but))
Expand All @@ -1240,8 +1240,8 @@ func TestWindow_TappedAndDoubleTapped(t *testing.T) {
w.WaitForEvents()
time.Sleep(500 * time.Millisecond)

assert.Equal(t, int32(1), atomic.LoadInt32(&tapped), "Single tap should have fired")
atomic.StoreInt32(&tapped, 0)
assert.Equal(t, int32(1), tapped.Load(), "Single tap should have fired")
tapped.Store(0)

w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Press, 0)
w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Release, 0)
Expand All @@ -1252,7 +1252,7 @@ func TestWindow_TappedAndDoubleTapped(t *testing.T) {
w.WaitForEvents()
time.Sleep(500 * time.Millisecond)

assert.Equal(t, int32(2), atomic.LoadInt32(&tapped), "Double tap should have fired")
assert.Equal(t, int32(2), tapped.Load(), "Double tap should have fired")
}

func TestWindow_MouseEventContainsModifierKeys(t *testing.T) {
Expand Down
12 changes: 7 additions & 5 deletions widget/bind_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// basicBinder stores a DataItem and a function to be called when it changes.
// It provides a convenient way to replace data and callback independently.
type basicBinder struct {
callback atomic.Value // func(binding.DataItem)
callback atomic.Pointer[func(binding.DataItem)]

dataListenerPairLock sync.RWMutex
dataListenerPair annotatedListener // access guarded by dataListenerPairLock
Expand All @@ -19,10 +19,12 @@ type basicBinder struct {
// Bind replaces the data item whose changes are tracked by the callback function.
func (binder *basicBinder) Bind(data binding.DataItem) {
listener := binding.NewDataListener(func() { // NB: listener captures `data` but always calls the up-to-date callback
f := binder.callback.Load()
if fn, ok := f.(func(binding.DataItem)); ok && fn != nil {
fn(data)
f := *binder.callback.Load()
if f == nil {
return
}

f(data)
})
data.AddListener(listener)
listenerInfo := annotatedListener{
Expand All @@ -47,7 +49,7 @@ func (binder *basicBinder) CallWithData(f func(data binding.DataItem)) {

// SetCallback replaces the function to be called when the data changes.
func (binder *basicBinder) SetCallback(f func(data binding.DataItem)) {
binder.callback.Store(f)
binder.callback.Store(&f)
}

// Unbind requests the callback to be no longer called when the previously bound
Expand Down

0 comments on commit 4ecf18b

Please sign in to comment.