-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnstart.go
72 lines (64 loc) · 1.17 KB
/
nstart.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 coap
import "sync"
type nstart struct {
count int
cond *sync.Cond
mux sync.Mutex
}
var nstartMap = map[string]*nstart{}
var nstartMux sync.Mutex
func nstartInc(addr string, nStart int) {
if nStart > 0 {
nstartMux.Lock()
if s, found := nstartMap[addr]; found {
nstartMux.Unlock()
s.cond.L.Lock()
for s.count >= nStart {
s.cond.Wait()
}
s.count++
s.cond.L.Unlock()
} else {
ns := &nstart{count: 1}
ns.cond = sync.NewCond(&ns.mux)
nstartMap[addr] = ns
nstartMux.Unlock()
}
}
}
func nstartCount(addr string, nStart int) int {
nstartMux.Lock()
if s, found := nstartMap[addr]; found {
nstartMux.Unlock()
if s.count > nStart {
return s.count - nStart
} else {
return 0
}
}
nstartMux.Unlock()
return -1
}
func nstartDec(addr string) {
nstartMux.Lock()
if s, found := nstartMap[addr]; found {
nstartMux.Unlock()
s.cond.L.Lock()
s.count--
s.cond.Signal()
s.cond.L.Unlock()
} else {
nstartMux.Unlock()
}
}
func NstartClear(addr string) {
nstartMux.Lock()
delete(nstartMap, addr)
nstartMux.Unlock()
}
func NstartEntryCount() int {
nstartMux.Lock()
l := len(nstartMap)
nstartMux.Unlock()
return l
}