-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscontrol.go
63 lines (53 loc) · 1.41 KB
/
scontrol.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
package scontrol
import "sync"
// Status is possible status of controller
type Status int
const (
StatusRunning Status = 0 // StatusRunning is normal status
StatusPause Status = 1 // StatusPause will block any Check() until it change
StatusStop Status = 2 // StatusStop is stop status, routine should exit after received this
)
// Controller provide cross routine control.
// DO NOT copy it!
type Controller struct {
s Status // status now
locker *sync.RWMutex // protect s
wg *sync.WaitGroup // act as Semaphore
}
// New return a new Controller
func New() *Controller {
return &Controller{
s: StatusRunning,
wg: &sync.WaitGroup{},
locker: &sync.RWMutex{},
}
}
// Set controller to a new status
func (c *Controller) Set(s Status) {
c.locker.Lock()
defer c.locker.Unlock()
// Set Value
old := c.s
c.s = s
// Set WaitGroup/Semaphore
if old != StatusPause && s == StatusPause {
c.wg.Add(1)
} else if old == StatusPause && s != StatusPause {
c.wg.Done()
}
}
// Check returns status now; if status is StatusPause, it will block until it change.
func (c *Controller) Check() Status {
c.wg.Wait() // if Status is not Pause, it will return immediately
c.locker.RLock()
defer c.locker.RUnlock()
tmp := c.s
return tmp
}
// Get return status immediately; no blocking.
func (c *Controller) Get() Status {
c.locker.RLock()
defer c.locker.RUnlock()
tmp := c.s
return tmp
}