Skip to content

Commit

Permalink
Merge pull request #3313 from jcking/go-runtime-thread-safety
Browse files Browse the repository at this point in the history
Use atomic instead of a shared mutex for DFA.s0
  • Loading branch information
parrt authored Dec 28, 2021
2 parents 13ba87e + f93cb83 commit 8fabd86
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions runtime/Go/antlr/dfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package antlr
import (
"sort"
"sync"
"sync/atomic"
"unsafe"
)

type DFA struct {
Expand All @@ -17,15 +19,14 @@ type DFA struct {

// states is all the DFA states. Use Map to get the old state back; Set can only
// indicate whether it is there.
states map[int]*DFAState
states map[int]*DFAState
statesMu sync.RWMutex

s0 *DFAState
s0Mu sync.RWMutex

// precedenceDfa is the backing field for isPrecedenceDfa and setPrecedenceDfa.
// True if the DFA is for a precedence decision and false otherwise.
precedenceDfa bool
precedenceDfa bool
precedenceDfaMu sync.RWMutex
}

Expand Down Expand Up @@ -111,15 +112,11 @@ func (d *DFA) setPrecedenceDfa(precedenceDfa bool) {
}

func (d *DFA) getS0() *DFAState {
d.s0Mu.RLock()
defer d.s0Mu.RUnlock()
return d.s0
return (*DFAState)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.s0))))
}

func (d *DFA) setS0(s *DFAState) {
d.s0Mu.Lock()
defer d.s0Mu.Unlock()
d.s0 = s
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&d.s0)), unsafe.Pointer(s))
}

func (d *DFA) getState(hash int) (*DFAState, bool) {
Expand Down

0 comments on commit 8fabd86

Please sign in to comment.