-
Notifications
You must be signed in to change notification settings - Fork 14
/
mu.go
57 lines (45 loc) · 873 Bytes
/
mu.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
package main
import (
"sync"
)
type ChatsWork struct {
m sync.Map
chat sync.Map
LenMu sync.Mutex
LockIncPlus sync.Mutex
StopTasks sync.Map
TorrentProcesses sync.Map
ChosenMessageIDs sync.Map
}
func (c *ChatsWork) IncPlus(messId int, chatId int64) {
c.LockIncPlus.Lock()
defer c.LockIncPlus.Unlock()
c.m.LoadOrStore(messId, c.Len())
c.chat.Store(chatId, true)
}
func (c *ChatsWork) IncMinus(messId int, chatId int64) {
c.LockIncPlus.Lock()
defer c.LockIncPlus.Unlock()
c.chat.Delete(chatId)
_, b := c.m.Load(messId)
if !b {
return
}
c.m.Delete(messId)
c.m.Range(func(messId, qq any) bool {
if qq != 0 {
c.m.Store(messId, qq.(int)-1)
}
return true
})
}
func (c *ChatsWork) Len() int {
c.LenMu.Lock()
defer c.LenMu.Unlock()
var lq int
c.m.Range(func(_, _ any) bool {
lq++
return true
})
return lq
}