-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.go
More file actions
145 lines (115 loc) · 2.9 KB
/
thread.go
File metadata and controls
145 lines (115 loc) · 2.9 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package goglib
import (
"log"
"sort"
"time"
)
// ------------------------------------------------------------------------------
// Constant
// ------------------------------------------------------------------------------
const (
THR_START_ID = 1
THR_MAX_ID = 10000
)
const def_thr_check_interval = 1000 // 1sec
// ID Pool
var THR_ID_POOL []int = []int{}
type ThreadFunc func(t *Thread, ch chan bool, arg1, arg2, arg3 interface{})
type Thread struct {
Interval int // millisecond
Arg1 interface{}
Arg2 interface{}
Arg3 interface{}
ChKill chan bool
StartFunc ThreadFunc
Timer time.Time // Thread Check timer
RunBase // Run Base
}
func checkKillChannel(ch chan bool) {
if len(ch) >= 1 {
for len(ch) > 0 {
<-ch
}
}
}
func getThreadID() int {
id := THR_START_ID
for idx := 1; idx < len(THR_ID_POOL); idx++ {
prevId := THR_ID_POOL[idx-1]
curId := THR_ID_POOL[idx]
if (prevId + 1) == curId {
if len(THR_ID_POOL)-1 == idx {
// 마지막 ID값 도달시
id = curId + 1
}
continue
}
if (curId - prevId) > 1 {
id = prevId + 1
break
}
}
return id
}
func NewThread() *Thread {
var t Thread = Thread{}
t.RunBase.ID = getThreadID()
sort.Ints(THR_ID_POOL)
// 여기서 threadID 발급
return &t
}
func (t *Thread) Init(f ThreadFunc, interval int, args ...interface{}) {
t.StartFunc = f
t.Interval = interval
t.ChKill = make(chan bool, 1)
for idx, arg := range args {
if idx > 2 {
break
}
if idx == 0 {
t.Arg1 = arg
} else if idx == 1 {
t.Arg2 = arg
} else if idx == 2 {
t.Arg3 = arg
}
}
}
func (t *Thread) Start() {
t.RunBase.Active = true
time.Sleep(time.Millisecond * 50)
go t.StartFunc(t, t.ChKill, t.Arg1, t.Arg2, t.Arg3)
}
func (t *Thread) Kill() {
// 체크 채널
// 체널 버퍼 1, 체널에 데이터가 있는지 검사
checkKillChannel(t.ChKill)
// send kill command
t.RunBase.Active = false
t.ChKill <- true
}
func (t *Thread) IsRunning(state *int) bool {
*state = RST_OK
if !CheckElapsedTime(&t.Timer, def_thr_check_interval) {
return true
}
// thread 실행 상태 확인
if !t.RunBase.checkRunInfo() {
log.Println("[IsRunning] CheckRunInfo Abnomal : ", *state)
*state = RST_ABNOMAL
return false
}
return true
}
// ------------------------------------------------------------------------------
// threadRegister (routine)
// ------------------------------------------------------------------------------
func (t *Thread) threadRegister(id int64) {
t.RunBase.threadRegister(id)
}
// ------------------------------------------------------------------------------
// threadDeregister (routine)
// ------------------------------------------------------------------------------
func (t *Thread) threadDeregister(id int64) {
t.RunBase.threadDeregister(id)
}