Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support concurrent state machine reads #62

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 17 additions & 13 deletions statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type StateMachine struct {
eventQueue list.List
firingMode FiringMode
firingMutex sync.Mutex
stateMutex sync.RWMutex
}

func newStateMachine() *StateMachine {
Expand Down Expand Up @@ -295,22 +296,25 @@ func (sm *StateMachine) setState(ctx context.Context, state State) error {
return sm.stateMutator(ctx, state)
}

func (sm *StateMachine) currentState(ctx context.Context) (sr *stateRepresentation, err error) {
var state State
state, err = sm.State(ctx)
if err == nil {
sr = sm.stateRepresentation(state)
func (sm *StateMachine) currentState(ctx context.Context) (*stateRepresentation, error) {
state, err := sm.State(ctx)
if err != nil {
return nil, err
}
return
return sm.stateRepresentation(state), nil
}

func (sm *StateMachine) stateRepresentation(state State) (sr *stateRepresentation) {
var ok bool
if sr, ok = sm.stateConfig[state]; !ok {
func (sm *StateMachine) stateRepresentation(state State) *stateRepresentation {
sm.stateMutex.RLock()
sr, ok := sm.stateConfig[state]
sm.stateMutex.RUnlock()
if !ok {
sr = newstateRepresentation(state)
sm.stateMutex.Lock()
sm.stateConfig[state] = sr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check whether state representation exists again.
like double checked locking

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done!

sm.stateMutex.Unlock()
}
return
return sr
}

func (sm *StateMachine) internalFire(ctx context.Context, trigger Trigger, args ...any) error {
Expand Down Expand Up @@ -354,7 +358,7 @@ func (sm *StateMachine) internalFireQueued(ctx context.Context, trigger Trigger,
return nil
}

func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, args ...any) (err error) {
func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, args ...any) error {
sm.ops.Add(1)
defer sm.ops.Add(^uint64(0))
var (
Expand All @@ -366,7 +370,7 @@ func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, ar
}
source, err := sm.State(ctx)
if err != nil {
return
return err
}
representativeState := sm.stateRepresentation(source)
var result triggerBehaviourResult
Expand Down Expand Up @@ -397,7 +401,7 @@ func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, ar
err = sr.InternalAction(ctx, transition, args...)
}
}
return
return err
}

func (sm *StateMachine) handleReentryTrigger(ctx context.Context, sr *stateRepresentation, transition Transition, args ...any) error {
Expand Down
15 changes: 15 additions & 0 deletions statemachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,21 @@ func TestStateMachine_String(t *testing.T) {
}
}

func TestStateMachine_String_Concurrent(t *testing.T) {
// Test that race mode doesn't complain about concurrent access to the state machine.
sm := NewStateMachine(stateA)
const n = 10
var wg sync.WaitGroup
wg.Add(n)
for i := 0; i < n; i++ {
go func() {
defer wg.Done()
_ = sm.String()
}()
}
wg.Wait()
}

func TestStateMachine_Firing_Queued(t *testing.T) {
sm := NewStateMachine(stateA)

Expand Down