Repository navigation
Expand file tree
/
Copy pathprocess.go
More file actions
88 lines (74 loc) · 1.99 KB
/
Copy pathprocess.go
File metadata and controls
88 lines (74 loc) · 1.99 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
package neffos
import (
"sync"
)
// processes is a collection of `process`.
type processes struct {
entries map[string]*process
mu sync.Mutex
}
func newProcesses() *processes {
return &processes{
entries: make(map[string]*process),
}
}
func (p *processes) get(name string) *process {
p.mu.Lock()
defer p.mu.Unlock()
entry, ok := p.entries[name]
if !ok {
entry = new(process)
p.entries[name] = entry
}
return entry
}
// process is used on connections on specific actions that needs to wait for an answer from the other side.
// Take for example the `Conn#handleMessage.tryNamespace` which waits for `Conn#askConnect` to finish on the specific namespace.
//
// A process is restartable: Start begins a run, Done ends it and releases any
// waiters, and Start may be called again afterwards to begin a new run. Wait
// blocks only while a run is in progress; it returns immediately otherwise,
// including for a process that was never started.
type process struct {
mu sync.Mutex
running bool
done chan struct{}
}
// Start marks the process as running, so that `Wait` blocks until the matching `Done`.
// Safe to call again after a previous `Done` to begin a new run.
func (p *process) Start() {
p.mu.Lock()
p.running = true
p.done = make(chan struct{})
p.mu.Unlock()
}
// Done ends the current run and releases any goroutines blocked in `Wait`.
// It is a no-op if the process isn't currently running (double `Done`, or
// `Done` without a prior `Start`).
func (p *process) Done() {
p.mu.Lock()
if !p.running {
p.mu.Unlock()
return
}
p.running = false
done := p.done
p.mu.Unlock()
close(done)
}
// Wait blocks until `Done` is called, but only while the process is running;
// otherwise it returns immediately.
func (p *process) Wait() {
p.mu.Lock()
running, done := p.running, p.done
p.mu.Unlock()
if running {
<-done
}
}
// isDone reports whether the process is not currently running.
func (p *process) isDone() bool {
p.mu.Lock()
defer p.mu.Unlock()
return !p.running
}