File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change 4
4
"context"
5
5
"fmt"
6
6
"sync"
7
+ "sync/atomic"
7
8
"time"
8
9
9
10
"github.com/btcsuite/btcd/btcec/v2"
@@ -150,8 +151,14 @@ type StateMachine[Event any, Env Environment] struct {
150
151
gm fn.GoroutineManager
151
152
quit chan struct {}
152
153
154
+ // startOnce and stopOnce are used to ensure that the state machine is
155
+ // only started and stopped once.
153
156
startOnce sync.Once
154
157
stopOnce sync.Once
158
+
159
+ // running is a flag that indicates if the state machine is currently
160
+ // running.
161
+ running atomic.Bool
155
162
}
156
163
157
164
// ErrorReporter is an interface that's used to report errors that occur during
@@ -221,6 +228,8 @@ func (s *StateMachine[Event, Env]) Start(ctx context.Context) {
221
228
_ = s .gm .Go (ctx , func (ctx context.Context ) {
222
229
s .driveMachine (ctx )
223
230
})
231
+
232
+ s .running .Store (true )
224
233
})
225
234
}
226
235
@@ -230,6 +239,8 @@ func (s *StateMachine[Event, Env]) Stop() {
230
239
s .stopOnce .Do (func () {
231
240
close (s .quit )
232
241
s .gm .Stop ()
242
+
243
+ s .running .Store (false )
233
244
})
234
245
}
235
246
@@ -333,6 +344,11 @@ func (s *StateMachine[Event, Env]) RemoveStateSub(sub StateSubscriber[
333
344
_ = s .newStateEvents .RemoveSubscriber (sub )
334
345
}
335
346
347
+ // IsRunning returns true if the state machine is currently running.
348
+ func (s * StateMachine [Event , Env ]) IsRunning () bool {
349
+ return s .running .Load ()
350
+ }
351
+
336
352
// executeDaemonEvent executes a daemon event, which is a special type of event
337
353
// that can be emitted as part of the state transition function of the state
338
354
// machine. An error is returned if the type of event is unknown.
You can’t perform that action at this time.
0 commit comments