From 0da5a032a5ab7fac8925eb22a52512c6b908c743 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Thu, 2 Feb 2023 20:42:14 +0100 Subject: [PATCH 1/2] use atomic.Uint64 for ops --- go.mod | 2 +- statemachine.go | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 9f7f4e9..f92baae 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/qmuntal/stateless -go 1.17 +go 1.19 require github.com/stretchr/testify v1.8.1 diff --git a/statemachine.go b/statemachine.go index 15b6c1b..c54aa29 100644 --- a/statemachine.go +++ b/statemachine.go @@ -64,8 +64,7 @@ func callEvents(events []TransitionFunc, ctx context.Context, transition Transit // It is safe to use the StateMachine concurrently, but non of the callbacks (state manipulation, actions, events, ...) are guarded, // so it is up to the client to protect them against race conditions. type StateMachine struct { - // ops is accessed atomically so we put it at the beginning of the struct to achieve 64 bit alignment - ops uint64 + ops atomic.Uint64 stateConfig map[State]*stateRepresentation triggerConfig map[Trigger]triggerWithParameters stateAccessor func(context.Context) (State, error) @@ -276,7 +275,7 @@ func (sm *StateMachine) Configure(state State) *StateConfiguration { // Firing returns true when the state machine is processing a trigger. func (sm *StateMachine) Firing() bool { - return atomic.LoadUint64(&sm.ops) != 0 + return sm.ops.Load() != 0 } // String returns a human-readable representation of the state machine. @@ -356,8 +355,8 @@ func (sm *StateMachine) internalFireQueued(ctx context.Context, trigger Trigger, } func (sm *StateMachine) internalFireOne(ctx context.Context, trigger Trigger, args ...interface{}) (err error) { - atomic.AddUint64(&sm.ops, 1) - defer atomic.AddUint64(&sm.ops, ^uint64(0)) + sm.ops.Add(1) + defer sm.ops.Add(^uint64(0)) var ( config triggerWithParameters ok bool From 9a8022e813af116c1444a94a73294b17fa9d53da Mon Sep 17 00:00:00 2001 From: qmuntal Date: Fri, 3 Feb 2023 17:42:17 +0100 Subject: [PATCH 2/2] bump github actions go versions --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bfca4f2..96ac334 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [1.16.x, 1.17.x, 1.18.x] + go-version: [1.19.x, 1.20.x] platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: