nextcloud-spreed-signaling/container/concurrentmap.go

66 lines
1.5 KiB
Go

/**
* Standalone signaling server for the Nextcloud Spreed app.
* Copyright (C) 2017 struktur AG
*
* @author Joachim Bauch <bauch@struktur.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package container
import (
"sync"
)
type ConcurrentMap[K comparable, V any] struct {
mu sync.RWMutex
// +checklocks:mu
d map[K]V
}
func (m *ConcurrentMap[K, V]) Set(key K, value V) {
m.mu.Lock()
defer m.mu.Unlock()
if m.d == nil {
m.d = make(map[K]V)
}
m.d[key] = value
}
func (m *ConcurrentMap[K, V]) Get(key K) (V, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
s, found := m.d[key]
return s, found
}
func (m *ConcurrentMap[K, V]) Del(key K) {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.d, key)
}
func (m *ConcurrentMap[K, V]) Len() int {
m.mu.RLock()
defer m.mu.RUnlock()
return len(m.d)
}
func (m *ConcurrentMap[K, V]) Clear() {
m.mu.Lock()
defer m.mu.Unlock()
m.d = nil
}