-
Notifications
You must be signed in to change notification settings - Fork 3
/
atomic.go
72 lines (61 loc) · 1.34 KB
/
atomic.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
package gott
import "sync"
type atomicBool struct {
val bool
mutex sync.Mutex
}
func (ab *atomicBool) Load() bool {
ab.mutex.Lock()
defer ab.mutex.Unlock()
return ab.val
}
func (ab *atomicBool) Store(val bool) {
ab.mutex.Lock()
defer ab.mutex.Unlock()
ab.val = val
}
type subscriptionList struct {
subs []*subscription
mutex sync.Mutex
}
func (s *subscriptionList) delete(index int) {
var newSubs []*subscription
newSubs = append(newSubs, s.subs[:index]...)
if index != len(s.subs)-1 {
newSubs = append(newSubs, s.subs[index+1:]...)
}
s.subs = newSubs
}
func (s *subscriptionList) Len() int {
s.mutex.Lock()
defer s.mutex.Unlock()
return len(s.subs)
}
func (s *subscriptionList) Add(sub *subscription) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.subs = append(s.subs, sub)
}
func (s *subscriptionList) Delete(index int) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.delete(index)
}
func (s *subscriptionList) Range(iterator func(i int, sub *subscription) bool) {
s.mutex.Lock()
defer s.mutex.Unlock()
for i, sub := range s.subs {
if next := iterator(i, sub); !next {
break
}
}
}
func (s *subscriptionList) RangeDelete(iterator func(i int, sub *subscription, delete func(index int)) bool) {
s.mutex.Lock()
defer s.mutex.Unlock()
for i, sub := range s.subs {
if next := iterator(i, sub, s.delete); !next {
break
}
}
}