t.lock.Unlock()
}
-func (t *Map) Load(k interface{}) (v interface{}) {
+func (t *Map) Load(k interface{}) (interface{},bool) {
m,_ := t.readOnly.Load().(map[interface{}]*ptr)
p,ok := m[k]
}
if !ok{
- return nil
+ return nil,false
}
return p.tryLoad()
}
+func (t *Map) LoadV(k interface{}) (v interface{}) {
+ v,_ = t.Load(k)
+ return
+}
+
func (t *Map) Delete(k interface{}) {
m,_ := t.readOnly.Load().(map[interface{}]*ptr)
// atomic.StorePointer(&t.p, unsafe.Pointer(v))
}
-func (t *ptr) tryLoad() (interface{}) {
+func (t *ptr) tryLoad() (interface{},bool) {
// p := atomic.LoadPointer(&t.p)
if t.p == nil{
- return nil
+ return nil,false
}
- return *(*interface{})(t.p)
+ return *(*interface{})(t.p),true
}
type pLock struct{
var c Map
//set
c.Store(0, 3)
- if c.Load(0) != 3{t.Error(`1`)}
+ if v,ok := c.Load(0);ok && v != 3{t.Error(`1`)}
//change
c.Store(0, 1)
- if c.Load(0) != 1{t.Error(`2`)}
+ if v,ok := c.Load(0);ok && v != 1{t.Error(`2`)}
//del
c.Store(0, nil)
- if c.Load(0) != nil{t.Error(`3`)}
+ if v,ok := c.Load(0);ok && v != nil{t.Error(`3`)}
}
func Benchmark_customMap_Set(b *testing.B) {
b.ResetTimer()
for i := 0; i < t; i++ {
go func(index int) {
- if c.Load(index).(int) != index {
+ if c.LoadV(index).(int) != index {
b.Error("q")
}
w.Done()
w.Done()
}(i)
go func(index int) {
- if t,ok := c.Load(index).(int);!ok || t != index && t != index+1{
+ if t,ok := c.LoadV(index).(int);!ok || t != index && t != index+1{
b.Error(`E`, index, t)
}
w.Done()