1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -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,7 +42,8 @@ func (queue *timedQueue) Pop() interface{} {
type TimedStringMap struct { type TimedStringMap struct {
timedQueue timedQueue
access sync.RWMutex queueMutex sync.Mutex
dataMutext sync.RWMutex
data map[string]interface{} data map[string]interface{}
interval int interval int
} }
@ -50,7 +51,8 @@ type TimedStringMap struct {
func NewTimedStringMap(updateInterval int) *TimedStringMap { func NewTimedStringMap(updateInterval int) *TimedStringMap {
m := &TimedStringMap{ m := &TimedStringMap{
timedQueue: make([]*timedQueueEntry, 0, 1024), timedQueue: make([]*timedQueueEntry, 0, 1024),
access: sync.RWMutex{}, queueMutex: sync.Mutex{},
dataMutext: sync.RWMutex{},
data: make(map[string]interface{}, 1024), data: make(map[string]interface{}, 1024),
interval: updateInterval, interval: updateInterval,
} }
@ -74,33 +76,36 @@ func (m *TimedStringMap) cleanup(tick <-chan time.Time) {
if entry.timeSec > nowSec { if entry.timeSec > nowSec {
break break
} }
m.access.Lock() m.queueMutex.Lock()
entry = heap.Pop(&m.timedQueue).(*timedQueueEntry) entry = heap.Pop(&m.timedQueue).(*timedQueueEntry)
m.access.Unlock() m.queueMutex.Unlock()
m.Remove(entry.value.(string)) m.Remove(entry.value.(string))
} }
} }
} }
func (m *TimedStringMap) Get(key string) (interface{}, bool) { func (m *TimedStringMap) Get(key string) (interface{}, bool) {
m.access.RLock() m.dataMutext.RLock()
value, ok := m.data[key] value, ok := m.data[key]
m.access.RUnlock() m.dataMutext.RUnlock()
return value, ok return value, ok
} }
func (m *TimedStringMap) Set(key string, value interface{}, time2Delete int64) { func (m *TimedStringMap) Set(key string, value interface{}, time2Delete int64) {
m.access.Lock() m.dataMutext.Lock()
m.data[key] = value m.data[key] = value
m.dataMutext.Unlock()
m.queueMutex.Lock()
heap.Push(&m.timedQueue, &timedQueueEntry{ heap.Push(&m.timedQueue, &timedQueueEntry{
timeSec: time2Delete, timeSec: time2Delete,
value: key, value: key,
}) })
m.access.Unlock() m.queueMutex.Unlock()
} }
func (m *TimedStringMap) Remove(key string) { func (m *TimedStringMap) Remove(key string) {
m.access.Lock() m.dataMutext.Lock()
delete(m.data, key) delete(m.data, key)
m.access.Unlock() m.dataMutext.Unlock()
} }