-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoroutine.go
65 lines (51 loc) · 871 Bytes
/
goroutine.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pooli
import (
"context"
"sync"
)
type Goroutine struct {
status Status
Pipe chan Task
ctx context.Context
cnl context.CancelFunc
m *sync.RWMutex
}
func NewGoroutine(ctx context.Context, pipe chan Task) *Goroutine {
ctx, cnl := context.WithCancel(ctx)
return &Goroutine{
status: Idle,
Pipe: pipe,
ctx: ctx,
cnl: cnl,
m: new(sync.RWMutex),
}
}
func (g *Goroutine) Start() {
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
wg.Done()
for {
select {
case <-g.ctx.Done():
return
case t := <-g.Pipe:
g.SetStatus(Progress)
ExecuteTask(g.ctx, t)
g.SetStatus(Idle)
}
}
}()
wg.Wait()
}
func (g *Goroutine) SetStatus(status Status) {
g.m.Lock()
defer g.m.Unlock()
g.status = status
}
func (g Goroutine) Status() Status {
return g.status
}
func (g *Goroutine) Kill() {
g.cnl()
}