1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

Update mutex usage in timed_map

This commit is contained in:
V2Ray 2015-09-23 21:31:06 +02:00
parent 70ad3d0174
commit 5604ff175b

View File

@ -42,15 +42,17 @@ func (queue *timedQueue) Pop() interface{} {
type TimedStringMap struct {
timedQueue
access sync.RWMutex
data map[string]interface{}
interval int
queueMutex sync.Mutex
dataMutext sync.RWMutex
data map[string]interface{}
interval int
}
func NewTimedStringMap(updateInterval int) *TimedStringMap {
m := &TimedStringMap{
timedQueue: make([]*timedQueueEntry, 0, 1024),
access: sync.RWMutex{},
queueMutex: sync.Mutex{},
dataMutext: sync.RWMutex{},
data: make(map[string]interface{}, 1024),
interval: updateInterval,
}
@ -74,33 +76,36 @@ func (m *TimedStringMap) cleanup(tick <-chan time.Time) {
if entry.timeSec > nowSec {
break
}
m.access.Lock()
m.queueMutex.Lock()
entry = heap.Pop(&m.timedQueue).(*timedQueueEntry)
m.access.Unlock()
m.queueMutex.Unlock()
m.Remove(entry.value.(string))
}
}
}
func (m *TimedStringMap) Get(key string) (interface{}, bool) {
m.access.RLock()
m.dataMutext.RLock()
value, ok := m.data[key]
m.access.RUnlock()
m.dataMutext.RUnlock()
return value, ok
}
func (m *TimedStringMap) Set(key string, value interface{}, time2Delete int64) {
m.access.Lock()
m.dataMutext.Lock()
m.data[key] = value
m.dataMutext.Unlock()
m.queueMutex.Lock()
heap.Push(&m.timedQueue, &timedQueueEntry{
timeSec: time2Delete,
value: key,
})
m.access.Unlock()
m.queueMutex.Unlock()
}
func (m *TimedStringMap) Remove(key string) {
m.access.Lock()
m.dataMutext.Lock()
delete(m.data, key)
m.access.Unlock()
m.dataMutext.Unlock()
}