mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Create a concurrent map
This commit is contained in:
parent
2594afc60f
commit
b687d8ea95
56
transcoder/src/cmap.go
Normal file
56
transcoder/src/cmap.go
Normal file
@ -0,0 +1,56 @@
|
||||
package src
|
||||
|
||||
import "sync"
|
||||
|
||||
type CMap[K comparable, V any] struct {
|
||||
data map[K]V
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewCMap[K comparable, V any]() CMap[K, V] {
|
||||
return CMap[K, V]{
|
||||
data: make(map[K]V),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *CMap[K, V]) Get(key K) (V, bool) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
ret, ok := m.data[key]
|
||||
return ret, ok
|
||||
}
|
||||
|
||||
func (m *CMap[K, V]) GetOrCreate(key K, create func() V) (V, bool) {
|
||||
m.lock.RLock()
|
||||
ret, ok := m.data[key]
|
||||
if ok {
|
||||
m.lock.RUnlock()
|
||||
return ret, false
|
||||
}
|
||||
m.lock.RUnlock()
|
||||
|
||||
// data does not exist, create it
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
// check if another gorountine already created it before we could lock
|
||||
ret, ok = m.data[key]
|
||||
if ok {
|
||||
return ret, false
|
||||
}
|
||||
|
||||
val := create()
|
||||
m.data[key] = val
|
||||
return val, true
|
||||
}
|
||||
|
||||
func (m *CMap[K, V]) GetOrSet(key K, val V) (V, bool) {
|
||||
return m.GetOrCreate(key, func() V { return val })
|
||||
}
|
||||
|
||||
func (m *CMap[K, V]) Remove(key K) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
delete(m.data, key)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user