-
Notifications
You must be signed in to change notification settings - Fork 0
/
routine.go
51 lines (42 loc) · 847 Bytes
/
routine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package xputil
import (
"sync/atomic"
)
type Routine interface {
Setup(string, *RtManager) error
Parent() *RtManager
IsAlive() bool
Start()
Stop()
Kill()
}
type BaseRoutine struct {
*BaseTasklet
Name string
parent *RtManager
alive atomic.Bool
}
func NewBaseRoutine(tsk Tasklet) *BaseRoutine {
return &BaseRoutine{
BaseTasklet: NewBaseTasklet(nil, tsk),
}
}
func (rt *BaseRoutine) Setup(name string, rm *RtManager) error {
rt.Name = name
rt.parent = rm
rt.Log = rm.Log.ChildLogger(name)
return nil
}
func (rt *BaseRoutine) Parent() *RtManager {
return rt.parent
}
func (rt *BaseRoutine) IsAlive() bool {
return rt.alive.Load()
}
func (rt *BaseRoutine) Start() {
// set alive status flag and clear when exit
rt.alive.Store(true)
defer rt.alive.Store(false)
// start the tasklet manager
rt.BaseTasklet.Start()
}