Skip to content

Commit

Permalink
context: use sync.Map for widget counter
Browse files Browse the repository at this point in the history
while work on HellSpawner, turns out that there is a data race
on this file
  • Loading branch information
gucio321 committed Nov 27, 2024
1 parent 9fe305a commit fa5e94f
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import (
// GenAutoID automatically generates widget's ID.
// It returns an unique value each time it is called.
func GenAutoID(id string) ID {
idx, ok := Context.widgetIndex[id]
idx := int(0)
idxAny, ok := Context.widgetIndex.Load(id)

if ok {
idx = idxAny.(int)

Check failure on line 20 in Context.go

View workflow job for this annotation

GitHub Actions / Lint

type assertion must be checked (forcetypeassert)
idx++
}

Context.widgetIndex[id] = idx
Context.widgetIndex.Store(id, idx)

return ID(fmt.Sprintf("%s##%d", id, idx))
}
Expand Down Expand Up @@ -51,7 +53,7 @@ type GIUContext struct {

isRunning bool

widgetIndex map[string]int
widgetIndex sync.Map

// Indicate whether current application is running
isAlive bool
Expand Down Expand Up @@ -83,7 +85,6 @@ func CreateContext(b backend.Backend[glfwbackend.GLFWWindowFlags]) *GIUContext {
textureLoadingQueue: queue.New(),
textureFreeingQueue: queue.New(),
m: &sync.Mutex{},
widgetIndex: make(map[string]int),
}

// Create font
Expand Down Expand Up @@ -140,7 +141,7 @@ func (c *GIUContext) cleanStates() {
return true
})

c.widgetIndex = make(map[string]int)
c.widgetIndex.Clear()
c.dirty = false
}

Expand Down

0 comments on commit fa5e94f

Please sign in to comment.