Skip to content

Commit

Permalink
prefer atomic.Pointer over atomic.Value
Browse files Browse the repository at this point in the history
  • Loading branch information
arnehormann committed Jul 20, 2024
1 parent 6fb7cd4 commit bf5a457
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions core_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
// There are two levels of concurrency here. First, the lock field in this struct must be held by any goroutine
// that is executing addPattern(), i.e. only one thread may be updating the state machine at one time.
// However, any number of goroutines may in parallel be executing matchesForFields while the addPattern
// update is in progress. The updateable atomic.Value allows the addPattern thread to change the maps and
// update is in progress. The updateable atomic.Pointer allows the addPattern thread to change the maps and
// slices in the structure atomically with atomic.Load() while matchesForFields threads are reading them.
type coreMatcher struct {
updateable atomic.Value // always holds a *coreFields
updateable atomic.Pointer[coreFields]
lock sync.Mutex
}

Expand All @@ -48,7 +48,7 @@ func newCoreMatcher() *coreMatcher {
}

func (m *coreMatcher) fields() *coreFields {
return m.updateable.Load().(*coreFields)
return m.updateable.Load()
}

// analyze traverses all the different per-field NFAs and gathers metadata that can be
Expand Down
4 changes: 2 additions & 2 deletions field_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// the fields that hold state are segregated in updateable, so they can be replaced atomically and make the coreMatcher
// thread-safe.
type fieldMatcher struct {
updateable atomic.Value // always holds an *fmFields
updateable atomic.Pointer[fmFields]
}

// fmFields contains the updateable fields in fieldMatcher.
Expand All @@ -28,7 +28,7 @@ type fmFields struct {
// fields / update / addExistsFalseFailure / addMatch exist to insulate callers from dealing with
// the atomic Load/Store business
func (m *fieldMatcher) fields() *fmFields {
return m.updateable.Load().(*fmFields)
return m.updateable.Load()
}

func (m *fieldMatcher) update(fields *fmFields) {
Expand Down
8 changes: 4 additions & 4 deletions value_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type bufpair struct {
// having a long chain of smallTables each with only one entry.
// To allow for concurrent access between one thread running AddPattern and many
// others running MatchesForEvent, the valueMatcher payload is stored in an
// atomic.Value
// atomic.Pointer
type valueMatcher struct {
updateable atomic.Value // always contains *vmFields
updateable atomic.Pointer[vmFields]
}
type vmFields struct {
startTable *smallTable
Expand All @@ -34,11 +34,11 @@ type vmFields struct {
}

func (m *valueMatcher) fields() *vmFields {
return m.updateable.Load().(*vmFields)
return m.updateable.Load()
}

func (m *valueMatcher) getFieldsForUpdate() *vmFields {
current := m.updateable.Load().(*vmFields)
current := m.updateable.Load()
freshState := *current // struct copy
return &freshState
}
Expand Down

0 comments on commit bf5a457

Please sign in to comment.