-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.go
More file actions
73 lines (59 loc) · 1.07 KB
/
thread.go
File metadata and controls
73 lines (59 loc) · 1.07 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package seed
import (
"context"
"errors"
"time"
"go.uber.org/atomic"
)
const TimeOutLimit = 5 * time.Second
// PushFunc ...
type PushFunc func(interface{}) error
// Thread ...
type Thread struct {
Seeder
push PushFunc
state *atomic.Int32
done chan bool
}
// Finished ...
func (t *Thread) Finished() {
t.SetState(StateStop)
t.done <- true
}
// Run ...
func (t *Thread) Run(context.Context) {
panic("implement me")
}
// SetState ...
func (t *Thread) SetState(state State) {
t.state.Store(int32(state))
}
// Push ...
func (t *Thread) Push(v interface{}) error {
if t.push != nil {
return t.push(v)
}
return errors.New("null push function")
}
// BeforeRun ...
func (t *Thread) BeforeRun(seed Seeder) {
t.Seeder = seed
}
// AfterRun ...
func (t *Thread) AfterRun(seed Seeder) {
}
// State ...
func (t *Thread) State() State {
return State(t.state.Load())
}
// Done ...
func (t *Thread) Done() <-chan bool {
return t.done
}
// NewThread ...
func NewThread() *Thread {
return &Thread{
state: atomic.NewInt32(int32(StateRunning)),
done: make(chan bool),
}
}