Store sets the value for a key.
(key K, value V)
| 146 | |
| 147 | // Store sets the value for a key. |
| 148 | func (m *MapOf[K, V]) Store(key K, value V) { |
| 149 | read, _ := m.read.Load().(readOnly[K, V]) |
| 150 | if e, ok := read.m[key]; ok && e.tryStore(&value) { |
| 151 | return |
| 152 | } |
| 153 | |
| 154 | m.mu.Lock() |
| 155 | read, _ = m.read.Load().(readOnly[K, V]) |
| 156 | if e, ok := read.m[key]; ok { |
| 157 | if e.unexpungeLocked() { |
| 158 | // The entry was previously expunged, which implies that there is a |
| 159 | // non-nil dirty map and this entry is not in it. |
| 160 | m.dirty[key] = e |
| 161 | } |
| 162 | e.storeLocked(&value) |
| 163 | } else if e, ok := m.dirty[key]; ok { |
| 164 | e.storeLocked(&value) |
| 165 | } else { |
| 166 | if !read.amended { |
| 167 | // We're adding the first new key to the dirty map. |
| 168 | // Make sure it is allocated and mark the read-only map as incomplete. |
| 169 | m.dirtyLocked() |
| 170 | m.read.Store(readOnly[K, V]{m: read.m, amended: true}) |
| 171 | } |
| 172 | m.dirty[key] = newEntry(value) |
| 173 | } |
| 174 | m.mu.Unlock() |
| 175 | } |
| 176 | |
| 177 | // tryStore stores a value if the entry has not been expunged. |
| 178 | // |