oh-my-posh/src/concurrent_map.go

41 lines
740 B
Go
Raw Normal View History

2021-09-21 11:22:59 -07:00
package main
import "sync"
type concurrentMap struct {
2021-09-21 22:53:59 -07:00
values map[string]interface{}
2021-09-21 11:22:59 -07:00
lock sync.RWMutex
}
func newConcurrentMap() *concurrentMap {
return &concurrentMap{
2021-09-21 22:53:59 -07:00
values: make(map[string]interface{}),
2021-09-21 11:22:59 -07:00
lock: sync.RWMutex{},
}
}
2021-09-21 22:53:59 -07:00
func (c *concurrentMap) set(key string, value interface{}) {
2021-09-21 11:22:59 -07:00
c.lock.Lock()
defer c.lock.Unlock()
c.values[key] = value
}
2021-09-21 22:53:59 -07:00
func (c *concurrentMap) get(key string) (interface{}, bool) {
2021-09-21 11:22:59 -07:00
c.lock.RLock()
defer c.lock.RUnlock()
if val, ok := c.values[key]; ok {
return val, true
}
return "", false
}
2021-09-21 22:53:59 -07:00
func (c *concurrentMap) remove(key string) {
c.lock.RLock()
defer c.lock.RUnlock()
delete(c.values, key)
}
func (c *concurrentMap) list() map[string]interface{} {
return c.values
}