-
Notifications
You must be signed in to change notification settings - Fork 290
/
dirty.go
70 lines (57 loc) · 1.68 KB
/
dirty.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
package store
import (
"sync"
"time"
)
// A little synchronization primitive that comes up frequently in build systems,
// with three assumptions:
//
// 1) An event can come in at any time that marks the status as "dirty"
// (think: the user edits a file).
// 2) Builds can take a long time.
// 3) When the build finishes, we want to mark the status as "clean" iff
// nothing has changed since the build started.
//
// Don't use this primitive if you do synchronization at a higher
// level (as EngineState does), or need more granular dirtyness tracking
// (as EngineState also does, see PendingFileChanges). But EngineState
// uses a similar architecture internally.
type DirtyBit struct {
mu sync.Mutex
dirtyAsOf time.Time
}
func NewDirtyBit() *DirtyBit {
return &DirtyBit{}
}
// Mark the bit as dirty.
// If the change happens and this is marked dirty later, that's usually ok.
// It just means IsDirty might have false positives (i.e., we do spurious builds).
func (b *DirtyBit) MarkDirty() {
b.mu.Lock()
defer b.mu.Unlock()
b.dirtyAsOf = time.Now()
}
func (b *DirtyBit) IsDirty() bool {
b.mu.Lock()
defer b.mu.Unlock()
return !b.dirtyAsOf.IsZero()
}
// If the bit is currently marked dirty, returns a StartToken
// to pass to FinishBuild. Otherwise, return false.
func (b *DirtyBit) StartBuildIfDirty() (DirtyStartToken, bool) {
b.mu.Lock()
defer b.mu.Unlock()
if b.dirtyAsOf.IsZero() {
return DirtyStartToken{}, false
}
return DirtyStartToken(time.Now()), true
}
func (b *DirtyBit) FinishBuild(t DirtyStartToken) {
b.mu.Lock()
defer b.mu.Unlock()
if time.Time(b.dirtyAsOf).After(time.Time(t)) {
return
}
b.dirtyAsOf = time.Time{}
}
type DirtyStartToken time.Time