-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathma_concurrent.go
56 lines (46 loc) · 1.02 KB
/
ma_concurrent.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
package movingaverage
import "sync"
type ConcurrentMovingAverage struct {
ma *MovingAverage
mux sync.RWMutex
}
func Concurrent(ma *MovingAverage) *ConcurrentMovingAverage {
return &ConcurrentMovingAverage{
ma: ma,
}
}
func (c *ConcurrentMovingAverage) Add(values ...float64) {
c.mux.Lock()
c.ma.Add(values...)
c.mux.Unlock()
}
func (c *ConcurrentMovingAverage) Avg() float64 {
c.mux.RLock()
defer c.mux.RUnlock()
return c.ma.Avg()
}
func (c *ConcurrentMovingAverage) Min() (float64, error) {
c.mux.RLock()
defer c.mux.RUnlock()
return c.ma.Min()
}
func (c *ConcurrentMovingAverage) Max() (float64, error) {
c.mux.RLock()
defer c.mux.RUnlock()
return c.ma.Max()
}
func (c *ConcurrentMovingAverage) Count() int {
c.mux.RLock()
defer c.mux.RUnlock()
return c.ma.Count()
}
func (c *ConcurrentMovingAverage) Values() []float64 {
c.mux.RLock()
defer c.mux.RUnlock()
return c.ma.Values()
}
func (c *ConcurrentMovingAverage) SlotsFilled() bool {
c.mux.RLock()
defer c.mux.RUnlock()
return c.ma.SlotsFilled()
}