Skip to content

Commit

Permalink
app: protect out of touchIDs
Browse files Browse the repository at this point in the history
From practical observation, out of touchIDs may occur. This conflicts the maximum number of handled touchs in iOS devices. Therefore the only reason that may cause out of touchIDs is that the touchIDs are intrinsically non-concurrent safe. This change adds a read-write lock to the touchIDs array.

Fixes fyne-io/fyne#2407.
  • Loading branch information
changkun committed Aug 27, 2021
1 parent 24ca07c commit 9c97630
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/darwin_ios.go
Expand Up @@ -125,7 +125,12 @@ func updateConfig(width, height, orientation int32) {
//
// It is widely reported that the iPhone can handle up to 5 simultaneous
// touch events, while the iPad can handle 11.
var touchIDs [11]uintptr
var (
// touchIDs may arrive concurrently, use a lock for safety.
// See https://github.com/fyne-io/fyne/issues/2407.
touchMu sync.RWMutex
touchIDs [11]uintptr
)

var touchEvents struct {
sync.Mutex
Expand All @@ -135,12 +140,17 @@ var touchEvents struct {
//export sendTouch
func sendTouch(cTouch, cTouchType uintptr, x, y float32) {
id := -1

touchMu.RLock()
for i, val := range touchIDs {
if val == cTouch {
id = i
break
}
}
touchMu.RUnlock()

touchMu.Lock()
if id == -1 {
for i, val := range touchIDs {
if val == 0 {
Expand All @@ -158,6 +168,7 @@ func sendTouch(cTouch, cTouchType uintptr, x, y float32) {
if t == touch.TypeEnd {
touchIDs[id] = 0
}
touchMu.Unlock()

theApp.eventsIn <- touch.Event{
X: x,
Expand Down

0 comments on commit 9c97630

Please sign in to comment.