func(m *Map)Load(key interface{})(value interface{}, ok bool){
read,_:= m.read.Load().(readOnly)
e, ok := read.m[key]if!ok && read.amended {
m.mu.Lock()// Avoid reporting a spurious miss if m.dirty got promoted while we were// blocked on m.mu. (If further loads of the same key will not miss, it's// not worth copying the dirty map for this key.)
read,_= m.read.Load().(readOnly)
e, ok = read.m[key]if!ok && read.amended {
e, ok = m.dirty[key]// Regardless of whether the entry was present, record a miss: this key// will take the slow path until the dirty map is promoted to the read// map.
m.missLocked()}
m.mu.Unlock()}if!ok {returnnil,false}return e.load()}func(e *entry)load()(value interface{}, ok bool){
p := atomic.LoadPointer(&e.p)if p ==nil|| p == expunged {returnnil,false}return*(*interface{})(p),true}// 如果在只读字典中未命中的次数和脏字典长度相同,那么就把脏字典刷到只读字典中,然后脏字典设置为nil并重新计数func(m *Map)missLocked(){
m.misses++if m.misses <len(m.dirty){return}
m.read.Store(readOnly{m: m.dirty})
m.dirty =nil
m.misses =0}
func(m *Map)Store(key, value interface{}){
read,_:= m.read.Load().(readOnly)if e, ok := read.m[key]; ok && e.tryStore(&value){return}
m.mu.Lock()
read,_= m.read.Load().(readOnly)if e, ok := read.m[key]; ok {if e.unexpungeLocked(){// The entry was previously expunged, which implies that there is a// non-nil dirty map and this entry is not in it.
m.dirty[key]= e
}
e.storeLocked(&value)}elseif e, ok := m.dirty[key]; ok {
e.storeLocked(&value)}else{if!read.amended {// We're adding the first new key to the dirty map.// Make sure it is allocated and mark the read-only map as incomplete.
m.dirtyLocked()
m.read.Store(readOnly{m: read.m, amended:true})}
m.dirty[key]=newEntry(value)}
m.mu.Unlock()}// tryStore stores a value if the entry has not been expunged.//// If the entry is expunged, tryStore returns false and leaves the entry// unchanged.func(e *entry)tryStore(i *interface{})bool{for{
p := atomic.LoadPointer(&e.p)if p == expunged {returnfalse}if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)){returntrue}}}
// Delete deletes the value for a key.func(m *Map)Delete(key interface{}){
read,_:= m.read.Load().(readOnly)
e, ok := read.m[key]if!ok && read.amended {
m.mu.Lock()
read,_= m.read.Load().(readOnly)
e, ok = read.m[key]if!ok && read.amended {delete(m.dirty, key)}
m.mu.Unlock()}if ok {
e.delete()}}func(e *entry)delete()(hadValue bool){for{
p := atomic.LoadPointer(&e.p)if p ==nil|| p == expunged {returnfalse}if atomic.CompareAndSwapPointer(&e.p, p,nil){returntrue}}}