proc.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
package main import "sync" type statusCode int32 const ( Stopped statusCode = iota // Process not running Running // Process was started and is very likely still running Stopping // Process is being stopped, but it might still be running ) type processState struct { lock sync.Mutex statusChange *sync.Cond status statusCode } func NewProcessState() *processState { p := &processState{} p.statusChange = sync.NewCond(&p.lock) return p } func (p *processState) Get() statusCode { p.lock.Lock() defer p.lock.Unlock() return p.status } func (p *processState) Set(status statusCode) { p.lock.Lock() defer p.lock.Unlock() if p.status == Stopped && status == Stopping { // Not a valid state transition. Ignore it. return } p.status = status p.statusChange.Broadcast() } func (p *processState) WaitTill(status statusCode) { p.lock.Lock() defer p.lock.Unlock() for p.status != status { p.statusChange.Wait() } }