Files
temporal/common/collection/sync_map.go
Alex Shtin 91893f1064 Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

78 lines
1.5 KiB
Go

package collection
import (
"maps"
"sync"
)
// SyncMap implements a simple mutex-wrapped map. SyncMap is copyable like a normal map[K]V.
type SyncMap[K comparable, V any] struct {
// Use a pointer to RWMutex instead of embedding so that the contents of this struct itself
// are immutable and copyable, and copies refer to the same RWMutex and map.
*sync.RWMutex
// For the same reason, contents (the pointer) should not be changed.
contents map[K]V
}
func NewSyncMap[K comparable, V any]() SyncMap[K, V] {
return SyncMap[K, V]{
RWMutex: &sync.RWMutex{},
contents: make(map[K]V),
}
}
func (m *SyncMap[K, V]) Get(key K) (value V, ok bool) {
m.RLock()
defer m.RUnlock()
value, ok = m.contents[key]
return
}
func (m *SyncMap[K, V]) GetOrSet(key K, value V) (v V, exist bool) {
m.RLock()
currentValue, ok := m.contents[key]
m.RUnlock()
if ok {
return currentValue, ok
}
m.Lock()
defer m.Unlock()
currentValue, ok = m.contents[key]
if ok {
return currentValue, ok
}
m.contents[key] = value
return value, false
}
func (m *SyncMap[K, V]) Set(key K, value V) {
m.Lock()
defer m.Unlock()
m.contents[key] = value
}
func (m *SyncMap[K, V]) Delete(key K) {
m.Lock()
defer m.Unlock()
delete(m.contents, key)
}
func (m *SyncMap[K, V]) Pop(key K) (value V, ok bool) {
m.Lock()
defer m.Unlock()
value, ok = m.contents[key]
if ok {
delete(m.contents, key)
}
return value, ok
}
func (m *SyncMap[K, V]) PopAll() map[K]V {
m.Lock()
defer m.Unlock()
contents := maps.Clone(m.contents)
clear(m.contents)
return contents
}