/
session_process.go
89 lines (77 loc) · 1.65 KB
/
session_process.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package realm
const (
ProcKilled = 1 << iota
)
type Process struct {
Name string
CreationState SessionState
ProcState uint8
Data interface{}
Cancel chan<- bool
}
func (s *Session) FindProcess(name string) *Process {
for _, proc := range s.Processes {
if proc.Name == name {
return proc
}
}
return nil
}
// Creates a background function that will run until canceled, or until the session state changes to a lower value
func (s *Session) CreateProcess(name string, fn func(*Session, <-chan bool)) *Process {
s.GuardSession.Lock()
ch := make(chan bool)
proc := &Process{
Name: name,
Cancel: ch,
CreationState: s.state,
}
go func() {
fn(s, ch)
// If process returns without being killed,
if proc.ProcState&ProcKilled == 0 {
s.GuardSession.Lock()
for i, allProc := range s.Processes {
if proc == allProc {
s.killProcess(i)
break
}
}
s.GuardSession.Unlock()
}
}()
s.Processes = append(s.Processes, proc)
s.GuardSession.Unlock()
return proc
}
func (s *Session) killProcess(i int) {
proc := s.Processes[i]
proc.ProcState |= ProcKilled
proc.Cancel <- true
close(proc.Cancel)
s.Processes = append(s.Processes[:i], s.Processes[i+1:]...)
}
func (s *Session) KillAllProcesses() {
s.GuardSession.Lock()
for i := range s.Processes {
s.killProcess(i)
}
s.Processes = nil
s.GuardSession.Unlock()
}
func (s *Session) KillProcessesWithTag(tag string) {
s.GuardSession.Lock()
for {
var proc int = -1
for i := range s.Processes {
if s.Processes[i].Name == tag {
proc = i
}
}
if proc == -1 {
break
}
s.killProcess(proc)
}
s.GuardSession.Unlock()
}