Skip to content

Commit

Permalink
Protect access/mutations of type cache with mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Nov 15, 2023
1 parent d21c0e0 commit feaa1b3
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions types/typemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"fmt"
"reflect"
"sync"
"unsafe"
)

Expand Down Expand Up @@ -146,19 +147,30 @@ func (m *typemap) serdeOf(x reflect.Type) (serde, bool) {
type doublemap[K, V comparable] struct {
fromK map[K]V
fromV map[V]K

mu sync.Mutex
}

func (m *doublemap[K, V]) getK(k K) (V, bool) {
m.mu.Lock()
defer m.mu.Unlock()

v, ok := m.fromK[k]
return v, ok
}

func (m *doublemap[K, V]) getV(v V) (K, bool) {
m.mu.Lock()
defer m.mu.Unlock()

k, ok := m.fromV[v]
return k, ok
}

func (m *doublemap[K, V]) add(k K, v V) V {
m.mu.Lock()
defer m.mu.Unlock()

if m.fromK == nil {
m.fromK = make(map[K]V)
m.fromV = make(map[V]K)
Expand Down

0 comments on commit feaa1b3

Please sign in to comment.