-
Notifications
You must be signed in to change notification settings - Fork 0
/
self.go
50 lines (42 loc) · 886 Bytes
/
self.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
package fleet
import "sync"
var (
self *Agent
selfLk sync.RWMutex
selfCond = sync.NewCond(selfLk.RLocker())
)
// Self returns the Agent instance returned by New() (or similar), and will
// wait if instance has not been instanciated yet. As such, Self() should not
// be used in func init(), but only in separate goroutines or after instance
// has been created.
func Self() *Agent {
selfLk.RLock()
defer selfLk.RUnlock()
for {
if self != nil {
return self
}
selfCond.Wait()
}
}
func selfNoWait() *Agent {
selfLk.RLock()
defer selfLk.RUnlock()
return self
}
func setSelf(a *Agent) {
selfLk.Lock()
defer selfLk.Unlock()
if self == nil {
self = a
}
selfCond.Broadcast()
}
// IsReady returns true if the fleet is initiated and configured properly
func IsReady() bool {
a := selfNoWait()
if a == nil {
return false
}
return a.GetStatus() == 1
}