1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2026-07-14 08:17:26 +08:00
Files
go-mixed/maps/rwlock_map.go
T
Charlie c44a325384 fix: 修复多处并发安全问题及锁实现重构
locker: 导出 RWLocker/EmptyLocker 接口, 新增 WithRWLocker, 重构
  SourceLocker 竞态、Synchronize 数据竞态

collections/linked_list: 补齐 ForEach/Size/GetAt/RemoveAt/ToList
  等方法的锁保护
collections/queue,stack: Peek/Size/IsEmpty 加锁
collections/list/linked_list: Front/Back/GetAt 加锁
maps: rwlock_map.Exist/Count, hash_map.Count 加锁
delay_queue/mem_store: Pop 原子化、Len/IsEmpty 加锁
cleanup_guard: Run() 检查 enable 标志
sets/ketama: 修复接口变更导致的 nil 空指针
2026-06-05 10:45:08 +08:00

91 lines
1.4 KiB
Go

package maps
import (
"sync"
)
var _ Map[string, any] = &rw_map[string, any]{}
type rw_map[K hashable, V any] struct {
m Map[K, V]
mu sync.RWMutex
}
func NewRWMap[K hashable, V any](maps ...map[K]V) *rw_map[K, V] {
merged := Merge(maps...)
return &rw_map[K, V]{m: NewHashMap(merged)}
}
func newRWMap[K hashable, V any](m Map[K, V]) *rw_map[K, V] {
return &rw_map[K, V]{m: m}
}
func (m *rw_map[K, V]) Set(key K, value V) {
m.mu.Lock()
m.m.Set(key, value)
m.mu.Unlock()
}
func (m *rw_map[K, V]) Get(key K) (V, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.m.Get(key)
}
func (m *rw_map[K, V]) Delete(key K) {
m.mu.Lock()
defer m.mu.Unlock()
m.m.Delete(key)
}
func (m *rw_map[K, V]) Keys() []K {
m.mu.RLock()
defer m.mu.RUnlock()
return m.m.Keys()
}
func (m *rw_map[K, V]) Values() []V {
m.mu.RLock()
defer m.mu.RUnlock()
return m.m.Values()
}
func (m *rw_map[K, V]) ToMap() map[K]V {
m.mu.RLock()
defer m.mu.RUnlock()
return m.m.ToMap()
}
func (m *rw_map[K, V]) Shrink() map[K]V {
return m.m.ToMap()
}
func (m *rw_map[K, V]) Exist(key K) bool {
m.mu.RLock()
defer m.mu.RUnlock()
return m.m.Exist(key)
}
func (m *rw_map[K, V]) Count() int {
m.mu.RLock()
defer m.mu.RUnlock()
return m.m.Count()
}
// func (m *rw_map[K, V]) Iter() <-chan *Entry[K, V] {
// m.mu.RLock()
// defer m.mu.RUnlock()
// return m.m.Iter()
// }
func (m *rw_map[K, V]) ForEach(f func(K, V) bool) {
}