@@ -46,7 +46,7 @@ type Tasker struct {
46
46
Log * log.Logger
47
47
exprs map [string ][]string
48
48
tasks map [string ]TaskFunc
49
- mutex map [string ]uint32
49
+ mutex map [string ]* uint32
50
50
ctxCancel context.CancelFunc
51
51
wg sync.WaitGroup
52
52
verbose bool
@@ -198,9 +198,9 @@ func (t *Tasker) Task(expr string, task TaskFunc, concurrent ...bool) *Tasker {
198
198
199
199
if ! concurrent [0 ] {
200
200
if len (t .mutex ) == 0 {
201
- t .mutex = map [string ]uint32 {}
201
+ t .mutex = make ( map [string ]* uint32 )
202
202
}
203
- t .mutex [ref ] = 0
203
+ t .mutex [ref ] = new ( uint32 )
204
204
}
205
205
206
206
return t
@@ -378,14 +378,7 @@ func (t *Tasker) runTasks(tasks map[string]TaskFunc) {
378
378
379
379
func (t * Tasker ) canRun (ref string ) bool {
380
380
lock , ok := t .mutex [ref ]
381
- if ! ok {
382
- return true
383
- }
384
- if atomic .CompareAndSwapUint32 (& lock , 0 , 1 ) {
385
- t .mutex [ref ] = 1
386
- return true
387
- }
388
- return false
381
+ return ! ok || atomic .CompareAndSwapUint32 (lock , 0 , 1 )
389
382
}
390
383
391
384
func (t * Tasker ) doRun (ctx context.Context , ref string , task TaskFunc , rc chan result ) {
@@ -400,8 +393,7 @@ func (t *Tasker) doRun(ctx context.Context, ref string, task TaskFunc, rc chan r
400
393
401
394
code , err := task (ctx )
402
395
if lock , ok := t .mutex [ref ]; ok {
403
- atomic .StoreUint32 (& lock , 0 )
404
- t .mutex [ref ] = 0
396
+ atomic .StoreUint32 (lock , 0 )
405
397
}
406
398
407
399
rc <- result {err , ref , code }
0 commit comments