From 03d9784bf682b05e3c4a46eb6cd32f4319e8636b Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 29 Jul 2023 21:27:47 +0800 Subject: Make status field concurrent safe --- recording/task.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'recording/task.go') diff --git a/recording/task.go b/recording/task.go index d1f9c79..328fe66 100644 --- a/recording/task.go +++ b/recording/task.go @@ -14,7 +14,7 @@ import ( "time" ) -type TaskStatus int +type TaskStatus uint32 const ( StNotStarted TaskStatus = iota @@ -48,7 +48,7 @@ type RunningTask struct { // ctx: the biggest context this task uses. It may create children contexts. ctx context.Context // status: running status - status TaskStatus + status atomic.Uint32 // hookStarted: called asynchronously when the task is started. This won't be called when restarting. hookStarted func() // hookStopped: called asynchronously when the task is stopped. This won't be called when restarting. @@ -58,8 +58,12 @@ type RunningTask struct { roomTitle atomic.Pointer[string] } +func (t *RunningTask) setStatus(status TaskStatus) { + t.status.Store(uint32(status)) +} + func (t *RunningTask) GetStatus() TaskStatus { - return t.status + return TaskStatus(t.status.Load()) } func (t *RunningTask) GetRoomTitle() *string { @@ -73,23 +77,24 @@ func NewRunningTask( hookStopped func(), logger logging.Logger, ) *RunningTask { - return &RunningTask{ + t := &RunningTask{ TaskConfig: config, ctx: ctx, - status: StNotStarted, hookStarted: hookStarted, hookStopped: hookStopped, logger: logger, } + t.setStatus(StNotStarted) + return t } func (t *RunningTask) StartTask() error { - st := t.status + st := t.GetStatus() switch st { case StNotStarted: // TODO real start go func() { - defer func() { t.status = StStopped }() + defer func() { t.setStatus(StStopped) }() t.hookStarted() defer t.hookStopped() // do the task -- cgit v1.2.3