Skip to content

Commit

Permalink
double checked locking
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Jul 24, 2023
1 parent 143dfc7 commit 7bc2a0b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,13 @@ func (sm *StateMachine) stateRepresentation(state State) *stateRepresentation {
sr, ok := sm.stateConfig[state]
sm.stateMutex.RUnlock()
if !ok {
sr = newstateRepresentation(state)
sm.stateMutex.Lock()
sm.stateConfig[state] = sr
sm.stateMutex.Unlock()
defer sm.stateMutex.Unlock()
// Check again, since another goroutine may have added it while we were waiting for the lock.
if sr, ok = sm.stateConfig[state]; !ok {
sr = newstateRepresentation(state)
sm.stateConfig[state] = sr
}
}
return sr
}
Expand Down

0 comments on commit 7bc2a0b

Please sign in to comment.