Skip to content

Commit

Permalink
Merge pull request #920 from gucio321/fix-racism
Browse files Browse the repository at this point in the history
context: use sync.Map for widget counter
  • Loading branch information
gucio321 authored Nov 27, 2024
2 parents 9fe305a + cad1b8d commit 4536adc
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ 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, ok = idxAny.(int)
Assert(ok, "Context", "GenAutoID", "unexpected type of widgetIndex value: expected int, instead found %T", idxAny)

idx++
}

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

return ID(fmt.Sprintf("%s##%d", id, idx))
}
Expand Down Expand Up @@ -51,7 +55,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 +87,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 +143,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 4536adc

Please sign in to comment.